Blame SOURCES/TestTranslations.java

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