author | xuelei |
Fri, 29 Jul 2011 02:50:58 -0700 | |
changeset 10138 | b7572da25d15 |
parent 7787 | d2420a14d0a2 |
child 13583 | dc0017b1a452 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
/* |
|
24 |
* |
|
25 |
*/ |
|
26 |
||
27 |
import java.text.*; |
|
28 |
import java.util.*; |
|
29 |
import sun.util.*; |
|
30 |
import sun.util.resources.*; |
|
31 |
||
32 |
public 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(); |
|
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
47 |
messageFormatTest(); |
2 | 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 |
} |
|
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
122 |
|
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
123 |
|
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
124 |
private static final String[] TYPES = { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
125 |
"date", |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
126 |
"time" |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
127 |
}; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
128 |
private static final String[] MODIFIERS = { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
129 |
"", |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
130 |
"short", |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
131 |
"medium", // Same as DEFAULT |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
132 |
"long", |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
133 |
"full" |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
134 |
}; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
135 |
|
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
136 |
void messageFormatTest() { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
137 |
for (Locale target : providerloc) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
138 |
for (String type : TYPES) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
139 |
for (String modifier : MODIFIERS) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
140 |
String pattern, expected; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
141 |
if (modifier.equals("")) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
142 |
pattern = String.format("%s={0,%s}", type, type); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
143 |
} else { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
144 |
pattern = String.format("%s={0,%s,%s}", type, type, modifier); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
145 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
146 |
if (modifier.equals("medium")) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
147 |
// medium is default. |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
148 |
expected = String.format("%s={0,%s}", type, type); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
149 |
} else { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
150 |
expected = pattern; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
151 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
152 |
MessageFormat mf = new MessageFormat(pattern, target); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
153 |
Format[] fmts = mf.getFormats(); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
154 |
if (fmts[0] instanceof SimpleDateFormat) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
155 |
continue; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
156 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
157 |
String toPattern = mf.toPattern(); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
158 |
if (!toPattern.equals(expected)) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
159 |
throw new RuntimeException("messageFormatTest: got '" + toPattern |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
160 |
+ "', expected '" + expected + "'"); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
161 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
162 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
163 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
164 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
165 |
} |
2 | 166 |
} |