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