Blame SOURCES/TestTranslations.java

70a365
/* TestTranslations -- Ensure translations are available for new timezones
70a365
   Copyright (C) 2022 Red Hat, Inc.
70a365
70a365
This program is free software: you can redistribute it and/or modify
70a365
it under the terms of the GNU Affero General Public License as
70a365
published by the Free Software Foundation, either version 3 of the
70a365
License, or (at your option) any later version.
70a365
70a365
This program is distributed in the hope that it will be useful,
70a365
but WITHOUT ANY WARRANTY; without even the implied warranty of
70a365
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
70a365
GNU Affero General Public License for more details.
70a365
70a365
You should have received a copy of the GNU Affero General Public License
70a365
along with this program.  If not, see <http://www.gnu.org/licenses/>.
70a365
*/
70a365
70a365
import java.text.DateFormatSymbols;
70a365
70a365
import java.time.ZoneId;
70a365
import java.time.format.TextStyle;
70a365
70a365
import java.util.Arrays;
70a365
import java.util.Collections;
70a365
import java.util.HashMap;
70a365
import java.util.Map;
70a365
import java.util.Locale;
70a365
import java.util.Objects;
70a365
import java.util.TimeZone;
70a365
70a365
public class TestTranslations {
70a365
f0e2fb
    private static Map<Locale,String[]> KYIV, CIUDAD_JUAREZ;
70a365
70a365
    static {
70a365
        Map<Locale,String[]> map = new HashMap<Locale,String[]>();
70a365
        map.put(Locale.US, new String[] { "Eastern European Time", "GMT+02:00", "EET",
70a365
                                          "Eastern European Summer Time", "GMT+03:00", "EEST",
70a365
                                          "Eastern European Time", "GMT+02:00", "EET"});
70a365
        map.put(Locale.FRANCE, new String[] { "Heure d'Europe de l'Est", "UTC+02:00", "EET",
70a365
                                              "Heure d'\u00e9t\u00e9 d'Europe de l'Est", "UTC+03:00", "EEST",
70a365
                                              "Heure d'Europe de l'Est", "UTC+02:00", "EET"});
70a365
        map.put(Locale.GERMANY, new String[] { "Osteurop\u00e4ische Zeit", "OEZ", "OEZ",
70a365
                                               "Osteurop\u00e4ische Sommerzeit", "OESZ", "OESZ",
70a365
                                               "Osteurop\u00e4ische Zeit", "OEZ", "OEZ"});
70a365
        KYIV = Collections.unmodifiableMap(map);
f0e2fb
f0e2fb
        map = new HashMap<Locale,String[]>();
f0e2fb
        map.put(Locale.US, new String[] { "Mountain Standard Time", "MST", "MST",
f0e2fb
                                          "Mountain Daylight Time", "MDT", "MDT",
f0e2fb
                                          "Mountain Time", "MT", "MT"});
f0e2fb
        map.put(Locale.FRANCE, new String[] { "Heure normale des Rocheuses", "UTC\u221207:00", "MST",
f0e2fb
                                              "Heure avanc\u00e9e des Rocheuses", "UTC\u221206:00", "MDT",
f0e2fb
                                              "Rocheuses", "UTC\u221207:00", "MT"});
f0e2fb
        map.put(Locale.GERMANY, new String[] { "Rocky Mountains Normalzeit", "GMT-07:00", "MST",
f0e2fb
                                               "Rocky Mountains Sommerzeit", "GMT-06:00", "MDT",
f0e2fb
                                               "Zeitzone Mountain", "GMT-07:00", "MT"});
f0e2fb
        CIUDAD_JUAREZ = Collections.unmodifiableMap(map);
70a365
    }
70a365
70a365
70a365
    public static void main(String[] args) {
70a365
        if (args.length < 1) {
70a365
            System.err.println("Test must be started with the name of the locale provider.");
70a365
            System.exit(1);
70a365
        }
70a365
70a365
        System.out.println("Checking sanity of full zone string set...");
70a365
        boolean invalid = Arrays.stream(Locale.getAvailableLocales())
70a365
            .peek(l -> System.out.println("Locale: " + l))
70a365
            .map(l -> DateFormatSymbols.getInstance(l).getZoneStrings())
70a365
            .flatMap(zs -> Arrays.stream(zs))
70a365
            .flatMap(names -> Arrays.stream(names))
70a365
            .filter(name -> Objects.isNull(name) || name.isEmpty())
70a365
            .findAny()
70a365
            .isPresent();
70a365
        if (invalid) {
70a365
            System.err.println("Zone string for a locale returned null or empty string");
70a365
            System.exit(2);
70a365
        }
70a365
f0e2fb
        String localeProvider = args[0];
f0e2fb
        testZone(localeProvider, KYIV,
f0e2fb
                 new String[] { "Europe/Kiev", "Europe/Kyiv", "Europe/Uzhgorod", "Europe/Zaporozhye" });
f0e2fb
        testZone(localeProvider, CIUDAD_JUAREZ,
f0e2fb
                 new String[] { "America/Cambridge_Bay", "America/Ciudad_Juarez" });
f0e2fb
    }
f0e2fb
f0e2fb
    private static void testZone(String localeProvider, Map<Locale,String[]> exp, String[] ids) {
f0e2fb
        for (Locale l : exp.keySet()) {
f0e2fb
            String[] expected = exp.get(l);
f0e2fb
            System.out.printf("Expected values for %s are %s\n", l, Arrays.toString(expected));
f0e2fb
            for (String id : ids) {
70a365
                String expectedShortStd = null;
70a365
                String expectedShortDST = null;
70a365
                String expectedShortGen = null;
70a365
70a365
                System.out.printf("Checking locale %s for %s...\n", l, id);
70a365
70a365
                if ("JRE".equals(localeProvider)) {
70a365
                    expectedShortStd = expected[2];
70a365
                    expectedShortDST = expected[5];
70a365
                    expectedShortGen = expected[8];
70a365
                } else if ("CLDR".equals(localeProvider)) {
70a365
                    expectedShortStd = expected[1];
70a365
                    expectedShortDST = expected[4];
70a365
                    expectedShortGen = expected[7];
70a365
                } else {
70a365
                    System.err.printf("Invalid locale provider %s\n", localeProvider);
70a365
                    System.exit(3);
70a365
                }
70a365
                System.out.printf("Locale Provider is %s, using short values %s, %s and %s\n",
70a365
                                  localeProvider, expectedShortStd, expectedShortDST, expectedShortGen);
70a365
70a365
                String longStd = TimeZone.getTimeZone(id).getDisplayName(false, TimeZone.LONG, l);
70a365
                String shortStd = TimeZone.getTimeZone(id).getDisplayName(false, TimeZone.SHORT, l);
70a365
                String longDST = TimeZone.getTimeZone(id).getDisplayName(true, TimeZone.LONG, l);
70a365
                String shortDST = TimeZone.getTimeZone(id).getDisplayName(true, TimeZone.SHORT, l);
70a365
                String longGen = ZoneId.of(id).getDisplayName(TextStyle.FULL, l);
70a365
                String shortGen = ZoneId.of(id).getDisplayName(TextStyle.SHORT, l);
70a365
70a365
                if (!expected[0].equals(longStd)) {
70a365
                    System.err.printf("Long standard display name for %s in %s was %s, expected %s\n",
70a365
                                      id, l, longStd, expected[0]);
70a365
                    System.exit(4);
70a365
                }
70a365
70a365
                if (!expectedShortStd.equals(shortStd)) {
70a365
                    System.err.printf("Short standard display name for %s in %s was %s, expected %s\n",
70a365
                                      id, l, shortStd, expectedShortStd);
70a365
                    System.exit(5);
70a365
                }
70a365
70a365
                if (!expected[3].equals(longDST)) {
70a365
                    System.err.printf("Long DST display name for %s in %s was %s, expected %s\n",
70a365
                                      id, l, longDST, expected[3]);
70a365
                    System.exit(6);
70a365
                }
70a365
70a365
                if (!expectedShortDST.equals(shortDST)) {
70a365
                    System.err.printf("Short DST display name for %s in %s was %s, expected %s\n",
70a365
                                      id, l, shortDST, expectedShortDST);
70a365
                    System.exit(7);
70a365
                }
70a365
70a365
                if (!expected[6].equals(longGen)) {
70a365
                    System.err.printf("Long generic display name for %s in %s was %s, expected %s\n",
70a365
                                      id, l, longGen, expected[6]);
70a365
                    System.exit(8);
70a365
                }
70a365
70a365
                if (!expectedShortGen.equals(shortGen)) {
70a365
                    System.err.printf("Short generic display name for %s in %s was %s, expected %s\n",
70a365
                                      id, l, shortGen, expectedShortGen);
70a365
                    System.exit(9);
70a365
                }
70a365
            }
70a365
        }
70a365
    }
70a365
}