summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes <enh@google.com>2010-02-12 16:51:53 -0800
committerElliott Hughes <enh@google.com>2010-02-12 16:51:53 -0800
commitb0e5963793d8980b349f8e553067c19fd31601c5 (patch)
tree337d982e59f897770137ef087535f6127149bc2f
parent4b7692c57a135501e0494cfbfd905e084d2fb79f (diff)
downloadlibcore-b0e5963793d8980b349f8e553067c19fd31601c5.zip
libcore-b0e5963793d8980b349f8e553067c19fd31601c5.tar.gz
libcore-b0e5963793d8980b349f8e553067c19fd31601c5.tar.bz2
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).
-rw-r--r--luni/src/main/java/java/util/Currency.java9
-rw-r--r--luni/src/test/java/java/util/CurrencyTest.java11
2 files changed, 20 insertions, 0 deletions
diff --git a/luni/src/main/java/java/util/Currency.java b/luni/src/main/java/java/util/Currency.java
index 6aa295a..6b6e902 100644
--- a/luni/src/main/java/java/util/Currency.java
+++ b/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/luni/src/test/java/java/util/CurrencyTest.java b/luni/src/test/java/java/util/CurrencyTest.java
index 16111d5..66354f5 100644
--- a/luni/src/test/java/java/util/CurrencyTest.java
+++ b/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) {
+ }
+ }
}