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