From 4562a70fdfa38139a7e73efc2fc44e275c49d3f6 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Fri, 12 Feb 2010 16:51:53 -0800 Subject: [PATCH] Throw IllegalArgumentException if Currency.getInstance is given an invalid currency code. This fixes an existing harmony DecimalFormatSymbolsTest failure, but I've added an explicit test for clarity (and to reduce the likelihood of regression). --- libcore/luni/src/main/java/java/util/Currency.java | 9 +++++++++ libcore/luni/src/test/java/java/util/CurrencyTest.java | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/libcore/luni/src/main/java/java/util/Currency.java b/libcore/luni/src/main/java/java/util/Currency.java index 6aa295a63..6b6e90260 100644 --- a/libcore/luni/src/main/java/java/util/Currency.java +++ b/libcore/luni/src/main/java/java/util/Currency.java @@ -58,8 +58,17 @@ public final class Currency implements Serializable { return; } + // Ensure that we throw if the our currency code isn't an ISO currency code. + String symbol = Resources.getCurrencySymbolNative(Locale.US.toString(), currencyCode); + if (symbol == null) { + throw new IllegalArgumentException(Msg.getString("K0322", currencyCode)); + } + this.defaultFractionDigits = Resources.getCurrencyFractionDigitsNative(currencyCode); if (defaultFractionDigits < 0) { + // In practice, I don't think this can fail because ICU doesn't care whether you give + // it a valid country code, and will just return a sensible default for the default + // locale's currency. throw new IllegalArgumentException(Msg.getString("K0322", currencyCode)); } // END android-changed diff --git a/libcore/luni/src/test/java/java/util/CurrencyTest.java b/libcore/luni/src/test/java/java/util/CurrencyTest.java index 16111d54b..66354f597 100644 --- a/libcore/luni/src/test/java/java/util/CurrencyTest.java +++ b/libcore/luni/src/test/java/java/util/CurrencyTest.java @@ -31,4 +31,15 @@ public class CurrencyTest extends junit.framework.TestCase { // Canada that Canadians give it a localized (to Canada) symbol. assertEquals("AED", Currency.getInstance("AED").getSymbol(Locale.CANADA)); } + + // Regression test to ensure that Currency.getInstance(String) throws if + // given an invalid ISO currency code. + public void test_getInstance_illegal_currency_code() throws Exception { + Currency.getInstance("USD"); + try { + Currency.getInstance("BOGO-DOLLARS"); + fail("expected IllegalArgumentException for invalid ISO currency code"); + } catch (IllegalArgumentException expected) { + } + } } -- 2.11.0