1 /*
  2  * Copyright (c) 2012, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @bug 8001562
 27  * @summary Verify that getAvailableLocales() in locale sensitive services
 28  * classes return compatible set of locales as in JDK7.
 29  * @modules jdk.localedata
 30  * @run junit JDK7LocaleServiceDiffs
 31  */
 32 
 33 import java.text.BreakIterator;
 34 import java.text.Collator;
 35 import java.text.DateFormat;
 36 import java.text.DateFormatSymbols;
 37 import java.text.DecimalFormatSymbols;
 38 import java.text.NumberFormat;
 39 import java.util.Arrays;
 40 import java.util.List;
 41 import java.util.Locale;
 42 import java.util.stream.Collectors;
 43 import java.util.stream.Stream;
 44 
 45 import org.junit.jupiter.params.ParameterizedTest;
 46 import org.junit.jupiter.params.provider.Arguments;
 47 import org.junit.jupiter.params.provider.MethodSource;
 48 
 49 public class JDK7LocaleServiceDiffs {
 50 
 51     static final List<String> jdk7availTags = List.of(
 52             "ar", "ar-AE", "ar-BH", "ar-DZ", "ar-EG", "ar-IQ", "ar-JO", "ar-KW",
 53             "ar-LB", "ar-LY", "ar-MA", "ar-OM", "ar-QA", "ar-SA", "ar-SD", "ar-SY",
 54             "ar-TN", "ar-YE", "be", "be-BY", "bg", "bg-BG", "ca", "ca-ES", "cs",
 55             "cs-CZ", "da", "da-DK", "de", "de-AT", "de-CH", "de-DE", "de-LU", "el",
 56             "el-CY", "el-GR", "en", "en-AU", "en-CA", "en-GB", "en-IE", "en-IN",
 57             "en-MT", "en-NZ", "en-PH", "en-SG", "en-US", "en-ZA", "es", "es-AR",
 58             "es-BO", "es-CL", "es-CO", "es-CR", "es-DO", "es-EC", "es-ES", "es-GT",
 59             "es-HN", "es-MX", "es-NI", "es-PA", "es-PE", "es-PR", "es-PY", "es-SV",
 60             "es-US", "es-UY", "es-VE", "et", "et-EE", "fi", "fi-FI", "fr", "fr-BE",
 61             "fr-CA", "fr-CH", "fr-FR", "fr-LU", "ga", "ga-IE", "he", "he-IL",
 62             "hi-IN", "hr", "hr-HR", "hu", "hu-HU", "id", "id-ID", "is", "is-IS",
 63             "it", "it-CH", "it-IT", "ja", "ja-JP",
 64             "ja-JP-u-ca-japanese-x-lvariant-JP", "ko", "ko-KR", "lt", "lt-LT", "lv",
 65             "lv-LV", "mk", "mk-MK", "ms", "ms-MY", "mt", "mt-MT", "nl", "nl-BE",
 66             "nl-NL", "no", "no-NO", "no-NO-x-lvariant-NY", "pl", "pl-PL", "pt",
 67             "pt-BR", "pt-PT", "ro", "ro-RO", "ru", "ru-RU", "sk", "sk-SK", "sl",
 68             "sl-SI", "sq", "sq-AL", "sr", "sr-BA", "sr-CS", "sr-Latn", "sr-Latn-BA",
 69             "sr-Latn-ME", "sr-Latn-RS", "sr-ME", "sr-RS", "sv", "sv-SE", "th",
 70             "th-TH", "th-TH-u-nu-thai-x-lvariant-TH", "tr", "tr-TR", "uk", "uk-UA",
 71             "vi", "vi-VN", "zh", "zh-CN", "zh-HK", "zh-SG", "zh-TW");
 72     static List<Locale> jdk7availLocs;
 73 
 74     static {
 75         jdk7availLocs = jdk7availTags.stream()
 76                 .map(Locale::forLanguageTag)
 77                 .collect(Collectors.toList());
 78     }
 79 
 80     /**
 81      * This test compares the locales returned by getAvailableLocales() from a
 82      * locale sensitive service to the available JDK7 locales. If the locales from
 83      * a locale sensitive service are found to not contain a JDK7 available tag,
 84      * the test will fail.
 85      */
 86     @ParameterizedTest
 87     @MethodSource("serviceProvider")
 88     public void compatibleLocalesTest(Class<?> c, List<Locale> locs) {
 89         diffLocale(c, locs);
 90     }
 91 
 92     static void diffLocale(Class<?> c, List<Locale> locs) {
 93         String diff = "";
 94 
 95         System.out.printf("Only in target locales (%s.getAvailableLocales()): ", c.getSimpleName());
 96         for (Locale l : locs) {
 97             if (!jdk7availLocs.contains(l)) {
 98                 diff += "\"" + l.toLanguageTag() + "\", ";
 99             }
100         }
101         System.out.println(diff);
102         diff = "";
103 
104         System.out.printf("Only in JDK7 (%s.getAvailableLocales()): ", c.getSimpleName());
105         for (Locale l : jdk7availLocs) {
106             if (!locs.contains(l)) {
107                 diff += "\"" + l.toLanguageTag() + "\", ";
108             }
109         }
110         System.out.println(diff);
111 
112         if (diff.length() > 0) {
113             throw new RuntimeException("Above locale(s) were not included in the target available locales");
114         }
115     }
116 
117     private static Stream<Arguments> serviceProvider() {
118         return Stream.of(
119                 Arguments.of(BreakIterator.class, Arrays.asList(BreakIterator.getAvailableLocales())),
120                 Arguments.of(Collator.class, Arrays.asList(Collator.getAvailableLocales())),
121                 Arguments.of(DateFormat.class, Arrays.asList(DateFormat.getAvailableLocales())),
122                 Arguments.of(DateFormatSymbols.class, Arrays.asList(DateFormatSymbols.getAvailableLocales())),
123                 Arguments.of(DecimalFormatSymbols.class, Arrays.asList(DecimalFormatSymbols.getAvailableLocales())),
124                 Arguments.of(NumberFormat.class, Arrays.asList(NumberFormat.getAvailableLocales())),
125                 Arguments.of(Locale.class, Arrays.asList(Locale.getAvailableLocales()))
126         );
127     }
128 }