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