Blame SOURCES/TestTranslations.java

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