1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.tests.java.util; 19 20 import java.util.ArrayList; 21 import java.util.Arrays; 22 import java.util.Collection; 23 import java.util.Currency; 24 import java.util.HashSet; 25 import java.util.Iterator; 26 import java.util.Locale; 27 import java.util.Set; 28 29 public class CurrencyTest extends junit.framework.TestCase { 30 31 private Locale originalLocale; 32 33 @Override 34 protected void setUp() throws Exception { 35 super.setUp(); 36 originalLocale = Locale.getDefault(); 37 } 38 39 @Override 40 protected void tearDown() { 41 Locale.setDefault(originalLocale); 42 } 43 44 /** 45 * java.util.Currency#getInstance(java.lang.String) 46 */ 47 public void test_getInstanceLjava_lang_String() { 48 // see test_getInstanceLjava_util_Locale() tests 49 } 50 51 /** 52 * java.util.Currency#getInstance(java.util.Locale) 53 */ 54 public void test_getInstanceLjava_util_Locale() { 55 /* 56 * the behaviour in all these three cases should be the same since this 57 * method ignores language and variant component of the locale. 58 */ 59 Currency c0 = Currency.getInstance("CAD"); 60 Currency c1 = Currency.getInstance(new Locale("en", "CA")); 61 assertTrue( 62 "Currency.getInstance(new Locale(\"en\",\"CA\")) isn't equal to Currency.getInstance(\"CAD\")", 63 c1 == c0); 64 Currency c2 = Currency.getInstance(new Locale("fr", "CA")); 65 assertTrue( 66 "Currency.getInstance(new Locale(\"fr\",\"CA\")) isn't equal to Currency.getInstance(\"CAD\")", 67 c2 == c0); 68 Currency c3 = Currency.getInstance(new Locale("", "CA")); 69 assertTrue( 70 "Currency.getInstance(new Locale(\"\",\"CA\")) isn't equal to Currency.getInstance(\"CAD\")", 71 c3 == c0); 72 73 c0 = Currency.getInstance("JPY"); 74 c1 = Currency.getInstance(new Locale("ja", "JP")); 75 assertTrue( 76 "Currency.getInstance(new Locale(\"ja\",\"JP\")) isn't equal to Currency.getInstance(\"JPY\")", 77 c1 == c0); 78 c2 = Currency.getInstance(new Locale("", "JP")); 79 assertTrue( 80 "Currency.getInstance(new Locale(\"\",\"JP\")) isn't equal to Currency.getInstance(\"JPY\")", 81 c2 == c0); 82 c3 = Currency.getInstance(new Locale("bogus", "JP")); 83 assertTrue( 84 "Currency.getInstance(new Locale(\"bogus\",\"JP\")) isn't equal to Currency.getInstance(\"JPY\")", 85 c3 == c0); 86 87 Locale localeGu = new Locale("gu", "IN"); 88 Currency cGu = Currency.getInstance(localeGu); 89 Locale localeKn = new Locale("kn", "IN"); 90 Currency cKn = Currency.getInstance(localeKn); 91 assertTrue("Currency.getInstance(Locale_" + localeGu.toString() + "))" 92 + "isn't equal to " + "Currency.getInstance(Locale_" 93 + localeKn.toString() + "))", cGu == cKn); 94 95 // some teritories do not have currencies, like Antarctica 96 Locale loc = new Locale("", "AQ"); 97 try { 98 Currency curr = Currency.getInstance(loc); 99 assertNull( 100 "Currency.getInstance(new Locale(\"\", \"AQ\")) did not return null", 101 curr); 102 } catch (IllegalArgumentException e) { 103 fail("Unexpected IllegalArgumentException " + e); 104 } 105 106 // unsupported/legacy iso3 countries 107 loc = new Locale("", "ZR"); 108 try { 109 Currency curr = Currency.getInstance(loc); 110 fail("Expected IllegalArgumentException"); 111 } catch (IllegalArgumentException e) { 112 } 113 114 loc = new Locale("", "ZAR"); 115 try { 116 Currency curr = Currency.getInstance(loc); 117 fail("Expected IllegalArgumentException"); 118 } catch (IllegalArgumentException e) { 119 } 120 121 loc = new Locale("", "FX"); 122 try { 123 Currency curr = Currency.getInstance(loc); 124 fail("Expected IllegalArgumentException"); 125 } catch (IllegalArgumentException e) { 126 } 127 128 loc = new Locale("", "FXX"); 129 try { 130 Currency curr = Currency.getInstance(loc); 131 fail("Expected IllegalArgumentException"); 132 } catch (IllegalArgumentException e) { 133 } 134 } 135 136 /** 137 * java.util.Currency#getSymbol() 138 */ 139 public void test_getSymbol() { 140 Currency currK = Currency.getInstance("KRW"); 141 Currency currI = Currency.getInstance("IEP"); 142 Currency currUS = Currency.getInstance("USD"); 143 144 Locale.setDefault(Locale.US); 145 // BEGIN android-changed 146 // KRW currency symbol is \u20a9 since CLDR1.7 release. 147 assertEquals("currK.getSymbol()", "\u20a9", currK.getSymbol()); 148 // IEP currency symbol is IEP since CLDR2.0 release. 149 assertEquals("currI.getSymbol()", "IEP", currI.getSymbol()); 150 // END android-changed 151 assertEquals("currUS.getSymbol()", "$", currUS.getSymbol()); 152 153 Locale.setDefault(new Locale("en", "IE")); 154 // BEGIN android-changed 155 assertEquals("currK.getSymbol()", "\u20a9", currK.getSymbol()); 156 assertEquals("currI.getSymbol()", "IEP", currI.getSymbol()); 157 assertEquals("currUS.getSymbol()", "$", currUS.getSymbol()); 158 // END android-changed 159 160 // Test what happens if the default is an invalid locale, one with the country Korea (KR) 161 // but a currently unsupported language. "kr" == Kanuri (Korean is actually "ko"). 162 // All these values are those defined in the "root" locale or the currency code if one isn't 163 // defined. 164 Locale.setDefault(new Locale("kr", "KR")); 165 // BEGIN android-changed 166 assertEquals("currK.getSymbol()", "\u20a9", currK.getSymbol()); 167 assertEquals("currI.getSymbol()", "IEP", currI.getSymbol()); 168 assertEquals("currUS.getSymbol()", "US$", currUS.getSymbol()); 169 // END android-changed 170 } 171 172 /** 173 * java.util.Currency#getSymbol(java.util.Locale) 174 */ 175 public void test_getSymbolLjava_util_Locale() { 176 //Tests was simplified because java specification not 177 // includes strong requirements for returning symbol. 178 // on android platform used wrong character for yen 179 // sign: \u00a5 instead of \uffe5 180 Locale[] desiredLocales = new Locale[]{ 181 Locale.JAPAN, Locale.JAPANESE, 182 Locale.FRANCE, Locale.FRENCH, 183 Locale.US, Locale.UK, 184 Locale.CANADA, Locale.CANADA_FRENCH, 185 Locale.ENGLISH, 186 new Locale("ja", "JP"), new Locale("", "JP"), 187 188 new Locale("fr", "FR"), new Locale("", "FR"), 189 190 new Locale("en", "US"), new Locale("", "US"), 191 new Locale("es", "US"), new Locale("ar", "US"), 192 new Locale("ja", "US"), 193 194 new Locale("en", "CA"), new Locale("fr", "CA"), 195 new Locale("", "CA"), new Locale("ar", "CA"), 196 197 new Locale("ja", "JP"), new Locale("", "JP"), 198 new Locale("ar", "JP"), 199 200 new Locale("ja", "AE"), new Locale("en", "AE"), 201 new Locale("ar", "AE"), 202 203 new Locale("da", "DK"), new Locale("", "DK"), 204 205 new Locale("da", ""), new Locale("ja", ""), 206 new Locale("en", "")}; 207 208 Set<Locale> availableLocales = new HashSet<Locale>(Arrays.asList(Locale.getAvailableLocales())); 209 210 ArrayList<Locale> locales = new ArrayList<Locale>(); 211 for (Locale desiredLocale : desiredLocales) { 212 if (availableLocales.contains(desiredLocale)) { 213 locales.add(desiredLocale); 214 } 215 } 216 217 Locale[] loc1 = locales.toArray(new Locale[locales.size()]); 218 219 String[] euro = new String[] {"EUR", "\u20ac"}; 220 // \u00a5 and \uffe5 are actually the same symbol, just different code points. 221 // But the RI returns the \uffe5 and Android returns those with \u00a5 222 String[] yen = new String[] {"JPY", "\u00a5", "\u00a5JP", "JP\u00a5", "\uffe5", "\uffe5JP", "JP\uffe5"}; 223 String[] dollar = new String[] {"USD", "$", "US$", "$US"}; 224 // BEGIN android-changed 225 // Starting CLDR 1.7 release, currency symbol for CAD changed to CA$ in some locales such as ja. 226 String[] cDollar = new String[] {"CA$", "CAD", "$", "Can$", "$CA"}; 227 // END android-changed 228 229 Currency currE = Currency.getInstance("EUR"); 230 Currency currJ = Currency.getInstance("JPY"); 231 Currency currUS = Currency.getInstance("USD"); 232 Currency currCA = Currency.getInstance("CAD"); 233 234 int i, j, k; 235 boolean flag; 236 237 for(k = 0; k < loc1.length; k++) { 238 Locale.setDefault(loc1[k]); 239 240 for (i = 0; i < loc1.length; i++) { 241 flag = false; 242 for (j = 0; j < euro.length; j++) { 243 if (currE.getSymbol(loc1[i]).equals(euro[j])) { 244 flag = true; 245 break; 246 } 247 } 248 assertTrue("Default Locale is: " + Locale.getDefault() 249 + ". For locale " + loc1[i] 250 + " the Euro currency returned " 251 + currE.getSymbol(loc1[i]) 252 + ". Expected was one of these: " 253 + Arrays.toString(euro), flag); 254 } 255 256 for (i = 0; i < loc1.length; i++) { 257 flag = false; 258 for (j = 0; j < yen.length; j++) { 259 if (currJ.getSymbol(loc1[i]).equals(yen[j])) { 260 flag = true; 261 break; 262 } 263 } 264 assertTrue("Default Locale is: " + Locale.getDefault() 265 + ". For locale " + loc1[i] 266 + " the Yen currency returned " 267 + currJ.getSymbol(loc1[i]) 268 + ". Expected was one of these: " 269 + Arrays.toString(yen), flag); 270 } 271 272 for (i = 0; i < loc1.length; i++) { 273 flag = false; 274 for (j = 0; j < dollar.length; j++) { 275 if (currUS.getSymbol(loc1[i]).equals(dollar[j])) { 276 flag = true; 277 break; 278 } 279 } 280 assertTrue("Default Locale is: " + Locale.getDefault() 281 + ". For locale " + loc1[i] 282 + " the Dollar currency returned " 283 + currUS.getSymbol(loc1[i]) 284 + ". Expected was one of these: " 285 + Arrays.toString(dollar), flag); 286 } 287 288 for (i = 0; i < loc1.length; i++) { 289 flag = false; 290 for (j = 0; j < cDollar.length; j++) { 291 if (currCA.getSymbol(loc1[i]).equals(cDollar[j])) { 292 flag = true; 293 break; 294 } 295 } 296 assertTrue("Default Locale is: " + Locale.getDefault() 297 + ". For locale " + loc1[i] 298 + " the Canadian Dollar currency returned " 299 + currCA.getSymbol(loc1[i]) 300 + ". Expected was one of these: " 301 + Arrays.toString(cDollar), flag); 302 } 303 } 304 } 305 306 /** 307 * java.util.Currency#getDefaultFractionDigits() 308 */ 309 public void test_getDefaultFractionDigits() { 310 311 Currency c1 = Currency.getInstance("TND"); 312 c1.getDefaultFractionDigits(); 313 assertEquals(" Currency.getInstance(\"" + c1 314 + "\") returned incorrect number of digits. ", 3, c1 315 .getDefaultFractionDigits()); 316 317 Currency c2 = Currency.getInstance("EUR"); 318 c2.getDefaultFractionDigits(); 319 assertEquals(" Currency.getInstance(\"" + c2 320 + "\") returned incorrect number of digits. ", 2, c2 321 .getDefaultFractionDigits()); 322 323 Currency c3 = Currency.getInstance("JPY"); 324 c3.getDefaultFractionDigits(); 325 assertEquals(" Currency.getInstance(\"" + c3 326 + "\") returned incorrect number of digits. ", 0, c3 327 .getDefaultFractionDigits()); 328 329 Currency c4 = Currency.getInstance("XXX"); 330 c4.getDefaultFractionDigits(); 331 assertEquals(" Currency.getInstance(\"" + c4 332 + "\") returned incorrect number of digits. ", -1, c4 333 .getDefaultFractionDigits()); 334 } 335 336 /** 337 * java.util.Currency#getCurrencyCode() Note: lines under remarks 338 * (Locale.CHINESE, Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN, 339 * Locale.ITALIAN, Locale.JAPANESE, Locale.KOREAN) raises exception 340 * on SUN VM 341 */ 342 public void test_getCurrencyCode() { 343 final Collection<Locale> locVal = Arrays.asList( 344 Locale.CANADA, 345 Locale.CANADA_FRENCH, 346 Locale.CHINA, 347 // Locale.CHINESE, 348 // Locale.ENGLISH, 349 Locale.FRANCE, 350 // Locale.FRENCH, 351 // Locale.GERMAN, 352 Locale.GERMANY, 353 // Locale.ITALIAN, 354 Locale.ITALY, Locale.JAPAN, 355 // Locale.JAPANESE, 356 Locale.KOREA, 357 // Locale.KOREAN, 358 Locale.PRC, Locale.SIMPLIFIED_CHINESE, Locale.TAIWAN, Locale.TRADITIONAL_CHINESE, 359 Locale.UK, Locale.US); 360 final Collection<String> locDat = Arrays.asList("CAD", "CAD", "CNY", "EUR", "EUR", "EUR", 361 "JPY", "KRW", "CNY", "CNY", "TWD", "TWD", "GBP", "USD"); 362 363 Iterator<String> dat = locDat.iterator(); 364 for (Locale l : locVal) { 365 String d = dat.next().trim(); 366 assertEquals("For locale " + l + " currency code wrong", Currency.getInstance(l) 367 .getCurrencyCode(), d); 368 } 369 } 370 371 /** 372 * java.util.Currency#toString() Note: lines under remarks 373 * (Locale.CHINESE, Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN, 374 * Locale.ITALIAN, Locale.JAPANESE, Locale.KOREAN) raises exception 375 * on SUN VM 376 */ 377 public void test_toString() { 378 final Collection<Locale> locVal = Arrays.asList( 379 Locale.CANADA, 380 Locale.CANADA_FRENCH, 381 Locale.CHINA, 382 // Locale.CHINESE, 383 // Locale.ENGLISH, 384 Locale.FRANCE, 385 // Locale.FRENCH, 386 // Locale.GERMAN, 387 Locale.GERMANY, 388 // Locale.ITALIAN, 389 Locale.ITALY, Locale.JAPAN, 390 // Locale.JAPANESE, 391 Locale.KOREA, 392 // Locale.KOREAN, 393 Locale.PRC, Locale.SIMPLIFIED_CHINESE, Locale.TAIWAN, Locale.TRADITIONAL_CHINESE, 394 Locale.UK, Locale.US); 395 final Collection<String> locDat = Arrays.asList("CAD", "CAD", "CNY", "EUR", "EUR", "EUR", 396 "JPY", "KRW", "CNY", "CNY", "TWD", "TWD", "GBP", "USD"); 397 398 Iterator<String> dat = locDat.iterator(); 399 for (Locale l : locVal) { 400 String d = dat.next().trim(); 401 assertEquals("For locale " + l + " Currency.toString method returns wrong value", 402 Currency.getInstance(l).toString(), d); 403 } 404 } 405 } 406