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 |
||
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
32 |
import com.foo.FooNumberFormat; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
33 |
|
2 | 34 |
public class NumberFormatProviderTest extends ProviderTest { |
35 |
||
36 |
com.foo.NumberFormatProviderImpl nfp = new com.foo.NumberFormatProviderImpl(); |
|
37 |
List<Locale> availloc = Arrays.asList(NumberFormat.getAvailableLocales()); |
|
38 |
List<Locale> providerloc = Arrays.asList(nfp.getAvailableLocales()); |
|
39 |
List<Locale> jreloc = Arrays.asList(LocaleData.getAvailableLocales()); |
|
40 |
||
41 |
public static void main(String[] s) { |
|
42 |
new NumberFormatProviderTest(); |
|
43 |
} |
|
44 |
||
45 |
NumberFormatProviderTest() { |
|
46 |
availableLocalesTest(); |
|
47 |
objectValidityTest(); |
|
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
48 |
messageFormatTest(); |
2 | 49 |
} |
50 |
||
51 |
void availableLocalesTest() { |
|
52 |
Set<Locale> localesFromAPI = new HashSet<Locale>(availloc); |
|
53 |
Set<Locale> localesExpected = new HashSet<Locale>(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 |
// pure JRE implementation |
|
66 |
ResourceBundle rb = LocaleData.getNumberFormatData(target); |
|
67 |
boolean jreSupportsLocale = jreloc.contains(target); |
|
68 |
||
69 |
// JRE string arrays |
|
70 |
String[] jreNumberPatterns = null; |
|
71 |
if (jreSupportsLocale) { |
|
72 |
try { |
|
73 |
jreNumberPatterns = rb.getStringArray("NumberPatterns"); |
|
74 |
} catch (MissingResourceException mre) {} |
|
75 |
} |
|
76 |
||
77 |
// result object |
|
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
78 |
String resultCur = getPattern(NumberFormat.getCurrencyInstance(target)); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
79 |
String resultInt = getPattern(NumberFormat.getIntegerInstance(target)); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
80 |
String resultNum = getPattern(NumberFormat.getNumberInstance(target)); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
81 |
String resultPer = getPattern(NumberFormat.getPercentInstance(target)); |
2 | 82 |
|
83 |
// provider's object (if any) |
|
84 |
String providersCur = null; |
|
85 |
String providersInt = null; |
|
86 |
String providersNum = null; |
|
87 |
String providersPer = null; |
|
88 |
if (providerloc.contains(target)) { |
|
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
89 |
NumberFormat dfCur = nfp.getCurrencyInstance(target); |
2 | 90 |
if (dfCur != null) { |
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
91 |
providersCur = getPattern(dfCur); |
2 | 92 |
} |
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
93 |
NumberFormat dfInt = nfp.getIntegerInstance(target); |
2 | 94 |
if (dfInt != null) { |
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
95 |
providersInt = getPattern(dfInt); |
2 | 96 |
} |
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
97 |
NumberFormat dfNum = nfp.getNumberInstance(target); |
2 | 98 |
if (dfNum != null) { |
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
99 |
providersNum = getPattern(dfNum); |
2 | 100 |
} |
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
101 |
NumberFormat dfPer = nfp.getPercentInstance(target); |
2 | 102 |
if (dfPer != null) { |
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
103 |
providersPer = getPattern(dfPer); |
2 | 104 |
} |
105 |
} |
|
106 |
||
107 |
// JRE's object (if any) |
|
108 |
// note that this totally depends on the current implementation |
|
109 |
String jresCur = null; |
|
110 |
String jresInt = null; |
|
111 |
String jresNum = null; |
|
112 |
String jresPer = null; |
|
113 |
if (jreSupportsLocale) { |
|
114 |
DecimalFormat dfCur = new DecimalFormat(jreNumberPatterns[1], |
|
115 |
DecimalFormatSymbols.getInstance(target)); |
|
116 |
if (dfCur != null) { |
|
117 |
adjustForCurrencyDefaultFractionDigits(dfCur); |
|
118 |
jresCur = dfCur.toPattern(); |
|
119 |
} |
|
120 |
DecimalFormat dfInt = new DecimalFormat(jreNumberPatterns[0], |
|
121 |
DecimalFormatSymbols.getInstance(target)); |
|
122 |
if (dfInt != null) { |
|
123 |
dfInt.setMaximumFractionDigits(0); |
|
124 |
dfInt.setDecimalSeparatorAlwaysShown(false); |
|
125 |
dfInt.setParseIntegerOnly(true); |
|
126 |
jresInt = dfInt.toPattern(); |
|
127 |
} |
|
128 |
DecimalFormat dfNum = new DecimalFormat(jreNumberPatterns[0], |
|
129 |
DecimalFormatSymbols.getInstance(target)); |
|
130 |
if (dfNum != null) { |
|
131 |
jresNum = dfNum.toPattern(); |
|
132 |
} |
|
133 |
DecimalFormat dfPer = new DecimalFormat(jreNumberPatterns[2], |
|
134 |
DecimalFormatSymbols.getInstance(target)); |
|
135 |
if (dfPer != null) { |
|
136 |
jresPer = dfPer.toPattern(); |
|
137 |
} |
|
138 |
} |
|
139 |
||
140 |
checkValidity(target, jresCur, providersCur, resultCur, jreSupportsLocale); |
|
141 |
checkValidity(target, jresInt, providersInt, resultInt, jreSupportsLocale); |
|
142 |
checkValidity(target, jresNum, providersNum, resultNum, jreSupportsLocale); |
|
143 |
checkValidity(target, jresPer, providersPer, resultPer, jreSupportsLocale); |
|
144 |
} |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Adjusts the minimum and maximum fraction digits to values that |
|
149 |
* are reasonable for the currency's default fraction digits. |
|
150 |
*/ |
|
151 |
void adjustForCurrencyDefaultFractionDigits(DecimalFormat df) { |
|
152 |
DecimalFormatSymbols dfs = df.getDecimalFormatSymbols(); |
|
153 |
Currency currency = dfs.getCurrency(); |
|
154 |
if (currency == null) { |
|
155 |
try { |
|
156 |
currency = Currency.getInstance(dfs.getInternationalCurrencySymbol()); |
|
157 |
} catch (IllegalArgumentException e) { |
|
158 |
} |
|
159 |
} |
|
160 |
if (currency != null) { |
|
161 |
int digits = currency.getDefaultFractionDigits(); |
|
162 |
if (digits != -1) { |
|
163 |
int oldMinDigits = df.getMinimumFractionDigits(); |
|
164 |
// Common patterns are "#.##", "#.00", "#". |
|
165 |
// Try to adjust all of them in a reasonable way. |
|
166 |
if (oldMinDigits == df.getMaximumFractionDigits()) { |
|
167 |
df.setMinimumFractionDigits(digits); |
|
168 |
df.setMaximumFractionDigits(digits); |
|
169 |
} else { |
|
170 |
df.setMinimumFractionDigits(Math.min(digits, oldMinDigits)); |
|
171 |
df.setMaximumFractionDigits(digits); |
|
172 |
} |
|
173 |
} |
|
174 |
} |
|
175 |
} |
|
7787
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
176 |
|
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
177 |
private static String getPattern(NumberFormat nf) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
178 |
if (nf instanceof DecimalFormat) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
179 |
return ((DecimalFormat)nf).toPattern(); |
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 |
if (nf instanceof FooNumberFormat) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
182 |
return ((FooNumberFormat)nf).toPattern(); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
183 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
184 |
return null; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
185 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
186 |
|
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
187 |
private static final String[] NUMBER_PATTERNS = { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
188 |
"num={0,number}", |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
189 |
"num={0,number,currency}", |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
190 |
"num={0,number,percent}", |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
191 |
"num={0,number,integer}" |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
192 |
}; |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
193 |
|
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
194 |
void messageFormatTest() { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
195 |
for (Locale target : providerloc) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
196 |
for (String pattern : NUMBER_PATTERNS) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
197 |
MessageFormat mf = new MessageFormat(pattern, target); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
198 |
String toPattern = mf.toPattern(); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
199 |
if (!pattern.equals(toPattern)) { |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
200 |
throw new RuntimeException("MessageFormat.toPattern: got '" |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
201 |
+ toPattern |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
202 |
+ "', expected '" + pattern + "'"); |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
203 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
204 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
205 |
} |
d2420a14d0a2
7003643: [Fmt-Me] MessageFormat.toPattern produces wrong quoted string and subformat modifiers
okutsu
parents:
5506
diff
changeset
|
206 |
} |
2 | 207 |
} |