Blame SOURCES/TestTranslations.java

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