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