Blame SOURCES/TestTranslations.java

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