Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Currency/CurrencyTest.java
41149 views
1
/*
2
* Copyright (c) 2007, 2016, 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
* @test
25
* @bug 4290801 4692419 4693631 5101540 5104960 6296410 6336600 6371531
26
* 6488442 7036905 8008577 8039317 8074350 8074351 8150324 8167143
27
* @summary Basic tests for Currency class.
28
* @modules java.base/java.util:open
29
* jdk.localedata
30
*/
31
32
import java.io.ByteArrayInputStream;
33
import java.io.ByteArrayOutputStream;
34
import java.io.ObjectInputStream;
35
import java.io.ObjectOutputStream;
36
import java.time.LocalDate;
37
import java.time.LocalTime;
38
import java.time.ZoneId;
39
import java.time.ZonedDateTime;
40
import java.util.Currency;
41
import java.util.Locale;
42
43
44
public class CurrencyTest {
45
46
public static void main(String[] args) throws Exception {
47
CheckDataVersion.check();
48
testCurrencyCodeValidation();
49
testLocaleMapping();
50
testSymbols();
51
testFractionDigits();
52
testSerialization();
53
testDisplayNames();
54
testFundsCodes();
55
}
56
57
static void testCurrencyCodeValidation() {
58
// test creation of some valid currencies
59
testValidCurrency("USD");
60
testValidCurrency("EUR");
61
testValidCurrency("GBP");
62
testValidCurrency("JPY");
63
testValidCurrency("CNY");
64
testValidCurrency("CHF");
65
66
// test creation of some fictitious currencies
67
testInvalidCurrency("AQD");
68
testInvalidCurrency("US$");
69
testInvalidCurrency("\u20AC");
70
}
71
72
static void testValidCurrency(String currencyCode) {
73
Currency currency1 = Currency.getInstance(currencyCode);
74
Currency currency2 = Currency.getInstance(currencyCode);
75
if (currency1 != currency2) {
76
throw new RuntimeException("Didn't get same instance for same currency code");
77
}
78
if (!currency1.getCurrencyCode().equals(currencyCode)) {
79
throw new RuntimeException("Currency code changed");
80
}
81
}
82
83
static void testInvalidCurrency(String currencyCode) {
84
boolean gotException = false;
85
try {
86
Currency currency = Currency.getInstance(currencyCode);
87
} catch (IllegalArgumentException e) {
88
gotException = true;
89
}
90
if (!gotException) {
91
throw new RuntimeException("didn't get specified exception");
92
}
93
}
94
95
static void testLocaleMapping() {
96
// very basic test: most countries have their own currency, and then
97
// their currency code is an extension of their country code.
98
Locale[] locales = Locale.getAvailableLocales();
99
int goodCountries = 0;
100
int ownCurrencies = 0;
101
for (int i = 0; i < locales.length; i++) {
102
Locale locale = locales[i];
103
String ctryCode = locale.getCountry();
104
int ctryLength = ctryCode.length();
105
if (ctryLength == 0 ||
106
ctryLength == 3 || // UN M.49 code
107
ctryCode.matches("AA|Q[M-Z]|X[A-Z]|ZZ" + // user defined codes
108
"AC|CP|DG|EA|EU|FX|IC|SU|TA|UK")) { // exceptional reservation codes
109
boolean gotException = false;
110
try {
111
Currency.getInstance(locale);
112
} catch (IllegalArgumentException e) {
113
gotException = true;
114
}
115
if (!gotException) {
116
throw new RuntimeException("didn't get specified exception");
117
}
118
} else {
119
goodCountries++;
120
Currency currency = Currency.getInstance(locale);
121
if (currency.getCurrencyCode().indexOf(locale.getCountry()) == 0) {
122
ownCurrencies++;
123
}
124
}
125
}
126
System.out.println("Countries tested: " + goodCountries +
127
", own currencies: " + ownCurrencies);
128
if (ownCurrencies < (goodCountries / 2 + 1)) {
129
throw new RuntimeException("suspicious: not enough countries have their own currency.");
130
}
131
132
// check a few countries that don't change their currencies too often
133
String[] country1 = {"US", "CA", "JP", "CN", "SG", "CH"};
134
String[] currency1 = {"USD", "CAD", "JPY", "CNY", "SGD", "CHF"};
135
for (int i = 0; i < country1.length; i++) {
136
checkCountryCurrency(country1[i], currency1[i]);
137
}
138
139
/*
140
* check currency changes
141
* In current implementation, there is no data of old currency and transition date at jdk/make/data/currency/CurrencyData.properties.
142
* So, all the switch data arrays are empty. In the future, if data of old currency and transition date are necessary for any country, the
143
* arrays here can be updated so that the program can check the currency switch.
144
*/
145
String[] switchOverCtry = {};
146
String[] switchOverOld = {};
147
String[] switchOverNew = {};
148
String[] switchOverTZ = {};
149
int[] switchOverYear = {};
150
int[] switchOverMonth = {}; // java.time APIs accept month starting from 1 i.e. 01 for January
151
int[] switchOverDay = {};
152
153
for (int i = 0; i < switchOverCtry.length; i++) {
154
ZoneId zoneId = ZoneId.of(switchOverTZ[i]);
155
ZonedDateTime zonedDateAndTime = ZonedDateTime.of(LocalDate.of(switchOverYear[i], switchOverMonth[i], switchOverDay[i]),
156
LocalTime.MIDNIGHT, zoneId);
157
ZonedDateTime currentZonedDateAndTime = ZonedDateTime.now(zoneId);
158
checkCountryCurrency(switchOverCtry[i], (currentZonedDateAndTime.isAfter(zonedDateAndTime) ||
159
currentZonedDateAndTime.isEqual(zonedDateAndTime)) ? switchOverNew[i] : switchOverOld[i]);
160
}
161
162
// check a country code which doesn't have a currency
163
checkCountryCurrency("AQ", null);
164
165
// check an invalid country code
166
boolean gotException = false;
167
try {
168
Currency.getInstance(new Locale("", "EU"));
169
} catch (IllegalArgumentException e) {
170
gotException = true;
171
}
172
if (!gotException) {
173
throw new RuntimeException("didn't get specified exception.");
174
}
175
}
176
177
static void checkCountryCurrency(String countryCode, String expected) {
178
Locale locale = new Locale("", countryCode);
179
Currency currency = Currency.getInstance(locale);
180
String code = (currency != null) ? currency.getCurrencyCode() : null;
181
if (!(expected == null ? code == null : expected.equals(code))) {
182
throw new RuntimeException("Wrong currency for " +
183
locale.getDisplayCountry() +
184
": expected " + expected + ", got " + code);
185
}
186
}
187
188
static void testSymbols() {
189
testSymbol("USD", Locale.US, "$");
190
testSymbol("EUR", Locale.GERMANY, "\u20AC");
191
testSymbol("USD", Locale.PRC, "US$");
192
}
193
194
static void testSymbol(String currencyCode, Locale locale, String expectedSymbol) {
195
String symbol = Currency.getInstance(currencyCode).getSymbol(locale);
196
if (!symbol.equals(expectedSymbol)) {
197
throw new RuntimeException("Wrong symbol for currency " +
198
currencyCode +": expected " + expectedSymbol +
199
", got " + symbol);
200
}
201
}
202
203
static void testFractionDigits() {
204
testFractionDigits("USD", 2);
205
testFractionDigits("EUR", 2);
206
testFractionDigits("JPY", 0);
207
testFractionDigits("XDR", -1);
208
209
testFractionDigits("BHD", 3);
210
testFractionDigits("IQD", 3);
211
testFractionDigits("JOD", 3);
212
testFractionDigits("KWD", 3);
213
testFractionDigits("LYD", 3);
214
testFractionDigits("OMR", 3);
215
testFractionDigits("TND", 3);
216
217
// Turkish Lira
218
testFractionDigits("TRL", 0);
219
testFractionDigits("TRY", 2);
220
}
221
222
static void testFractionDigits(String currencyCode, int expectedFractionDigits) {
223
int digits = Currency.getInstance(currencyCode).getDefaultFractionDigits();
224
if (digits != expectedFractionDigits) {
225
throw new RuntimeException("Wrong number of fraction digits for currency " +
226
currencyCode +": expected " + expectedFractionDigits +
227
", got " + digits);
228
}
229
}
230
231
static void testSerialization() throws Exception {
232
Currency currency1 = Currency.getInstance("DEM");
233
234
ByteArrayOutputStream baos = new ByteArrayOutputStream();
235
ObjectOutputStream oStream = new ObjectOutputStream(baos);
236
oStream.writeObject(currency1);
237
oStream.flush();
238
byte[] bytes = baos.toByteArray();
239
240
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
241
ObjectInputStream iStream = new ObjectInputStream(bais);
242
Currency currency2 = (Currency) iStream.readObject();
243
244
if (currency1 != currency2) {
245
throw new RuntimeException("serialization breaks class invariant");
246
}
247
}
248
249
static void testDisplayNames() {
250
// null argument test
251
try {
252
testDisplayName("USD", null, "");
253
throw new RuntimeException("getDisplayName(NULL) did not throw an NPE.");
254
} catch (NullPointerException npe) {}
255
256
testDisplayName("USD", Locale.ENGLISH, "US Dollar");
257
testDisplayName("FRF", Locale.FRENCH, "franc fran\u00e7ais");
258
testDisplayName("DEM", Locale.GERMAN, "Deutsche Mark");
259
testDisplayName("ESP", new Locale("es"), "peseta espa\u00f1ola");
260
testDisplayName("ITL", new Locale("it"), "lira italiana");
261
testDisplayName("JPY", Locale.JAPANESE, "\u65e5\u672c\u5186");
262
testDisplayName("KRW", Locale.KOREAN, "\ub300\ud55c\ubbfc\uad6d \uc6d0");
263
testDisplayName("SEK", new Locale("sv"), "svensk krona");
264
testDisplayName("CNY", Locale.SIMPLIFIED_CHINESE, "\u4eba\u6c11\u5e01");
265
testDisplayName("TWD", Locale.TRADITIONAL_CHINESE, "\u65b0\u53f0\u5e63");
266
}
267
268
static void testDisplayName(String currencyCode, Locale locale, String expectedName) {
269
String name = Currency.getInstance(currencyCode).getDisplayName(locale);
270
if (!name.equals(expectedName)) {
271
throw new RuntimeException("Wrong display name for currency " +
272
currencyCode +": expected '" + expectedName +
273
"', got '" + name + "'");
274
}
275
}
276
static void testFundsCodes() {
277
testValidCurrency("BOV");
278
testValidCurrency("CHE");
279
testValidCurrency("CHW");
280
testValidCurrency("CLF");
281
testValidCurrency("COU");
282
testValidCurrency("MXV");
283
testValidCurrency("USN");
284
testValidCurrency("UYI");
285
286
testFractionDigits("BOV", 2);
287
testFractionDigits("CHE", 2);
288
testFractionDigits("CHW", 2);
289
testFractionDigits("CLF", 4);
290
testFractionDigits("COU", 2);
291
testFractionDigits("MXV", 2);
292
testFractionDigits("USN", 2);
293
testFractionDigits("UYI", 0);
294
295
testNumericCode("BOV", 984);
296
testNumericCode("CHE", 947);
297
testNumericCode("CHW", 948);
298
testNumericCode("CLF", 990);
299
testNumericCode("COU", 970);
300
testNumericCode("MXV", 979);
301
testNumericCode("USN", 997);
302
testNumericCode("UYI", 940);
303
}
304
305
static void testNumericCode(String currencyCode, int expectedNumeric) {
306
int numeric = Currency.getInstance(currencyCode).getNumericCode();
307
if (numeric != expectedNumeric) {
308
throw new RuntimeException("Wrong numeric code for currency " +
309
currencyCode +": expected " + expectedNumeric +
310
", got " + numeric);
311
}
312
}
313
}
314
315