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