author | lana |
Thu, 26 Dec 2013 12:04:16 -0800 | |
changeset 23010 | 6dadb192ad81 |
parent 15260 | 7af2d7a87806 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
15260
diff
changeset
|
2 |
* Copyright (c) 2007, 2013, 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.*; |
|
13583 | 29 |
import sun.util.locale.provider.*; |
2 | 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()); |
|
14418
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
37 |
List<Locale> jreloc = Arrays.asList(LocaleProviderAdapter.forJRE().getAvailableLocales()); |
13583 | 38 |
List<Locale> jreimplloc = Arrays.asList(LocaleProviderAdapter.forJRE().getDateFormatProvider().getAvailableLocales()); |
2 | 39 |
|
40 |
public static void main(String[] s) { |
|
41 |
new DateFormatProviderTest(); |
|
42 |
} |
|
43 |
||
44 |
DateFormatProviderTest() { |
|
14418
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
45 |
availableLocalesTest(); |
2 | 46 |
objectValidityTest(); |
47 |
extendedVariantTest(); |
|
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
48 |
messageFormatTest(); |
2 | 49 |
} |
50 |
||
14418
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
51 |
void availableLocalesTest() { |
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
52 |
Set<Locale> localesFromAPI = new HashSet<>(availloc); |
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
53 |
Set<Locale> localesExpected = new HashSet<>(jreloc); |
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
54 |
localesExpected.addAll(providerloc); |
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
55 |
if (localesFromAPI.equals(localesExpected)) { |
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
56 |
System.out.println("availableLocalesTest passed."); |
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
57 |
} else { |
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
58 |
throw new RuntimeException("availableLocalesTest failed"); |
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
59 |
} |
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
60 |
} |
967376921f20
8001205: Calendar.getDisplayName(...): Returns null when provider is SPI but there is no SPI implementation
naoto
parents:
13583
diff
changeset
|
61 |
|
2 | 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); |
|
13583 | 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; |
|
2 | 85 |
} |
86 |
// pure JRE implementation |
|
15260 | 87 |
ResourceBundle rb = ((ResourceBundleBasedAdapter)LocaleProviderAdapter.forJRE()).getLocaleData().getDateFormatData(target); |
13583 | 88 |
boolean jreSupportsLocale = jreimplloc.contains(target); |
2 | 89 |
|
90 |
// JRE string arrays |
|
13583 | 91 |
String[] jreDatePatterns = null; |
92 |
String[] jreTimePatterns = null; |
|
2 | 93 |
String[] jreDateTimePatterns = null; |
94 |
if (jreSupportsLocale) { |
|
95 |
try { |
|
13583 | 96 |
jreDatePatterns = (String[])rb.getObject(dkey); |
97 |
jreTimePatterns = (String[])rb.getObject(tkey); |
|
98 |
jreDateTimePatterns = (String[])rb.getObject(dtkey); |
|
2 | 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) { |
|
13583 | 115 |
Object[] dateTimeArgs = {jreTimePatterns[style], |
116 |
jreDatePatterns[style]}; |
|
117 |
String pattern = MessageFormat.format(jreDateTimePatterns[0], dateTimeArgs); |
|
2 | 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 |
} |
|
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
140 |
|
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
141 |
|
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
142 |
private static final String[] TYPES = { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
143 |
"date", |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
144 |
"time" |
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 |
private static final String[] MODIFIERS = { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
147 |
"", |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
148 |
"short", |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
149 |
"medium", // Same as DEFAULT |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
150 |
"long", |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
151 |
"full" |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
152 |
}; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
153 |
|
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
154 |
void messageFormatTest() { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
155 |
for (Locale target : providerloc) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
156 |
for (String type : TYPES) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
157 |
for (String modifier : MODIFIERS) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
158 |
String pattern, expected; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
159 |
if (modifier.equals("")) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
160 |
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
|
161 |
} else { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
162 |
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
|
163 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
164 |
if (modifier.equals("medium")) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
165 |
// medium is default. |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
166 |
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
|
167 |
} else { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
168 |
expected = pattern; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
169 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
170 |
MessageFormat mf = new MessageFormat(pattern, target); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
171 |
Format[] fmts = mf.getFormats(); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
172 |
if (fmts[0] instanceof SimpleDateFormat) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
173 |
continue; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
174 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
175 |
String toPattern = mf.toPattern(); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
176 |
if (!toPattern.equals(expected)) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
177 |
throw new RuntimeException("messageFormatTest: got '" + toPattern |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
178 |
+ "', expected '" + expected + "'"); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
179 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
180 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
181 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
182 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
183 |
} |
2 | 184 |
} |