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