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