Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/java/util/Currency/ValidateISO4217.java
66644 views
1
/*
2
* Copyright (c) 2007, 2022, 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 4691089 4819436 4942982 5104960 6544471 6627549 7066203 7195759
26
* 8039317 8074350 8074351 8145952 8187946 8193552 8202026 8204269
27
* 8208746 8209775 8264792 8274658 8283277
28
* @summary Validate ISO 4217 data for Currency class.
29
* @modules java.base/java.util:open
30
* jdk.localedata
31
*/
32
33
/*
34
* ############################################################################
35
*
36
* ValidateISO4217 is a tool to detect differences between the latest ISO 4217
37
* data and and Java's currency data which is based on ISO 4217.
38
* If there is a difference, the following file which includes currency data
39
* may need to be updated.
40
* src/share/classes/java/util/CurrencyData.properties
41
*
42
* ############################################################################
43
*
44
* 1) Make a golden-data file.
45
* From BSi's ISO4217 data (TABLE A1.doc), extract four (or eight, if currency is changing)
46
* fields and save as ./tablea1.txt.
47
* <Country code>\t<Currency code>\t<Numeric code>\t<Minor unit>[\t<Cutover Date>\t<new Currency code>\t<new Numeric code>\t<new Minor unit>]
48
* The Cutover Date is given in SimpleDateFormat's 'yyyy-MM-dd-HH-mm-ss' format in the GMT time zone.
49
*
50
* 2) Compile ValidateISO4217.java
51
*
52
* 3) Execute ValidateISO4217 as follows:
53
* java ValidateISO4217
54
*/
55
56
import java.io.*;
57
import java.text.*;
58
import java.util.*;
59
60
public class ValidateISO4217 {
61
62
static final int ALPHA_NUM = 26;
63
64
static final byte UNDEFINED = 0;
65
static final byte DEFINED = 1;
66
static final byte SKIPPED = 2;
67
68
/* input files */
69
static final String datafile = "tablea1.txt";
70
71
/* alpha2-code table */
72
static byte[] codes = new byte[ALPHA_NUM * ALPHA_NUM];
73
74
static final String[][] additionalCodes = {
75
/* Defined in ISO 4217 list, but don't have code and minor unit info. */
76
{"AQ", "", "", "0"}, // Antarctica
77
78
/*
79
* Defined in ISO 4217 list, but don't have code and minor unit info in
80
* it. On the other hand, both code and minor unit are defined in
81
* .properties file. I don't know why, though.
82
*/
83
{"GS", "GBP", "826", "2"}, // South Georgia And The South Sandwich Islands
84
85
/* Not defined in ISO 4217 list, but defined in .properties file. */
86
{"AX", "EUR", "978", "2"}, // \u00c5LAND ISLANDS
87
{"PS", "ILS", "376", "2"}, // Palestinian Territory, Occupied
88
89
/* Not defined in ISO 4217 list, but added in ISO 3166 country code list */
90
{"JE", "GBP", "826", "2"}, // Jersey
91
{"GG", "GBP", "826", "2"}, // Guernsey
92
{"IM", "GBP", "826", "2"}, // Isle of Man
93
{"BL", "EUR", "978", "2"}, // Saint Barthelemy
94
{"MF", "EUR", "978", "2"}, // Saint Martin
95
96
/* Defined neither in ISO 4217 nor ISO 3166 list */
97
{"XK", "EUR", "978", "2"}, // Kosovo
98
};
99
100
/* Codes that are obsolete, do not have related country, extra currency */
101
static final String otherCodes =
102
"ADP-AFA-ATS-AYM-AZM-BEF-BGL-BOV-BYB-BYR-CHE-CHW-CLF-COU-CUC-CYP-"
103
+ "DEM-EEK-ESP-FIM-FRF-GHC-GRD-GWP-IEP-ITL-LTL-LUF-LVL-MGF-MRO-MTL-MXV-MZM-NLG-"
104
+ "PTE-ROL-RUR-SDD-SIT-SLL-SKK-SRG-STD-TMM-TPE-TRL-VEF-UYI-USN-USS-VEB-VED-"
105
+ "XAG-XAU-XBA-XBB-XBC-XBD-XDR-XFO-XFU-XPD-XPT-XSU-XTS-XUA-XXX-"
106
+ "YUM-ZMK-ZWD-ZWN-ZWR";
107
108
static boolean err = false;
109
110
static Set<Currency> testCurrencies = new HashSet<Currency>();
111
112
public static void main(String[] args) throws Exception {
113
CheckDataVersion.check();
114
test1();
115
test2();
116
getAvailableCurrenciesTest();
117
118
if (err) {
119
throw new RuntimeException("Failed: Validation ISO 4217 data");
120
}
121
}
122
123
static void test1() throws Exception {
124
125
try (FileReader fr = new FileReader(new File(System.getProperty("test.src", "."), datafile));
126
BufferedReader in = new BufferedReader(fr))
127
{
128
String line;
129
SimpleDateFormat format = null;
130
131
while ((line = in.readLine()) != null) {
132
if (line.length() == 0 || line.charAt(0) == '#') {
133
continue;
134
}
135
136
StringTokenizer tokens = new StringTokenizer(line, "\t");
137
String country = tokens.nextToken();
138
if (country.length() != 2) {
139
continue;
140
}
141
142
String currency;
143
String numeric;
144
String minorUnit;
145
int tokensCount = tokens.countTokens();
146
if (tokensCount < 3) {
147
currency = "";
148
numeric = "0";
149
minorUnit = "0";
150
} else {
151
currency = tokens.nextToken();
152
numeric = tokens.nextToken();
153
minorUnit = tokens.nextToken();
154
testCurrencies.add(Currency.getInstance(currency));
155
156
// check for the cutover
157
if (tokensCount > 3) {
158
if (format == null) {
159
format = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.US);
160
format.setTimeZone(TimeZone.getTimeZone("GMT"));
161
format.setLenient(false);
162
}
163
if (format.parse(tokens.nextToken()).getTime() <
164
System.currentTimeMillis()) {
165
currency = tokens.nextToken();
166
numeric = tokens.nextToken();
167
minorUnit = tokens.nextToken();
168
testCurrencies.add(Currency.getInstance(currency));
169
}
170
}
171
}
172
int index = toIndex(country);
173
testCountryCurrency(country, currency, Integer.parseInt(numeric),
174
Integer.parseInt(minorUnit), index);
175
}
176
}
177
178
for (int i = 0; i < additionalCodes.length; i++) {
179
int index = toIndex(additionalCodes[i][0]);
180
if (additionalCodes[i][1].length() != 0) {
181
testCountryCurrency(additionalCodes[i][0], additionalCodes[i][1],
182
Integer.parseInt(additionalCodes[i][2]),
183
Integer.parseInt(additionalCodes[i][3]), index);
184
testCurrencies.add(Currency.getInstance(additionalCodes[i][1]));
185
} else {
186
codes[index] = SKIPPED;
187
}
188
}
189
}
190
191
static int toIndex(String s) {
192
return ((s.charAt(0) - 'A') * ALPHA_NUM + s.charAt(1) - 'A');
193
}
194
195
static void testCountryCurrency(String country, String currencyCode,
196
int numericCode, int digits, int index) {
197
if (currencyCode.length() == 0) {
198
return;
199
}
200
testCurrencyDefined(currencyCode, numericCode, digits);
201
202
Locale loc = new Locale("", country);
203
try {
204
Currency currency = Currency.getInstance(loc);
205
if (!currency.getCurrencyCode().equals(currencyCode)) {
206
System.err.println("Error: [" + country + ":" +
207
loc.getDisplayCountry() + "] expected: " + currencyCode +
208
", got: " + currency.getCurrencyCode());
209
err = true;
210
}
211
212
if (codes[index] != UNDEFINED) {
213
System.out.println("Warning: [" + country + ":" +
214
loc.getDisplayCountry() +
215
"] multiple definitions. currency code=" + currencyCode);
216
}
217
codes[index] = DEFINED;
218
}
219
catch (Exception e) {
220
System.err.println("Error: " + e + ": Country=" + country);
221
err = true;
222
}
223
}
224
225
static void testCurrencyDefined(String currencyCode, int numericCode, int digits) {
226
try {
227
Currency currency = currency = Currency.getInstance(currencyCode);
228
229
if (currency.getNumericCode() != numericCode) {
230
System.err.println("Error: [" + currencyCode + "] expected: " +
231
numericCode + "; got: " + currency.getNumericCode());
232
err = true;
233
}
234
235
if (currency.getDefaultFractionDigits() != digits) {
236
System.err.println("Error: [" + currencyCode + "] expected: " +
237
digits + "; got: " + currency.getDefaultFractionDigits());
238
err = true;
239
}
240
}
241
catch (Exception e) {
242
System.err.println("Error: " + e + ": Currency code=" +
243
currencyCode);
244
err = true;
245
}
246
}
247
248
static void test2() {
249
for (int i = 0; i < ALPHA_NUM; i++) {
250
for (int j = 0; j < ALPHA_NUM; j++) {
251
char[] code = new char[2];
252
code[0] = (char)('A'+ i);
253
code[1] = (char)('A'+ j);
254
String country = new String(code);
255
boolean ex;
256
257
if (codes[toIndex(country)] == UNDEFINED) {
258
ex = false;
259
try {
260
Currency.getInstance(new Locale("", country));
261
}
262
catch (IllegalArgumentException e) {
263
ex = true;
264
}
265
if (!ex) {
266
System.err.println("Error: This should be an undefined code and throw IllegalArgumentException: " +
267
country);
268
err = true;
269
}
270
} else if (codes[toIndex(country)] == SKIPPED) {
271
Currency cur = null;
272
try {
273
cur = Currency.getInstance(new Locale("", country));
274
}
275
catch (Exception e) {
276
System.err.println("Error: " + e + ": Country=" +
277
country);
278
err = true;
279
}
280
if (cur != null) {
281
System.err.println("Error: Currency.getInstance() for an this locale should return null: " +
282
country);
283
err = true;
284
}
285
}
286
}
287
}
288
}
289
290
/**
291
* This test depends on test1(), where 'testCurrencies' set is constructed
292
*/
293
static void getAvailableCurrenciesTest() {
294
Set<Currency> jreCurrencies = Currency.getAvailableCurrencies();
295
296
// add otherCodes
297
StringTokenizer st = new StringTokenizer(otherCodes, "-");
298
while (st.hasMoreTokens()) {
299
testCurrencies.add(Currency.getInstance(st.nextToken()));
300
}
301
302
if (!testCurrencies.containsAll(jreCurrencies)) {
303
System.err.print("Error: getAvailableCurrencies() returned extra currencies than expected: ");
304
jreCurrencies.removeAll(testCurrencies);
305
for (Currency c : jreCurrencies) {
306
System.err.print(" "+c);
307
}
308
System.err.println();
309
err = true;
310
}
311
}
312
}
313
314