Path: blob/master/test/jdk/java/util/PluggableLocale/DateFormatProviderTest.java
41149 views
/*1* Copyright (c) 2007, 2018, 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 4052440 7003643 8062588 821040626* @summary DateFormatProvider tests27* @library providersrc/foobarutils28* providersrc/fooprovider29* @modules java.base/sun.util.locale.provider30* java.base/sun.util.resources31* @build com.foobar.Utils32* com.foo.*33* @run main/othervm -Djava.locale.providers=JRE,SPI DateFormatProviderTest34*/3536import java.text.DateFormat;37import java.text.Format;38import java.text.MessageFormat;39import java.text.SimpleDateFormat;40import java.util.Arrays;41import java.util.Calendar;42import java.util.HashSet;43import java.util.List;44import java.util.Locale;45import java.util.MissingResourceException;46import java.util.ResourceBundle;47import java.util.Set;4849import com.foo.DateFormatProviderImpl;5051import sun.util.locale.provider.LocaleProviderAdapter;52import sun.util.locale.provider.ResourceBundleBasedAdapter;5354public class DateFormatProviderTest extends ProviderTest {5556DateFormatProviderImpl dfp = new DateFormatProviderImpl();57List<Locale> availloc = Arrays.asList(DateFormat.getAvailableLocales());58List<Locale> providerloc = Arrays.asList(dfp.getAvailableLocales());59List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales());60List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getDateFormatProvider().getAvailableLocales());6162public static void main(String[] s) {63new DateFormatProviderTest();64}6566DateFormatProviderTest() {67availableLocalesTest();68objectValidityTest();69extendedVariantTest();70messageFormatTest();71}7273void availableLocalesTest() {74Set<Locale> localesFromAPI = new HashSet<>(availloc);75Set<Locale> localesExpected = new HashSet<>(jreloc);76localesExpected.addAll(providerloc);77if (localesFromAPI.equals(localesExpected)) {78System.out.println("availableLocalesTest passed.");79} else {80throw new RuntimeException("availableLocalesTest failed");81}82}8384void objectValidityTest() {8586for (Locale target: availloc) {87// Get the key for the date/time patterns which is88// specific to each calendar system.89Calendar cal = Calendar.getInstance(target);90String dkey = "DatePatterns";91String tkey = "TimePatterns";92String dtkey = "DateTimePatterns";93switch (cal.getCalendarType()) {94case "java.util.JapaneseImperialCalendar":95dkey = "japanese"+ "." + dkey;96tkey = "japanese"+ "." + tkey;97dtkey = "japanese"+ "." + dtkey;98break;99case "sun.util.BuddhistCalendar":100dkey = "buddhist"+ "." + dkey;101tkey = "buddhist"+ "." + tkey;102dtkey = "buddhist"+ "." + dtkey;103break;104case "java.util.GregorianCalendar":105default:106break;107}108// pure JRE implementation109ResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getDateFormatData(target);110boolean jreSupportsLocale = jreimplloc.contains(target);111112// JRE string arrays113String[] jreDatePatterns = null;114String[] jreTimePatterns = null;115String[] jreDateTimePatterns = null;116if (jreSupportsLocale) {117try {118jreDatePatterns = (String[])rb.getObject(dkey);119jreTimePatterns = (String[])rb.getObject(tkey);120jreDateTimePatterns = (String[])rb.getObject(dtkey);121} catch (MissingResourceException mre) {}122}123124for (int style = DateFormat.FULL; style <= DateFormat.SHORT; style ++) {125// result object126DateFormat result = DateFormat.getDateTimeInstance(style, style, target);127128// provider's object (if any)129DateFormat providersResult = null;130if (providerloc.contains(target)) {131providersResult = dfp.getDateTimeInstance(style, style, target);132}133134// JRE's object (if any)135DateFormat jresResult = null;136if (jreSupportsLocale) {137Object[] dateTimeArgs = {jreTimePatterns[style],138jreDatePatterns[style]};139String pattern = MessageFormat.format(jreDateTimePatterns[0], dateTimeArgs);140jresResult = new SimpleDateFormat(pattern, target);141}142143checkValidity(target, jresResult, providersResult, result, jreSupportsLocale);144}145}146}147148// Check that fallback correctly occurs with locales with variant including '_'s149// This test assumes that the provider supports the ja_JP_osaka locale, and JRE does not.150void extendedVariantTest() {151Locale[] testlocs = {new Locale("ja", "JP", "osaka_extended"),152new Locale("ja", "JP", "osaka_extended_further"),153new Locale("ja", "JP", "osaka_")};154for (Locale test: testlocs) {155DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);156DateFormat provider = dfp.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);157if (!df.equals(provider)) {158throw new RuntimeException("variant fallback failed. test locale: "+test);159}160}161}162163164private static final String[] TYPES = {165"date",166"time"167};168private static final String[] MODIFIERS = {169"",170"short",171"medium", // Same as DEFAULT172"long",173"full"174};175176void messageFormatTest() {177for (Locale target : providerloc) {178for (String type : TYPES) {179for (String modifier : MODIFIERS) {180String pattern, expected;181if (modifier.equals("")) {182pattern = String.format("%s={0,%s}", type, type);183} else {184pattern = String.format("%s={0,%s,%s}", type, type, modifier);185}186if (modifier.equals("medium")) {187// medium is default.188expected = String.format("%s={0,%s}", type, type);189} else {190expected = pattern;191}192MessageFormat mf = new MessageFormat(pattern, target);193Format[] fmts = mf.getFormats();194if (fmts[0] instanceof SimpleDateFormat) {195continue;196}197String toPattern = mf.toPattern();198if (!toPattern.equals(expected)) {199throw new RuntimeException("messageFormatTest: got '" + toPattern200+ "', expected '" + expected + "'");201}202}203}204}205}206}207208