8314611: Provide more explicative error message parsing Currencies

Reviewed-by: naoto
This commit is contained in:
Justin Lu 2023-08-30 23:40:34 +00:00
parent df5e6e5d48
commit 3c8a6678fe
2 changed files with 41 additions and 13 deletions

View file

@ -319,7 +319,8 @@ public final class Currency implements Serializable {
// or in the list of other currencies. // or in the list of other currencies.
boolean found = false; boolean found = false;
if (currencyCode.length() != 3) { if (currencyCode.length() != 3) {
throw new IllegalArgumentException(); throw new IllegalArgumentException("The input currency code must " +
"have a length of 3 characters");
} }
char char1 = currencyCode.charAt(0); char char1 = currencyCode.charAt(0);
char char2 = currencyCode.charAt(1); char char2 = currencyCode.charAt(1);
@ -342,7 +343,8 @@ public final class Currency implements Serializable {
if (!found) { if (!found) {
OtherCurrencyEntry ocEntry = OtherCurrencyEntry.findEntry(currencyCode); OtherCurrencyEntry ocEntry = OtherCurrencyEntry.findEntry(currencyCode);
if (ocEntry == null) { if (ocEntry == null) {
throw new IllegalArgumentException(); throw new IllegalArgumentException("The input currency code" +
" is not a valid ISO 4217 code");
} }
defaultFractionDigits = ocEntry.fraction; defaultFractionDigits = ocEntry.fraction;
numericCode = ocEntry.numericCode; numericCode = ocEntry.numericCode;
@ -397,7 +399,8 @@ public final class Currency implements Serializable {
String country = CalendarDataUtility.findRegionOverride(locale).getCountry(); String country = CalendarDataUtility.findRegionOverride(locale).getCountry();
if (country == null || !country.matches("^[a-zA-Z]{2}$")) { if (country == null || !country.matches("^[a-zA-Z]{2}$")) {
throw new IllegalArgumentException(); throw new IllegalArgumentException("The country of the input locale" +
" is not a valid ISO 3166 country code");
} }
char char1 = country.charAt(0); char char1 = country.charAt(0);
@ -414,7 +417,8 @@ public final class Currency implements Serializable {
} else { } else {
// special cases // special cases
if (tableEntry == INVALID_COUNTRY_ENTRY) { if (tableEntry == INVALID_COUNTRY_ENTRY) {
throw new IllegalArgumentException(); throw new IllegalArgumentException("The country of the input locale" +
" is not a valid ISO 3166 country code");
} }
if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) { if (tableEntry == COUNTRY_WITHOUT_CURRENCY_ENTRY) {
return null; return null;
@ -679,7 +683,8 @@ public final class Currency implements Serializable {
*/ */
private static int getMainTableEntry(char char1, char char2) { private static int getMainTableEntry(char char1, char char2) {
if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') { if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {
throw new IllegalArgumentException(); throw new IllegalArgumentException("The country code is not a " +
"valid ISO 3166 code");
} }
return mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')]; return mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')];
} }
@ -690,7 +695,8 @@ public final class Currency implements Serializable {
*/ */
private static void setMainTableEntry(char char1, char char2, int entry) { private static void setMainTableEntry(char char1, char char2, int entry) {
if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') { if (char1 < 'A' || char1 > 'Z' || char2 < 'A' || char2 > 'Z') {
throw new IllegalArgumentException(); throw new IllegalArgumentException("The country code is not a " +
"valid ISO 3166 code");
} }
mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')] = entry; mainTable[(char1 - 'A') * A_TO_Z + (char2 - 'A')] = entry;
} }

View file

@ -80,14 +80,31 @@ public class CurrencyTest {
// Calling getInstance() with an invalid currency code should throw an IAE // Calling getInstance() with an invalid currency code should throw an IAE
@ParameterizedTest @ParameterizedTest
@MethodSource("invalidCurrencies") @MethodSource("non4217Currencies")
public void invalidCurrencyTest(String currencyCode) { public void invalidCurrencyTest(String currencyCode) {
assertThrows(IllegalArgumentException.class, () -> IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
Currency.getInstance(currencyCode), "getInstance() did not throw IAE"); Currency.getInstance(currencyCode), "getInstance() did not throw IAE");
assertEquals("The input currency code is not a" +
" valid ISO 4217 code", ex.getMessage());
} }
private static Stream<String> invalidCurrencies() { private static Stream<String> non4217Currencies() {
return Stream.of("AQD", "US$", "\u20AC"); return Stream.of("AQD", "US$");
}
// Calling getInstance() with a currency code not 3 characters long should throw
// an IAE
@ParameterizedTest
@MethodSource("invalidLengthCurrencies")
public void invalidCurrencyLengthTest(String currencyCode) {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
Currency.getInstance(currencyCode), "getInstance() did not throw IAE");
assertEquals("The input currency code must have a length of 3" +
" characters", ex.getMessage());
}
private static Stream<String> invalidLengthCurrencies() {
return Stream.of("\u20AC", "", "12345");
} }
} }
@ -144,7 +161,10 @@ public class CurrencyTest {
ctryLength == 3 || // UN M.49 code ctryLength == 3 || // UN M.49 code
ctryCode.matches("AA|Q[M-Z]|X[A-JL-Z]|ZZ" + // user defined codes, excluding "XK" (Kosovo) ctryCode.matches("AA|Q[M-Z]|X[A-JL-Z]|ZZ" + // user defined codes, excluding "XK" (Kosovo)
"AC|CP|DG|EA|EU|FX|IC|SU|TA|UK")) { // exceptional reservation codes "AC|CP|DG|EA|EU|FX|IC|SU|TA|UK")) { // exceptional reservation codes
assertThrows(IllegalArgumentException.class, () -> Currency.getInstance(locale), "Did not throw IAE"); IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> Currency.getInstance(locale), "Did not throw IAE");
assertEquals("The country of the input locale is not a" +
" valid ISO 3166 country code", ex.getMessage());
} else { } else {
goodCountries++; goodCountries++;
Currency currency = Currency.getInstance(locale); Currency currency = Currency.getInstance(locale);
@ -163,8 +183,10 @@ public class CurrencyTest {
// Check an invalid country code // Check an invalid country code
@Test @Test
public void invalidCountryTest() { public void invalidCountryTest() {
assertThrows(IllegalArgumentException.class, ()-> IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
Currency.getInstance(Locale.of("", "EU")), "Did not throw IAE"); ()-> Currency.getInstance(Locale.of("", "EU")), "Did not throw IAE");
assertEquals("The country of the input locale is not a valid" +
" ISO 3166 country code", ex.getMessage());
} }
// Ensure a selection of countries have the expected currency // Ensure a selection of countries have the expected currency