Blame SOURCES/TestTranslations.java

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