OSDN Git Service

Throw IllegalArgumentException if Currency.getInstance is given an invalid currency...
authorElliott Hughes <enh@google.com>
Sat, 13 Feb 2010 00:51:53 +0000 (16:51 -0800)
committerElliott Hughes <enh@google.com>
Sat, 13 Feb 2010 00:51:53 +0000 (16:51 -0800)
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
libcore/luni/src/test/java/java/util/CurrencyTest.java

index 6aa295a..6b6e902 100644 (file)
@@ -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
index 16111d5..66354f5 100644 (file)
@@ -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) {
+        }
+    }
 }