Blame SOURCES/TestTranslations.java

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