Blame SOURCES/TestTranslations.java

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