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