1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package libcore.java.util; 18 19 import java.util.Currency; 20 import java.util.Locale; 21 import java.util.Set; 22 23 import libcore.libcore.util.SerializationTester; 24 25 public class CurrencyTest extends junit.framework.TestCase { 26 // Regression test to ensure that Currency.getSymbol(Locale) returns the 27 // currency code if ICU doesn't have a localization of the symbol. The 28 // harmony Currency tests don't test this, and their DecimalFormat tests 29 // only test it as a side-effect, and in a way that only detected my 30 // specific mistake of returning null (returning "stinky" would have 31 // passed). 32 public void test_getSymbol_fallback() throws Exception { 33 // This assumes that AED never becomes a currency important enough to 34 // Canada that Canadians give it a localized (to Canada) symbol. 35 assertEquals("AED", Currency.getInstance("AED").getSymbol(Locale.CANADA)); 36 } 37 38 public void test_getSymbol_locale() { 39 Currency currency = Currency.getInstance("DEM"); 40 assertEquals("DEM", currency.getSymbol(Locale.FRANCE)); 41 assertEquals("DM", currency.getSymbol(Locale.GERMANY)); 42 assertEquals("DEM", currency.getSymbol(Locale.US)); 43 } 44 45 /** 46 * Checks that the no-argument version of {@link Currency#getSymbol()} uses the 47 * default DISPLAY locale as opposed to the default locale or the default FORMAT 48 * locale. 49 */ 50 public void test_getSymbol_noLocaleArgument() { 51 Currency currency = Currency.getInstance("DEM"); 52 Locales locales = Locales.getAndSetDefaultForTest(Locale.US, Locale.GERMANY, Locale.FRANCE); 53 try { 54 // getAndSetDefaultForTest(uncategorizedLocale, displayLocale, formatLocale) 55 assertEquals("DM", currency.getSymbol()); 56 } finally { 57 locales.setAsDefault(); 58 } 59 } 60 61 // Regression test to ensure that Currency.getInstance(String) throws if 62 // given an invalid ISO currency code. 63 public void test_getInstance_illegal_currency_code() throws Exception { 64 Currency.getInstance("USD"); 65 try { 66 Currency.getInstance("BOGO-DOLLARS"); 67 fail("expected IllegalArgumentException for invalid ISO currency code"); 68 } catch (IllegalArgumentException expected) { 69 } 70 } 71 72 public void testGetAvailableCurrencies() throws Exception { 73 Set<Currency> all = Currency.getAvailableCurrencies(); 74 // Confirm that a few well-known stable currencies are present. 75 assertTrue(all.toString(), all.contains(Currency.getInstance("CHF"))); 76 assertTrue(all.toString(), all.contains(Currency.getInstance("EUR"))); 77 assertTrue(all.toString(), all.contains(Currency.getInstance("GBP"))); 78 assertTrue(all.toString(), all.contains(Currency.getInstance("JPY"))); 79 assertTrue(all.toString(), all.contains(Currency.getInstance("USD"))); 80 } 81 82 public void test_getDisplayName_locale_chf() throws Exception { 83 Currency currency = Currency.getInstance("CHF"); 84 assertEquals("Swiss Franc", currency.getDisplayName(Locale.US)); 85 assertEquals("Schweizer Franken", currency.getDisplayName(new Locale("de", "CH"))); 86 assertEquals("franc suisse", currency.getDisplayName(new Locale("fr", "CH"))); 87 assertEquals("franco svizzero", currency.getDisplayName(new Locale("it", "CH"))); 88 } 89 90 public void test_getDisplayName_locale_dem() throws Exception { 91 Currency currency = Currency.getInstance("DEM"); 92 assertEquals("Deutsche Mark", currency.getDisplayName(Locale.GERMANY)); 93 assertEquals("German Mark", currency.getDisplayName(Locale.US)); 94 assertEquals("mark allemand", currency.getDisplayName(Locale.FRANCE)); 95 } 96 97 public void test_getDisplayName_null() { 98 Currency currency = Currency.getInstance("CHF"); 99 try { 100 currency.getDisplayName(null); 101 fail(); 102 } catch (NullPointerException expected) { 103 } 104 } 105 106 /** 107 * Checks that the no-argument version of {@link Currency#getDisplayName()} uses 108 * the default DISPLAY locale, as opposed to the default locale or the default 109 * FORMAT locale. 110 */ 111 public void test_getDisplayName_noLocaleArgument() { 112 Currency currency = Currency.getInstance("DEM"); 113 // getAndSetDefaultForTest(uncategorizedLocale, displayLocale, formatLocale) 114 Locales locales = Locales.getAndSetDefaultForTest(Locale.US, Locale.GERMANY, Locale.FRANCE); 115 try { 116 assertEquals("Deutsche Mark", currency.getDisplayName()); 117 } finally { 118 locales.setAsDefault(); 119 } 120 } 121 122 public void test_getDefaultFractionDigits() throws Exception { 123 assertEquals(2, Currency.getInstance("USD").getDefaultFractionDigits()); 124 assertEquals(0, Currency.getInstance("JPY").getDefaultFractionDigits()); 125 assertEquals(-1, Currency.getInstance("XXX").getDefaultFractionDigits()); 126 } 127 128 // http://code.google.com/p/android/issues/detail?id=38622 129 public void test_getSymbol_38622() throws Exception { 130 // The CLDR data had the Portuguese symbol for "EUR" in pt, not in pt_PT. 131 // We weren't falling back from pt_PT to pt, so we didn't find it and would 132 // default to U+00A4 CURRENCY SIGN () rather than . 133 Locale pt_BR = new Locale("pt", "BR"); 134 Locale pt_PT = new Locale("pt", "PT"); 135 assertEquals("R$", Currency.getInstance(pt_BR).getSymbol(pt_BR)); 136 assertEquals("R$", Currency.getInstance(pt_BR).getSymbol(pt_PT)); 137 assertEquals("", Currency.getInstance(pt_PT).getSymbol(pt_BR)); 138 assertEquals("", Currency.getInstance(pt_PT).getSymbol(pt_PT)); 139 } 140 141 public void test_nullLocales() { 142 Currency currency = Currency.getInstance(Locale.getDefault()); 143 try { 144 currency.getSymbol(null); 145 fail(); 146 } catch (NullPointerException expected) {} 147 } 148 149 public void testSerialization() throws Exception { 150 Currency usd = Currency.getInstance("USD"); 151 String actual = SerializationTester.serializeHex(usd); 152 String expected = "ACED0005737200126A6176612E7574696C2E43757272656E6379FDCD934A5911A91F02" + 153 "00014C000C63757272656E6379436F64657400124C6A6176612F6C616E672F537472696E673B7870" + 154 "740003555344"; 155 assertEquals(expected, actual); 156 157 Currency deserializedUsd = (Currency) SerializationTester.deserializeHex(expected); 158 assertSame("Currency objects should be singletons", usd, deserializedUsd); 159 } 160 161 public void test_getNumericCode() throws Exception { 162 assertEquals(840, Currency.getInstance("USD").getNumericCode()); 163 assertEquals(826, Currency.getInstance("GBP").getNumericCode()); 164 assertEquals(999, Currency.getInstance("XXX").getNumericCode()); 165 assertEquals(0, Currency.getInstance("XFU").getNumericCode()); 166 } 167 168 } 169