• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         String[] cDollar = new String[] {"CA$", "CAD", "$", "Can$", "$CA"};
239         // END Android-changed
240 
241         Currency currE   = Currency.getInstance("EUR");
242         Currency currJ   = Currency.getInstance("JPY");
243         Currency currUS  = Currency.getInstance("USD");
244         Currency currCA  = Currency.getInstance("CAD");
245 
246         int i, j, k;
247         boolean flag;
248 
249         for(k = 0; k < loc1.length; k++) {
250             Locale.setDefault(loc1[k]);
251 
252             for (i = 0; i < loc1.length; i++) {
253                 flag = false;
254                 for  (j = 0; j < euro.length; j++) {
255                     if (currE.getSymbol(loc1[i]).equals(euro[j])) {
256                         flag = true;
257                         break;
258                     }
259                 }
260                 assertTrue("Default Locale is: " + Locale.getDefault()
261                         + ". For locale " + loc1[i]
262                         + " the Euro currency returned "
263                         + currE.getSymbol(loc1[i])
264                         + ". Expected was one of these: "
265                         + Arrays.toString(euro), flag);
266             }
267 
268             for (i = 0; i < loc1.length; i++) {
269                 flag = false;
270                 for  (j = 0; j < yen.length; j++) {
271                     if (currJ.getSymbol(loc1[i]).equals(yen[j])) {
272                         flag = true;
273                         break;
274                     }
275                 }
276                 assertTrue("Default Locale is: " + Locale.getDefault()
277                         + ". For locale " + loc1[i]
278                         + " the Yen currency returned "
279                         + currJ.getSymbol(loc1[i])
280                         + ". Expected was one of these: "
281                         + Arrays.toString(yen), flag);
282             }
283 
284             for (i = 0; i < loc1.length; i++) {
285                 flag = false;
286                 for  (j = 0; j < dollar.length; j++) {
287                     if (currUS.getSymbol(loc1[i]).equals(dollar[j])) {
288                         flag = true;
289                         break;
290                     }
291                 }
292                 assertTrue("Default Locale is: " + Locale.getDefault()
293                         + ". For locale " + loc1[i]
294                         + " the Dollar currency returned "
295                         + currUS.getSymbol(loc1[i])
296                         + ". Expected was one of these: "
297                         + Arrays.toString(dollar), flag);
298             }
299 
300             for (i = 0; i < loc1.length; i++) {
301                 flag = false;
302                 for  (j = 0; j < cDollar.length; j++) {
303                     if (currCA.getSymbol(loc1[i]).equals(cDollar[j])) {
304                         flag = true;
305                         break;
306                     }
307                 }
308                 assertTrue("Default Locale is: " + Locale.getDefault()
309                         + ". For locale " + loc1[i]
310                         + " the Canadian Dollar currency returned "
311                         + currCA.getSymbol(loc1[i])
312                         + ". Expected was one of these: "
313                         + Arrays.toString(cDollar), flag);
314             }
315         }
316     }
317 
318     /**
319      * java.util.Currency#getDefaultFractionDigits()
320      */
test_getDefaultFractionDigits()321     public void test_getDefaultFractionDigits() {
322 
323         Currency c1 = Currency.getInstance("TND");
324         c1.getDefaultFractionDigits();
325         assertEquals(" Currency.getInstance(\"" + c1
326                 + "\") returned incorrect number of digits. ", 3, c1
327                 .getDefaultFractionDigits());
328 
329         Currency c2 = Currency.getInstance("EUR");
330         c2.getDefaultFractionDigits();
331         assertEquals(" Currency.getInstance(\"" + c2
332                 + "\") returned incorrect number of digits. ", 2, c2
333                 .getDefaultFractionDigits());
334 
335         Currency c3 = Currency.getInstance("JPY");
336         c3.getDefaultFractionDigits();
337         assertEquals(" Currency.getInstance(\"" + c3
338                 + "\") returned incorrect number of digits. ", 0, c3
339                 .getDefaultFractionDigits());
340 
341         Currency c4 = Currency.getInstance("XXX");
342         c4.getDefaultFractionDigits();
343         assertEquals(" Currency.getInstance(\"" + c4
344                 + "\") returned incorrect number of digits. ", -1, c4
345                 .getDefaultFractionDigits());
346     }
347 
348     /**
349      * java.util.Currency#getCurrencyCode() Note: lines under remarks
350      *        (Locale.CHINESE, Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN,
351      *        Locale.ITALIAN, Locale.JAPANESE, Locale.KOREAN) raises exception
352      *        on SUN VM
353      */
test_getCurrencyCode()354     public void test_getCurrencyCode() {
355         final Collection<Locale> locVal = Arrays.asList(
356                 Locale.CANADA,
357                 Locale.CANADA_FRENCH,
358                 Locale.CHINA,
359                 // Locale.CHINESE,
360                 // Locale.ENGLISH,
361                 Locale.FRANCE,
362                 // Locale.FRENCH,
363                 // Locale.GERMAN,
364                 Locale.GERMANY,
365                 // Locale.ITALIAN,
366                 Locale.ITALY, Locale.JAPAN,
367                 // Locale.JAPANESE,
368                 Locale.KOREA,
369                 // Locale.KOREAN,
370                 Locale.PRC, Locale.SIMPLIFIED_CHINESE, Locale.TAIWAN, Locale.TRADITIONAL_CHINESE,
371                 Locale.UK, Locale.US);
372         final Collection<String> locDat = Arrays.asList("CAD", "CAD", "CNY", "EUR", "EUR", "EUR",
373                 "JPY", "KRW", "CNY", "CNY", "TWD", "TWD", "GBP", "USD");
374 
375         Iterator<String> dat = locDat.iterator();
376         for (Locale l : locVal) {
377             String d = dat.next().trim();
378             assertEquals("For locale " + l + " currency code wrong", Currency.getInstance(l)
379                     .getCurrencyCode(), d);
380         }
381     }
382 
383     /**
384      * java.util.Currency#toString() Note: lines under remarks
385      *        (Locale.CHINESE, Locale.ENGLISH, Locale.FRENCH, Locale.GERMAN,
386      *        Locale.ITALIAN, Locale.JAPANESE, Locale.KOREAN) raises exception
387      *        on SUN VM
388      */
test_toString()389     public void test_toString() {
390         final Collection<Locale> locVal = Arrays.asList(
391                 Locale.CANADA,
392                 Locale.CANADA_FRENCH,
393                 Locale.CHINA,
394                 // Locale.CHINESE,
395                 // Locale.ENGLISH,
396                 Locale.FRANCE,
397                 // Locale.FRENCH,
398                 // Locale.GERMAN,
399                 Locale.GERMANY,
400                 // Locale.ITALIAN,
401                 Locale.ITALY, Locale.JAPAN,
402                 // Locale.JAPANESE,
403                 Locale.KOREA,
404                 // Locale.KOREAN,
405                 Locale.PRC, Locale.SIMPLIFIED_CHINESE, Locale.TAIWAN, Locale.TRADITIONAL_CHINESE,
406                 Locale.UK, Locale.US);
407         final Collection<String> locDat = Arrays.asList("CAD", "CAD", "CNY", "EUR", "EUR", "EUR",
408                 "JPY", "KRW", "CNY", "CNY", "TWD", "TWD", "GBP", "USD");
409 
410         Iterator<String> dat = locDat.iterator();
411         for (Locale l : locVal) {
412             String d = dat.next().trim();
413             assertEquals("For locale " + l + " Currency.toString method returns wrong value",
414                     Currency.getInstance(l).toString(), d);
415         }
416     }
417 }
418