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
18package org.apache.harmony.tests.java.text;
19
20import java.io.ByteArrayInputStream;
21import java.io.ByteArrayOutputStream;
22import java.io.File;
23import java.io.ObjectInputStream;
24import java.io.ObjectOutputStream;
25import java.net.URL;
26import java.text.DecimalFormat;
27import java.text.DecimalFormatSymbols;
28import java.text.NumberFormat;
29import java.util.Currency;
30import java.util.Locale;
31import java.util.ServiceConfigurationError;
32
33import junit.framework.TestCase;
34
35import org.apache.harmony.testframework.serialization.SerializationTest;
36
37public class DecimalFormatSymbolsTest extends TestCase {
38
39    DecimalFormatSymbols dfs;
40
41    DecimalFormatSymbols dfsUS;
42
43    /**
44     * @tests java.text.DecimalFormatSymbols#DecimalFormatSymbols()
45     */
46    public void test_Constructor() {
47        // Test for method java.text.DecimalFormatSymbols()
48        // Used in tests
49    }
50
51    /**
52     * @tests java.text.DecimalFormatSymbols#DecimalFormatSymbols(java.util.Locale)
53     */
54    public void test_ConstructorLjava_util_Locale() {
55        DecimalFormatSymbols dfs = new DecimalFormatSymbols(new Locale("en",
56                "us"));
57        assertEquals("Returned incorrect symbols", '%', dfs.getPercent());
58    }
59
60
61    /**
62     * @tests java.text.DecimalFormatSymbols#getAvailableLocales()
63     */
64    public void test_getAvailableLocales_no_provider() throws Exception {
65        Locale[] locales = DecimalFormatSymbols.getAvailableLocales();
66        assertNotNull(locales);
67        // must contain Locale.US
68        boolean flag = false;
69        for (Locale locale : locales) {
70            if (locale.equals(Locale.US)) {
71                flag = true;
72                break;
73            }
74        }
75        assertTrue(flag);
76    }
77
78    /**
79     * @tests java.text.DecimalFormatSymbols#getInstance()
80     */
81    public void test_getInstance() {
82        assertEquals(new DecimalFormatSymbols(), DecimalFormatSymbols.getInstance());
83        assertEquals(new DecimalFormatSymbols(Locale.getDefault()),
84                DecimalFormatSymbols.getInstance());
85
86        assertNotSame(DecimalFormatSymbols.getInstance(), DecimalFormatSymbols.getInstance());
87    }
88
89    public void test_getInstanceLjava_util_Locale() {
90        try {
91            DecimalFormatSymbols.getInstance(null);
92            fail();
93        } catch (NullPointerException expected) {
94        }
95
96        assertEquals(new DecimalFormatSymbols(Locale.GERMANY), DecimalFormatSymbols.getInstance(Locale.GERMANY));
97
98        Locale locale = new Locale("not exist language", "not exist country");
99        DecimalFormatSymbols symbols = DecimalFormatSymbols.getInstance(locale);
100        assertNotNull(symbols);
101    }
102
103    /**
104     * @tests java.text.DecimalFormatSymbols#equals(java.lang.Object)
105     */
106    public void test_equalsLjava_lang_Object() {
107        assertTrue("Equal objects returned false", dfs.equals(dfs.clone()));
108        dfs.setDigit('B');
109        assertTrue("Un-Equal objects returned true", !dfs
110                .equals(new DecimalFormatSymbols()));
111
112    }
113
114    /**
115     * @tests java.text.DecimalFormatSymbols#getCurrency()
116     */
117    public void test_getCurrency() {
118        Currency currency = Currency.getInstance("USD");
119        assertEquals("Returned incorrect currency",
120                dfsUS.getCurrency(), currency);
121
122        Currency currK = Currency.getInstance("KRW");
123        Currency currX = Currency.getInstance("XXX");
124        Currency currE = Currency.getInstance("EUR");
125        // Currency currF = Currency.getInstance("FRF");
126
127        DecimalFormatSymbols dfs1 = new DecimalFormatSymbols(new Locale("ko",
128                "KR"));
129        assertTrue("Test1: Returned incorrect currency",
130                dfs1.getCurrency() == currK);
131        assertEquals("Test1: Returned incorrect currencySymbol", "\u20a9", dfs1
132                .getCurrencySymbol());
133        assertEquals("Test1: Returned incorrect intlCurrencySymbol", "KRW",
134                dfs1.getInternationalCurrencySymbol());
135
136        dfs1 = new DecimalFormatSymbols(new Locale("", "KR"));
137        assertTrue("Test2: Returned incorrect currency",
138                dfs1.getCurrency() == currK);
139        assertEquals("Test2: Returned incorrect currencySymbol", "\u20a9", dfs1
140                .getCurrencySymbol());
141        assertEquals("Test2: Returned incorrect intlCurrencySymbol", "KRW",
142                dfs1.getInternationalCurrencySymbol());
143
144        dfs1 = new DecimalFormatSymbols(new Locale("ko", ""));
145        assertTrue("Test3: Returned incorrect currency",
146                dfs1.getCurrency() == currX);
147        assertEquals("Test3: Returned incorrect currencySymbol", "\u00a4", dfs1
148                .getCurrencySymbol());
149        assertEquals("Test3: Returned incorrect intlCurrencySymbol", "XXX",
150                dfs1.getInternationalCurrencySymbol());
151
152        dfs1 = new DecimalFormatSymbols(new Locale("fr", "FR"));
153        assertTrue("Test4: Returned incorrect currency",
154                dfs1.getCurrency() == currE);
155        assertEquals("Test4: Returned incorrect currencySymbol", "\u20ac", dfs1
156                .getCurrencySymbol());
157        assertEquals("Test4: Returned incorrect intlCurrencySymbol", "EUR",
158                dfs1.getInternationalCurrencySymbol());
159
160        // RI fails these tests since it doesn't have the PREEURO variant
161        // dfs1 = new DecimalFormatSymbols(new Locale("fr", "FR","PREEURO"));
162        // assertTrue("Test5: Returned incorrect currency", dfs1.getCurrency()
163        // == currF);
164        // assertTrue("Test5: Returned incorrect currencySymbol",
165        // dfs1.getCurrencySymbol().equals("F"));
166        // assertTrue("Test5: Returned incorrect intlCurrencySymbol",
167        // dfs1.getInternationalCurrencySymbol().equals("FRF"));
168    }
169
170    /**
171     * @tests java.text.DecimalFormatSymbols#getCurrencySymbol()
172     */
173    public void test_getCurrencySymbol() {
174        assertEquals("Returned incorrect currencySymbol", "$", dfsUS
175                .getCurrencySymbol());
176    }
177
178    /**
179     * @tests java.text.DecimalFormatSymbols#getDecimalSeparator()
180     */
181    public void test_getDecimalSeparator() {
182        dfs.setDecimalSeparator('*');
183        assertEquals("Returned incorrect DecimalSeparator symbol", '*', dfs
184                .getDecimalSeparator());
185    }
186
187    /**
188     * @tests java.text.DecimalFormatSymbols#getDigit()
189     */
190    public void test_getDigit() {
191        dfs.setDigit('*');
192        assertEquals("Returned incorrect Digit symbol", '*', dfs.getDigit());
193    }
194
195    /**
196     * @tests java.text.DecimalFormatSymbols#getExponentSeparator()
197     */
198    public void test_getExponentSeparator() {
199        dfs.setExponentSeparator("EE");
200        assertEquals("Returned incorrect Exponent Separator symbol", "EE", dfs
201                .getExponentSeparator());
202    }
203
204    /**
205     * @tests java.text.DecimalFormatSymbols#getGroupingSeparator()
206     */
207    public void test_getGroupingSeparator() {
208        dfs.setGroupingSeparator('*');
209        assertEquals("Returned incorrect GroupingSeparator symbol", '*', dfs
210                .getGroupingSeparator());
211    }
212
213    /**
214     * @tests java.text.DecimalFormatSymbols#getInfinity()
215     */
216    public void test_getInfinity() {
217        dfs.setInfinity("&");
218        assertTrue("Returned incorrect Infinity symbol",
219                dfs.getInfinity() == "&");
220    }
221
222    /**
223     * @tests java.text.DecimalFormatSymbols#getInternationalCurrencySymbol()
224     */
225    public void test_getInternationalCurrencySymbol() {
226        assertEquals("Returned incorrect InternationalCurrencySymbol", "USD",
227                dfsUS.getInternationalCurrencySymbol());
228    }
229
230    /**
231     * @tests java.text.DecimalFormatSymbols#getMinusSign()
232     */
233    public void test_getMinusSign() {
234        dfs.setMinusSign('&');
235        assertEquals("Returned incorrect MinusSign symbol", '&', dfs
236                .getMinusSign());
237    }
238
239    /**
240     * @tests java.text.DecimalFormatSymbols#getNaN()
241     */
242    public void test_getNaN() {
243        dfs.setNaN("NAN!!");
244        assertEquals("Returned incorrect nan symbol", "NAN!!", dfs.getNaN());
245    }
246
247    /**
248     * @tests java.text.DecimalFormatSymbols#getPatternSeparator()
249     */
250    public void test_getPatternSeparator() {
251        dfs.setPatternSeparator('X');
252        assertEquals("Returned incorrect PatternSeparator symbol", 'X', dfs
253                .getPatternSeparator());
254    }
255
256    /**
257     * @tests java.text.DecimalFormatSymbols#getPercent()
258     */
259    public void test_getPercent() {
260        dfs.setPercent('*');
261        assertEquals("Returned incorrect Percent symbol", '*', dfs.getPercent());
262    }
263
264    /**
265     * @tests java.text.DecimalFormatSymbols#getPerMill()
266     */
267    public void test_getPerMill() {
268        dfs.setPerMill('#');
269        assertEquals("Returned incorrect PerMill symbol", '#', dfs.getPerMill());
270    }
271
272    /**
273     * @tests java.text.DecimalFormatSymbols#getZeroDigit()
274     */
275    public void test_getZeroDigit() {
276        dfs.setZeroDigit('*');
277        assertEquals("Returned incorrect ZeroDigit symbol", '*', dfs
278                .getZeroDigit());
279    }
280
281    /**
282     * @tests java.text.DecimalFormatSymbols#setCurrency(java.util.Currency)
283     */
284    public void test_setCurrencyLjava_util_Currency() {
285        Locale locale = Locale.CANADA;
286        DecimalFormatSymbols dfs = ((DecimalFormat) NumberFormat
287                .getCurrencyInstance(locale)).getDecimalFormatSymbols();
288
289        try {
290            dfs.setCurrency(null);
291            fail("Expected NullPointerException");
292        } catch (NullPointerException e) {
293        }
294
295        Currency currency = Currency.getInstance("JPY");
296        dfs.setCurrency(currency);
297
298        assertTrue("Returned incorrect currency", currency == dfs.getCurrency());
299        assertEquals("Returned incorrect currency symbol", currency.getSymbol(
300                locale), dfs.getCurrencySymbol());
301        assertTrue("Returned incorrect international currency symbol", currency
302                .getCurrencyCode().equals(dfs.getInternationalCurrencySymbol()));
303    }
304
305    /**
306     * @tests java.text.DecimalFormatSymbols#setDecimalSeparator(char)
307     */
308    public void test_setDecimalSeparatorC() {
309        dfs.setDecimalSeparator('*');
310        assertEquals("Returned incorrect DecimalSeparator symbol", '*', dfs
311                .getDecimalSeparator());
312    }
313
314    /**
315     * @tests java.text.DecimalFormatSymbols#setDigit(char)
316     */
317    public void test_setDigitC() {
318        dfs.setDigit('*');
319        assertEquals("Returned incorrect Digit symbol", '*', dfs.getDigit());
320    }
321
322    /**
323     * @tests java.text.DecimalFormatSymbols#setExponentSeparator(String)
324     */
325    public void test_setExponentSeparator() {
326        try {
327            dfs.setExponentSeparator(null);
328            fail("Should throw NullPointerException");
329        } catch (NullPointerException e) {
330            // expected
331        }
332
333        dfs.setExponentSeparator("");
334        assertEquals("Returned incorrect Exponent Separator symbol", "", dfs
335                .getExponentSeparator());
336
337        dfs.setExponentSeparator("what ever you want");
338        assertEquals("Returned incorrect Exponent Separator symbol",
339                "what ever you want", dfs.getExponentSeparator());
340
341        dfs.setExponentSeparator(" E ");
342        assertEquals("Returned incorrect Exponent Separator symbol", " E ", dfs
343                .getExponentSeparator());
344    }
345
346    /**
347     * @tests java.text.DecimalFormatSymbols#setGroupingSeparator(char)
348     */
349    public void test_setGroupingSeparatorC() {
350        dfs.setGroupingSeparator('*');
351        assertEquals("Returned incorrect GroupingSeparator symbol", '*', dfs
352                .getGroupingSeparator());
353    }
354
355    /**
356     * @tests java.text.DecimalFormatSymbols#setInfinity(java.lang.String)
357     */
358    public void test_setInfinityLjava_lang_String() {
359        dfs.setInfinity("&");
360        assertTrue("Returned incorrect Infinity symbol",
361                dfs.getInfinity() == "&");
362    }
363
364    /**
365     * @tests java.text.DecimalFormatSymbols#setInternationalCurrencySymbol(java.lang.String)
366     */
367    public void test_setInternationalCurrencySymbolLjava_lang_String() {
368        Locale locale = Locale.CANADA;
369        DecimalFormatSymbols dfs = ((DecimalFormat) NumberFormat
370                .getCurrencyInstance(locale)).getDecimalFormatSymbols();
371        Currency currency = Currency.getInstance("JPY");
372        dfs.setInternationalCurrencySymbol(currency.getCurrencyCode());
373
374        assertTrue("Test1: Returned incorrect currency", currency == dfs
375                .getCurrency());
376        assertEquals("Test1: Returned incorrect currency symbol", currency
377                .getSymbol(locale), dfs.getCurrencySymbol());
378        assertTrue("Test1: Returned incorrect international currency symbol",
379                currency.getCurrencyCode().equals(
380                        dfs.getInternationalCurrencySymbol()));
381
382        dfs.setInternationalCurrencySymbol("bogus");
383        // RI support this legacy country code
384        // assertNotNull("Test2: Returned incorrect currency", dfs.getCurrency());
385        assertEquals("Test2: Returned incorrect international currency symbol",
386                "bogus", dfs.getInternationalCurrencySymbol());
387    }
388
389    /**
390     * @tests java.text.DecimalFormatSymbols#setMinusSign(char)
391     */
392    public void test_setMinusSignC() {
393        dfs.setMinusSign('&');
394        assertEquals("Returned incorrect MinusSign symbol", '&', dfs
395                .getMinusSign());
396    }
397
398    /**
399     * @tests java.text.DecimalFormatSymbols#setNaN(java.lang.String)
400     */
401    public void test_setNaNLjava_lang_String() {
402        dfs.setNaN("NAN!!");
403        assertEquals("Returned incorrect nan symbol", "NAN!!", dfs.getNaN());
404    }
405
406    /**
407     * @tests java.text.DecimalFormatSymbols#setPatternSeparator(char)
408     */
409    public void test_setPatternSeparatorC() {
410        dfs.setPatternSeparator('X');
411        assertEquals("Returned incorrect PatternSeparator symbol", 'X', dfs
412                .getPatternSeparator());
413    }
414
415    /**
416     * @tests java.text.DecimalFormatSymbols#setPercent(char)
417     */
418    public void test_setPercentC() {
419        dfs.setPercent('*');
420        assertEquals("Returned incorrect Percent symbol", '*', dfs.getPercent());
421    }
422
423    /**
424     * @tests java.text.DecimalFormatSymbols#setPerMill(char)
425     */
426    public void test_setPerMillC() {
427        dfs.setPerMill('#');
428        assertEquals("Returned incorrect PerMill symbol", '#', dfs.getPerMill());
429    }
430
431    /**
432     * @tests java.text.DecimalFormatSymbols#setZeroDigit(char)
433     */
434    public void test_setZeroDigitC() {
435        dfs.setZeroDigit('*');
436        assertEquals("Set incorrect ZeroDigit symbol", '*', dfs.getZeroDigit());
437    }
438
439    /**
440     * Sets up the fixture, for example, open a network connection. This method
441     * is called before a test is executed.
442     */
443    protected void setUp() {
444        dfs = new DecimalFormatSymbols();
445        dfsUS = new DecimalFormatSymbols(new Locale("en", "us"));
446    }
447
448    /**
449     * Tears down the fixture, for example, close a network connection. This
450     * method is called after a test is executed.
451     */
452    protected void tearDown() {
453    }
454
455    // Test serialization mechanism of DecimalFormatSymbols
456    public void test_serialization() throws Exception {
457        DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRANCE);
458        Currency currency = symbols.getCurrency();
459        assertNotNull(currency);
460
461        // serialize
462        ByteArrayOutputStream byteOStream = new ByteArrayOutputStream();
463        ObjectOutputStream objectOStream = new ObjectOutputStream(byteOStream);
464        objectOStream.writeObject(symbols);
465
466        // and deserialize
467        ObjectInputStream objectIStream = new ObjectInputStream(
468                new ByteArrayInputStream(byteOStream.toByteArray()));
469        DecimalFormatSymbols symbolsD = (DecimalFormatSymbols) objectIStream
470                .readObject();
471
472        // The associated currency will not persist
473        currency = symbolsD.getCurrency();
474        assertNotNull(currency);
475    }
476
477    /**
478     * Assert that Harmony can correct read an instance that was created by
479     * the Java 1.5 RI. The actual values may differ on Harmony and other JREs,
480     * so we only assert the values that are known to be in the serialized data.
481     */
482    public void test_RIHarmony_compatible() throws Exception {
483        DecimalFormatSymbols dfs;
484        ObjectInputStream i = null;
485        try {
486            i = new ObjectInputStream(getClass().getClassLoader().getResourceAsStream(
487                    "serialization/org/apache/harmony/tests/java/text/DecimalFormatSymbols.ser"));
488            dfs = (DecimalFormatSymbols) i.readObject();
489        } finally {
490            try {
491                if (i != null) {
492                    i.close();
493                }
494            } catch (Exception e) {
495            }
496        }
497        assertDecimalFormatSymbolsRIFrance(dfs);
498    }
499
500    static void assertDecimalFormatSymbolsRIFrance(DecimalFormatSymbols dfs) {
501        // Values based on Java 1.5 RI DecimalFormatSymbols for Locale.FRANCE
502        /*
503         * currency = [EUR]
504         * currencySymbol = [€][U+20ac]
505         * decimalSeparator = [,][U+002c]
506         * digit = [#][U+0023]
507         * groupingSeparator = [ ][U+00a0]
508         * infinity = [∞][U+221e]
509         * internationalCurrencySymbol = [EUR]
510         * minusSign = [-][U+002d]
511         * monetaryDecimalSeparator = [,][U+002c]
512         * naN = [�][U+fffd]
513         * patternSeparator = [;][U+003b]
514         * perMill = [‰][U+2030]
515         * percent = [%][U+0025]
516         * zeroDigit = [0][U+0030]
517         */
518        assertEquals("EUR", dfs.getCurrency().getCurrencyCode());
519        assertEquals("\u20AC", dfs.getCurrencySymbol());
520        assertEquals(',', dfs.getDecimalSeparator());
521        assertEquals('#', dfs.getDigit());
522        assertEquals('\u00a0', dfs.getGroupingSeparator());
523        assertEquals("\u221e", dfs.getInfinity());
524        assertEquals("EUR", dfs.getInternationalCurrencySymbol());
525        assertEquals('-', dfs.getMinusSign());
526        assertEquals(',', dfs.getMonetaryDecimalSeparator());
527        // RI's default NaN is U+FFFD, Harmony's is based on ICU
528        assertEquals("\uFFFD", dfs.getNaN());
529        assertEquals('\u003b', dfs.getPatternSeparator());
530        assertEquals('\u2030', dfs.getPerMill());
531        assertEquals('%', dfs.getPercent());
532        assertEquals('0', dfs.getZeroDigit());
533    }
534
535    /**
536     * @tests serialization/deserialization compatibility with RI6.
537     */
538    public void testSerializationCompatibility() throws Exception {
539        DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.US);
540        symbols.setExponentSeparator("EE");
541        symbols.setNaN("NaN");
542        SerializationTest.verifyGolden(this, symbols);
543    }
544
545    /**
546     * @tests serialization/deserialization compatibility.
547     */
548    public void testSerializationSelf() throws Exception {
549        DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ITALIAN);
550        SerializationTest.verifySelf(symbols);
551    }
552}
553