Path: blob/master/test/jdk/java/util/Calendar/Bug8167273.java
41149 views
/*1* Copyright (c) 2017, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8167273 8251317 825879426* @summary Test27* Era names retrieved from Calendar and DateFormatSymbols class28* should match for default providers preference29* as well as when preference list is [COMPAT, CLDR],30* Empty era names are not retrieved from DateformatSymbols class.31* Equivalent locales specified for [zh-HK, no-NO, no] for32* CLDR Provider works correctly.33* Implict COMPAT Locale nb is reflected in available locales34* for all Providers for COMPAT.35* @modules java.base/sun.util.locale.provider36* java.base/sun.util.spi37* jdk.localedata38* @run main/othervm -Djava.locale.providers=COMPAT,CLDR Bug8167273 testEraName39* @run main/othervm Bug8167273 testEraName40* @run main/othervm -Djava.locale.providers=CLDR Bug8167273 testCldr41* @run main/othervm Bug8167273 testEmptyEraNames42* @run main/othervm -Djava.locale.providers=COMPAT Bug8167273 testCompat43*/44import java.text.DateFormatSymbols;45import java.util.Arrays;46import java.util.Calendar;47import java.util.HashSet;48import java.util.List;49import java.util.Locale;50import java.util.Map;51import java.util.Set;5253import sun.util.locale.provider.LocaleProviderAdapter;54import sun.util.locale.provider.LocaleProviderAdapter.Type;5556public class Bug8167273 {5758public static void main(String[] args) throws Exception {59switch (args[0]) {60case "testEraName":61testEraName();62break;63case "testEmptyEraNames":64testEmptyEraNames();65break;66case "testCldr":67testCldrSupportedLocales();68break;69case "testCompat":70testCompatSupportedLocale();71break;72default:73throw new RuntimeException("no test was specified.");74}75}7677/**78* tests that era names retrieved from Calendar.getDisplayNames map should79* match with that of Era names retrieved from DateFormatSymbols.getEras()80* method for all Gregorian Calendar locales .81*/82public static void testEraName() {83Set<Locale> allLocales = Set.of(Locale.getAvailableLocales());84Set<Locale> JpThlocales = Set.of(85new Locale("th", "TH"), Locale.forLanguageTag("th-Thai-TH"),86new Locale("ja", "JP", "JP"), new Locale("th", "TH", "TH")87);88Set<Locale> allLocs = new HashSet<>(allLocales);89// Removing Japanese and Thai Locales to check Gregorian Calendar Locales90allLocs.removeAll(JpThlocales);91allLocs.forEach((locale) -> {92Calendar cal = Calendar.getInstance(locale);93Map<String, Integer> names = cal.getDisplayNames(Calendar.ERA, Calendar.ALL_STYLES, locale);94DateFormatSymbols symbols = new DateFormatSymbols(locale);95String[] eras = symbols.getEras();96for (String era : eras) {97if (!names.containsKey(era)) {98reportMismatch(names.keySet(), eras, locale);99}100}101});102}103104private static void reportMismatch(Set<String> CalendarEras, String[] dfsEras, Locale locale) {105System.out.println("For Locale " + locale + "era names in calendar map are " + CalendarEras);106for (String era : dfsEras) {107System.out.println("For Locale " + locale + " era names in DateFormatSymbols era array are " + era);108}109throw new RuntimeException(" Era name retrieved from Calendar class do not match with"110+ " retrieved from DateFormatSymbols for Locale " + locale);111112}113114/**115* tests that Eras names returned from DateFormatSymbols.getEras()116* and Calendar.getDisplayNames() should not be empty for any Locale.117*/118private static void testEmptyEraNames() {119Set<Locale> allLocales = Set.of(Locale.getAvailableLocales());120allLocales.forEach((loc) -> {121DateFormatSymbols dfs = new DateFormatSymbols(loc);122Calendar cal = Calendar.getInstance(loc);123Map<String, Integer> names = cal.getDisplayNames(Calendar.ERA, Calendar.ALL_STYLES, loc);124Set<String> CalendarEraNames = names.keySet();125String[] eras = dfs.getEras();126for (String era : eras) {127if (era.isEmpty()) {128throw new RuntimeException("Empty era names retrieved for DateFomatSymbols.getEras"129+ " for locale " + loc);130}131}132CalendarEraNames.stream().filter((erakey) -> (erakey.isEmpty())).forEachOrdered((l) -> {133throw new RuntimeException("Empty era names retrieved for Calendar.getDisplayName"134+ " for locale " + loc);135});136});137138}139140/**141* tests that CLDR provider should return true for locale zh_HK, no-NO and142* no.143*/144private static void testCldrSupportedLocales() {145Set<Locale> locales = Set.of(Locale.forLanguageTag("zh-HK"),146Locale.forLanguageTag("no-NO"),147Locale.forLanguageTag("no"));148LocaleProviderAdapter cldr = LocaleProviderAdapter.forType(Type.CLDR);149Set<Locale> availableLocs = Set.of(cldr.getAvailableLocales());150Set<String> langtags = new HashSet<>();151availableLocs.forEach((loc) -> {152langtags.add(loc.toLanguageTag());153});154155locales.stream().filter((loc) -> (!cldr.isSupportedProviderLocale(loc, langtags))).forEachOrdered((loc) -> {156throw new RuntimeException("Locale " + loc + " is not supported by CLDR Locale Provider");157});158}159160/**161* Tests that locale nb should be supported by JRELocaleProvider .162*/163private static void testCompatSupportedLocale() {164LocaleProviderAdapter jre = LocaleProviderAdapter.forJRE();165checkPresenceCompat("BreakIteratorProvider",166jre.getBreakIteratorProvider().getAvailableLocales());167checkPresenceCompat("CollatorProvider",168jre.getCollatorProvider().getAvailableLocales());169checkPresenceCompat("DateFormatProvider",170jre.getDateFormatProvider().getAvailableLocales());171checkPresenceCompat("DateFormatSymbolsProvider",172jre.getDateFormatSymbolsProvider().getAvailableLocales());173checkPresenceCompat("DecimalFormatSymbolsProvider",174jre.getDecimalFormatSymbolsProvider().getAvailableLocales());175checkPresenceCompat("NumberFormatProvider",176jre.getNumberFormatProvider().getAvailableLocales());177checkPresenceCompat("CurrencyNameProvider",178jre.getCurrencyNameProvider().getAvailableLocales());179checkPresenceCompat("LocaleNameProvider",180jre.getLocaleNameProvider().getAvailableLocales());181checkPresenceCompat("TimeZoneNameProvider",182jre.getTimeZoneNameProvider().getAvailableLocales());183checkPresenceCompat("CalendarDataProvider",184jre.getCalendarDataProvider().getAvailableLocales());185checkPresenceCompat("CalendarNameProvider",186jre.getCalendarNameProvider().getAvailableLocales());187checkPresenceCompat("CalendarProvider",188jre.getCalendarProvider().getAvailableLocales());189}190191private static void checkPresenceCompat(String testName, Locale[] got) {192List<Locale> gotLocalesList = Arrays.asList(got);193Locale nb = Locale.forLanguageTag("nb");194if (!gotLocalesList.contains(nb)) {195throw new RuntimeException("Locale nb not supported by JREProvider for "196+ testName + " test ");197}198}199}200201202