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