Skip to content

Commit ac41bc3

Browse files
author
Justin Lu
committed
8354343: Hardening of Currency tests for not yet defined future ISO 4217 currency
Reviewed-by: naoto
1 parent 8bd5645 commit ac41bc3

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

test/jdk/java/util/Currency/ValidateISO4217.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* @bug 4691089 4819436 4942982 5104960 6544471 6627549 7066203 7195759
2727
* 8039317 8074350 8074351 8145952 8187946 8193552 8202026 8204269
2828
* 8208746 8209775 8264792 8274658 8283277 8296239 8321480 8334653
29-
* 8354344
29+
* 8354343 8354344
3030
* @summary Validate ISO 4217 data for Currency class.
3131
* @modules java.base/java.util:open
3232
* jdk.localedata
@@ -71,6 +71,7 @@
7171
import java.util.Set;
7272
import java.util.StringTokenizer;
7373
import java.util.TimeZone;
74+
import java.util.stream.Collectors;
7475
import java.util.stream.Stream;
7576

7677
import org.junit.jupiter.api.BeforeAll;
@@ -111,6 +112,8 @@ public class ValidateISO4217 {
111112
private static final List<Arguments> additionalCodes = new ArrayList<Arguments>();
112113
// Currencies to test (derived from ISO4217Codes and additionalCodes)
113114
private static final Set<Currency> testCurrencies = new HashSet<>();
115+
// Special case currencies that should only exist after the cut-over occurs
116+
private static final Set<String> currenciesNotYetDefined = new HashSet<>();
114117
// Codes that are obsolete, do not have related country, extra currency
115118
private static final String otherCodes =
116119
"ADP-AFA-ATS-AYM-AZM-BEF-BGL-BOV-BYB-BYR-CHE-CHW-CLF-COU-CUC-CYP-"
@@ -235,13 +238,14 @@ private static void patchClass(String name) throws IOException {
235238
}
236239

237240
// Sets up the following test data:
238-
// ISO4217Codes, additionalCodes, testCurrencies, codes
241+
// ISO4217Codes, additionalCodes, testCurrencies, codes, currenciesNotYetDefined
239242
static void setUpTestingData() throws Exception {
240243
// These functions laterally setup 'testCurrencies' and 'codes'
241244
// at the same time
242245
setUpISO4217Codes();
243246
setUpAdditionalCodes();
244247
setUpOtherCurrencies();
248+
setUpNotYetDefined();
245249
}
246250

247251
// Parse the ISO4217 file and populate ISO4217Codes and testCurrencies.
@@ -294,6 +298,11 @@ private static void processColumns(StringTokenizer tokens, String country) throw
294298
numeric = tokens.nextToken();
295299
minorUnit = tokens.nextToken();
296300
testCurrencies.add(Currency.getInstance(currency));
301+
} else {
302+
// Add all future currencies to the set.
303+
// We process it later once 'testCurrencies' is complete
304+
// to only include ones that should not be defined yet.
305+
currenciesNotYetDefined.add(tokens.nextToken());
297306
}
298307
}
299308
}
@@ -339,6 +348,17 @@ private static void setUpOtherCurrencies() {
339348
}
340349
}
341350

351+
// Future currencies that are already defined as ISO 4217 codes should be
352+
// removed. For example, in CW=ANG;2025-04-01-04-00-00;EUR "EUR" would be
353+
// removed as it is an already valid ISO 4217 code
354+
private static void setUpNotYetDefined() {
355+
var allFutureCurrencies = testCurrencies
356+
.stream()
357+
.map(Currency::getCurrencyCode)
358+
.collect(Collectors.toSet());
359+
currenciesNotYetDefined.removeIf(allFutureCurrencies::contains);
360+
}
361+
342362
// Check that the data file is up-to-date
343363
@Test
344364
public void dataVersionTest() {
@@ -419,6 +439,18 @@ private static List<String> codeCombos() {
419439
return codeCombos;
420440
}
421441

442+
// Any future currencies that do not already exist before the cut-over
443+
// should not be instantiable. This scenario is when a country transfers
444+
// to a new code, that is not already a valid ISO 4217 code. For example,
445+
// what occurred in the 176 update situation.
446+
@Test
447+
public void nonDefinedFutureCurrenciesTest() {
448+
for (String curr : currenciesNotYetDefined) {
449+
assertThrows(IllegalArgumentException.class, () -> Currency.getInstance(curr),
450+
"The future cut-over currency: %s should not exist".formatted(curr));
451+
}
452+
}
453+
422454
// This method ensures that getAvailableCurrencies() returns
423455
// the expected amount of currencies.
424456
@Test
@@ -427,6 +459,8 @@ public void getAvailableCurrenciesTest() {
427459
// Ensure that testCurrencies has all the JRE currency codes
428460
assertTrue(testCurrencies.containsAll(jreCurrencies),
429461
getSetDiffs(jreCurrencies, testCurrencies));
462+
// Implicitly checks that jreCurrencies does not contain any currencies
463+
// defined in currenciesNotYetDefined
430464
}
431465

432466
private static String getSetDiffs(Set<Currency> jreCurrencies, Set<Currency> testCurrencies) {

0 commit comments

Comments
 (0)