Blame SOURCES/TestTranslations.java

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