0N/A/*
3307N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A *
0N/A */
0N/A
0N/Aimport java.text.*;
0N/Aimport java.util.*;
0N/Aimport sun.util.*;
0N/Aimport sun.util.resources.*;
0N/A
0N/Apublic class DateFormatProviderTest extends ProviderTest {
0N/A
0N/A com.foo.DateFormatProviderImpl dfp = new com.foo.DateFormatProviderImpl();
0N/A List<Locale> availloc = Arrays.asList(DateFormat.getAvailableLocales());
0N/A List<Locale> providerloc = Arrays.asList(dfp.getAvailableLocales());
0N/A List<Locale> jreloc = Arrays.asList(LocaleData.getAvailableLocales());
0N/A
0N/A public static void main(String[] s) {
0N/A new DateFormatProviderTest();
0N/A }
0N/A
0N/A DateFormatProviderTest() {
0N/A availableLocalesTest();
0N/A objectValidityTest();
0N/A extendedVariantTest();
3307N/A messageFormatTest();
0N/A }
0N/A
0N/A void availableLocalesTest() {
0N/A Set<Locale> localesFromAPI = new HashSet<Locale>(availloc);
0N/A Set<Locale> localesExpected = new HashSet<Locale>(jreloc);
0N/A localesExpected.addAll(providerloc);
0N/A if (localesFromAPI.equals(localesExpected)) {
0N/A System.out.println("availableLocalesTest passed.");
0N/A } else {
0N/A throw new RuntimeException("availableLocalesTest failed");
0N/A }
0N/A }
0N/A
0N/A void objectValidityTest() {
0N/A
0N/A for (Locale target: availloc) {
0N/A // Get the key for the date/time patterns which is
0N/A // specific to each calendar system.
0N/A Calendar cal = Calendar.getInstance(target);
0N/A String key = "DateTimePatterns";
0N/A if (!cal.getClass().getName().equals("java.util.GregorianCalendar")) {
0N/A // e.g., "java.util.JapaneseImperialCalendar.DateTimePatterns"
0N/A key = cal.getClass().getName() + "." + key;
0N/A }
0N/A // pure JRE implementation
0N/A ResourceBundle rb = LocaleData.getDateFormatData(target);
0N/A boolean jreSupportsLocale = jreloc.contains(target);
0N/A
0N/A // JRE string arrays
0N/A String[] jreDateTimePatterns = null;
0N/A if (jreSupportsLocale) {
0N/A try {
0N/A jreDateTimePatterns = (String[])rb.getObject(key);
0N/A } catch (MissingResourceException mre) {}
0N/A }
0N/A
0N/A for (int style = DateFormat.FULL; style <= DateFormat.SHORT; style ++) {
0N/A // result object
0N/A DateFormat result = DateFormat.getDateTimeInstance(style, style, target);
0N/A
0N/A // provider's object (if any)
0N/A DateFormat providersResult = null;
0N/A if (providerloc.contains(target)) {
0N/A providersResult = dfp.getDateTimeInstance(style, style, target);
0N/A }
0N/A
0N/A // JRE's object (if any)
0N/A DateFormat jresResult = null;
0N/A if (jreSupportsLocale) {
0N/A Object[] dateTimeArgs = {jreDateTimePatterns[style],
0N/A jreDateTimePatterns[style + 4]};
0N/A String pattern = MessageFormat.format(jreDateTimePatterns[8], dateTimeArgs);
0N/A jresResult = new SimpleDateFormat(pattern, target);
0N/A }
0N/A
0N/A checkValidity(target, jresResult, providersResult, result, jreSupportsLocale);
0N/A }
0N/A }
0N/A }
0N/A
0N/A // Check that fallback correctly occurs with locales with variant including '_'s
0N/A // This test assumes that the provider supports the ja_JP_osaka locale, and JRE does not.
0N/A void extendedVariantTest() {
0N/A Locale[] testlocs = {new Locale("ja", "JP", "osaka_extended"),
0N/A new Locale("ja", "JP", "osaka_extended_further"),
0N/A new Locale("ja", "JP", "osaka_")};
0N/A for (Locale test: testlocs) {
0N/A DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
0N/A DateFormat provider = dfp.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
0N/A if (!df.equals(provider)) {
0N/A throw new RuntimeException("variant fallback failed. test locale: "+test);
0N/A }
0N/A }
0N/A }
3307N/A
3307N/A
3307N/A private static final String[] TYPES = {
3307N/A "date",
3307N/A "time"
3307N/A };
3307N/A private static final String[] MODIFIERS = {
3307N/A "",
3307N/A "short",
3307N/A "medium", // Same as DEFAULT
3307N/A "long",
3307N/A "full"
3307N/A };
3307N/A
3307N/A void messageFormatTest() {
3307N/A for (Locale target : providerloc) {
3307N/A for (String type : TYPES) {
3307N/A for (String modifier : MODIFIERS) {
3307N/A String pattern, expected;
3307N/A if (modifier.equals("")) {
3307N/A pattern = String.format("%s={0,%s}", type, type);
3307N/A } else {
3307N/A pattern = String.format("%s={0,%s,%s}", type, type, modifier);
3307N/A }
3307N/A if (modifier.equals("medium")) {
3307N/A // medium is default.
3307N/A expected = String.format("%s={0,%s}", type, type);
3307N/A } else {
3307N/A expected = pattern;
3307N/A }
3307N/A MessageFormat mf = new MessageFormat(pattern, target);
3307N/A Format[] fmts = mf.getFormats();
3307N/A if (fmts[0] instanceof SimpleDateFormat) {
3307N/A continue;
3307N/A }
3307N/A String toPattern = mf.toPattern();
3307N/A if (!toPattern.equals(expected)) {
3307N/A throw new RuntimeException("messageFormatTest: got '" + toPattern
3307N/A + "', expected '" + expected + "'");
3307N/A }
3307N/A }
3307N/A }
3307N/A }
3307N/A }
0N/A}