Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/PluggableLocale/DateFormatSymbolsProviderTest.java
47209 views
/*1* Copyright (c) 2007, 2013, 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*/22/*23*24*/2526import java.text.*;27import java.util.*;28import sun.util.locale.provider.*;29import sun.util.resources.*;3031public class DateFormatSymbolsProviderTest extends ProviderTest {3233com.foo.DateFormatSymbolsProviderImpl dfsp = new com.foo.DateFormatSymbolsProviderImpl();34List<Locale> availloc = Arrays.asList(DateFormatSymbols.getAvailableLocales());35List<Locale> providerloc = Arrays.asList(dfsp.getAvailableLocales());36List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales());37List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getDateFormatSymbolsProvider().getAvailableLocales());3839public static void main(String[] s) {40new DateFormatSymbolsProviderTest();41}4243DateFormatSymbolsProviderTest() {44availableLocalesTest();45objectValidityTest();46hashCodeTest();47}4849void availableLocalesTest() {50Set<Locale> localesFromAPI = new HashSet<>(availloc);51Set<Locale> localesExpected = new HashSet<>(jreloc);52localesExpected.addAll(providerloc);53if (localesFromAPI.equals(localesExpected)) {54System.out.println("availableLocalesTest passed.");55} else {56throw new RuntimeException("availableLocalesTest failed");57}58}5960void objectValidityTest() {6162for (Locale target: availloc) {63// pure JRE implementation64ResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getDateFormatData(target);65boolean jreSupportsLocale = jreimplloc.contains(target);6667// JRE string arrays68String[][] jres = new String[6][];69if (jreSupportsLocale) {70try {71jres[0] = (String[])rb.getObject("MonthNames");72jres[1] = (String[])rb.getObject("MonthAbbreviations");73jres[2] = (String[])rb.getObject("DayNames");74jres[3] = (String[])rb.getObject("DayAbbreviations");75jres[4] = (String[])rb.getObject("AmPmMarkers");76jres[5] = (String[])rb.getObject("Eras");77} catch (MissingResourceException mre) {}78}7980// result object81DateFormatSymbols dfs = DateFormatSymbols.getInstance(target);82String[][] result = new String[6][];83result[0] = dfs.getMonths();84result[1] = dfs.getShortMonths();85// note that weekdays are 1-based86String[] tmp = dfs.getWeekdays();87result[2] = new String[7];88System.arraycopy(tmp, 1, result[2], 0, result[2].length);89tmp = dfs.getShortWeekdays();90result[3] = new String[7];91System.arraycopy(tmp, 1, result[3], 0, result[3].length);92result[4] = dfs.getAmPmStrings();93result[5] = dfs.getEras();9495// provider's object (if any)96DateFormatSymbols providersDfs= null;97String[][] providers = new String[6][];98if (providerloc.contains(target)) {99providersDfs = dfsp.getInstance(target);100providers[0] = providersDfs.getMonths();101providers[1] = providersDfs.getShortMonths();102// note that weekdays are 1-based103tmp = dfs.getWeekdays();104providers[2] = new String[7];105System.arraycopy(tmp, 1, providers[2], 0, providers[2].length);106tmp = dfs.getShortWeekdays();107providers[3] = new String[7];108System.arraycopy(tmp, 1, providers[3], 0, providers[3].length);109providers[4] = providersDfs.getAmPmStrings();110providers[5] = providersDfs.getEras();111}112113for (int i = 0; i < result.length; i ++) {114for (int j = 0; j < result[i].length; j++) {115String jresStr =116(jres[i] != null ? jres[i][j] : null);117String providersStr =118(providers[i] != null ? providers[i][j] : null);119String resultStr =120(result[i] != null ? result[i][j] : null);121checkValidity(target, jresStr, providersStr, resultStr, jreSupportsLocale);122}123}124}125}126127// Bug 7200341.128void hashCodeTest() {129for (Locale target: availloc) {130// look for provider's object131DateFormatSymbols dfs = DateFormatSymbols.getInstance(target);132if (dfs.getClass().getSimpleName().equals("FooDateFormatSymbols")) {133// call its hashCode(). success if no ArrayIndexOutOfBoundsException is thrown.134dfs.hashCode();135break;136}137}138}139}140141142