DateFormatProviderTest.java revision 5747:131a683a2ce0
1/*
2 * Copyright (c) 2007, 2012, 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.locale.provider.*;
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> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getDateFormatProvider().getAvailableLocales());
38
39    public static void main(String[] s) {
40        new DateFormatProviderTest();
41    }
42
43    DateFormatProviderTest() {
44        objectValidityTest();
45        extendedVariantTest();
46        messageFormatTest();
47    }
48
49    void objectValidityTest() {
50
51        for (Locale target: availloc) {
52            // Get the key for the date/time patterns which is
53            // specific to each calendar system.
54            Calendar cal = Calendar.getInstance(target);
55            String dkey = "DatePatterns";
56            String tkey = "TimePatterns";
57            String dtkey = "DateTimePatterns";
58            switch (cal.getCalendarType()) {
59                case "java.util.JapaneseImperialCalendar":
60                    dkey = "japanese"+ "." + dkey;
61                    tkey = "japanese"+ "." + tkey;
62                    dtkey = "japanese"+ "." + dtkey;
63                    break;
64                case "sun.util.BuddhistCalendar":
65                    dkey = "buddhist"+ "." + dkey;
66                    tkey = "buddhist"+ "." + tkey;
67                    dtkey = "buddhist"+ "." + dtkey;
68                    break;
69                case "java.util.GregorianCalendar":
70                default:
71                    break;
72            }
73            // pure JRE implementation
74            ResourceBundle rb = LocaleProviderAdapter.forJRE().getLocaleData().getDateFormatData(target);
75            boolean jreSupportsLocale = jreimplloc.contains(target);
76
77            // JRE string arrays
78            String[] jreDatePatterns = null;
79            String[] jreTimePatterns = null;
80            String[] jreDateTimePatterns = null;
81            if (jreSupportsLocale) {
82                try {
83                    jreDatePatterns = (String[])rb.getObject(dkey);
84                    jreTimePatterns = (String[])rb.getObject(tkey);
85                    jreDateTimePatterns = (String[])rb.getObject(dtkey);
86                } catch (MissingResourceException mre) {}
87            }
88
89            for (int style = DateFormat.FULL; style <= DateFormat.SHORT; style ++) {
90                // result object
91                DateFormat result = DateFormat.getDateTimeInstance(style, style, target);
92
93                // provider's object (if any)
94                DateFormat providersResult = null;
95                if (providerloc.contains(target)) {
96                    providersResult = dfp.getDateTimeInstance(style, style, target);
97                }
98
99                // JRE's object (if any)
100                DateFormat jresResult = null;
101                if (jreSupportsLocale) {
102                    Object[] dateTimeArgs = {jreTimePatterns[style],
103                                             jreDatePatterns[style]};
104                    String pattern = MessageFormat.format(jreDateTimePatterns[0], dateTimeArgs);
105                    jresResult = new SimpleDateFormat(pattern, target);
106                }
107
108                checkValidity(target, jresResult, providersResult, result, jreSupportsLocale);
109            }
110        }
111    }
112
113    // Check that fallback correctly occurs with locales with variant including '_'s
114    // This test assumes that the provider supports the ja_JP_osaka locale, and JRE does not.
115    void extendedVariantTest() {
116        Locale[] testlocs = {new Locale("ja", "JP", "osaka_extended"),
117                             new Locale("ja", "JP", "osaka_extended_further"),
118                             new Locale("ja", "JP", "osaka_")};
119        for (Locale test: testlocs) {
120            DateFormat df = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
121            DateFormat provider = dfp.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, test);
122            if (!df.equals(provider)) {
123                throw new RuntimeException("variant fallback failed. test locale: "+test);
124            }
125        }
126    }
127
128
129    private static final String[] TYPES = {
130        "date",
131        "time"
132    };
133    private static final String[] MODIFIERS = {
134        "",
135        "short",
136        "medium", // Same as DEFAULT
137        "long",
138        "full"
139    };
140
141    void messageFormatTest() {
142        for (Locale target : providerloc) {
143            for (String type : TYPES) {
144                for (String modifier : MODIFIERS) {
145                    String pattern, expected;
146                    if (modifier.equals("")) {
147                        pattern = String.format("%s={0,%s}", type, type);
148                    } else {
149                        pattern = String.format("%s={0,%s,%s}", type, type, modifier);
150                    }
151                    if (modifier.equals("medium")) {
152                        // medium is default.
153                        expected = String.format("%s={0,%s}", type, type);
154                    } else {
155                        expected = pattern;
156                    }
157                    MessageFormat mf = new MessageFormat(pattern, target);
158                    Format[] fmts = mf.getFormats();
159                    if (fmts[0] instanceof SimpleDateFormat) {
160                        continue;
161                    }
162                    String toPattern = mf.toPattern();
163                    if (!toPattern.equals(expected)) {
164                        throw new RuntimeException("messageFormatTest: got '" + toPattern
165                                                   + "', expected '" + expected + "'");
166                    }
167                }
168            }
169        }
170    }
171}
172