Skip to content

Commit 4e4f759

Browse files
committed
8314611: Provide more explicative error message parsing Currencies
Backport-of: 3c8a6678feac8e3225bc1c44593a78d9e7c4d77c
1 parent 06ff0d6 commit 4e4f759

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

src/java.base/share/classes/java/util/Currency.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ private static Currency getInstance(String currencyCode, int defaultFractionDigi
319319
// or in the list of other currencies.
320320
boolean found = false;
321321
if (currencyCode.length() != 3) {
322-
throw new IllegalArgumentException();
322+
throw new IllegalArgumentException("The input currency code must " +
323+
"have a length of 3 characters");
323324
}
324325
char char1 = currencyCode.charAt(0);
325326
char char2 = currencyCode.charAt(1);
@@ -342,7 +343,8 @@ private static Currency getInstance(String currencyCode, int defaultFractionDigi
342343
if (!found) {
343344
OtherCurrencyEntry ocEntry = OtherCurrencyEntry.findEntry(currencyCode);
344345
if (ocEntry == null) {
345-
throw new IllegalArgumentException();
346+
throw new IllegalArgumentException("The input currency code" +
347+
" is not a valid ISO 4217 code");
346348
}
347349
defaultFractionDigits = ocEntry.fraction;
348350
numericCode = ocEntry.numericCode;
@@ -397,7 +399,8 @@ public static Currency getInstance(Locale locale) {
397399
String country = CalendarDataUtility.findRegionOverride(locale).getCountry();
398400

399401
if (country == null || !country.matches("^[a-zA-Z]{2}$")) {
400-
throw new IllegalArgumentException();
402+
throw new IllegalArgumentException("The country of the input locale" +
403+
" is not a valid ISO 3166 country code");
401404
}
402405

403406
char char1 = country.charAt(0);
@@ -414,7 +417,8 @@ public static Currency getInstance(Locale locale) {
414417
} else {
415418
// special cases
416419
if (tableEntry == INVALID_COUNTRY_ENTRY) {
417-
throw new IllegalArgumentException();
420+
throw new IllegalArgumentException("The country of the input locale" +
421+
" is not a valid ISO 3166 country code");
418422
}
419423
if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) {
420424
return null;
@@ -679,7 +683,8 @@ private Object readResolve() {
679683
*/
680684
private static int getMainTableEntry(char char1, char char2) {
681685
if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {
682-
throw new IllegalArgumentException();
686+
throw new IllegalArgumentException("The country code is not a " +
687+
"valid ISO 3166 code");
683688
}
684689
return mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')];
685690
}
@@ -690,7 +695,8 @@ private static int getMainTableEntry(char char1, char char2) {
690695
*/
691696
private static void setMainTableEntry(char char1, char char2, int entry) {
692697
if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {
693-
throw new IllegalArgumentException();
698+
throw new IllegalArgumentException("The country code is not a " +
699+
"valid ISO 3166 code");
694700
}
695701
mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')] = entry;
696702
}

test/jdk/java/util/Currency/CurrencyTest.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,31 @@ private static Stream<String> validCurrencies() {
8080

8181
// Calling getInstance() with an invalid currency code should throw an IAE
8282
@ParameterizedTest
83-
@MethodSource("invalidCurrencies")
83+
@MethodSource("non4217Currencies")
8484
public void invalidCurrencyTest(String currencyCode) {
85-
assertThrows(IllegalArgumentException.class, () ->
85+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
8686
Currency.getInstance(currencyCode), "getInstance() did not throw IAE");
87+
assertEquals("The input currency code is not a" +
88+
" valid ISO 4217 code", ex.getMessage());
8789
}
8890

89-
private static Stream<String> invalidCurrencies() {
90-
return Stream.of("AQD", "US$", "\u20AC");
91+
private static Stream<String> non4217Currencies() {
92+
return Stream.of("AQD", "US$");
93+
}
94+
95+
// Calling getInstance() with a currency code not 3 characters long should throw
96+
// an IAE
97+
@ParameterizedTest
98+
@MethodSource("invalidLengthCurrencies")
99+
public void invalidCurrencyLengthTest(String currencyCode) {
100+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
101+
Currency.getInstance(currencyCode), "getInstance() did not throw IAE");
102+
assertEquals("The input currency code must have a length of 3" +
103+
" characters", ex.getMessage());
104+
}
105+
106+
private static Stream<String> invalidLengthCurrencies() {
107+
return Stream.of("\u20AC", "", "12345");
91108
}
92109
}
93110

@@ -144,7 +161,10 @@ public void localeMappingTest() {
144161
ctryLength == 3 || // UN M.49 code
145162
ctryCode.matches("AA|Q[M-Z]|X[A-JL-Z]|ZZ" + // user defined codes, excluding "XK" (Kosovo)
146163
"AC|CP|DG|EA|EU|FX|IC|SU|TA|UK")) { // exceptional reservation codes
147-
assertThrows(IllegalArgumentException.class, () -> Currency.getInstance(locale), "Did not throw IAE");
164+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
165+
() -> Currency.getInstance(locale), "Did not throw IAE");
166+
assertEquals("The country of the input locale is not a" +
167+
" valid ISO 3166 country code", ex.getMessage());
148168
} else {
149169
goodCountries++;
150170
Currency currency = Currency.getInstance(locale);
@@ -163,8 +183,10 @@ public void localeMappingTest() {
163183
// Check an invalid country code
164184
@Test
165185
public void invalidCountryTest() {
166-
assertThrows(IllegalArgumentException.class, ()->
167-
Currency.getInstance(Locale.of("", "EU")), "Did not throw IAE");
186+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
187+
()-> Currency.getInstance(Locale.of("", "EU")), "Did not throw IAE");
188+
assertEquals("The country of the input locale is not a valid" +
189+
" ISO 3166 country code", ex.getMessage());
168190
}
169191

170192
// Ensure a selection of countries have the expected currency

0 commit comments

Comments
 (0)