8314611: Provide more explicative error message parsing Currencies
Reviewed-by: naoto
This commit is contained in:
parent
df5e6e5d48
commit
3c8a6678fe
@ -319,7 +319,8 @@ public final class Currency implements Serializable {
|
||||
// or in the list of other currencies.
|
||||
boolean found = false;
|
||||
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 char2 = currencyCode.charAt(1);
|
||||
@ -342,7 +343,8 @@ public final class Currency implements Serializable {
|
||||
if (!found) {
|
||||
OtherCurrencyEntry ocEntry = OtherCurrencyEntry.findEntry(currencyCode);
|
||||
if (ocEntry == null) {
|
||||
throw new IllegalArgumentException();
|
||||
throw new IllegalArgumentException("The input currency code" +
|
||||
" is not a valid ISO 4217 code");
|
||||
}
|
||||
defaultFractionDigits = ocEntry.fraction;
|
||||
numericCode = ocEntry.numericCode;
|
||||
@ -397,7 +399,8 @@ public final class Currency implements Serializable {
|
||||
String country = CalendarDataUtility.findRegionOverride(locale).getCountry();
|
||||
|
||||
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);
|
||||
@ -414,7 +417,8 @@ public final class Currency implements Serializable {
|
||||
} else {
|
||||
// special cases
|
||||
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) {
|
||||
return null;
|
||||
@ -679,7 +683,8 @@ public final class Currency implements Serializable {
|
||||
*/
|
||||
private static int getMainTableEntry(char char1, char char2) {
|
||||
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')];
|
||||
}
|
||||
@ -690,7 +695,8 @@ public final class Currency implements Serializable {
|
||||
*/
|
||||
private static void setMainTableEntry(char char1, char char2, int entry) {
|
||||
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;
|
||||
}
|
||||
|
@ -80,14 +80,31 @@ public class CurrencyTest {
|
||||
|
||||
// Calling getInstance() with an invalid currency code should throw an IAE
|
||||
@ParameterizedTest
|
||||
@MethodSource("invalidCurrencies")
|
||||
@MethodSource("non4217Currencies")
|
||||
public void invalidCurrencyTest(String currencyCode) {
|
||||
assertThrows(IllegalArgumentException.class, () ->
|
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () ->
|
||||
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() {
|
||||
return Stream.of("AQD", "US$", "\u20AC");
|
||||
private static Stream<String> non4217Currencies() {
|
||||
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
|
||||
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
|
||||
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 {
|
||||
goodCountries++;
|
||||
Currency currency = Currency.getInstance(locale);
|
||||
@ -163,8 +183,10 @@ public class CurrencyTest {
|
||||
// Check an invalid country code
|
||||
@Test
|
||||
public void invalidCountryTest() {
|
||||
assertThrows(IllegalArgumentException.class, ()->
|
||||
Currency.getInstance(Locale.of("", "EU")), "Did not throw IAE");
|
||||
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
||||
()-> 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
|
||||
|
Loading…
x
Reference in New Issue
Block a user