Fix DecimalFormatSymbols.setCurrency.
We cache values from the Currency but can allow those cached values to be
overwritten, so even if a later call to setCurrency is for the same Currency
as last time, we can't assume that's a no-op.
Bug: 18437206
Bug: https://code.google.com/p/android/issues/detail?id=79925
Change-Id: Ic44dbc66a190567438b8b9f866f2a6cdee693e10
diff --git a/luni/src/main/java/java/text/DecimalFormatSymbols.java b/luni/src/main/java/java/text/DecimalFormatSymbols.java
index 9750e6d..2f1d4f4 100644
--- a/luni/src/main/java/java/text/DecimalFormatSymbols.java
+++ b/luni/src/main/java/java/text/DecimalFormatSymbols.java
@@ -414,9 +414,6 @@
if (currency == null) {
throw new NullPointerException("currency == null");
}
- if (currency == this.currency) {
- return;
- }
this.currency = currency;
intlCurrencySymbol = currency.getCurrencyCode();
currencySymbol = currency.getSymbol(locale);
diff --git a/luni/src/test/java/libcore/java/text/DecimalFormatSymbolsTest.java b/luni/src/test/java/libcore/java/text/DecimalFormatSymbolsTest.java
index 619c38e..3e0aeba 100644
--- a/luni/src/test/java/libcore/java/text/DecimalFormatSymbolsTest.java
+++ b/luni/src/test/java/libcore/java/text/DecimalFormatSymbolsTest.java
@@ -21,6 +21,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.DecimalFormatSymbols;
+import java.util.Currency;
import java.util.Locale;
public class DecimalFormatSymbolsTest extends junit.framework.TestCase {
@@ -57,4 +58,32 @@
// The two objects should claim to be equal.
assertEquals(originalDfs, deserializedDfs);
}
+
+ // https://code.google.com/p/android/issues/detail?id=79925
+ public void testSetSameCurrency() throws Exception {
+ DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
+ dfs.setCurrency(Currency.getInstance("USD"));
+ assertEquals("$", dfs.getCurrencySymbol());
+ dfs.setCurrencySymbol("poop");
+ assertEquals("poop", dfs.getCurrencySymbol());
+ dfs.setCurrency(Currency.getInstance("USD"));
+ assertEquals("$", dfs.getCurrencySymbol());
+ }
+
+ public void testSetNulInternationalCurrencySymbol() throws Exception {
+ Currency usd = Currency.getInstance("USD");
+
+ DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US);
+ dfs.setCurrency(usd);
+ assertEquals(usd, dfs.getCurrency());
+ assertEquals("$", dfs.getCurrencySymbol());
+ assertEquals("USD", dfs.getInternationalCurrencySymbol());
+
+ // Setting the international currency symbol to null sets the currency to null too,
+ // but not the currency symbol.
+ dfs.setInternationalCurrencySymbol(null);
+ assertEquals(null, dfs.getCurrency());
+ assertEquals("$", dfs.getCurrencySymbol());
+ assertEquals(null, dfs.getInternationalCurrencySymbol());
+ }
}