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 libcore.java.text;
19 
20 import java.io.ObjectInputStream;
21 import java.text.DecimalFormatSymbols;
22 import java.util.Locale;
23 import junit.framework.TestCase;
24 
25 public class OldDecimalFormatSymbolsTest extends TestCase {
26 
27     DecimalFormatSymbols dfs;
28 
setUp()29     protected void setUp() {
30         dfs = new DecimalFormatSymbols();
31     }
32 
test_RIHarmony_compatible()33     public void test_RIHarmony_compatible() throws Exception {
34         ObjectInputStream i = null;
35         try {
36             DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRANCE);
37             i = new ObjectInputStream(
38                     getClass()
39                             .getClassLoader()
40                             .getResourceAsStream(
41                     "serialization/java/text/DecimalFormatSymbols.ser"));
42             DecimalFormatSymbols riSymbols = (DecimalFormatSymbols) i.readObject();
43             // RI's default NaN is U+FFFD, Harmony's is based on ICU
44             // This suggests an RI bug, assuming that non-UTF8 bytes are UTF8 and
45             // getting a conversion failure.
46             riSymbols.setNaN("NaN");
47             assertEquals(symbols, riSymbols);
48         } catch(NullPointerException e) {
49             assertNotNull("Failed to load /serialization/java/text/" +
50                     "DecimalFormatSymbols.ser", i);
51         } finally {
52             try {
53                 if (i != null) {
54                     i.close();
55                 }
56             } catch (Exception e) {
57             }
58         }
59     }
60 
61 
test_Constructor()62     public void test_Constructor() {
63         new DecimalFormatSymbols();
64     }
65 
66     /**
67      * java.text.DecimalFormatSymbols#DecimalFormatSymbols(java.util.Locale)
68      */
test_ConstructorLjava_util_Locale()69     public void test_ConstructorLjava_util_Locale() {
70         try {
71             new DecimalFormatSymbols(null);
72             fail("NullPointerException was not thrown.");
73         } catch(NullPointerException npe) {
74             //expected
75         }
76     }
77 
test_getMonetaryDecimalSeparator()78     public void test_getMonetaryDecimalSeparator() {
79         dfs.setMonetaryDecimalSeparator(',');
80         assertEquals("Returned incorrect MonetaryDecimalSeparator symbol",
81                 ',', dfs.getMonetaryDecimalSeparator());
82     }
83 
test_hashCode()84     public void test_hashCode() {
85         DecimalFormatSymbols dfs1 = new DecimalFormatSymbols();
86         DecimalFormatSymbols dfs2 = (DecimalFormatSymbols) dfs1.clone();
87         assertTrue("Hash codes of equal object are equal", dfs2
88                 .hashCode() == dfs1.hashCode());
89         dfs1.setInfinity("infinity_infinity");
90         assertTrue("Hash codes of non-equal objects are equal", dfs2
91                 .hashCode() != dfs1.hashCode());
92     }
93 
test_clone()94     public void test_clone() {
95         // case 1: Compare of internal variables of cloned objects
96         DecimalFormatSymbols fs = new DecimalFormatSymbols(Locale.US);
97         DecimalFormatSymbols fsc = (DecimalFormatSymbols) fs.clone();
98         assertEquals(fs.getCurrency(), fsc.getCurrency());
99 
100         // case 2: Compare of clones
101         fs = new DecimalFormatSymbols();
102         DecimalFormatSymbols fsc2 = (DecimalFormatSymbols) (fs.clone());
103         // make sure the objects are equal
104         assertTrue("Object's clone isn't equal!", fs.equals(fsc2));
105 
106         // case 3:
107         // change the content of the clone and make sure it's not equal
108         // anymore
109         // verifies that it's data is now distinct from the original
110         fs.setNaN("not-a-number");
111         assertTrue("Object's changed clone should not be equal!", !fs.equals(fsc2));
112     }
113 
test_setCurrencySymbolLjava_lang_String()114     public void test_setCurrencySymbolLjava_lang_String() {
115         dfs.setCurrencySymbol("$");
116         assertEquals("Returned incorrect CurrencySymbol symbol", "$", dfs.getCurrencySymbol());
117     }
118 
test_setMonetaryDecimalSeparatorC()119     public void test_setMonetaryDecimalSeparatorC() {
120         dfs.setMonetaryDecimalSeparator('#');
121         assertEquals("Returned incorrect MonetaryDecimalSeparator symbol",
122                 '#', dfs.getMonetaryDecimalSeparator());
123     }
124 
test_DecimalFormatSymbols_France()125     public void test_DecimalFormatSymbols_France() {
126         /*
127          * currency = [EUR]
128          * currencySymbol = [U+20ac] // EURO SIGN
129          * decimalSeparator = [,][U+002c]
130          * digit = [#][U+0023]
131          * groupingSeparator = [U+00a0] // NON-BREAKING SPACE
132          * infinity = [U+221e] // INFINITY
133          * internationalCurrencySymbol = [EUR]
134          * minusSign = [-][U+002d]
135          * monetaryDecimalSeparator = [,][U+002c]
136          * naN = "NaN"
137          * patternSeparator = [;][U+003b]
138          * perMill = [U+2030] // PER MILLE
139          * percent = [%][U+0025]
140          * zeroDigit = [0][U+0030]
141          */
142         DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.FRANCE);
143         assertEquals("EUR", dfs.getCurrency().getCurrencyCode());
144         assertEquals("\u20AC", dfs.getCurrencySymbol());
145         assertEquals(',', dfs.getDecimalSeparator());
146         assertEquals('#', dfs.getDigit());
147         assertEquals('\u00a0', dfs.getGroupingSeparator());
148         assertEquals("\u221e", dfs.getInfinity());
149         assertEquals("EUR", dfs.getInternationalCurrencySymbol());
150         assertEquals('-', dfs.getMinusSign());
151         assertEquals(',', dfs.getMonetaryDecimalSeparator());
152         // RI's default NaN is U+FFFD, Harmony's is based on ICU
153         // This suggests an RI bug, assuming that non-UTF8 bytes are UTF8 and
154         // getting a conversion failure.
155         assertEquals("NaN", dfs.getNaN());
156         assertEquals('\u003b', dfs.getPatternSeparator());
157         assertEquals('\u2030', dfs.getPerMill());
158         assertEquals('%', dfs.getPercent());
159         assertEquals('0', dfs.getZeroDigit());
160     }
161 }
162