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