Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/PluggableLocale/DateFormatProviderTest.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 DateFormatProviderTest extends ProviderTest {3233com.foo.DateFormatProviderImpl dfp = new com.foo.DateFormatProviderImpl();34List<Locale> availloc = Arrays.asList(DateFormat.getAvailableLocales());35List<Locale> providerloc = Arrays.asList(dfp.getAvailableLocales());36List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales());37List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getDateFormatProvider().getAvailableLocales());3839public static void main(String[] s) {40new DateFormatProviderTest();41}4243DateFormatProviderTest() {44availableLocalesTest();45objectValidityTest();46extendedVariantTest();47messageFormatTest();48}4950void availableLocalesTest() {51Set<Locale> localesFromAPI = new HashSet<>(availloc);52Set<Locale> localesExpected = new HashSet<>(jreloc);53localesExpected.addAll(providerloc);54if (localesFromAPI.equals(localesExpected)) {55System.out.println("availableLocalesTest passed.");56} else {57throw new RuntimeException("availableLocalesTest failed");58}59}6061void objectValidityTest() {6263for (Locale target: availloc) {64// Get the key for the date/time patterns which is65// specific to each calendar system.66Calendar cal = Calendar.getInstance(target);67String dkey = "DatePatterns";68String tkey = "TimePatterns";69String dtkey = "DateTimePatterns";70switch (cal.getCalendarType()) {71case "java.util.JapaneseImperialCalendar":72dkey = "japanese"+ "." + dkey;73tkey = "japanese"+ "." + tkey;74dtkey = "japanese"+ "." + dtkey;75break;76case "sun.util.BuddhistCalendar":77dkey = "buddhist"+ "." + dkey;78tkey = "buddhist"+ "." + tkey;79dtkey = "buddhist"+ "." + dtkey;80break;81case "java.util.GregorianCalendar":82default:83break;84}85// pure JRE implementation86ResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getDateFormatData(target);87boolean jreSupportsLocale = jreimplloc.contains(target);8889// JRE string arrays90String[] jreDatePatterns = null;91String[] jreTimePatterns = null;92String[] jreDateTimePatterns = null;93if (jreSupportsLocale) {94try {95jreDatePatterns = (String[])rb.getObject(dkey);96jreTimePatterns = (String[])rb.getObject(tkey);97jreDateTimePatterns = (String[])rb.getObject(dtkey);98} catch (MissingResourceException mre) {}99}100101for (int style = DateFormat.FULL; style <= DateFormat.SHORT; style ++) {102// result object103DateFormat result = DateFormat.getDateTimeInstance(style, style, target);104105// provider's object (if any)106DateFormat providersResult = null;107if (providerloc.contains(target)) {108providersResult = dfp.getDateTimeInstance(style, style, target);109}110111// JRE's object (if any)112DateFormat jresResult = null;113if (jreSupportsLocale) {114Object[] dateTimeArgs = {jreTimePatterns[style],115jreDatePatterns[style]};116String pattern = MessageFormat.format(jreDateTimePatterns[0], dateTimeArgs);117jresResult = new SimpleDateFormat(pattern, target);118}119120checkValidity(target, jresResult, providersResult, result, jreSupportsLocale);121}122}123}124125// Check that fallback correctly occurs with locales with variant including '_'s126// This test assumes that the provider supports the ja_JP_osaka locale, and JRE does not.127void extendedVariantTest() {128Locale[] testlocs = {new Locale("ja", "JP", "osaka_extended"),129new Locale("ja", "JP", "osaka_extended_further"),130new Locale("ja", "JP", "osaka_")};131for (Locale test: testlocs) {132DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);133DateFormat provider = dfp.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);134if (!df.equals(provider)) {135throw new RuntimeException("variant fallback failed. test locale: "+test);136}137}138}139140141private static final String[] TYPES = {142"date",143"time"144};145private static final String[] MODIFIERS = {146"",147"short",148"medium", // Same as DEFAULT149"long",150"full"151};152153void messageFormatTest() {154for (Locale target : providerloc) {155for (String type : TYPES) {156for (String modifier : MODIFIERS) {157String pattern, expected;158if (modifier.equals("")) {159pattern = String.format("%s={0,%s}", type, type);160} else {161pattern = String.format("%s={0,%s,%s}", type, type, modifier);162}163if (modifier.equals("medium")) {164// medium is default.165expected = String.format("%s={0,%s}", type, type);166} else {167expected = pattern;168}169MessageFormat mf = new MessageFormat(pattern, target);170Format[] fmts = mf.getFormats();171if (fmts[0] instanceof SimpleDateFormat) {172continue;173}174String toPattern = mf.toPattern();175if (!toPattern.equals(expected)) {176throw new RuntimeException("messageFormatTest: got '" + toPattern177+ "', expected '" + expected + "'");178}179}180}181}182}183}184185186