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 setUp()34 protected void setUp() throws Exception { 35 super.setUp(); 36 originalLocale = Locale.getDefault(); 37 } 38 39 @Override tearDown()40 protected void tearDown() { 41 Locale.setDefault(originalLocale); 42 } 43 44 /** 45 * java.util.Currency#getInstance(java.lang.String) 46 */ test_getInstanceLjava_lang_String()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 */ test_getInstanceLjava_util_Locale()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 try { 136 Currency.getInstance((Locale) null); 137 fail("Expected NullPointerException"); 138 } catch (NullPointerException expected) { 139 } 140 141 try { 142 Currency.getInstance((String) null); 143 fail("Expected NullPointerException"); 144 } catch (NullPointerException expected) { 145 } 146 } 147 148 /** 149 * java.util.Currency#getSymbol() 150 */ test_getSymbol()151 public void test_getSymbol() { 152 Currency currK = Currency.getInstance("KRW"); 153 Currency currI = Currency.getInstance("IEP"); 154 Currency currUS = Currency.getInstance("USD"); 155 156 Locale.setDefault(Locale.US); 157 // BEGIN Android-changed 158 // KRW currency symbol is \u20a9 since CLDR1.7 release. 159 assertEquals("currK.getSymbol()", "\u20a9", currK.getSymbol()); 160 // IEP currency symbol is IEP since CLDR2.0 release. 161 assertEquals("currI.getSymbol()", "IEP", currI.getSymbol()); 162 // END Android-changed 163 assertEquals("currUS.getSymbol()", "$", currUS.getSymbol()); 164 165 Locale.setDefault(new Locale("en", "IE")); 166 // BEGIN Android-changed 167 assertEquals("currK.getSymbol()", "\u20a9", currK.getSymbol()); 168 assertEquals("currI.getSymbol()", "IEP", currI.getSymbol()); 169 assertEquals("currUS.getSymbol()", "US$", currUS.getSymbol()); 170 // END Android-changed 171 172 // Test what happens if the default is an invalid locale, one with the country Korea (KR) 173 // but a currently unsupported language. "kr" == Kanuri (Korean is actually "ko"). 174 // All these values are those defined in the "root" locale or the currency code if one isn't 175 // defined. 176 Locale.setDefault(new Locale("kr", "KR")); 177 // BEGIN Android-changed 178 assertEquals("currK.getSymbol()", "\u20a9", currK.getSymbol()); 179 assertEquals("currI.getSymbol()", "IEP", currI.getSymbol()); 180 assertEquals("currUS.getSymbol()", "US$", currUS.getSymbol()); 181 // END Android-changed 182 } 183 184 /** 185 * java.util.Currency#getSymbol(java.util.Locale) 186 */ test_getSymbolLjava_util_Locale()187 public void test_getSymbolLjava_util_Locale() { 188 //Tests was simplified because java specification not 189 // includes strong requirements for returning symbol. 190 // on android platform used wrong character for yen 191 // sign: \u00a5 instead of \uffe5 192 Locale[] desiredLocales = new Locale[]{ 193 Locale.JAPAN, Locale.JAPANESE, 194 Locale.FRANCE, Locale.FRENCH, 195 Locale.US, Locale.UK, 196 Locale.CANADA, Locale.CANADA_FRENCH, 197 Locale.ENGLISH, 198 new Locale("ja", "JP"), new Locale("", "JP"), 199 200 new Locale("fr", "FR"), new Locale("", "FR"), 201 202 new Locale("en", "US"), new Locale("", "US"), 203 new Locale("es", "US"), new Locale("ar", "US"), 204 new Locale("ja", "US"), 205 206 new Locale("en", "CA"), new Locale("fr", "CA"), 207 new Locale("", "CA"), new Locale("ar", "CA"), 208 209 new Locale("ja", "JP"), new Locale("", "JP"), 210 new Locale("ar", "JP"), 211 212 new Locale("ja", "AE"), new Locale("en", "AE"), 213 new Locale("ar", "AE"), 214 215 new Locale("da", "DK"), new Locale("", "DK"), 216 217 new Locale("da", ""), new Locale("ja", ""), 218 new Locale("en", "")}; 219 220 Set<Locale> availableLocales = new HashSet<Locale>(Arrays.asList(Locale.getAvailableLocales())); 221 222 ArrayList<Locale> locales = new ArrayList<Locale>(); 223 for (Locale desiredLocale : desiredLocales) { 224 if (availableLocales.contains(desiredLocale)) { 225 locales.add(desiredLocale); 226 } 227 } 228 229 Locale[] loc1 = locales.toArray(new Locale[locales.size()]); 230 231 String[] euro = new String[] {"EUR", "\u20ac"}; 232 // \u00a5 and \uffe5 are actually the same symbol, just different code points. 233 // But the RI returns the \uffe5 and Android returns those with \u00a5 234 String[] yen = new String[] {"JPY", "\u00a5", "\u00a5JP", "JP\u00a5", "\uffe5", "\uffe5JP", "JP\uffe5"}; 235 String[] dollar = new String[] {"USD", "$", "US$", "$US", "$ US"}; 236 // BEGIN Android-changed 237 // Starting CLDR 1.7 release, currency symbol for CAD changed to CA$ in some locales such as ja. 238 // Starting CLDR 38 release, currency symbol for CAD changed to $ CA in some locales such as ja-JP. 239 String[] cDollar = new String[] {"CA$", "CAD", "$", "Can$", "$CA", "$ CA"}; 240 // END Android-changed 241 242 Currency currE = Currency.getInstance("EUR"); 243 Currency currJ = Currency.getInstance("JPY"); 244 Currency currUS = Currency.getInstance("USD"); 245 Currency currCA = Currency.getInstance("CAD"); 246 247 int i, j, k; 248 boolean flag; 249 250 for(k = 0; k < loc1.length; k++) { 251 Locale.setDefault(loc1[k]); 252 253 for (i = 0; i < loc1.length; i++) { 254 flag = false; 255 for (j = 0; j < euro.length; j++) { 256 if (currE.getSymbol(loc1[i]).equals(euro[j])) { 257 flag = true; 258 break; 259 } 260 } 261 assertTrue("Default Locale is: " + Locale.getDefault() 262 + ". For locale " + loc1[i] 263 + " the Euro currency returned " 264 + currE.getSymbol(loc1[i]) 265 + ". Expected was one of these: " 266 + Arrays.toString(euro), flag); 267 } 268 269 for (i = 0; i < loc1.length; i++) { 270 flag = false; 271 for (j = 0; j < yen.length; j++) { 272 if (currJ.getSymbol(loc1[i]).equals(yen[j])) { 273 flag = true; 274 break; 275 } 276 } 277 assertTrue("Default Locale is: " + Locale.getDefault() 278 + ". For locale " + loc1[i] 279 + " the Yen currency returned " 280 + currJ.getSymbol(loc1[i]) 281 + ". Expected was one of these: " 282 + Arrays.toString(yen), flag); 283 } 284 285 for (i = 0; i < loc1.length; i++) { 286 flag = false; 287 for (j = 0; j < dollar.length; j++) { 288 if (currUS.getSymbol(loc1[i]).equals(dollar[j])) { 289 flag = true; 290 break; 291 } 292 } 293 assertTrue("Default Locale is: " + Locale.getDefault() 294 + ". For locale " + loc1[i] 295 + " the Dollar currency returned " 296 + currUS.getSymbol(loc1[i]) 297 + ". Expected was one of these: " 298 + Arrays.toString(dollar), flag); 299 } 300 301 for (i = 0; i < loc1.length; i++) { 302 flag = false; 303 for (j = 0; j < cDollar.length; j++) { 304 if (currCA.getSymbol(loc1[i]).equals(cDollar[j])) { 305 flag = true; 306 break; 307 } 308 } 309 assertTrue("Default Locale is: " + Locale.getDefault() 310 + ". For locale " + loc1[i] 311 + " the Canadian Dollar currency returned " 312 + currCA.getSymbol(loc1[i]) 313 + ". Expected was one of these: " 314 + Arrays.toString(cDollar), flag); 315 } 316 } 317 } 318 319 /** 320 * java.util.Currency#getDefaultFractionDigits() 321 */ test_getDefaultFractionDigits()322 public void test_getDefaultFractionDigits() { 323 324 Currency c1 = Currency.getInstance("TND"); 325 c1.getDefaultFractionDigits(); 326 assertEquals(" Currency.getInstance(\"" + c1 327 + "\") returned incorrect number of digits. ", 3, c1 328 .getDefaultFractionDigits()); 329 330 Currency c2 = Currency.getInstance("EUR"); 331 c2.getDefaultFractionDigits(); 332 assertEquals(" Currency.getInstance(\"" + c2 333 + "\") returned incorrect number of digits. ", 2, c2 334 .getDefaultFractionDigits()); 335 336 Currency c3 = Currency.getInstance("JPY"); 337 c3.getDefaultFractionDigits(); 338 assertEquals(" Currency.getInstance(\"" + c3 339 + "\") returned incorrect number of digits. ", 0, c3 340 .getDefaultFractionDigits()); 341 342 Currency c4 = Currency.getInstance("XXX"); 343 c4.getDefaultFractionDigits(); 344 assertEquals(" Currency.getInstance(\"" + c4 345 + "\") returned incorrect number of digits. ", -1, c4 346 .getDefaultFractionDigits()); 347 } 348 349 /** 350 * java.util.Currency#getCurrencyCode() Note: lines under remarks 351 * (Locale.CHINESE, Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN, 352 * Locale.ITALIAN, Locale.JAPANESE, Locale.KOREAN) raises exception 353 * on SUN VM 354 */ test_getCurrencyCode()355 public void test_getCurrencyCode() { 356 final Collection<Locale> locVal = Arrays.asList( 357 Locale.CANADA, 358 Locale.CANADA_FRENCH, 359 Locale.CHINA, 360 // Locale.CHINESE, 361 // Locale.ENGLISH, 362 Locale.FRANCE, 363 // Locale.FRENCH, 364 // Locale.GERMAN, 365 Locale.GERMANY, 366 // Locale.ITALIAN, 367 Locale.ITALY, Locale.JAPAN, 368 // Locale.JAPANESE, 369 Locale.KOREA, 370 // Locale.KOREAN, 371 Locale.PRC, Locale.SIMPLIFIED_CHINESE, Locale.TAIWAN, Locale.TRADITIONAL_CHINESE, 372 Locale.UK, Locale.US); 373 final Collection<String> locDat = Arrays.asList("CAD", "CAD", "CNY", "EUR", "EUR", "EUR", 374 "JPY", "KRW", "CNY", "CNY", "TWD", "TWD", "GBP", "USD"); 375 376 Iterator<String> dat = locDat.iterator(); 377 for (Locale l : locVal) { 378 String d = dat.next().trim(); 379 assertEquals("For locale " + l + " currency code wrong", Currency.getInstance(l) 380 .getCurrencyCode(), d); 381 } 382 } 383 384 /** 385 * java.util.Currency#toString() Note: lines under remarks 386 * (Locale.CHINESE, Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN, 387 * Locale.ITALIAN, Locale.JAPANESE, Locale.KOREAN) raises exception 388 * on SUN VM 389 */ test_toString()390 public void test_toString() { 391 final Collection<Locale> locVal = Arrays.asList( 392 Locale.CANADA, 393 Locale.CANADA_FRENCH, 394 Locale.CHINA, 395 // Locale.CHINESE, 396 // Locale.ENGLISH, 397 Locale.FRANCE, 398 // Locale.FRENCH, 399 // Locale.GERMAN, 400 Locale.GERMANY, 401 // Locale.ITALIAN, 402 Locale.ITALY, Locale.JAPAN, 403 // Locale.JAPANESE, 404 Locale.KOREA, 405 // Locale.KOREAN, 406 Locale.PRC, Locale.SIMPLIFIED_CHINESE, Locale.TAIWAN, Locale.TRADITIONAL_CHINESE, 407 Locale.UK, Locale.US); 408 final Collection<String> locDat = Arrays.asList("CAD", "CAD", "CNY", "EUR", "EUR", "EUR", 409 "JPY", "KRW", "CNY", "CNY", "TWD", "TWD", "GBP", "USD"); 410 411 Iterator<String> dat = locDat.iterator(); 412 for (Locale l : locVal) { 413 String d = dat.next().trim(); 414 assertEquals("For locale " + l + " Currency.toString method returns wrong value", 415 Currency.getInstance(l).toString(), d); 416 } 417 } 418 } 419