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 package libcore.java.text;
18 
19 import libcore.test.annotation.NonCts;
20 import libcore.test.reasons.NonCtsReasons;
21 
22 import java.text.ChoiceFormat;
23 import java.text.DecimalFormat;
24 import java.text.FieldPosition;
25 import java.text.NumberFormat;
26 import java.text.ParseException;
27 import java.text.ParsePosition;
28 import java.util.Currency;
29 import java.util.Locale;
30 import junit.framework.TestCase;
31 
32 public class OldNumberFormatTest extends TestCase {
33 
test_getIntegerInstanceLjava_util_Locale()34     public void test_getIntegerInstanceLjava_util_Locale() throws ParseException {
35         DecimalFormat format = (DecimalFormat) NumberFormat.getIntegerInstance(Locale.US);
36         assertEquals("#,##0", format.toPattern());
37         assertEquals("-36", format.format(-35.76));
38         assertEquals(Long.valueOf(-36), format.parse("-36"));
39         assertEquals(Long.valueOf(-36), format.parseObject("-36"));
40         assertEquals(0, format.getMaximumFractionDigits());
41         assertTrue(format.isParseIntegerOnly());
42 
43         // try with a locale that has a different integer pattern
44         Locale chLocale = new Locale("de", "CH");
45         format = (DecimalFormat) NumberFormat.getIntegerInstance(chLocale);
46         assertEquals("#,##0", format.toPattern());
47         assertEquals("-36", format.format(-35.76));
48         assertEquals(Long.valueOf(-36), format.parse("-36"));
49         assertEquals(Long.valueOf(-36), format.parseObject("-36"));
50         assertEquals(0, format.getMaximumFractionDigits());
51         assertTrue(format.isParseIntegerOnly());
52 
53         Locale arLocale = new Locale("ar", "EG");
54         format = (DecimalFormat) NumberFormat.getIntegerInstance(arLocale);
55         String variant = (format.toPattern().indexOf(';') > 0) ? "#,##0;-#,##0" : "#,##0";
56         assertEquals(variant, format.toPattern());
57         assertEquals("\u0666\u0667", format.format(67));
58 
59         assertEquals("-\u0666", format.format(-6));
60         assertEquals(-36L, format.parse("-36"));
61 
62         // New Arabic formats do not support '-' to right of digits.
63         assertEquals(36L, format.parseObject("36-"));
64         assertEquals(0, format.getMaximumFractionDigits());
65         assertTrue(format.isParseIntegerOnly());
66     }
67 
test_setMaximumIntegerDigits()68     public void test_setMaximumIntegerDigits() {
69         NumberFormat format = NumberFormat.getInstance();
70         format.setMaximumIntegerDigits(2);
71         assertEquals("Wrong result: case 1", "23", format.format(123));
72 
73         format.setMaximumIntegerDigits(Integer.MIN_VALUE);
74         assertEquals("Wrong result: case 2", ".0", format.format(123));
75 
76         format.setMaximumIntegerDigits(0);
77         assertEquals("Wrong result: case 2", ".0", format.format(123));
78     }
79 
test_setCurrencyLjava_util_Currency()80     public void test_setCurrencyLjava_util_Currency() {
81         // Test for method void setCurrency(java.util.Currency)
82         // a subclass that supports currency formatting
83         Currency currA = Currency.getInstance("ARS");
84         NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU"));
85         format.setCurrency(currA);
86         assertSame("Returned incorrect currency", currA, format.getCurrency());
87 
88         // a subclass that doesn't support currency formatting
89         ChoiceFormat cformat = new ChoiceFormat(
90                 "0#Less than one|1#one|1<Between one and two|2<Greater than two");
91         try {
92             ((NumberFormat) cformat).setCurrency(currA);
93             fail("Expected UnsupportedOperationException");
94         } catch (UnsupportedOperationException e) {
95         }
96 
97         try {
98             NumberFormat.getInstance().setCurrency(null);
99             fail("NullPointerException was thrown.");
100         } catch(NullPointerException npe) {
101             //expected
102         }
103 
104         try {
105             NumberFormat.getIntegerInstance().setCurrency(null);
106             fail("NullPointerException was thrown.");
107         } catch(NullPointerException npe) {
108             //expected
109         }
110     }
111 
test_parseObjectLjava_lang_StringLjava_text_ParsePosition()112     public void test_parseObjectLjava_lang_StringLjava_text_ParsePosition() {
113         // regression test for HARMONY-1003
114         assertNull(NumberFormat.getInstance().parseObject("0",
115                 new ParsePosition(-1)));
116 
117         parseObjectTest(NumberFormat.getInstance(), "123.123",
118                 new ParsePosition(1), Double.valueOf(23.123), 7, true);
119 
120         parseObjectTest(NumberFormat.getInstance(), "123.123abc123",
121                 new ParsePosition(3), Double.valueOf(0.123), 7, true);
122 
123         parseObjectTest(NumberFormat.getInstance(Locale.FRANCE), "asd123,123abc123",
124                         new ParsePosition(3), Double.valueOf(123.123), 10, true);
125 
126         parseObjectTest(NumberFormat.getInstance(Locale.FRANCE), "test test",
127                         new ParsePosition(0), null, 0, false);
128 
129         parseObjectTest(NumberFormat.getIntegerInstance(),
130                 "asd123.123abc123",
131                 new ParsePosition(3), Long.valueOf(123), 6, true);
132 
133         parseObjectTest(NumberFormat.getNumberInstance(),
134                 "$-123,123.123#",
135                 new ParsePosition(1), Double.valueOf(-123123.123), 13, true);
136         parseObjectTest(NumberFormat.getNumberInstance(),
137                 "$-123,123.123#",
138                 new ParsePosition(0), null, 0, false);
139         parseObjectTest(NumberFormat.getNumberInstance(),
140                 "$-123,123.123#",
141                 new ParsePosition(13), null, 13, false);
142         parseObjectTest(NumberFormat.getPercentInstance(),
143                 "%20.123#",
144                 new ParsePosition(0), Double.valueOf(20.123), 0, false);
145         parseObjectTest(NumberFormat.getPercentInstance(),
146                 "%-200,123.123#",
147                 new ParsePosition(0), null, 0, false);
148 
149 
150         // Regression for HARMONY-1685
151         try {
152             NumberFormat.getInstance().parseObject("test", null);
153             fail("NullPointerException expected");
154         } catch (NullPointerException e) {
155             // expected
156         }
157     }
158 
parseObjectTest(NumberFormat nf, String sourseStr, ParsePosition position, Object resultObj, int outIndex, boolean isSuccess)159     void parseObjectTest(NumberFormat nf, String sourseStr, ParsePosition position,
160             Object resultObj, int outIndex, boolean isSuccess) {
161         int indexBefore = position.getIndex();
162         Object result = nf.parseObject(sourseStr, position);
163         if(isSuccess) {
164             assertEquals(resultObj, result);
165             assertEquals(outIndex, position.getIndex());
166         } else {
167             assertNull(result);
168             assertEquals(indexBefore, position.getIndex());
169             assertEquals(outIndex, position.getErrorIndex());
170         }
171     }
172 
test_clone()173     public void test_clone() {
174 
175         int max_digits = 100;
176         NumberFormat nf1 = NumberFormat.getInstance();
177         nf1.setMaximumIntegerDigits(max_digits);
178 
179         NumberFormat nf2 = (NumberFormat) nf1.clone();
180         NumberFormat nf3 = (NumberFormat) nf1.clone();
181 
182         assertTrue("Clonned object is not equal to object", nf2.equals(nf1));
183         assertTrue("Two clonned objects are not equal", nf2.equals(nf3));
184 
185         assertTrue("Max digits value is incorrect for clonned object", nf2
186                 .getMaximumIntegerDigits() == max_digits);
187 
188         nf1.setMaximumIntegerDigits(10);
189         assertTrue(
190                 "Max digits value is incorrect for clonned object after changing this value for object",
191                 nf2.getMaximumIntegerDigits() == max_digits);
192     }
193 
test_equals()194     public void test_equals() {
195 
196         NumberFormat nf1 = NumberFormat.getInstance();
197         NumberFormat nf2 = NumberFormat.getInstance();
198 
199         assertTrue("Objects are not equal", nf1.equals(nf2));
200         assertTrue("The same Objects are not equal", nf1.equals(nf1));
201 
202         nf2.setMaximumIntegerDigits(100);
203         assertFalse("Different NumberFormat are equal", nf1.equals(nf2));
204 
205         nf1.setMaximumIntegerDigits(100);
206         assertTrue("Equivalent Objects are not equal", nf1.equals(nf2));
207 
208         nf1 = NumberFormat.getIntegerInstance();
209         nf2 = NumberFormat.getIntegerInstance(Locale.CHINA);
210         assertFalse("Different NumberFormat are equal", nf1.equals(nf2));
211 
212         assertFalse("Object is equal null", nf1.equals(null));
213     }
214 
test_formatLdouble()215     public void test_formatLdouble() {
216         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
217 
218         String out = nf1.format(1234567890.0123456789);
219         assertEquals("Wrong result for double : " + out, "1,234,567,890.012",
220                 out.toString());
221 
222         out = nf1.format(-1234567890.0123456789);
223         assertEquals("Wrong result for double : " + out, "-1,234,567,890.012",
224                 out.toString());
225 
226         Locale deLocale = new Locale("de", "CH");
227         NumberFormat nf2 = NumberFormat.getInstance(deLocale);
228         out = nf2.format(-1234567890.0123456789);
229         // use de_CH instead
230         // assertEquals("Wrong result for double : " + out, "1,234,567,890.012-",
231         //         out.toString());
232         assertEquals("Wrong result for double : " + out, "-1’234’567’890.012", out.toString());
233 
234         out = nf1.format(1.0001);
235         assertEquals("Wrong result for for double: " + out, "1", out.toString());
236 
237         out = nf1.format(5.0);
238         assertEquals("Wrong result for for double: " + out, "5", out.toString());
239         // END Android-changed
240     }
241 
test_formatLlong()242     public void test_formatLlong() {
243         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
244 
245         String out = nf1.format(Long.MAX_VALUE);
246         assertEquals("Wrong result for double : " + out,
247                 "9,223,372,036,854,775,807", out.toString());
248 
249         out = nf1.format(Long.MIN_VALUE);
250         assertEquals("Wrong result for double : " + out,
251                 "-9,223,372,036,854,775,808", out.toString());
252 
253         Locale deLocale = new Locale("de", "CH");
254         NumberFormat nf2 = NumberFormat.getInstance(deLocale);
255         out = nf2.format(-1234567890);
256         // use de_CH instead
257         // assertEquals("Wrong result for double : " + out, "-1 234 567 890", out
258         //         .toString());
259         assertEquals("Wrong result for double : " + out, "-1’234’567’890", out.toString());
260 
261         // the Locale data of icu uses \uc2a0
262         out = nf1.format(1);
263         assertEquals("Wrong result for for double: " + out, "1", out.toString());
264 
265         out = nf1.format(0);
266         assertEquals("Wrong result for for double: " + out, "0", out.toString());
267         // END Android-changed
268     }
269 
test_getAvailableLocales()270     public void test_getAvailableLocales() {
271 
272         Locale[] l = NumberFormat.getAvailableLocales();
273         assertFalse("returned Locale array is null", l == null);
274         assertTrue("returned Locale length <= 0", l.length > 0);
275         Locale[] resl = Locale.getAvailableLocales();
276         assertTrue("returned Locale arrays are different",
277                 l.length == resl.length);
278         boolean isUS = false;
279         for (int i = 0; i < resl.length; i++) {
280             assertEquals("elements " + i + " are not equal: ", resl[i], l[i]);
281             if (l[i].equals(Locale.US))
282                 isUS = true;
283         }
284         assertTrue("there is no Locale.US", isUS);
285     }
286 
test_getCurrencyInstance()287     public void test_getCurrencyInstance() {
288 
289         Locale.setDefault(Locale.US);
290         NumberFormat format = NumberFormat.getCurrencyInstance();
291 
292         assertNotSame("Instance is null", null, format);
293         assertTrue("Object is not instance of NumberFormat",
294                 format instanceof NumberFormat);
295 
296         assertEquals(
297                 "Test1: NumberFormat.getCurrencyInstance().format(35.76) returned wrong value",
298                 "$35.76", format.format(35.76));
299         assertEquals(
300                 "Test2: NumberFormat.getCurrencyInstance().format(123456.789) returned wrong value",
301                 "$123,456.79", format.format(123456.789));
302         assertEquals(
303                 "Test3: NumberFormat.getCurrencyInstance().format(0.1) returned wrong value",
304                 "$0.10", format.format(0.1));
305         assertEquals(
306                 "Test4: NumberFormat.getCurrencyInstance().format(0.999) returned wrong value",
307                 "$1.00", format.format(0.999));
308     }
309 
310     @NonCts(bug = 287231726, reason = NonCtsReasons.NON_BREAKING_BEHAVIOR_FIX)
test_getCurrencyInstanceLjava_util_Locale()311     public void test_getCurrencyInstanceLjava_util_Locale() {
312         Locale usLocale = Locale.US;
313         NumberFormat format = NumberFormat.getCurrencyInstance(usLocale);
314 
315         assertNotSame("Instance is null", null, format);
316         assertTrue(format instanceof NumberFormat);
317 
318         assertEquals("$35.76", format.format(35.76));
319         assertEquals("$123,456.79", format.format(123456.789));
320         assertEquals("$0.10", format.format(0.1));
321         assertEquals("$1.00", format.format(0.999));
322 
323         Locale atLocale = new Locale("de", "AT");
324         format = NumberFormat.getCurrencyInstance(atLocale);
325         // BEGIN Android-changed: ICU uses non-breaking space after the euro sign; the RI uses ' '.
326         assertEquals("\u20ac\u00a035,76", format.format(35.76));
327         assertEquals("\u20ac\u00a0123.456,79", format.format(123456.789));
328         assertEquals("\u20ac\u00a00,10", format.format(0.1));
329         assertEquals("\u20ac\u00a01,00", format.format(0.999));
330         try {
331             NumberFormat.getCurrencyInstance(null);
332             fail("java.lang.NullPointerException is not thrown");
333         } catch (java.lang.NullPointerException expected) {
334         }
335     }
336 
test_getInstance()337     public void test_getInstance() {
338         Locale.setDefault(Locale.US);
339         NumberFormat format = NumberFormat.getInstance();
340 
341         assertNotSame("Instance is null", null, format);
342         assertTrue("Object is not instance of NumberFormat",
343                 format instanceof NumberFormat);
344 
345         assertEquals(
346                 "Test1: NumberFormat.getInstance().format(1234567890.0987654321) returned wrong value",
347                 "1,234,567,890.099", format.format(1234567890.0987654321));
348         assertEquals(
349                 "Test2: ((DecimalFormat) NumberFormat.getInstance()).toPattern returned wrong value",
350                 "#,##0.###", ((DecimalFormat) format).toPattern());
351         assertEquals(
352                 "Test3: NumberFormat.getInstance().format(123456789) returned wrong value",
353                 "123,456,789", format.format(123456789));
354     }
355 
test_getInstanceLjava_util_Locale()356     public void test_getInstanceLjava_util_Locale() {
357         Locale de_CH = new Locale("de", "CH");
358         Locale.setDefault(Locale.US);
359         NumberFormat format = NumberFormat.getInstance(de_CH);
360 
361         assertNotSame(null, format);
362         assertTrue(format instanceof NumberFormat);
363 
364         assertEquals("1’234’567’890.099", format.format(1234567890.0987654321));
365         assertEquals("#,##0.###", ((DecimalFormat) format).toPattern());
366         assertEquals("123’456’789", format.format(123456789));
367 
368         try {
369             NumberFormat.getInstance(null);
370             fail("java.lang.NullPointerException is not thrown");
371         } catch (java.lang.NullPointerException expected) {
372         }
373     }
374 
test_getNumberInstance()375     public void test_getNumberInstance() {
376         Locale.setDefault(Locale.US);
377         NumberFormat format = NumberFormat.getNumberInstance();
378 
379         assertNotSame("Instance is null", null, format);
380         assertTrue("Object is not instance of NumberFormat",
381                 format instanceof NumberFormat);
382 
383         assertEquals(
384                 "Test1: NumberFormat.getNumberInstance().format(1234567890.0987654321) returned wrong value",
385                 "1,234,567,890.099", format.format(1234567890.0987654321));
386         assertEquals(
387                 "Test2: ((DecimalFormat) NumberFormat.getNumberInstance()).toPattern returned wrong value",
388                 "#,##0.###", ((DecimalFormat) format).toPattern());
389         assertEquals(
390                 "Test3: NumberFormat.getNumberInstance().format(123456789) returned wrong value",
391                 "123,456,789", format.format(123456789));
392     }
393 
test_getNumberInstanceLjava_util_Locale()394     public void test_getNumberInstanceLjava_util_Locale() {
395         Locale.setDefault(Locale.US);
396         Locale deLocale = new Locale("de", "CH");
397         NumberFormat format = NumberFormat.getNumberInstance(deLocale);
398         assertNotSame("Instance is null", null, format);
399         assertTrue("Object is not instance of NumberFormat", format instanceof NumberFormat);
400 
401         assertEquals("-1’234’567’890.099", format.format(-1234567890.0987654321));
402         assertEquals("#,##0.###", ((DecimalFormat) format).toPattern());
403         assertEquals("123’456’789", format.format(123456789));
404 
405         try {
406             NumberFormat.getInstance(null);
407             fail("java.lang.NullPointerException is not thrown");
408         } catch (java.lang.NullPointerException expected) {
409         }
410     }
411 
test_getPercentInstance()412     public void test_getPercentInstance() {
413         Locale.setDefault(Locale.US);
414         NumberFormat format = NumberFormat.getPercentInstance();
415 
416         assertNotSame("Instance is null", null, format);
417         assertTrue("Object is not instance of NumberFormat",
418                 format instanceof NumberFormat);
419 
420         assertEquals(
421                 "Test1: NumberFormat.getPercentInstance().format(1234567890.0987654321) returned wrong value",
422                 "123,456,789,010%", format.format(1234567890.0987654321));
423         assertEquals(
424                 "Test2: ((DecimalFormat) NumberFormat.getPercentInstance()).toPattern returned wrong value",
425                 "#,##0%", ((DecimalFormat) format).toPattern());
426         assertEquals(
427                 "Test3: NumberFormat.getPercentInstance().format(123456789) returned wrong value",
428                 "12,345,678,900%", format.format(123456789));
429     }
430 
test_getPercentInstanceLjava_util_Locale()431     public void test_getPercentInstanceLjava_util_Locale() {
432         Locale csLocale = new Locale("cs", "CZ");
433         Locale.setDefault(Locale.US);
434 
435         NumberFormat format = NumberFormat.getPercentInstance(csLocale);
436         assertNotSame("Instance is null", null, format);
437         assertTrue("Object is not instance of NumberFormat", format instanceof NumberFormat);
438 
439         assertEquals("123\u00a0456\u00a0789\u00a0010\u00a0%", format.format(1234567890.0987654321));
440         assertEquals("#,##0\u00a0%", ((DecimalFormat) format).toPattern());
441         assertEquals("12\u00a0345\u00a0678\u00a0900\u00a0%", format.format(123456789));
442 
443         try {
444             NumberFormat.getInstance(null);
445             fail("java.lang.NullPointerException is not thrown");
446         } catch (java.lang.NullPointerException expected) {
447         }
448     }
449 
test_getMaximumFractionDigits()450     public void test_getMaximumFractionDigits() {
451         NumberFormat nf1 = NumberFormat.getInstance();
452 
453         nf1.setMaximumFractionDigits(Integer.MAX_VALUE);
454         int result = nf1.getMaximumFractionDigits();
455         assertTrue("getMaximumFractionDigits returns " + result
456                 + " instead of: " + Integer.MAX_VALUE,
457                 result == Integer.MAX_VALUE);
458 
459         nf1.setMaximumFractionDigits(0);
460         result = nf1.getMaximumFractionDigits();
461         assertTrue("getMaximumFractionDigits returns " + result
462                 + " instead of 0", result == 0);
463 
464         nf1.setMinimumFractionDigits(Integer.MAX_VALUE);
465         result = nf1.getMaximumFractionDigits();
466         assertTrue("getMaximumFractionDigits returns " + result
467                 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE);
468     }
469 
test_getMinimumFractionDigits()470     public void test_getMinimumFractionDigits() {
471         NumberFormat nf1 = NumberFormat.getInstance();
472         nf1.setMinimumFractionDigits(Integer.MAX_VALUE);
473         int result = nf1.getMinimumFractionDigits();
474         assertTrue("getMinimumFractionDigits returns " + result
475                 + " instead of: " + Integer.MAX_VALUE,
476                 result == Integer.MAX_VALUE);
477 
478         nf1.setMaximumFractionDigits(0);
479         result = nf1.getMinimumFractionDigits();
480         assertTrue("getMinimumFractionDigits returns " + result
481                 + " instead of 0", result == 0);
482 
483         nf1.setMinimumFractionDigits(52);
484         result = nf1.getMinimumFractionDigits();
485         assertTrue("getMinimumFractionDigits returns " + result
486                 + " instead of 52", result == 52);
487     }
488 
test_getMaximumIntegerDigits()489     public void test_getMaximumIntegerDigits() {
490         NumberFormat nf1 = NumberFormat.getInstance();
491         nf1.setMaximumIntegerDigits(Integer.MAX_VALUE);
492         int result = nf1.getMaximumIntegerDigits();
493         assertTrue("getMaximumIntegerDigits returns " + result
494                 + " instead of: " + Integer.MAX_VALUE,
495                 result == Integer.MAX_VALUE);
496 
497         nf1.setMaximumIntegerDigits(0);
498         result = nf1.getMaximumIntegerDigits();
499         assertTrue("getMaximumIntegerDigits returns " + result
500                 + " instead of 0", result == 0);
501 
502         nf1.setMinimumIntegerDigits(Integer.MAX_VALUE);
503         result = nf1.getMaximumIntegerDigits();
504         assertTrue("getMaximumIntegerigits returns " + result
505                 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE);
506     }
507 
test_getMinimumIntegernDigits()508     public void test_getMinimumIntegernDigits() {
509         NumberFormat nf1 = NumberFormat.getInstance();
510         nf1.setMinimumIntegerDigits(Integer.MAX_VALUE);
511         int result = nf1.getMinimumIntegerDigits();
512         assertTrue("getMinimumIntegerDigits returns " + result
513                 + " instead of: " + Integer.MAX_VALUE,
514                 result == Integer.MAX_VALUE);
515 
516         nf1.setMaximumIntegerDigits(0);
517         result = nf1.getMinimumIntegerDigits();
518         assertTrue("getMinimumIntegerDigits returns " + result
519                 + " instead of 0", result == 0);
520 
521         nf1.setMinimumIntegerDigits(0x12034);
522         result = nf1.getMinimumIntegerDigits();
523         assertTrue("getMinimumIntegerDigits returns " + result
524                 + " instead of 5148", result == 73780);
525     }
526 
test_hashCode()527     public void test_hashCode() {
528 
529         NumberFormat nf1 = NumberFormat.getInstance();
530         NumberFormat nf11 = NumberFormat.getInstance();
531         NumberFormat nf2 = NumberFormat.getInstance(Locale.US);
532         NumberFormat nf3 = NumberFormat.getPercentInstance();
533         NumberFormat nf4 = NumberFormat.getCurrencyInstance();
534         NumberFormat nf5 = NumberFormat
535                 .getNumberInstance(new Locale("mk", "MK"));
536         NumberFormat nf6 = NumberFormat.getInstance(Locale.US);
537 
538         assertTrue("Hash codes are not equal: case 1", nf1.hashCode() == nf2
539                 .hashCode());
540         assertTrue("Hash codes are not equal: case 2", nf1.hashCode() == nf11
541                 .hashCode());
542         assertTrue("Hash codes are not equal: case 3", nf1.hashCode() == nf3
543                 .hashCode());
544         assertFalse("Hash codes are equal: case 4", nf3.hashCode() == nf4
545                 .hashCode());
546         assertFalse("Hash codes are equal: case 5", nf4.hashCode() == nf5
547                 .hashCode());
548         assertTrue("Hash codes are not equal: case 6", nf5.hashCode() == nf6
549                 .hashCode());
550 
551         nf1.setMaximumFractionDigits(0);
552         assertTrue("Hash codes are not equal: case 7", nf1.hashCode() == nf11
553                 .hashCode());
554     }
555 
test_isGroupingUsed()556     public void test_isGroupingUsed() {
557         NumberFormat nf1 = NumberFormat.getInstance();
558         assertTrue("grouping is not used for NumberFormat.getInstance", nf1
559                 .isGroupingUsed());
560 
561         nf1.setGroupingUsed(false);
562         assertFalse(
563                 "grouping is used for NumberFormat.getInstance after setting false",
564                 nf1.isGroupingUsed());
565 
566         nf1.setGroupingUsed(true);
567         assertTrue(
568                 "grouping is not used for NumberFormat.getInstance after setting true",
569                 nf1.isGroupingUsed());
570     }
571 
test_setGroupingUsed()572     public void test_setGroupingUsed() {
573         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
574         nf1.setGroupingUsed(false);
575 
576         assertEquals("grouping is used for 1234567890.1", "1234567890.1",
577                 nf1.format(1234567890.1));
578 
579         assertEquals("grouping is used for -1234567890.1", "-1234567890.1",
580                 nf1.format(-1234567890.1));
581 
582         nf1.setGroupingUsed(false);
583 
584         assertEquals("grouping is used for 1234567890.1", "1234567890.1",
585                 nf1.format(1234567890.1));
586 
587         assertEquals("grouping is used for -1234567890.1", "-1234567890.1",
588                 nf1.format(-1234567890.1));
589 
590         nf1.setGroupingUsed(true);
591 
592         assertEquals("grouping is not used for 1234567890.1",
593                 "1,234,567,890.1", nf1.format(1234567890.1));
594 
595         assertEquals("grouping is not used for -1234567890.1",
596                 "-1,234,567,890.1", nf1.format(-1234567890.1));
597 
598         Locale csLocale = new Locale("cs", "CZ");
599         NumberFormat nf2 = NumberFormat.getPercentInstance(csLocale);
600         nf2.setGroupingUsed(false);
601 
602         assertEquals("123456789010\u00a0%", nf2.format(1234567890.1));
603 
604         assertEquals("-123456789010\u00a0%", nf2.format(-1234567890.1));
605         assertEquals("1,234,567,890.1", nf1.format(1234567890.1));
606 
607         nf2.setGroupingUsed(true);
608         assertEquals("123\u00a0456\u00a0789\u00a0010\u00a0%", nf2.format(1234567890.1));
609 
610         assertEquals("-123\u00a0456\u00a0789\u00a0010\u00a0%", nf2.format(-1234567890.1));
611 
612         nf2.setGroupingUsed(true);
613         assertEquals("123\u00a0456\u00a0789\u00a0010\u00a0%", nf2.format(1234567890.1));
614 
615         assertEquals("-123\u00a0456\u00a0789\u00a0010\u00a0%", nf2.format(-1234567890.1));
616     }
617 
test_isParseIntegerOnly()618     public void test_isParseIntegerOnly() {
619         NumberFormat nf1 = NumberFormat.getInstance();
620         assertTrue("ParseIntegerOnly is not used for NumberFormat.getInstance",
621                 nf1.isGroupingUsed());
622 
623         nf1.setParseIntegerOnly(false);
624         assertFalse(
625                 "ParseIntegerOnly is used for NumberFormat.getInstance after setting false",
626                 nf1.isParseIntegerOnly());
627 
628         nf1.setParseIntegerOnly(true);
629         assertTrue(
630                 "ParseIntegerOnly is not used for NumberFormat.getInstance after setting true",
631                 nf1.isParseIntegerOnly());
632     }
633 
test_setParseIntegerOnly()634     public void test_setParseIntegerOnly() {
635         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
636         nf1.setParseIntegerOnly(true);
637 
638         assertEquals("ParseIntegerOnly is not used for 1234567890.1",
639                 "1,234,567,890.1", nf1.format(1234567890.1));
640         assertEquals("ParseIntegerOnly is not used for -1234567890.1",
641                 "-1,234,567,890.1", nf1.format(-1234567890.1));
642         assertEquals("ParseIntegerOnly is not used for -1234567890.",
643                 "-1,234,567,890", nf1.format(-1234567890.));
644 
645         nf1.setParseIntegerOnly(false);
646 
647         assertEquals("ParseIntegerOnly is not used for 1234567890.1",
648                 "1,234,567,890.1", nf1.format(1234567890.1));
649         assertEquals("ParseIntegerOnly is not used for -1234567890.1",
650                 "-1,234,567,890.1", nf1.format(-1234567890.1));
651         assertEquals("ParseIntegerOnly is not used for -1234567890.",
652                 "-1,234,567,890", nf1.format(-1234567890.));
653     }
654 
test_setMaximumFractionDigits()655     public void test_setMaximumFractionDigits() {
656         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
657         nf1.setMaximumFractionDigits(Integer.MAX_VALUE);
658         int result = nf1.getMaximumFractionDigits();
659         assertTrue("setMaximumFractionDigits set " + result
660                 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE);
661         nf1.setMaximumFractionDigits(0);
662         result = nf1.getMaximumFractionDigits();
663         assertTrue("setMaximumFractionDigits set " + result + " instead of 0",
664                 result == 0);
665         assertEquals("format of 1234567890.0987654321 returns incorrect value",
666                 "1,234,567,890", nf1.format(1234567890.0987654321));
667         nf1.setMaximumFractionDigits(5);
668         result = nf1.getMaximumFractionDigits();
669         assertTrue("setMaximumFractionDigits set " + result + " instead of 5",
670                 result == 5);
671         assertEquals(
672                 "format of 1234567890.0987654321 returns incorrect value with MaximumFractionDigits = 5",
673                 "1,234,567,890.09877", nf1.format(1234567890.0987654321));
674         assertEquals(
675                 "format of -1234567890 returns incorrect value with MaximumFractionDigits = 5",
676                 "-1,234,567,890", nf1.format(-1234567890));
677         nf1.setMaximumFractionDigits(Integer.MIN_VALUE);
678         result = nf1.getMaximumFractionDigits();
679         assertTrue("setMaximumFractionDigits set " + result
680                 + " instead of Integer.MIN_VALUE", result == 0);
681         assertEquals(
682                 "format of 1234567890.0987654321 returns incorrect value with MaximumFractionDigits = 5",
683                 "1,234,567,890", nf1.format(1234567890.0987654321));
684     }
685 
test_setMinimumFractionDigits()686     public void test_setMinimumFractionDigits() {
687 
688         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
689         nf1.setMinimumFractionDigits(Integer.MAX_VALUE);
690         int result = nf1.getMinimumFractionDigits();
691         assertTrue("setMinimumFractionDigits set " + result
692                 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE);
693         nf1.setMinimumFractionDigits(0);
694         result = nf1.getMinimumFractionDigits();
695         assertTrue("setMinimumFractionDigits set " + result + " instead of 0",
696                 result == 0);
697         nf1.setMinimumFractionDigits(5);
698         result = nf1.getMinimumFractionDigits();
699         assertTrue("setMinimumFractionDigits set " + result + " instead of 5",
700                 result == 5);
701         assertEquals(
702                 "format of 1234567890.0987654321 returns incorrect value with MinimumFractionDigits = 5",
703                 "1,234,567,890.09000", nf1.format(1234567890.09));
704         assertEquals(
705                 "format of -1234567890 returns incorrect value with MinimumFractionDigits = 5",
706                 "-1,234,567,890.00000", nf1.format(-1234567890));
707         nf1.setMinimumFractionDigits(Integer.MIN_VALUE);
708         result = nf1.getMinimumFractionDigits();
709         assertTrue("setMinimumFractionDigits set " + result
710                 + " instead of Integer.MIN_VALUE", result == 0);
711         assertEquals(
712                 "format of 1234567890.098 returns incorrect value with MinimumFractionDigits = 5",
713                 "1,234,567,890.098", nf1.format(1234567890.098));
714     }
715 
test_setMinimumIntegerDigits()716     public void test_setMinimumIntegerDigits() {
717 
718         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
719         nf1.setMinimumIntegerDigits(Integer.MAX_VALUE);
720         int result = nf1.getMinimumIntegerDigits();
721         assertTrue("setMinimumIntegerDigits set " + result
722                 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE);
723         nf1.setMinimumIntegerDigits(0);
724         result = nf1.getMinimumIntegerDigits();
725         assertTrue("setMinimumIntegerDigits set " + result + " instead of 0",
726                 result == 0);
727         nf1.setMinimumIntegerDigits(5);
728         result = nf1.getMinimumIntegerDigits();
729         assertTrue("setMinimumIntegerDigits set " + result + " instead of 5",
730                 result == 5);
731         assertEquals(
732                 "format of 123.09 returns incorrect value with MinimumIntegerDigits = 5",
733                 "00,123.09", nf1.format(123.09));
734         assertEquals(
735                 "format of -123 returns incorrect value with MinimumIntegerDigits = 5",
736                 "-00,123", nf1.format(-123));
737         nf1.setMinimumIntegerDigits(Integer.MIN_VALUE);
738         result = nf1.getMinimumIntegerDigits();
739         assertTrue("setMinimumIntegerDigits set " + result
740                 + " instead of Integer.MIN_VALUE", result == 0);
741     }
742 
743     // Broken Test: Fails in CTS, passes in CoreTestRunner
test_parseLjava_lang_String()744     public void test_parseLjava_lang_String() {
745         NumberFormat nf1 = NumberFormat.getInstance();
746         try {
747             assertEquals(
748                     "Test1: NumberFormat.getInstance().parse(\"1234567890.1\") returned wrong number",
749                     Double.valueOf(1234567890.1), nf1.parse("1234567890.1"));
750         } catch (java.text.ParseException pe) {
751             fail("java.text.ParseException is thrown for 1234567890.1");
752         }
753 
754         try {
755             assertEquals(
756                     "Test2: NumberFormat.getInstance().parse(\"-1234567890.1\") returned wrong number",
757                     Double.valueOf(-1234567890.1), nf1.parse("-1,234,567,890.1"));
758         } catch (java.text.ParseException pe) {
759             fail("java.text.ParseException is thrown for -1,234,567,890.1");
760         }
761 
762         try {
763             nf1.parse("@1,234,567,8901");
764             fail("java.text.ParseException is not thrown for 1,234,567,890z1");
765         } catch (java.text.ParseException pe) {
766             // expected
767         }
768 
769         nf1 = NumberFormat.getPercentInstance();
770         try {
771             assertEquals(
772                     "Test3: NumberFormat.getPercentInstance().parse(\"-123%\") returned wrong number",
773                     Double.valueOf(-1.23), nf1.parse("-123%"));
774         } catch (java.text.ParseException pe) {
775             fail("java.text.ParseException is thrown for -123%");
776         }
777 
778         nf1 = NumberFormat.getCurrencyInstance();
779         try {
780             assertEquals(
781                     "Test4: NumberFormat.getCurrencyInstance().parse(\"$123\") returned wrong number",
782                     Long.valueOf(123), nf1.parse("$123"));
783         } catch (java.text.ParseException pe) {
784             fail("java.text.ParseException is thrown for $123");
785         }
786 
787         try {
788             assertEquals(
789                     "Test4: NumberFormat.getCurrencyInstance().parse(\"$123abc\") returned wrong number",
790                     Long.valueOf(123), nf1.parse("$123abc"));
791         } catch (java.text.ParseException pe) {
792             fail("java.text.ParseException is thrown for $123");
793         }
794 
795         nf1 = NumberFormat.getIntegerInstance();
796         try {
797             assertEquals(
798                     "Test5: NumberFormat.getIntegerInstance().parse(\"-123.123\") returned wrong number",
799                     nf1.parseObject("-123.123"), nf1.parse("-123.123"));
800         } catch (java.text.ParseException pe) {
801             fail("java.text.ParseException is thrown for $123");
802         }
803     }
804 
test_constructor()805     public void test_constructor() {
806         MyNumberFormat mf = new MyNumberFormat();
807         assertFalse("Greated NumberFormat object is null", mf == null);
808         assertTrue(
809                 "Greated NumberFormat object is not instance of NumberFormat",
810                 mf instanceof NumberFormat);
811     }
812 
813     class MyNumberFormat extends NumberFormat {
814         static final long serialVersionUID = 1L;
815 
MyNumberFormat()816         public MyNumberFormat() {
817             super();
818         }
819 
format(double number, StringBuffer toAppendTo, FieldPosition pos)820         public StringBuffer format(double number, StringBuffer toAppendTo,
821                 FieldPosition pos) {
822 
823             return new StringBuffer();
824         }
825 
parse(String source, ParsePosition parsePosition)826         public Number parse(String source, ParsePosition parsePosition) {
827 
828             return Double.valueOf(0);
829         }
830 
format(long number, StringBuffer toAppendTo, FieldPosition pos)831         public StringBuffer format(long number, StringBuffer toAppendTo,
832                 FieldPosition pos) {
833             return new StringBuffer();
834         }
835 
836     }
837 }
838