OSDN Git Service

c047d3e3c231f03d734dd92a932ca9b153850215
[android-x86/dalvik.git] / libcore / text / src / test / java / org / apache / harmony / text / tests / java / text / NumberFormatTest.java
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 org.apache.harmony.text.tests.java.text;
18
19 import dalvik.annotation.KnownFailure;
20 import dalvik.annotation.TestTargets;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetNew;
23 import dalvik.annotation.TestTargetClass;
24
25 import junit.framework.TestCase;
26
27 import java.text.ChoiceFormat;
28 import java.text.DecimalFormat;
29 import java.text.FieldPosition;
30 import java.text.NumberFormat;
31 import java.text.ParseException;
32 import java.text.ParsePosition;
33 import java.util.Currency;
34 import java.util.Locale;
35
36 @TestTargetClass(NumberFormat.class) 
37 public class NumberFormatTest extends TestCase {
38
39     /**
40      * @tests java.text.NumberFormat#format(java.lang.Object,
41      *        java.lang.StringBuffer, java.text.FieldPosition)
42      */
43     @TestTargetNew(
44         level = TestLevel.COMPLETE,
45         notes = "",
46         method = "format",
47         args = {java.lang.Object.class, java.lang.StringBuffer.class, java.text.FieldPosition.class}
48     )
49     public void test_formatLjava_lang_ObjectLjava_lang_StringBufferLjava_text_FieldPosition() {
50         FieldPosition pos;
51         StringBuffer out;
52         DecimalFormat format = (DecimalFormat) NumberFormat
53                 .getInstance(Locale.US);
54
55         pos = new FieldPosition(0);
56         out = format.format(new Long(Long.MAX_VALUE), new StringBuffer(), pos);
57         assertEquals("Wrong result L1: " + out, "9,223,372,036,854,775,807",
58                 out.toString());
59
60         pos = new FieldPosition(0);
61         out = format.format(new Long(Long.MIN_VALUE), new StringBuffer(), pos);
62         assertEquals("Wrong result L2: " + out, "-9,223,372,036,854,775,808",
63                 out.toString());
64
65         pos = new FieldPosition(0);
66         out = format.format(new java.math.BigInteger(String
67                 .valueOf(Long.MAX_VALUE)), new StringBuffer(), pos);
68         assertEquals("Wrong result BI1: " + out, "9,223,372,036,854,775,807",
69                 out.toString());
70
71         pos = new FieldPosition(0);
72         out = format.format(new java.math.BigInteger(String
73                 .valueOf(Long.MIN_VALUE)), new StringBuffer(), pos);
74         assertEquals("Wrong result BI2: " + out, "-9,223,372,036,854,775,808",
75                 out.toString());
76
77         java.math.BigInteger big;
78         pos = new FieldPosition(0);
79         big = new java.math.BigInteger(String.valueOf(Long.MAX_VALUE))
80                 .add(new java.math.BigInteger("1"));
81         out = format.format(big, new StringBuffer(), pos);
82         assertEquals("Wrong result BI3: " + out, "9,223,372,036,854,775,808",
83                 out.toString());
84
85         pos = new FieldPosition(0);
86         big = new java.math.BigInteger(String.valueOf(Long.MIN_VALUE))
87                 .add(new java.math.BigInteger("-1"));
88         out = format.format(big, new StringBuffer(), pos);
89         assertEquals("Wrong result BI4: " + out, "-9,223,372,036,854,775,809",
90                 out.toString());
91
92         pos = new FieldPosition(0);
93         out = format.format(new java.math.BigDecimal("51.348"),
94                 new StringBuffer(), pos);
95         assertEquals("Wrong result BD1: " + out, "51.348", out.toString());
96
97         pos = new FieldPosition(0);
98         out = format.format(new java.math.BigDecimal("51"), new StringBuffer(),
99                 pos);
100         assertEquals("Wrong result BD2: " + out, "51", out.toString());
101
102     }
103
104     /**
105      * @tests java.text.NumberFormat#getIntegerInstance()
106      */
107     @TestTargetNew(
108         level = TestLevel.COMPLETE,
109         notes = "",
110         method = "getIntegerInstance",
111         args = {}
112     )
113     public void test_getIntegerInstance() throws ParseException {
114         // Test for method java.text.NumberFormat getIntegerInstance()
115         Locale origLocale = Locale.getDefault();
116         Locale.setDefault(Locale.US);
117
118         DecimalFormat format = (DecimalFormat) NumberFormat
119                 .getIntegerInstance();
120
121         assertEquals(
122                 "Test1: NumberFormat.getIntegerInstance().toPattern() returned wrong pattern",
123                 "#,##0", format.toPattern());
124         assertEquals(
125                 "Test2: NumberFormat.getIntegerInstance().format(35.76) returned wrong value",
126                 "36", format.format(35.76));
127         assertEquals(
128                 "Test3: NumberFormat.getIntegerInstance().parse(\"35.76\") returned wrong number",
129                 new Long(35), format.parse("35.76"));
130         assertEquals(
131                 "Test4: NumberFormat.getIntegerInstance().parseObject(\"35.76\") returned wrong number",
132                 new Long(35), format.parseObject("35.76"));
133         Locale.setDefault(origLocale);
134     }
135
136     /**
137      * @tests java.text.NumberFormat#getIntegerInstance(java.util.Locale)
138      */
139     @TestTargetNew(
140         level = TestLevel.COMPLETE,
141         notes = "",
142         method = "getIntegerInstance",
143         args = {java.util.Locale.class}
144     )
145     @KnownFailure("Some locales were removed last minute in cupcake")
146     public void test_getIntegerInstanceLjava_util_Locale()
147             throws ParseException {
148         // Test for method java.text.NumberFormat
149         // getIntegerInstance(java.util.Locale)
150         Locale usLocale = Locale.US;
151         // BEGIN android-changed
152         // use de_CH instead
153         // Locale arLocale = new Locale("ar", "AE");
154         Locale chLocale = new Locale("de", "CH");
155         // END android-changed
156
157         DecimalFormat format = (DecimalFormat) NumberFormat
158                 .getIntegerInstance(usLocale);
159         assertEquals(
160                 "Test1: NumberFormat.getIntegerInstance().toPattern() returned wrong pattern",
161                 "#,##0", format.toPattern());
162         assertEquals(
163                 "Test2: NumberFormat.getIntegerInstance().format(-35.76) returned wrong value",
164                 "-36", format.format(-35.76));
165         assertEquals(
166                 "Test3: NumberFormat.getIntegerInstance().parse(\"-36\") returned wrong number",
167                 new Long(-36), format.parse("-36"));
168         assertEquals(
169                 "Test4: NumberFormat.getIntegerInstance().parseObject(\"-36\") returned wrong number",
170                 new Long(-36), format.parseObject("-36"));
171         assertEquals(
172                 "Test5: NumberFormat.getIntegerInstance().getMaximumFractionDigits() returned wrong value",
173                 0, format.getMaximumFractionDigits());
174         assertTrue(
175                 "Test6: NumberFormat.getIntegerInstance().isParseIntegerOnly() returned wrong value",
176                 format.isParseIntegerOnly());
177
178         // try with a locale that has a different integer pattern
179         // BEGIN android-changed
180         // use de_CH instead
181         // format = (DecimalFormat) NumberFormat.getIntegerInstance(arLocale);
182         format = (DecimalFormat) NumberFormat.getIntegerInstance(chLocale);
183         assertEquals(
184                 "Test7: NumberFormat.getIntegerInstance(new Locale(\"de\", \"CH\")).toPattern() returned wrong pattern",
185                 "#,##0", format.toPattern());
186         assertEquals(
187                 "Test8: NumberFormat.getIntegerInstance(new Locale(\"de\", \"CH\")).format(-35.76) returned wrong value",
188                 "-36", format.format(-35.76));
189         assertEquals(
190                 "Test9: NumberFormat.getIntegerInstance(new Locale(\"de\", \"CH\")).parse(\"-36-\") returned wrong number",
191                 new Long(-36), format.parse("-36"));
192         assertEquals(
193                 "Test10: NumberFormat.getIntegerInstance(new Locale(\"de\", \"CH\")).parseObject(\"36-\") returned wrong number",
194                 new Long(-36), format.parseObject("-36"));
195
196         assertEquals(
197                 "Test11: NumberFormat.getIntegerInstance(new Locale(\"de\", \"CH\")).getMaximumFractionDigits() returned wrong value",
198                 0, format.getMaximumFractionDigits());
199         assertTrue(
200                 "Test12: NumberFormat.getIntegerInstance(new Locale(\"de\", \"CH\")).isParseIntegerOnly() returned wrong value",
201                 format.isParseIntegerOnly());
202         // use de_CH instead
203         /*assertEquals(
204                 "Test7: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).toPattern() returned wrong pattern",
205                 "#,##0;#,##0-", format.toPattern());
206         assertEquals(
207                 "Test8: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).format(-35.76) returned wrong value",
208                 "36-", format.format(-35.76));
209         assertEquals(
210                 "Test9: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).parse(\"-36-\") returned wrong number",
211                 new Long(-36), format.parse("36-"));
212         assertEquals(
213                 "Test10: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).parseObject(\"36-\") returned wrong number",
214                 new Long(-36), format.parseObject("36-"));
215
216         assertEquals(
217                 "Test11: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).getMaximumFractionDigits() returned wrong value",
218                 0, format.getMaximumFractionDigits());
219         assertTrue(
220                 "Test12: NumberFormat.getIntegerInstance(new Locale(\"ar\", \"AE\")).isParseIntegerOnly() returned wrong value",
221                 format.isParseIntegerOnly());*/
222         // END android-changed
223     }
224
225     /**
226      * @tests java.text.NumberFormat#getCurrency()
227      */
228     @TestTargetNew(
229         level = TestLevel.COMPLETE,
230         notes = "",
231         method = "getCurrency",
232         args = {}
233     )
234     public void test_getCurrency() {
235         // Test for method java.util.Currency getCurrency()
236
237         // a subclass that supports currency formatting
238         Currency currH = Currency.getInstance("HUF");
239         NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU"));
240         assertSame("Returned incorrect currency", currH, format.getCurrency());
241
242         // a subclass that doesn't support currency formatting
243         ChoiceFormat cformat = new ChoiceFormat(
244                 "0#Less than one|1#one|1<Between one and two|2<Greater than two");
245         try {
246             ((NumberFormat) cformat).getCurrency();
247             fail("Expected UnsupportedOperationException");
248         } catch (UnsupportedOperationException e) {
249         }
250     }
251
252     /**
253      * @tests java.text.NumberFormat#setMaximumIntegerDigits()
254      */
255     @TestTargetNew(
256         level = TestLevel.COMPLETE,
257         notes = "",
258         method = "setMaximumIntegerDigits",
259         args = {int.class}
260     )
261     public void test_setMaximumIntegerDigits() {
262         NumberFormat format = NumberFormat.getInstance();
263         format.setMaximumIntegerDigits(2);
264         assertEquals("Wrong result: case 1", "23", format.format(123));
265
266         format.setMaximumIntegerDigits(Integer.MIN_VALUE);
267         assertEquals("Wrong result: case 2", "0", format.format(123));
268     }
269
270     /**
271      * @tests java.text.NumberFormat#setCurrency(java.util.Currency)
272      */
273     @TestTargetNew(
274         level = TestLevel.COMPLETE,
275         notes = "",
276         method = "setCurrency",
277         args = {java.util.Currency.class}
278     )
279     public void test_setCurrencyLjava_util_Currency() {
280         // Test for method void setCurrency(java.util.Currency)
281         // a subclass that supports currency formatting
282         Currency currA = Currency.getInstance("ARS");
283         NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU"));
284         format.setCurrency(currA);
285         assertSame("Returned incorrect currency", currA, format.getCurrency());
286
287         // a subclass that doesn't support currency formatting
288         ChoiceFormat cformat = new ChoiceFormat(
289                 "0#Less than one|1#one|1<Between one and two|2<Greater than two");
290         try {
291             ((NumberFormat) cformat).setCurrency(currA);
292             fail("Expected UnsupportedOperationException");
293         } catch (UnsupportedOperationException e) {
294         }
295         
296         try {
297             NumberFormat.getInstance().setCurrency(null);
298             fail("NullPointerException was thrown.");
299         } catch(NullPointerException npe) {
300             //expected   
301         }
302         
303         try {
304             NumberFormat.getIntegerInstance().setCurrency(null);
305             fail("NullPointerException was thrown.");
306         } catch(NullPointerException npe) {
307             //expected   
308         }        
309     }
310
311     /**
312      * @tests java.text.NumberFormat#parseObject(java.lang.String,
313      *        java.text.ParsePosition)
314      */
315     @TestTargetNew(
316         level = TestLevel.COMPLETE,
317         notes = "",
318         method = "parseObject",
319         args = {java.lang.String.class, java.text.ParsePosition.class}
320     )
321     @KnownFailure("Some locales were removed last minute in cupcake")
322     public void test_parseObjectLjava_lang_StringLjava_text_ParsePosition() {
323         // regression test for HARMONY-1003
324         assertNull(NumberFormat.getInstance().parseObject("0",
325                 new ParsePosition(-1)));
326
327         parseObjectTest(NumberFormat.getInstance(), "123.123", 
328                 new ParsePosition(1), new Double(23.123), 7, true);
329         
330         parseObjectTest(NumberFormat.getInstance(), "123.123abc123", 
331                 new ParsePosition(3), new Double(0.123), 7, true);        
332         
333         parseObjectTest(NumberFormat.getInstance(Locale.FRANCE), 
334                 "asd123,123abc123", 
335                 new ParsePosition(3), new Double(123.123), 10, true); 
336         
337         parseObjectTest(NumberFormat.getInstance(Locale.FRANCE), 
338                 "test test", 
339                 new ParsePosition(0), null, 0, false); 
340         
341         parseObjectTest(NumberFormat.getIntegerInstance(), 
342                 "asd123.123abc123", 
343                 new ParsePosition(3), new Long(123), 6, true);
344         
345         parseObjectTest(NumberFormat.getNumberInstance(), 
346                 "$-123,123.123#", 
347                 new ParsePosition(1), new Double(-123123.123), 13, true);
348         parseObjectTest(NumberFormat.getNumberInstance(), 
349                 "$-123,123.123#", 
350                 new ParsePosition(0), null, 0, false);      
351         parseObjectTest(NumberFormat.getNumberInstance(), 
352                 "$-123,123.123#", 
353                 new ParsePosition(13), null, 13, false); 
354         parseObjectTest(NumberFormat.getPercentInstance(), 
355                 "%20.123#", 
356                 new ParsePosition(0), new Double(20.123), 0, false);        
357         parseObjectTest(NumberFormat.getPercentInstance(), 
358                 "%-200,123.123#", 
359                 new ParsePosition(0), null, 0, false);        
360         
361         
362         // Regression for HARMONY-1685
363         try {
364             NumberFormat.getInstance().parseObject("test", null);
365             fail("NullPointerException expected");
366         } catch (NullPointerException e) {
367             // expected
368         }
369     }
370     
371     void parseObjectTest(NumberFormat nf, String sourseStr, ParsePosition position, 
372             Object resultObj, int outIndex, boolean isSuccess) {
373         int indexBefore = position.getIndex();
374         Object result = nf.parseObject(sourseStr, position);
375         if(isSuccess) {
376             assertEquals(resultObj, result);
377             assertEquals(outIndex, position.getIndex());
378         } else {
379             assertNull(result);
380             assertEquals(indexBefore, position.getIndex());
381             assertEquals(outIndex, position.getErrorIndex());
382         }
383     }
384
385     /**
386      * @tests java.text.NumberFormat#clone()
387      */
388     @TestTargetNew(
389         level = TestLevel.COMPLETE,
390         notes = "",
391         method = "clone",
392         args = {}
393     )
394     public void test_clone() {
395
396         int max_digits = 100;
397         NumberFormat nf1 = NumberFormat.getInstance();
398         nf1.setMaximumIntegerDigits(max_digits);
399
400         NumberFormat nf2 = (NumberFormat) nf1.clone();
401         NumberFormat nf3 = (NumberFormat) nf1.clone();
402
403         assertTrue("Clonned object is not equal to object", nf2.equals(nf1));
404         assertTrue("Two clonned objects are not equal", nf2.equals(nf3));
405
406         assertTrue("Max digits value is incorrect for clonned object", nf2
407                 .getMaximumIntegerDigits() == max_digits);
408
409         nf1.setMaximumIntegerDigits(10);
410         assertTrue(
411                 "Max digits value is incorrect for clonned object after changing this value for object",
412                 nf2.getMaximumIntegerDigits() == max_digits);
413     }
414
415     /**
416      * @tests java.text.NumberFormat#equals(Object)
417      */
418     @TestTargetNew(
419         level = TestLevel.COMPLETE,
420         notes = "",
421         method = "equals",
422         args = {java.lang.Object.class}
423     )
424     public void test_equals() {
425
426         NumberFormat nf1 = NumberFormat.getInstance();
427         NumberFormat nf2 = NumberFormat.getInstance();
428
429         assertTrue("Objects are not equal", nf1.equals(nf2));
430         assertTrue("THe same Objects are not equal", nf1.equals(nf1));
431
432         nf2.setMaximumIntegerDigits(100);
433         assertFalse("Different NumberFormat are equal", nf1.equals(nf2));
434
435         nf2.setMaximumIntegerDigits(nf1.getMaximumIntegerDigits());
436         assertTrue("THe same Objects are not equal", nf1.equals(nf2));
437
438         nf1 = NumberFormat.getIntegerInstance();
439         nf2 = NumberFormat.getIntegerInstance(Locale.CHINA);
440         assertFalse("Different NumberFormat are equal", nf1.equals(nf2));
441
442         assertFalse("Object is equal null", nf1.equals(null));
443     }
444
445     /**
446      * @tests java.text.NumberFormat#format(double)
447      */
448     @TestTargetNew(
449         level = TestLevel.COMPLETE,
450         notes = "",
451         method = "format",
452         args = {double.class}
453     )
454     @KnownFailure("Some locales were removed last minute in cupcake")
455     public void test_formatLdouble() {
456         // BEGIN android-changed
457         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
458         // use de_CH instead
459         // NumberFormat nf2 = NumberFormat.getInstance(new Locale("ar", "AR"));
460         NumberFormat nf2 = NumberFormat.getInstance(new Locale("de", "CH"));
461
462         String out = nf1.format(1234567890.0123456789);
463         assertEquals("Wrong result for double : " + out, "1,234,567,890.012",
464                 out.toString());
465
466         out = nf1.format(-1234567890.0123456789);
467         assertEquals("Wrong result for double : " + out, "-1,234,567,890.012",
468                 out.toString());
469
470         out = nf2.format(-1234567890.0123456789);
471         // use de_CH instead
472         // assertEquals("Wrong result for double : " + out, "1,234,567,890.012-",
473         //         out.toString());
474         assertEquals("Wrong result for double : " + out, "-1'234'567'890.012",
475                 out.toString());
476
477         out = nf1.format(1.0001);
478         assertEquals("Wrong result for for double: " + out, "1", out.toString());
479
480         out = nf1.format(5.0);
481         assertEquals("Wrong result for for double: " + out, "5", out.toString());
482         // END android-changed
483     }
484
485     /**
486      * @tests java.text.NumberFormat#format(long)
487      */
488     @TestTargetNew(
489         level = TestLevel.COMPLETE,
490         notes = "",
491         method = "format",
492         args = {long.class}
493     )
494     @KnownFailure("Some locales were removed last minute in cupcake")
495     public void test_formatLlong() {
496         // BEGIN android-changed
497         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
498         // use de_CH instead
499         // NumberFormat nf2 = NumberFormat.getInstance(Locale.CANADA_FRENCH);
500         NumberFormat nf2 = NumberFormat.getInstance(new Locale("de", "CH"));
501
502         String out = nf1.format(Long.MAX_VALUE);
503         assertEquals("Wrong result for double : " + out,
504                 "9,223,372,036,854,775,807", out.toString());
505
506         out = nf1.format(Long.MIN_VALUE);
507         assertEquals("Wrong result for double : " + out,
508                 "-9,223,372,036,854,775,808", out.toString());
509
510         out = nf2.format(-1234567890);
511         // use de_CH instead
512         // assertEquals("Wrong result for double : " + out, "-1 234 567 890", out
513         //         .toString());
514         assertEquals("Wrong result for double : " + out, "-1'234'567'890", out
515                 .toString());
516
517         // the Locale data of icu uses \uc2a0
518         out = nf1.format(1);
519         assertEquals("Wrong result for for double: " + out, "1", out.toString());
520
521         out = nf1.format(0);
522         assertEquals("Wrong result for for double: " + out, "0", out.toString());
523         // END android-changed
524     }
525
526     /**
527      * @tests java.text.NumberFormat#getAvailableLocales()
528      */
529     @TestTargetNew(
530         level = TestLevel.COMPLETE,
531         notes = "",
532         method = "getAvailableLocales",
533         args = {}
534     )
535     public void test_getAvailableLocales() {
536
537         Locale[] l = NumberFormat.getAvailableLocales();
538         assertFalse("returned Locale array is null", l == null);
539         assertTrue("returned Locale length <= 0", l.length > 0);
540         Locale[] resl = Locale.getAvailableLocales();
541         assertTrue("returned Locale arrays are different",
542                 l.length == resl.length);
543         boolean isUS = false;
544         for (int i = 0; i < resl.length; i++) {
545             assertEquals("elements " + i + " are not equal: ", resl[i], l[i]);
546             if (l[i].equals(Locale.US))
547                 isUS = true;
548         }
549         assertTrue("there is no Locale.US", isUS);
550     }
551
552     /**
553      * @tests java.text.NumberFormat#getCurrencyInstance()
554      */
555     @TestTargetNew(
556         level = TestLevel.COMPLETE,
557         notes = "",
558         method = "getCurrencyInstance",
559         args = {}
560     )
561     public void test_getCurrencyInstance() {
562
563         Locale.setDefault(Locale.US);
564         NumberFormat format = NumberFormat.getCurrencyInstance();
565
566         assertNotSame("Instance is null", null, format);
567         assertTrue("Object is not instance of NumberFormat",
568                 format instanceof NumberFormat);
569
570         assertEquals(
571                 "Test1: NumberFormat.getCurrencyInstance().format(35.76) returned wrong value",
572                 "$35.76", format.format(35.76));
573         assertEquals(
574                 "Test2: NumberFormat.getCurrencyInstance().format(123456.789) returned wrong value",
575                 "$123,456.79", format.format(123456.789));
576         assertEquals(
577                 "Test3: NumberFormat.getCurrencyInstance().format(0.1) returned wrong value",
578                 "$0.10", format.format(0.1));
579         assertEquals(
580                 "Test4: NumberFormat.getCurrencyInstance().format(0.999) returned wrong value",
581                 "$1.00", format.format(0.999));
582     }
583
584     /**
585      * @tests java.text.NumberFormat#getCurrencyInstance(java.util.Locale)
586      */
587     @TestTargetNew(
588         level = TestLevel.COMPLETE,
589         notes = "",
590         method = "getCurrencyInstance",
591         args = {java.util.Locale.class}
592     )
593     @KnownFailure("Some locales were removed last minute in cupcake")
594     public void test_getCurrencyInstanceLjava_util_Locale() {
595         // BEGIN android-changed
596         Locale usLocale = Locale.US;
597         // use de_AT instead
598         // Locale mkLocale = new Locale("mk", "MK");
599         Locale atLocale = new Locale("de", "AT");
600
601         NumberFormat format = NumberFormat.getCurrencyInstance(usLocale);
602
603         assertNotSame("Instance is null", null, format);
604         assertTrue("Object is not instance of NumberFormat",
605                 format instanceof NumberFormat);
606
607         assertEquals(
608                 "Test1: NumberFormat.getCurrencyInstance(Locale.US).format(35.76) returned wrong value",
609                 "$35.76", format.format(35.76));
610         assertEquals(
611                 "Test2: NumberFormat.getCurrencyInstance(Locale.US).format(123456.789) returned wrong value",
612                 "$123,456.79", format.format(123456.789));
613         assertEquals(
614                 "Test3: NumberFormat.getCurrencyInstance(Locale.US).format(0.1) returned wrong value",
615                 "$0.10", format.format(0.1));
616         assertEquals(
617                 "Test4: NumberFormat.getCurrencyInstance(Locale.US).format(0.999) returned wrong value",
618                 "$1.00", format.format(0.999));
619
620         // use de_AT instead
621         // format = NumberFormat.getCurrencyInstance(mkLocale);
622         format = NumberFormat.getCurrencyInstance(atLocale);
623         
624         assertEquals(
625                 "Test5: NumberFormat.getCurrencyInstance(new Locale(\"de\", \"AT\")).format(35.76) returned wrong value",
626                 "\u20ac 35,76", format.format(35.76));
627         assertEquals(
628                 "Test6: NumberFormat.getCurrencyInstance(new Locale(\"de\", \"AT\")).format(123456.789) returned wrong value",
629                 "\u20ac 123.456,79", format.format(123456.789));
630         assertEquals(
631                 "Test7: NumberFormat.getCurrencyInstance(new Locale(\"de\", \"AT\")).format(0.1) returned wrong value",
632                 "\u20ac 0,10", format.format(0.1));
633         assertEquals(
634                 "Test8: NumberFormat.getCurrencyInstance(new Locale(\"de\", \"AT\")).format(0.999) returned wrong value",
635                 "\u20ac 1,00", format.format(0.999));
636         // use de_AT instead
637         /*assertEquals(
638                 "Test5: NumberFormat.getCurrencyInstance(new Locale(\"mk\", \"MK\")).format(35.76) returned wrong value",
639                 "Den 35,76", format.format(35.76));
640         assertEquals(
641                 "Test6: NumberFormat.getCurrencyInstance(new Locale(\"mk\", \"MK\")).format(123456.789) returned wrong value",
642                 "Den 123.456,79", format.format(123456.789));
643         assertEquals(
644                 "Test7: NumberFormat.getCurrencyInstance(new Locale(\"mk\", \"MK\")).format(0.1) returned wrong value",
645                 "Den 0,1", format.format(0.1));
646         assertEquals(
647                 "Test8: NumberFormat.getCurrencyInstance(new Locale(\"mk\", \"MK\")).format(0.999) returned wrong value",
648                 "Den 1", format.format(0.999));*/
649         try {
650             NumberFormat.getCurrencyInstance(null);
651             fail("java.lang.NullPointerException is not thrown");
652         } catch (java.lang.NullPointerException npe) {
653             // expested
654         }
655         // END android-changed
656     }
657
658     /**
659      * @tests java.text.NumberFormat#getInstance()
660      */
661     @TestTargetNew(
662         level = TestLevel.COMPLETE,
663         notes = "",
664         method = "getInstance",
665         args = {}
666     )
667     public void test_getInstance() {
668         Locale.setDefault(Locale.US);
669         NumberFormat format = NumberFormat.getInstance();
670
671         assertNotSame("Instance is null", null, format);
672         assertTrue("Object is not instance of NumberFormat",
673                 format instanceof NumberFormat);
674
675         assertEquals(
676                 "Test1: NumberFormat.getInstance().format(1234567890.0987654321) returned wrong value",
677                 "1,234,567,890.099", format.format(1234567890.0987654321));
678         assertEquals(
679                 "Test2: ((DecimalFormat) NumberFormat.getInstance()).toPattern returned wrong value",
680                 "#,##0.###", ((DecimalFormat) format).toPattern());
681         assertEquals(
682                 "Test3: NumberFormat.getInstance().format(123456789) returned wrong value",
683                 "123,456,789", format.format(123456789));
684     }
685
686     /**
687      * @tests java.text.NumberFormat#getInstance(Locale)
688      */
689     @TestTargetNew(
690         level = TestLevel.COMPLETE,
691         notes = "",
692         method = "getInstance",
693         args = {java.util.Locale.class}
694     )
695     @KnownFailure("Some locales were removed last minute in cupcake")
696     public void test_getInstanceLjava_util_Locale() {
697         // BEGIN android-changed
698         Locale.setDefault(Locale.US);
699         // use de_CH instead
700         // NumberFormat format = NumberFormat.getInstance(new Locale("ar", "AR"));
701         NumberFormat format = NumberFormat.getInstance(new Locale("de", "CH"));
702
703         assertNotSame("Instance is null", null, format);
704         assertTrue("Object is not instance of NumberFormat",
705                 format instanceof NumberFormat);
706
707         assertEquals(
708                 "Test1: NumberFormat.getInstance().format(1234567890.0987654321) returned wrong value",
709                 "1'234'567'890.099", format.format(1234567890.0987654321));
710         assertEquals(
711                 "Test2: ((DecimalFormat) NumberFormat.getInstance()).toPattern returned wrong value",
712                 "#,##0.###", ((DecimalFormat) format).toPattern());
713         assertEquals(
714                 "Test3: NumberFormat.getInstance().format(123456789) returned wrong value",
715                 "123'456'789", format.format(123456789));
716         // use de_CH instead
717         /*assertEquals(
718                 "Test1: NumberFormat.getInstance().format(1234567890.0987654321) returned wrong value",
719                 "1,234,567,890.099", format.format(1234567890.0987654321));
720         assertEquals(
721                 "Test2: ((DecimalFormat) NumberFormat.getInstance()).toPattern returned wrong value",
722                 "#,##0.###;#,##0.###-", ((DecimalFormat) format).toPattern());
723         assertEquals(
724                 "Test3: NumberFormat.getInstance().format(123456789) returned wrong value",
725                 "123,456,789", format.format(123456789));*/
726         try {
727             NumberFormat.getInstance(null);
728             fail("java.lang.NullPointerException is not thrown");
729         } catch (java.lang.NullPointerException npe) {
730             // expested
731         }
732         // END android-changed
733     }
734
735     /**
736      * @tests java.text.NumberFormat#getNumberInstance()
737      */
738     @TestTargetNew(
739         level = TestLevel.COMPLETE,
740         notes = "",
741         method = "getNumberInstance",
742         args = {}
743     )
744     public void test_getNumberInstance() {
745         Locale.setDefault(Locale.US);
746         NumberFormat format = NumberFormat.getNumberInstance();
747
748         assertNotSame("Instance is null", null, format);
749         assertTrue("Object is not instance of NumberFormat",
750                 format instanceof NumberFormat);
751
752         assertEquals(
753                 "Test1: NumberFormat.getNumberInstance().format(1234567890.0987654321) returned wrong value",
754                 "1,234,567,890.099", format.format(1234567890.0987654321));
755         assertEquals(
756                 "Test2: ((DecimalFormat) NumberFormat.getNumberInstance()).toPattern returned wrong value",
757                 "#,##0.###", ((DecimalFormat) format).toPattern());
758         assertEquals(
759                 "Test3: NumberFormat.getNumberInstance().format(123456789) returned wrong value",
760                 "123,456,789", format.format(123456789));
761     }
762
763     /**
764      * @tests java.text.NumberFormat#getNumberInstance(Locale)
765      */
766     @TestTargetNew(
767         level = TestLevel.COMPLETE,
768         notes = "",
769         method = "getNumberInstance",
770         args = {java.util.Locale.class}
771     )
772     @KnownFailure("Some locales were removed last minute in cupcake")
773     public void test_getNumberInstanceLjava_util_Locale() {
774         // BEGIN android-changed
775         Locale.setDefault(Locale.US);
776         // use de_CH instead
777         NumberFormat format = NumberFormat.getNumberInstance(new Locale("de",
778                 "CH"));
779         // NumberFormat format = NumberFormat.getNumberInstance(new Locale("ar",
780         //         "AR"));
781
782         assertNotSame("Instance is null", null, format);
783         assertTrue("Object is not instance of NumberFormat",
784                 format instanceof NumberFormat);
785
786         assertEquals(
787                 "Test1: NumberFormat.getNumberInstance().format(-1234567890.0987654321) returned wrong value",
788                 "-1'234'567'890.099", format.format(-1234567890.0987654321));
789         assertEquals(
790                 "Test2: ((DecimalFormat) NumberFormat.getNumberInstance()).toPattern returned wrong value",
791                 "#,##0.###", ((DecimalFormat) format).toPattern());
792         assertEquals(
793                 "Test3: NumberFormat.getNumberInstance().format(123456789) returned wrong value",
794                 "123'456'789", format.format(123456789));
795         // use de_CH instead
796         /*assertEquals(
797                 "Test1: NumberFormat.getNumberInstance().format(-1234567890.0987654321) returned wrong value",
798                 "1,234,567,890.099-", format.format(-1234567890.0987654321));
799         assertEquals(
800                 "Test2: ((DecimalFormat) NumberFormat.getNumberInstance()).toPattern returned wrong value",
801                 "#,##0.###;#,##0.###-", ((DecimalFormat) format).toPattern());
802         assertEquals(
803                 "Test3: NumberFormat.getNumberInstance().format(123456789) returned wrong value",
804                 "123,456,789", format.format(123456789));*/
805         try {
806             NumberFormat.getInstance(null);
807             fail("java.lang.NullPointerException is not thrown");
808         } catch (java.lang.NullPointerException npe) {
809             // expested
810         }
811         // END android-changed
812     }
813
814     /**
815      * @tests java.text.NumberFormat#getPercentInstance()
816      */
817     @TestTargetNew(
818         level = TestLevel.COMPLETE,
819         notes = "",
820         method = "getPercentInstance",
821         args = {}
822     )
823     public void test_getPercentInstance() {
824         Locale.setDefault(Locale.US);
825         NumberFormat format = NumberFormat.getPercentInstance();
826
827         assertNotSame("Instance is null", null, format);
828         assertTrue("Object is not instance of NumberFormat",
829                 format instanceof NumberFormat);
830
831         assertEquals(
832                 "Test1: NumberFormat.getPercentInstance().format(1234567890.0987654321) returned wrong value",
833                 "123,456,789,010%", format.format(1234567890.0987654321));
834         assertEquals(
835                 "Test2: ((DecimalFormat) NumberFormat.getPercentInstance()).toPattern returned wrong value",
836                 "#,##0%", ((DecimalFormat) format).toPattern());
837         assertEquals(
838                 "Test3: NumberFormat.getPercentInstance().format(123456789) returned wrong value",
839                 "12,345,678,900%", format.format(123456789));
840     }
841
842     /**
843      * @tests java.text.NumberFormat#getPercentInstance(Locale)
844      */
845     @TestTargetNew(
846         level = TestLevel.COMPLETE,
847         notes = "",
848         method = "getPercentInstance",
849         args = {java.util.Locale.class}
850     )
851     @KnownFailure("Some locales were removed last minute in cupcake")
852     public void test_getPercentInstanceLjava_util_Locale() {
853         Locale.setDefault(Locale.US);
854         NumberFormat format = NumberFormat.getPercentInstance(new Locale("cs",
855                 "CZ"));
856
857         assertNotSame("Instance is null", null, format);
858         assertTrue("Object is not instance of NumberFormat",
859                 format instanceof NumberFormat);
860
861         assertEquals(
862                 "Test1: NumberFormat.getPercentInstance().format(1234567890.0987654321) returned wrong value",
863                 "123\u00a0456\u00a0789\u00a0010%", format.format(1234567890.0987654321));
864         assertEquals(
865                 "Test2: ((DecimalFormat) NumberFormat.getPercentInstance()).toPattern returned wrong value",
866                 "#,##0%", ((DecimalFormat) format).toPattern());
867         assertEquals(
868                 "Test3: NumberFormat.getPercentInstance().format(123456789) returned wrong value",
869                 "12\u00a0345\u00a0678\u00a0900%", format.format(123456789));
870         try {
871             NumberFormat.getInstance(null);
872             fail("java.lang.NullPointerException is not thrown");
873         } catch (java.lang.NullPointerException npe) {
874             // expested
875         }
876     }
877
878     /**
879      * @tests java.text.NumberFormat#getMaximumFractionDigits()
880      */
881     @TestTargetNew(
882         level = TestLevel.COMPLETE,
883         notes = "",
884         method = "getMaximumFractionDigits",
885         args = {}
886     )
887     public void test_getMaximumFractionDigits() {
888         NumberFormat nf1 = NumberFormat.getInstance();
889
890         nf1.setMaximumFractionDigits(Integer.MAX_VALUE);
891         int result = nf1.getMaximumFractionDigits();
892         assertTrue("getMaximumFractionDigits returns " + result
893                 + " instead of: " + Integer.MAX_VALUE,
894                 result == Integer.MAX_VALUE);
895
896         nf1.setMaximumFractionDigits(0);
897         result = nf1.getMaximumFractionDigits();
898         assertTrue("getMaximumFractionDigits returns " + result
899                 + " instead of 0", result == 0);
900
901         nf1.setMinimumFractionDigits(Integer.MAX_VALUE);
902         result = nf1.getMaximumFractionDigits();
903         assertTrue("getMaximumFractionDigits returns " + result
904                 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE);
905     }
906
907     /**
908      * @tests java.text.NumberFormat#getMinimumFractionDigits()
909      */
910     @TestTargetNew(
911         level = TestLevel.COMPLETE,
912         notes = "",
913         method = "getMinimumFractionDigits",
914         args = {}
915     )
916     public void test_getMinimumFractionDigits() {
917         NumberFormat nf1 = NumberFormat.getInstance();
918         nf1.setMinimumFractionDigits(Integer.MAX_VALUE);
919         int result = nf1.getMinimumFractionDigits();
920         assertTrue("getMinimumFractionDigits returns " + result
921                 + " instead of: " + Integer.MAX_VALUE,
922                 result == Integer.MAX_VALUE);
923
924         nf1.setMaximumFractionDigits(0);
925         result = nf1.getMinimumFractionDigits();
926         assertTrue("getMinimumFractionDigits returns " + result
927                 + " instead of 0", result == 0);
928
929         nf1.setMinimumFractionDigits(52);
930         result = nf1.getMinimumFractionDigits();
931         assertTrue("getMinimumFractionDigits returns " + result
932                 + " instead of 52", result == 52);
933     }
934
935     /**
936      * @tests java.text.NumberFormat#getMaximumIntegerDigits()
937      */
938     @TestTargetNew(
939         level = TestLevel.COMPLETE,
940         notes = "",
941         method = "getMaximumIntegerDigits",
942         args = {}
943     )
944     public void test_getMaximumIntegerDigits() {
945         NumberFormat nf1 = NumberFormat.getInstance();
946         nf1.setMaximumIntegerDigits(Integer.MAX_VALUE);
947         int result = nf1.getMaximumIntegerDigits();
948         assertTrue("getMaximumIntegerDigits returns " + result
949                 + " instead of: " + Integer.MAX_VALUE,
950                 result == Integer.MAX_VALUE);
951
952         nf1.setMaximumIntegerDigits(0);
953         result = nf1.getMaximumIntegerDigits();
954         assertTrue("getMaximumIntegerDigits returns " + result
955                 + " instead of 0", result == 0);
956
957         nf1.setMinimumIntegerDigits(Integer.MAX_VALUE);
958         result = nf1.getMaximumIntegerDigits();
959         assertTrue("getMaximumIntegerigits returns " + result
960                 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE);
961     }
962
963     /**
964      * @tests java.text.NumberFormat#getMinimumIntegerDigits()
965      */
966     @TestTargetNew(
967         level = TestLevel.COMPLETE,
968         notes = "",
969         method = "getMinimumIntegerDigits",
970         args = {}
971     )
972     public void test_getMinimumIntegernDigits() {
973         NumberFormat nf1 = NumberFormat.getInstance();
974         nf1.setMinimumIntegerDigits(Integer.MAX_VALUE);
975         int result = nf1.getMinimumIntegerDigits();
976         assertTrue("getMinimumIntegerDigits returns " + result
977                 + " instead of: " + Integer.MAX_VALUE,
978                 result == Integer.MAX_VALUE);
979
980         nf1.setMaximumIntegerDigits(0);
981         result = nf1.getMinimumIntegerDigits();
982         assertTrue("getMinimumIntegerDigits returns " + result
983                 + " instead of 0", result == 0);
984
985         nf1.setMinimumIntegerDigits(0x12034);
986         result = nf1.getMinimumIntegerDigits();
987         assertTrue("getMinimumIntegerDigits returns " + result
988                 + " instead of 5148", result == 73780);
989     }
990
991     /**
992      * @tests java.text.NumberFormat#hashCode()
993      */
994     @TestTargetNew(
995         level = TestLevel.COMPLETE,
996         notes = "",
997         method = "hashCode",
998         args = {}
999     )
1000     public void test_hashCode() {
1001
1002         NumberFormat nf1 = NumberFormat.getInstance();
1003         NumberFormat nf11 = NumberFormat.getInstance();
1004         NumberFormat nf2 = NumberFormat.getInstance(Locale.US);
1005         NumberFormat nf3 = NumberFormat.getPercentInstance();
1006         NumberFormat nf4 = NumberFormat.getCurrencyInstance();
1007         NumberFormat nf5 = NumberFormat
1008                 .getNumberInstance(new Locale("mk", "MK"));
1009         NumberFormat nf6 = NumberFormat.getInstance(Locale.US);
1010
1011         assertTrue("Hash codes are not equal: case 1", nf1.hashCode() == nf2
1012                 .hashCode());
1013         assertTrue("Hash codes are not equal: case 2", nf1.hashCode() == nf11
1014                 .hashCode());
1015         assertTrue("Hash codes are not equal: case 3", nf1.hashCode() == nf3
1016                 .hashCode());
1017         assertFalse("Hash codes are equal: case 4", nf3.hashCode() == nf4
1018                 .hashCode());
1019         assertFalse("Hash codes are equal: case 5", nf4.hashCode() == nf5
1020                 .hashCode());
1021         assertTrue("Hash codes are not equal: case 6", nf5.hashCode() == nf6
1022                 .hashCode());
1023
1024         nf1.setMaximumFractionDigits(0);
1025         assertTrue("Hash codes are not equal: case 7", nf1.hashCode() == nf11
1026                 .hashCode());
1027     }
1028
1029     /**
1030      * @tests java.text.NumberFormat#isGroupingUsed()
1031      */
1032     @TestTargetNew(
1033         level = TestLevel.COMPLETE,
1034         notes = "",
1035         method = "isGroupingUsed",
1036         args = {}
1037     )
1038     public void test_isGroupingUsed() {
1039         NumberFormat nf1 = NumberFormat.getInstance();
1040         assertTrue("grouping is not used for NumberFormat.getInstance", nf1
1041                 .isGroupingUsed());
1042
1043         nf1.setGroupingUsed(false);
1044         assertFalse(
1045                 "grouping is used for NumberFormat.getInstance after setting false",
1046                 nf1.isGroupingUsed());
1047
1048         nf1.setGroupingUsed(true);
1049         assertTrue(
1050                 "grouping is not used for NumberFormat.getInstance after setting true",
1051                 nf1.isGroupingUsed());
1052     }
1053
1054     /**
1055      * @tests java.text.NumberFormat#setGroupingUsed(boolean)
1056      */
1057     @TestTargetNew(
1058         level = TestLevel.COMPLETE,
1059         notes = "",
1060         method = "setGroupingUsed",
1061         args = {boolean.class}
1062     )
1063     @KnownFailure("Some locales were removed last minute in cupcake")
1064     public void test_setGroupingUsed() {
1065         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
1066         nf1.setGroupingUsed(false);
1067
1068         assertEquals("grouping is used for 1234567890.1", "1234567890.1",
1069                 nf1.format(1234567890.1));
1070
1071         assertEquals("grouping is used for -1234567890.1", "-1234567890.1",
1072                 nf1.format(-1234567890.1));
1073
1074         nf1.setGroupingUsed(false);
1075
1076         assertEquals("grouping is used for 1234567890.1", "1234567890.1",
1077                 nf1.format(1234567890.1));
1078
1079         assertEquals("grouping is used for -1234567890.1", "-1234567890.1",
1080                 nf1.format(-1234567890.1));
1081
1082         nf1.setGroupingUsed(true);
1083
1084         assertEquals("grouping is not used for 1234567890.1",
1085                 "1,234,567,890.1", nf1.format(1234567890.1));
1086
1087         assertEquals("grouping is not used for -1234567890.1",
1088                 "-1,234,567,890.1", nf1.format(-1234567890.1));
1089
1090         NumberFormat nf2 = NumberFormat.getPercentInstance(new Locale("cs",
1091                 "CZ"));
1092         nf2.setGroupingUsed(false);
1093         assertEquals(
1094                 "Locale(\"cs\", \"CZ\"): grouping is used for 1234567890.1",
1095                 "123456789010%", nf2.format(1234567890.1));
1096
1097         assertEquals(
1098                 "Locale(\"cs\", \"CZ\"): grouping is used for -1234567890.1",
1099                 "-123456789010%", nf2.format(-1234567890.1));
1100         assertEquals("grouping is not used for 1234567890.1",
1101                 "1,234,567,890.1", nf1.format(1234567890.1));
1102
1103         nf2.setGroupingUsed(true);
1104         assertEquals(
1105                 "Locale(\"cs\", \"CZ\"): grouping is not used for 1234567890.1",
1106                 "123\u00a0456\u00a0789\u00a0010%", nf2.format(1234567890.1));
1107
1108         assertEquals(
1109                 "Locale(\"cs\", \"CZ\"): grouping is not used for -1234567890.1",
1110                 "-123\u00a0456\u00a0789\u00a0010%", nf2.format(-1234567890.1));
1111
1112         nf2.setGroupingUsed(true);
1113         assertEquals(
1114                 "Locale(\"cs\", \"CZ\"): grouping is not used for 1234567890.1",
1115                 "123\u00a0456\u00a0789\u00a0010%", nf2.format(1234567890.1));
1116
1117         assertEquals(
1118                 "Locale(\"cs\", \"CZ\"): grouping is not used for -1234567890.1",
1119                 "-123\u00a0456\u00a0789\u00a0010%", nf2.format(-1234567890.1));
1120     }
1121
1122     /**
1123      * @tests java.text.NumberFormat#isParseIntegerOnly()
1124      */
1125     @TestTargetNew(
1126         level = TestLevel.COMPLETE,
1127         notes = "",
1128         method = "isParseIntegerOnly",
1129         args = {}
1130     )
1131     public void test_isParseIntegerOnly() {
1132         NumberFormat nf1 = NumberFormat.getInstance();
1133         assertTrue("ParseIntegerOnly is not used for NumberFormat.getInstance",
1134                 nf1.isGroupingUsed());
1135
1136         nf1.setParseIntegerOnly(false);
1137         assertFalse(
1138                 "ParseIntegerOnly is used for NumberFormat.getInstance after setting false",
1139                 nf1.isParseIntegerOnly());
1140
1141         nf1.setParseIntegerOnly(true);
1142         assertTrue(
1143                 "ParseIntegerOnly is not used for NumberFormat.getInstance after setting true",
1144                 nf1.isParseIntegerOnly());
1145     }
1146
1147     /**
1148      * @tests java.text.NumberFormat#setParseIntegerOnly(boolean)
1149      */
1150     @TestTargetNew(
1151         level = TestLevel.COMPLETE,
1152         notes = "",
1153         method = "setParseIntegerOnly",
1154         args = {boolean.class}
1155     )
1156     public void test_setParseIntegerOnly() {
1157         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
1158         nf1.setParseIntegerOnly(true);
1159
1160         assertEquals("ParseIntegerOnly is not used for 1234567890.1",
1161                 "1,234,567,890.1", nf1.format(1234567890.1));
1162         assertEquals("ParseIntegerOnly is not used for -1234567890.1",
1163                 "-1,234,567,890.1", nf1.format(-1234567890.1));
1164         assertEquals("ParseIntegerOnly is not used for -1234567890.",
1165                 "-1,234,567,890", nf1.format(-1234567890.));
1166
1167         nf1.setParseIntegerOnly(false);
1168
1169         assertEquals("ParseIntegerOnly is not used for 1234567890.1",
1170                 "1,234,567,890.1", nf1.format(1234567890.1));
1171         assertEquals("ParseIntegerOnly is not used for -1234567890.1",
1172                 "-1,234,567,890.1", nf1.format(-1234567890.1));
1173         assertEquals("ParseIntegerOnly is not used for -1234567890.",
1174                 "-1,234,567,890", nf1.format(-1234567890.));
1175     }
1176
1177     /**
1178      * @tests java.text.NumberFormat#setMaximumFractionDigits(int)
1179      */
1180     @TestTargetNew(
1181         level = TestLevel.COMPLETE,
1182         notes = "",
1183         method = "setMaximumFractionDigits",
1184         args = {int.class}
1185     )
1186     public void test_setMaximumFractionDigits() {
1187         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
1188         nf1.setMaximumFractionDigits(Integer.MAX_VALUE);
1189         int result = nf1.getMaximumFractionDigits();
1190         assertTrue("setMaximumFractionDigits set " + result
1191                 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE);
1192         nf1.setMaximumFractionDigits(0);
1193         result = nf1.getMaximumFractionDigits();
1194         assertTrue("setMaximumFractionDigits set " + result + " instead of 0",
1195                 result == 0);
1196         assertEquals("format of 1234567890.0987654321 returns incorrect value",
1197                 "1,234,567,890", nf1.format(1234567890.0987654321));
1198         nf1.setMaximumFractionDigits(5);
1199         result = nf1.getMaximumFractionDigits();
1200         assertTrue("setMaximumFractionDigits set " + result + " instead of 5",
1201                 result == 5);
1202         assertEquals(
1203                 "format of 1234567890.0987654321 returns incorrect value with MaximumFractionDigits = 5",
1204                 "1,234,567,890.09877", nf1.format(1234567890.0987654321));
1205         assertEquals(
1206                 "format of -1234567890 returns incorrect value with MaximumFractionDigits = 5",
1207                 "-1,234,567,890", nf1.format(-1234567890));
1208         nf1.setMaximumFractionDigits(Integer.MIN_VALUE);
1209         result = nf1.getMaximumFractionDigits();
1210         assertTrue("setMaximumFractionDigits set " + result
1211                 + " instead of Integer.MIN_VALUE", result == 0);
1212         assertEquals(
1213                 "format of 1234567890.0987654321 returns incorrect value with MaximumFractionDigits = 5",
1214                 "1,234,567,890", nf1.format(1234567890.0987654321));
1215     }
1216
1217     /**
1218      * @tests java.text.NumberFormat#setMinimumFractionDigits(int)
1219      */
1220     @TestTargetNew(
1221         level = TestLevel.COMPLETE,
1222         notes = "",
1223         method = "setMinimumFractionDigits",
1224         args = {int.class}
1225     )
1226     public void test_setMinimumFractionDigits() {
1227
1228         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
1229         nf1.setMinimumFractionDigits(Integer.MAX_VALUE);
1230         int result = nf1.getMinimumFractionDigits();
1231         assertTrue("setMinimumFractionDigits set " + result
1232                 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE);
1233         nf1.setMinimumFractionDigits(0);
1234         result = nf1.getMinimumFractionDigits();
1235         assertTrue("setMinimumFractionDigits set " + result + " instead of 0",
1236                 result == 0);
1237         nf1.setMinimumFractionDigits(5);
1238         result = nf1.getMinimumFractionDigits();
1239         assertTrue("setMinimumFractionDigits set " + result + " instead of 5",
1240                 result == 5);
1241         assertEquals(
1242                 "format of 1234567890.0987654321 returns incorrect value with MinimumFractionDigits = 5",
1243                 "1,234,567,890.09000", nf1.format(1234567890.09));
1244         assertEquals(
1245                 "format of -1234567890 returns incorrect value with MinimumFractionDigits = 5",
1246                 "-1,234,567,890.00000", nf1.format(-1234567890));
1247         nf1.setMinimumFractionDigits(Integer.MIN_VALUE);
1248         result = nf1.getMinimumFractionDigits();
1249         assertTrue("setMinimumFractionDigits set " + result
1250                 + " instead of Integer.MIN_VALUE", result == 0);
1251         assertEquals(
1252                 "format of 1234567890.098 returns incorrect value with MinimumFractionDigits = 5",
1253                 "1,234,567,890.098", nf1.format(1234567890.098));
1254     }
1255
1256     /**
1257      * @tests java.text.NumberFormat#setMinimumIntegerDigits(int)
1258      */
1259     @TestTargetNew(
1260         level = TestLevel.COMPLETE,
1261         notes = "",
1262         method = "setMinimumIntegerDigits",
1263         args = {int.class}
1264     )
1265     public void test_setMinimumIntegerDigits() {
1266
1267         NumberFormat nf1 = NumberFormat.getInstance(Locale.US);
1268         nf1.setMinimumIntegerDigits(Integer.MAX_VALUE);
1269         int result = nf1.getMinimumIntegerDigits();
1270         assertTrue("setMinimumIntegerDigits set " + result
1271                 + " instead of Integer.MAX_VALUE", result == Integer.MAX_VALUE);
1272         nf1.setMinimumIntegerDigits(0);
1273         result = nf1.getMinimumIntegerDigits();
1274         assertTrue("setMinimumIntegerDigits set " + result + " instead of 0",
1275                 result == 0);
1276         nf1.setMinimumIntegerDigits(5);
1277         result = nf1.getMinimumIntegerDigits();
1278         assertTrue("setMinimumIntegerDigits set " + result + " instead of 5",
1279                 result == 5);
1280         assertEquals(
1281                 "format of 123.09 returns incorrect value with MinimumIntegerDigits = 5",
1282                 "00,123.09", nf1.format(123.09));
1283         assertEquals(
1284                 "format of -123 returns incorrect value with MinimumIntegerDigits = 5",
1285                 "-00,123", nf1.format(-123));
1286         nf1.setMinimumIntegerDigits(Integer.MIN_VALUE);
1287         result = nf1.getMinimumIntegerDigits();
1288         assertTrue("setMinimumIntegerDigits set " + result
1289                 + " instead of Integer.MIN_VALUE", result == 0);
1290     }
1291
1292     /**
1293      * @tests java.text.NumberFormat#parse(String)
1294      */
1295     @TestTargetNew(
1296         level = TestLevel.COMPLETE,
1297         notes = "",
1298         method = "parse",
1299         args = {java.lang.String.class}
1300     )
1301     public void test_parseLjava_lang_String() {
1302         NumberFormat nf1 = NumberFormat.getInstance();
1303         try {
1304             assertEquals(
1305                     "Test1: NumberFormat.getInstance().parse(\"1234567890.1\") returned wrong number",
1306                     new Double(1234567890.1), nf1.parse("1234567890.1"));
1307         } catch (java.text.ParseException pe) {
1308             fail("java.text.ParseException is thrown for 1234567890.1");
1309         }
1310
1311         try {
1312             assertEquals(
1313                     "Test2: NumberFormat.getInstance().parse(\"-1234567890.1\") returned wrong number",
1314                     new Double(-1234567890.1), nf1.parse("-1,234,567,890.1"));
1315         } catch (java.text.ParseException pe) {
1316             fail("java.text.ParseException is thrown for -1,234,567,890.1");
1317         }
1318
1319         try {
1320             nf1.parse("@1,234,567,8901");
1321             fail("java.text.ParseException is not thrown for 1,234,567,890z1");
1322         } catch (java.text.ParseException pe) {
1323             // expected
1324         }
1325
1326         nf1 = NumberFormat.getPercentInstance();
1327         try {
1328             assertEquals(
1329                     "Test3: NumberFormat.getPercentInstance().parse(\"-123%\") returned wrong number",
1330                     new Double(-1.23), nf1.parse("-123%"));
1331         } catch (java.text.ParseException pe) {
1332             fail("java.text.ParseException is thrown for -123%");
1333         }
1334
1335         nf1 = NumberFormat.getCurrencyInstance();
1336         try {
1337             assertEquals(
1338                     "Test4: NumberFormat.getCurrencyInstance().parse(\"$123\") returned wrong number",
1339                     new Long(123), nf1.parse("$123"));
1340         } catch (java.text.ParseException pe) {
1341             fail("java.text.ParseException is thrown for $123");
1342         }
1343
1344         try {
1345             assertEquals(
1346                     "Test4: NumberFormat.getCurrencyInstance().parse(\"$123abc\") returned wrong number",
1347                     new Long(123), nf1.parse("$123abc"));
1348         } catch (java.text.ParseException pe) {
1349             fail("java.text.ParseException is thrown for $123");
1350         }
1351
1352         nf1 = NumberFormat.getIntegerInstance();
1353         try {
1354             assertEquals(
1355                     "Test5: NumberFormat.getIntegerInstance().parse(\"-123.123\") returned wrong number",
1356                     nf1.parseObject("-123.123"), nf1.parse("-123.123"));
1357         } catch (java.text.ParseException pe) {
1358             fail("java.text.ParseException is thrown for $123");
1359         }
1360     }
1361
1362     /**
1363      * @tests java.text.NumberFormat#NumberFormat()
1364      */
1365     @TestTargetNew(
1366         level = TestLevel.COMPLETE,
1367         notes = "",
1368         method = "NumberFormat",
1369         args = {}
1370     )
1371     public void test_constructor() {
1372         MyNumberFormat mf = new MyNumberFormat();
1373         assertFalse("Greated NumberFormat object is null", mf == null);
1374         assertTrue(
1375                 "Greated NumberFormat object is not instance of NumberFormat",
1376                 mf instanceof NumberFormat);
1377     }
1378
1379     class MyNumberFormat extends NumberFormat {
1380         static final long serialVersionUID = 1L;
1381
1382         public MyNumberFormat() {
1383             super();
1384         }
1385
1386         public StringBuffer format(double number, StringBuffer toAppendTo,
1387                 FieldPosition pos) {
1388
1389             return new StringBuffer();
1390         }
1391
1392         public Number parse(String source, ParsePosition parsePosition) {
1393
1394             return new Double(0);
1395         }
1396
1397         public StringBuffer format(long number, StringBuffer toAppendTo,
1398                 FieldPosition pos) {
1399             return new StringBuffer();
1400         }
1401
1402     }
1403 }