DateFormatProviderTest.java revision 3307:94e0438c74c3
1/*
2 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23/*
24 *
25 */
26
27import java.text.*;
28import java.util.*;
29import sun.util.*;
30import sun.util.resources.*;
31
32public class DateFormatProviderTest extends ProviderTest {
33
34    com.foo.DateFormatProviderImpl dfp = new com.foo.DateFormatProviderImpl();
35    List<Locale> availloc = Arrays.asList(DateFormat.getAvailableLocales());
36    List<Locale> providerloc = Arrays.asList(dfp.getAvailableLocales());
37    List<Locale> jreloc = Arrays.asList(LocaleData.getAvailableLocales());
38
39    public static void main(String[] s) {
40        new DateFormatProviderTest();
41    }
42
43    DateFormatProviderTest() {
44        availableLocalesTest();
45        objectValidityTest();
46        extendedVariantTest();
47        messageFormatTest();
48    }
49
50    void availableLocalesTest() {
51        Set<Locale> localesFromAPI = new HashSet<Locale>(availloc);
52        Set<Locale> localesExpected = new HashSet<Locale>(jreloc);
53        localesExpected.addAll(providerloc);
54        if (localesFromAPI.equals(localesExpected)) {
55            System.out.println("availableLocalesTest passed.");
56        } else {
57            throw new RuntimeException("availableLocalesTest failed");
58        }
59    }
60
61    void objectValidityTest() {
62
63        for (Locale target: availloc) {
64            // Get the key for the date/time patterns which is
65            // specific to each calendar system.
66            Calendar cal = Calendar.getInstance(target);
67            String key = "DateTimePatterns";
68            if (!cal.getClass().getName().equals("java.util.GregorianCalendar")) {
69                // e.g., "java.util.JapaneseImperialCalendar.DateTimePatterns"
70                key = cal.getClass().getName() + "." + key;
71            }
72            // pure JRE implementation
73            ResourceBundle rb = LocaleData.getDateFormatData(target);
74            boolean jreSupportsLocale = jreloc.contains(target);
75
76            // JRE string arrays
77            String[] jreDateTimePatterns = null;
78            if (jreSupportsLocale) {
79                try {
80                    jreDateTimePatterns = (String[])rb.getObject(key);
81                } catch (MissingResourceException mre) {}
82            }
83
84            for (int style = DateFormat.FULL; style <= DateFormat.SHORT; style ++) {
85                // result object
86                DateFormat result = DateFormat.getDateTimeInstance(style, style, target);
87
88                // provider's object (if any)
89                DateFormat providersResult = null;
90                if (providerloc.contains(target)) {
91                    providersResult = dfp.getDateTimeInstance(style, style, target);
92                }
93
94                // JRE's object (if any)
95                DateFormat jresResult = null;
96                if (jreSupportsLocale) {
97                    Object[] dateTimeArgs = {jreDateTimePatterns[style],
98                                             jreDateTimePatterns[style + 4]};
99                    String pattern = MessageFormat.format(jreDateTimePatterns[8], dateTimeArgs);
100                    jresResult = new SimpleDateFormat(pattern, target);
101                }
102
103                checkValidity(target, jresResult, providersResult, result, jreSupportsLocale);
104            }
105        }
106    }
107
108    // Check that fallback correctly occurs with locales with variant including '_'s
109    // This test assumes that the provider supports the ja_JP_osaka locale, and JRE does not.
110    void extendedVariantTest() {
111        Locale[] testlocs = {new Locale("ja", "JP", "osaka_extended"),
112                             new Locale("ja", "JP", "osaka_extended_further"),
113                             new Locale("ja", "JP", "osaka_")};
114        for (Locale test: testlocs) {
115            DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
116            DateFormat provider = dfp.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
117            if (!df.equals(provider)) {
118                throw new RuntimeException("variant fallback failed. test locale: "+test);
119            }
120        }
121    }
122
123
124    private static final String[] TYPES = {
125        "date",
126        "time"
127    };
128    private static final String[] MODIFIERS = {
129        "",
130        "short",
131        "medium", // Same as DEFAULT
132        "long",
133        "full"
134    };
135
136    void messageFormatTest() {
137        for (Locale target : providerloc) {
138            for (String type : TYPES) {
139                for (String modifier : MODIFIERS) {
140                    String pattern, expected;
141                    if (modifier.equals("")) {
142                        pattern = String.format("%s={0,%s}", type, type);
143                    } else {
144                        pattern = String.format("%s={0,%s,%s}", type, type, modifier);
145                    }
146                    if (modifier.equals("medium")) {
147                        // medium is default.
148                        expected = String.format("%s={0,%s}", type, type);
149                    } else {
150                        expected = pattern;
151                    }
152                    MessageFormat mf = new MessageFormat(pattern, target);
153                    Format[] fmts = mf.getFormats();
154                    if (fmts[0] instanceof SimpleDateFormat) {
155                        continue;
156                    }
157                    String toPattern = mf.toPattern();
158                    if (!toPattern.equals(expected)) {
159                        throw new RuntimeException("messageFormatTest: got '" + toPattern
160                                                   + "', expected '" + expected + "'");
161                    }
162                }
163            }
164        }
165    }
166}
167