Blame SOURCES/TestTranslations.java

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