FormatterTest.java revision 8ba0b9bad4ee13bb30fa75c531921802c05611ac
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package tests.api.java.util;
17
18import tests.support.Support_Locale;
19
20import java.io.BufferedOutputStream;
21import java.io.Closeable;
22import java.io.File;
23import java.io.FileNotFoundException;
24import java.io.FileOutputStream;
25import java.io.FilePermission;
26import java.io.Flushable;
27import java.io.IOException;
28import java.io.OutputStream;
29import java.io.PipedOutputStream;
30import java.io.PrintStream;
31import java.io.UnsupportedEncodingException;
32import java.math.BigDecimal;
33import java.math.BigInteger;
34import java.nio.charset.Charset;
35import java.security.Permission;
36import java.util.Arrays;
37import java.util.Calendar;
38import java.util.Date;
39import java.util.DuplicateFormatFlagsException;
40import java.util.FormatFlagsConversionMismatchException;
41import java.util.Formattable;
42import java.util.FormattableFlags;
43import java.util.Formatter;
44import java.util.FormatterClosedException;
45import java.util.IllegalFormatCodePointException;
46import java.util.IllegalFormatConversionException;
47import java.util.IllegalFormatException;
48import java.util.IllegalFormatFlagsException;
49import java.util.IllegalFormatPrecisionException;
50import java.util.IllegalFormatWidthException;
51import java.util.Locale;
52import java.util.MissingFormatArgumentException;
53import java.util.MissingFormatWidthException;
54import java.util.TimeZone;
55import java.util.UnknownFormatConversionException;
56
57import junit.framework.TestCase;
58import dalvik.annotation.AndroidOnly;
59import dalvik.annotation.BrokenTest;
60import dalvik.annotation.KnownFailure;
61import dalvik.annotation.TestLevel;
62import dalvik.annotation.TestTargetClass;
63import dalvik.annotation.TestTargetNew;
64
65@TestTargetClass(Formatter.class)
66public class FormatterTest extends TestCase {
67
68    class MockAppendable implements Appendable {
69        public Appendable append(CharSequence arg0) throws IOException {
70            return null;
71        }
72
73        public Appendable append(char arg0) throws IOException {
74            return null;
75        }
76
77        public Appendable append(CharSequence arg0, int arg1, int arg2)
78                throws IOException {
79            return null;
80        }
81    }
82
83    class MockSecurityManager extends SecurityManager {
84        public void checkPermission(Permission p) {
85            if (p.getActions().equals("write") && p instanceof FilePermission) {
86                throw new SecurityException("Always throw security exception");
87            }
88        }
89
90        public void checkPermission(Permission p, Object ctx) {
91            checkPermission(p);
92        }
93    }
94
95    class MockFormattable implements Formattable {
96        public void formatTo(Formatter formatter, int flags, int width,
97                int precision) throws IllegalFormatException {
98            if ((flags & FormattableFlags.UPPERCASE) != 0) {
99                formatter.format("CUSTOMIZED FORMAT FUNCTION" + " WIDTH: "
100                        + width + " PRECISION: " + precision);
101            } else {
102                formatter.format("customized format function" + " width: "
103                        + width + " precision: " + precision);
104            }
105        }
106
107        public String toString() {
108            return "formattable object";
109        }
110
111        public int hashCode() {
112            return 0xf;
113        }
114    }
115
116    class MockDestination implements Appendable, Flushable {
117
118        private StringBuilder data = new StringBuilder();
119
120        private boolean enabled = false;
121
122        public Appendable append(char c) throws IOException {
123            if (enabled) {
124                data.append(c);
125                enabled = true; // enable it after the first append
126            } else {
127                throw new IOException();
128            }
129            return this;
130        }
131
132        public Appendable append(CharSequence csq) throws IOException {
133            if (enabled) {
134                data.append(csq);
135                enabled = true; // enable it after the first append
136            } else {
137                throw new IOException();
138            }
139            return this;
140        }
141
142        public Appendable append(CharSequence csq, int start, int end)
143                throws IOException {
144            if (enabled) {
145                data.append(csq, start, end);
146                enabled = true; // enable it after the first append
147            } else {
148                throw new IOException();
149            }
150            return this;
151        }
152
153        public void flush() throws IOException {
154            throw new IOException("Always throw IOException");
155        }
156
157        public String toString() {
158            return data.toString();
159        }
160    }
161
162    private File notExist;
163
164    private File fileWithContent;
165
166    private File readOnly;
167
168    private File secret;
169
170    private TimeZone defaultTimeZone;
171
172    /**
173     * @tests java.util.Formatter#Formatter()
174     */
175    @TestTargetNew(
176        level = TestLevel.COMPLETE,
177        notes = "",
178        method = "Formatter",
179        args = {}
180    )
181    public void test_Constructor() {
182        Formatter f = new Formatter();
183        assertNotNull(f);
184        assertTrue(f.out() instanceof StringBuilder);
185        assertEquals(f.locale(), Locale.getDefault());
186        assertNotNull(f.toString());
187    }
188
189    /**
190     * @tests java.util.Formatter#Formatter(Appendable)
191     */
192    @TestTargetNew(
193        level = TestLevel.COMPLETE,
194        notes = "",
195        method = "Formatter",
196        args = {java.lang.Appendable.class}
197    )
198    @AndroidOnly("the RI trows an exception that makes no sense. See comment.")
199    public void test_ConstructorLjava_lang_Appendable() {
200        MockAppendable ma = new MockAppendable();
201        Formatter f1 = new Formatter(ma);
202        assertEquals(ma, f1.out());
203        assertEquals(f1.locale(), Locale.getDefault());
204        assertNotNull(f1.toString());
205
206        Formatter f2 = new Formatter((Appendable) null);
207        /*
208         * If a(the input param) is null then a StringBuilder will be created
209         * and the output can be attained by invoking the out() method. But RI
210         * raises an error of FormatterClosedException when invoking out() or
211         * toString().
212         */
213        Appendable sb = f2.out();
214        assertTrue(sb instanceof StringBuilder);
215        assertNotNull(f2.toString());
216    }
217
218    /**
219     * @tests java.util.Formatter#Formatter(Locale)
220     */
221    @TestTargetNew(
222        level = TestLevel.COMPLETE,
223        notes = "",
224        method = "Formatter",
225        args = {java.util.Locale.class}
226    )
227    public void test_ConstructorLjava_util_Locale() {
228        Formatter f1 = new Formatter(Locale.FRANCE);
229        assertTrue(f1.out() instanceof StringBuilder);
230        assertEquals(f1.locale(), Locale.FRANCE);
231        assertNotNull(f1.toString());
232
233        Formatter f2 = new Formatter((Locale) null);
234        assertNull(f2.locale());
235        assertTrue(f2.out() instanceof StringBuilder);
236        assertNotNull(f2.toString());
237    }
238
239    /**
240     * @tests java.util.Formatter#Formatter(Appendable, Locale)
241     */
242    @TestTargetNew(
243        level = TestLevel.COMPLETE,
244        notes = "",
245        method = "Formatter",
246        args = {java.lang.Appendable.class, java.util.Locale.class}
247    )
248    public void test_ConstructorLjava_lang_AppendableLjava_util_Locale() {
249        MockAppendable ma = new MockAppendable();
250        Formatter f1 = new Formatter(ma, Locale.CANADA);
251        assertEquals(ma, f1.out());
252        assertEquals(f1.locale(), Locale.CANADA);
253
254        Formatter f2 = new Formatter(ma, null);
255        assertNull(f2.locale());
256        assertEquals(ma, f1.out());
257
258        Formatter f3 = new Formatter(null, Locale.GERMAN);
259        assertEquals(f3.locale(), Locale.GERMAN);
260        assertTrue(f3.out() instanceof StringBuilder);
261    }
262
263    /**
264     * @tests java.util.Formatter#Formatter(String)
265     */
266    @TestTargetNew(
267        level = TestLevel.COMPLETE,
268        notes = "",
269        method = "Formatter",
270        args = {java.lang.String.class}
271    )
272    public void test_ConstructorLjava_lang_String() throws IOException {
273        Formatter f = null;
274        try {
275            f = new Formatter((String) null);
276            fail("should throw NullPointerException");
277        } catch (NullPointerException e1) {
278            // expected
279        }
280
281        f = new Formatter(notExist.getPath());
282        assertEquals(f.locale(), Locale.getDefault());
283        f.close();
284
285        f = new Formatter(fileWithContent.getPath());
286        assertEquals(0, fileWithContent.length());
287        f.close();
288
289        try {
290            f = new Formatter(readOnly.getPath());
291            if (!("root".equals(System.getProperty("user.name")))) {
292                fail("should throw FileNotFoundException");
293            }
294        } catch (FileNotFoundException e) {
295            // expected
296        }
297
298        SecurityManager oldsm = System.getSecurityManager();
299        System.setSecurityManager(new MockSecurityManager());
300        try {
301            f = new Formatter(secret.getPath());
302            fail("should throw SecurityException");
303        } catch (SecurityException se) {
304            // expected
305        } finally {
306            System.setSecurityManager(oldsm);
307        }
308    }
309
310    /**
311     * @tests java.util.Formatter#Formatter(String, String)
312     */
313    @TestTargetNew(
314        level = TestLevel.COMPLETE,
315        notes = "",
316        method = "Formatter",
317        args = {java.lang.String.class, java.lang.String.class}
318    )
319    public void test_ConstructorLjava_lang_StringLjava_lang_String()
320            throws IOException {
321        Formatter f = null;
322        try {
323            f = new Formatter((String) null, Charset.defaultCharset().name());
324            fail("should throw NullPointerException");
325        } catch (NullPointerException e1) {
326            // expected
327        }
328
329        try {
330            f = new Formatter(notExist.getPath(), null);
331            fail("should throw NullPointerException");
332        } catch (NullPointerException e2) {
333            // expected
334        }
335
336        f = new Formatter(notExist.getPath(), Charset.defaultCharset().name());
337        assertEquals(f.locale(), Locale.getDefault());
338        f.close();
339
340        try {
341            f = new Formatter(notExist.getPath(), "ISO 1111-1");
342            fail("should throw UnsupportedEncodingException");
343        } catch (UnsupportedEncodingException e1) {
344            // expected
345        }
346
347        f = new Formatter(fileWithContent.getPath(), "UTF-16BE");
348        assertEquals(0, fileWithContent.length());
349        f.close();
350
351        try {
352            f = new Formatter(readOnly.getPath(), "UTF-16BE");
353            if (!("root".equals(System.getProperty("user.name")))) {
354                fail("should throw FileNotFoundException");
355            }
356        } catch (FileNotFoundException e) {
357            // expected
358        }
359
360        SecurityManager oldsm = System.getSecurityManager();
361        System.setSecurityManager(new MockSecurityManager());
362        try {
363            f = new Formatter(secret.getPath(), "UTF-16BE");
364            fail("should throw SecurityException");
365        } catch (SecurityException se) {
366            // expected
367        } finally {
368            System.setSecurityManager(oldsm);
369        }
370    }
371
372    /**
373     * @tests java.util.Formatter#Formatter(String, String, Locale)
374     */
375    @TestTargetNew(
376        level = TestLevel.COMPLETE,
377        notes = "",
378        method = "Formatter",
379        args = {java.lang.String.class, java.lang.String.class, java.util.Locale.class}
380    )
381    public void test_ConstructorLjava_lang_StringLjava_lang_StringLjava_util_Locale()
382            throws IOException {
383        Formatter f = null;
384        try {
385            f = new Formatter((String) null, Charset.defaultCharset().name(),
386                    Locale.KOREA);
387            fail("should throw NullPointerException");
388        } catch (NullPointerException e1) {
389            // expected
390        }
391
392        try {
393            f = new Formatter(notExist.getPath(), null, Locale.KOREA);
394            fail("should throw NullPointerException");
395        } catch (NullPointerException e2) {
396            // expected
397        }
398
399        f = new Formatter(notExist.getPath(), Charset.defaultCharset().name(),
400                null);
401        assertNotNull(f);
402        f.close();
403
404        f = new Formatter(notExist.getPath(), Charset.defaultCharset().name(),
405                Locale.KOREA);
406        assertEquals(f.locale(), Locale.KOREA);
407        f.close();
408
409        try {
410            f = new Formatter(notExist.getPath(), "ISO 1111-1", Locale.CHINA);
411            fail("should throw UnsupportedEncodingException");
412        } catch (UnsupportedEncodingException e1) {
413            // expected
414        }
415
416        f = new Formatter(fileWithContent.getPath(), "UTF-16BE",
417                Locale.CANADA_FRENCH);
418        assertEquals(0, fileWithContent.length());
419        f.close();
420
421        try {
422            f = new Formatter(readOnly.getPath(), Charset.defaultCharset()
423                    .name(), Locale.ITALY);
424            if (!("root".equals(System.getProperty("user.name")))) {
425                fail("should throw FileNotFoundException");
426            }
427        } catch (FileNotFoundException e) {
428            // expected
429        }
430
431        SecurityManager oldsm = System.getSecurityManager();
432        System.setSecurityManager(new MockSecurityManager());
433        try {
434            f = new Formatter(secret.getPath(),
435                    Charset.defaultCharset().name(), Locale.SIMPLIFIED_CHINESE);
436            fail("should throw SecurityException");
437        } catch (SecurityException se) {
438            // expected
439        } finally {
440            System.setSecurityManager(oldsm);
441        }
442    }
443
444    /**
445     * @tests java.util.Formatter#Formatter(File)
446     */
447    @TestTargetNew(
448        level = TestLevel.COMPLETE,
449        notes = "",
450        method = "Formatter",
451        args = {java.io.File.class}
452    )
453    public void test_ConstructorLjava_io_File() throws IOException {
454        Formatter f = null;
455        try {
456            f = new Formatter((File) null);
457            fail("should throw NullPointerException");
458        } catch (NullPointerException e1) {
459            // expected
460        }
461
462        f = new Formatter(notExist);
463        assertEquals(f.locale(), Locale.getDefault());
464        f.close();
465
466        f = new Formatter(fileWithContent);
467        assertEquals(0, fileWithContent.length());
468        f.close();
469
470        try {
471            f = new Formatter(readOnly);
472            if (!("root".equals(System.getProperty("user.name")))) {
473                fail("should throw FileNotFoundException");
474            }
475        } catch (FileNotFoundException e) {
476            // expected
477        }
478
479        SecurityManager oldsm = System.getSecurityManager();
480        System.setSecurityManager(new MockSecurityManager());
481        try {
482            f = new Formatter(secret);
483            fail("should throw SecurityException");
484        } catch (SecurityException se) {
485            // expected
486        } finally {
487            System.setSecurityManager(oldsm);
488        }
489    }
490
491    /**
492     * @tests java.util.Formatter#Formatter(File, String)
493     */
494    @TestTargetNew(
495        level = TestLevel.COMPLETE,
496        notes = "",
497        method = "Formatter",
498        args = {java.io.File.class, java.lang.String.class}
499    )
500    public void test_ConstructorLjava_io_FileLjava_lang_String()
501            throws IOException {
502        Formatter f = null;
503        try {
504            f = new Formatter((File) null, Charset.defaultCharset().name());
505            fail("should throw NullPointerException");
506        } catch (NullPointerException e1) {
507            // expected
508        }
509
510        f = new Formatter(notExist, Charset.defaultCharset().name());
511        assertEquals(f.locale(), Locale.getDefault());
512        f.close();
513
514        f = new Formatter(fileWithContent, "UTF-16BE");
515        assertEquals(0, fileWithContent.length());
516        f.close();
517
518        try {
519            f = new Formatter(readOnly, Charset.defaultCharset().name());
520            if (!("root".equals(System.getProperty("user.name")))) {
521                fail("should throw FileNotFoundException");
522            }
523        } catch (FileNotFoundException e) {
524            // expected
525        }
526
527        SecurityManager oldsm = System.getSecurityManager();
528        System.setSecurityManager(new MockSecurityManager());
529        try {
530            f = new Formatter(secret, Charset.defaultCharset().name());
531            fail("should throw SecurityException");
532        } catch (SecurityException se) {
533            // expected
534        } finally {
535            System.setSecurityManager(oldsm);
536        }
537
538        try {
539            f = new Formatter(notExist, null);
540            fail("should throw NullPointerException");
541        } catch (NullPointerException e2) {
542            // expected
543        } finally {
544            if (notExist.exists()) {
545                // Fail on RI on Windows, because output stream is created and
546                // not closed when exception thrown
547                assertTrue(notExist.delete());
548            }
549        }
550
551        try {
552            f = new Formatter(notExist, "ISO 1111-1");
553            fail("should throw UnsupportedEncodingException");
554        } catch (UnsupportedEncodingException e1) {
555            // expected
556        } finally {
557            if (notExist.exists()) {
558                // Fail on RI on Windows, because output stream is created and
559                // not closed when exception thrown
560                assertTrue(notExist.delete());
561            }
562        }
563    }
564
565    /**
566     * @tests java.util.Formatter#Formatter(File, String, Locale)
567     */
568    @TestTargetNew(
569        level = TestLevel.COMPLETE,
570        notes = "",
571        method = "Formatter",
572        args = {java.io.File.class, java.lang.String.class, java.util.Locale.class}
573    )
574    public void test_ConstructorLjava_io_FileLjava_lang_StringLjava_util_Locale()
575            throws IOException {
576        Formatter f = null;
577        try {
578            f = new Formatter((File) null, Charset.defaultCharset().name(),
579                    Locale.KOREA);
580            fail("should throw NullPointerException");
581        } catch (NullPointerException e1) {
582            // expected
583        }
584
585        try {
586            f = new Formatter(notExist, null, Locale.KOREA);
587            fail("should throw NullPointerException");
588        } catch (NullPointerException e2) {
589            // expected
590        }
591
592        f = new Formatter(notExist, Charset.defaultCharset().name(), null);
593        assertNotNull(f);
594        f.close();
595
596        f = new Formatter(notExist, Charset.defaultCharset().name(),
597                Locale.KOREA);
598        assertEquals(f.locale(), Locale.KOREA);
599        f.close();
600
601        try {
602            f = new Formatter(notExist, "ISO 1111-1", Locale.CHINA);
603            fail("should throw UnsupportedEncodingException");
604        } catch (UnsupportedEncodingException e1) {
605            // expected
606        }
607        f = new Formatter(fileWithContent.getPath(), "UTF-16BE",
608                Locale.CANADA_FRENCH);
609        assertEquals(0, fileWithContent.length());
610        f.close();
611
612        try {
613            f = new Formatter(readOnly.getPath(), Charset.defaultCharset()
614                    .name(), Locale.ITALY);
615            if (!("root".equals(System.getProperty("user.name")))) {
616                fail("should throw FileNotFoundException");
617            }
618        } catch (FileNotFoundException e) {
619            // expected
620        }
621
622        SecurityManager oldsm = System.getSecurityManager();
623        System.setSecurityManager(new MockSecurityManager());
624        try {
625            f = new Formatter(secret.getPath(),
626                    Charset.defaultCharset().name(), Locale.SIMPLIFIED_CHINESE);
627            fail("should throw SecurityException");
628        } catch (SecurityException se) {
629            // expected
630        } finally {
631            System.setSecurityManager(oldsm);
632        }
633    }
634
635    /**
636     * @tests java.util.Formatter#Formatter(PrintStream)
637     */
638    @TestTargetNew(
639        level = TestLevel.COMPLETE,
640        notes = "",
641        method = "Formatter",
642        args = {java.io.PrintStream.class}
643    )
644    public void test_ConstructorLjava_io_PrintStream() throws IOException {
645        Formatter f = null;
646        try {
647            f = new Formatter((PrintStream) null);
648            fail("should throw NullPointerException");
649        } catch (NullPointerException e1) {
650            // expected
651        }
652
653        PrintStream ps = new PrintStream(notExist, "UTF-16BE");
654        f = new Formatter(ps);
655        assertEquals(Locale.getDefault(), f.locale());
656        f.close();
657    }
658
659    /**
660     * @tests java.util.Formatter#Formatter(OutputStream)
661     */
662    @TestTargetNew(
663        level = TestLevel.COMPLETE,
664        notes = "",
665        method = "Formatter",
666        args = {java.io.OutputStream.class}
667    )
668    public void test_ConstructorLjava_io_OutputStream() throws IOException {
669        Formatter f = null;
670        try {
671            f = new Formatter((OutputStream) null);
672            fail("should throw NullPointerException");
673        } catch (NullPointerException e1) {
674            // expected
675        }
676
677        OutputStream os = new FileOutputStream(notExist);
678        f = new Formatter(os);
679        assertEquals(Locale.getDefault(), f.locale());
680        f.close();
681    }
682
683    /**
684     * @tests java.util.Formatter#Formatter(OutputStream, String)
685     */
686    @TestTargetNew(
687        level = TestLevel.COMPLETE,
688        notes = "",
689        method = "Formatter",
690        args = {java.io.OutputStream.class, java.lang.String.class}
691    )
692    public void test_ConstructorLjava_io_OutputStreamLjava_lang_String()
693            throws IOException {
694        Formatter f = null;
695        try {
696            f = new Formatter((OutputStream) null, Charset.defaultCharset()
697                    .name());
698            fail("should throw NullPointerException");
699        } catch (NullPointerException e1) {
700            // expected
701        }
702
703        OutputStream os = null;
704        try {
705            os = new FileOutputStream(notExist);
706            f = new Formatter(os, null);
707            fail("should throw NullPointerException");
708        } catch (NullPointerException e2) {
709            // expected
710        } finally {
711            os.close();
712        }
713
714        try {
715            os = new PipedOutputStream();
716            f = new Formatter(os, "TMP-1111");
717            fail("should throw UnsupportedEncodingException");
718        } catch (UnsupportedEncodingException e1) {
719            // expected
720        } finally {
721            os.close();
722        }
723
724        os = new FileOutputStream(fileWithContent);
725        f = new Formatter(os, "UTF-16BE");
726        assertEquals(Locale.getDefault(), f.locale());
727        f.close();
728    }
729
730    /**
731     * Test method for 'java.util.Formatter.Formatter(OutputStream, String,
732     * Locale)
733     */
734    @TestTargetNew(
735        level = TestLevel.COMPLETE,
736        notes = "",
737        method = "Formatter",
738        args = {java.io.OutputStream.class, java.lang.String.class, java.util.Locale.class}
739    )
740    public void test_ConstructorLjava_io_OutputStreamLjava_lang_StringLjava_util_Locale()
741            throws IOException {
742        Formatter f = null;
743        try {
744            f = new Formatter((OutputStream) null, Charset.defaultCharset()
745                    .name(), Locale.getDefault());
746            fail("should throw NullPointerException");
747        } catch (NullPointerException e1) {
748            // expected
749        }
750
751        OutputStream os = null;
752        try {
753            os = new FileOutputStream(notExist);
754            f = new Formatter(os, null, Locale.getDefault());
755            fail("should throw NullPointerException");
756        } catch (NullPointerException e2) {
757            // expected
758        } finally {
759            os.close();
760        }
761
762        os = new FileOutputStream(notExist);
763        f = new Formatter(os, Charset.defaultCharset().name(), null);
764        f.close();
765
766        try {
767            os = new PipedOutputStream();
768            f = new Formatter(os, "TMP-1111", Locale.getDefault());
769            fail("should throw UnsupportedEncodingException");
770        } catch (UnsupportedEncodingException e1) {
771            // expected
772        }
773
774        os = new FileOutputStream(fileWithContent);
775        f = new Formatter(os, "UTF-16BE", Locale.ENGLISH);
776        assertEquals(Locale.ENGLISH, f.locale());
777        f.close();
778    }
779
780    /**
781     * @tests java.util.Formatter#locale()
782     */
783    @TestTargetNew(
784        level = TestLevel.COMPLETE,
785        notes = "",
786        method = "locale",
787        args = {}
788    )
789    public void test_locale() {
790        Formatter f = null;
791        f = new Formatter((Locale) null);
792        assertNull(f.locale());
793        f = new Formatter(Locale.FRANCE);
794        assertEquals(f.locale(), Locale.FRANCE);
795
796        f.close();
797        try {
798            f.locale();
799            fail("should throw FormatterClosedException");
800        } catch (FormatterClosedException e) {
801            // expected
802        }
803    }
804
805    /**
806     * @tests java.util.Formatter#out()
807     */
808    @TestTargetNew(
809        level = TestLevel.COMPLETE,
810        notes = "",
811        method = "out",
812        args = {}
813    )
814    public void test_out() {
815        Formatter f = null;
816        f = new Formatter();
817        assertNotNull(f.out());
818        assertTrue(f.out() instanceof StringBuilder);
819        f.close();
820        try {
821            f.out();
822            fail("should throw FormatterClosedException");
823        } catch (FormatterClosedException e) {
824            // expected
825        }
826
827    }
828
829    /**
830     * @tests java.util.Formatter#flush()
831     */
832    @TestTargetNew(
833        level = TestLevel.COMPLETE,
834        notes = "",
835        method = "flush",
836        args = {}
837    )
838    public void test_flush() throws IOException {
839        Formatter f = null;
840        f = new Formatter(notExist);
841        assertTrue(f instanceof Flushable);
842        f.close();
843        try {
844            f.flush();
845            fail("should throw FormatterClosedException");
846        } catch (FormatterClosedException e) {
847            // expected
848        }
849
850        f = new Formatter();
851        // For destination that does not implement Flushable
852        // No exception should be thrown
853        f.flush();
854    }
855
856    /**
857     * @tests java.util.Formatter#close()
858     */
859    @TestTargetNew(
860        level = TestLevel.COMPLETE,
861        notes = "",
862        method = "close",
863        args = {}
864    )
865    public void test_close() throws IOException {
866        Formatter f = new Formatter(notExist);
867        assertTrue(f instanceof Closeable);
868        f.close();
869        // close next time will not throw exception
870        f.close();
871        assertNull(f.ioException());
872    }
873
874    /**
875     * @tests java.util.Formatter#toString()
876     */
877    @TestTargetNew(
878        level = TestLevel.COMPLETE,
879        notes = "",
880        method = "toString",
881        args = {}
882    )
883    public void test_toString() {
884        Formatter f = new Formatter();
885        assertNotNull(f.toString());
886        assertEquals(f.out().toString(), f.toString());
887        f.close();
888        try {
889            f.toString();
890            fail("should throw FormatterClosedException");
891        } catch (FormatterClosedException e) {
892            // expected
893        }
894    }
895
896    /**
897     * @tests java.util.Formatter#ioException()
898     */
899    @TestTargetNew(
900        level = TestLevel.COMPLETE,
901        notes = "",
902        method = "ioException",
903        args = {}
904    )
905    public void test_ioException() throws IOException {
906        Formatter f = null;
907        f = new Formatter(new MockDestination());
908        assertNull(f.ioException());
909        f.flush();
910        assertNotNull(f.ioException());
911        f.close();
912
913        MockDestination md = new MockDestination();
914        f = new Formatter(md);
915        f.format("%s%s", "1", "2");
916        // format stop working after IOException
917        assertNotNull(f.ioException());
918        assertEquals("", f.toString());
919    }
920
921    /**
922     * @tests java.util.Formatter#format(String, Object...) for null parameter
923     */
924    @TestTargetNew(
925        level = TestLevel.PARTIAL_COMPLETE,
926        notes = "Verifies null as a parameter.",
927        method = "format",
928        args = {java.lang.String.class, java.lang.Object[].class}
929    )
930    public void test_formatLjava_lang_String$Ljava_lang_Object_null() {
931        Formatter f = new Formatter();
932        try {
933            f.format((String) null, "parameter");
934            fail("should throw NullPointerException");
935        } catch (NullPointerException e) {
936            // expected
937        }
938
939        f = new Formatter();
940        f.format("hello", (Object[]) null);
941        assertEquals("hello", f.toString());
942    }
943
944    /**
945     * @tests java.util.Formatter#format(String, Object...) for argument index
946     */
947    @TestTargetNew(
948        level = TestLevel.PARTIAL_COMPLETE,
949        notes = "Doesn't verify IllegalFormatException, FormatterClosedException.",
950        method = "format",
951        args = {java.lang.String.class, java.lang.Object[].class}
952    )
953    public void test_formatLjava_lang_String$Ljava_lang_Object_ArgIndex() {
954        Formatter formatter = new Formatter(Locale.US);
955        formatter.format("%1$s%2$s%3$s%4$s%5$s%6$s%7$s%8$s%9$s%11$s%10$s", "1",
956                "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");
957        assertEquals("1234567891110", formatter.toString());
958
959        formatter = new Formatter(Locale.JAPAN);
960        formatter.format("%0$s", "hello");
961        assertEquals("hello", formatter.toString());
962
963        try {
964            formatter = new Formatter(Locale.US);
965            formatter.format("%-1$s", "1", "2");
966            fail("should throw UnknownFormatConversionException");
967        } catch (UnknownFormatConversionException e) {
968            // expected
969        }
970
971        try {
972            formatter = new Formatter(Locale.US);
973            formatter.format("%$s", "hello", "2");
974            fail("should throw UnknownFormatConversionException");
975        } catch (UnknownFormatConversionException e) {
976            // expected
977        }
978
979        try {
980            Formatter f = new Formatter(Locale.US);
981            f.format("%", "string");
982            fail("should throw UnknownFormatConversionException");
983        } catch (UnknownFormatConversionException e) {
984            // expected
985        }
986
987        formatter = new Formatter(Locale.FRANCE);
988        formatter.format("%1$s%2$s%3$s%4$s%5$s%6$s%7$s%8$s%<s%s%s%<s", "1",
989                "2", "3", "4", "5", "6", "7", "8", "9", "10", "11");
990        assertEquals("123456788122", formatter.toString());
991
992        formatter = new Formatter(Locale.FRANCE);
993        formatter.format(
994                "xx%1$s22%2$s%s%<s%5$s%<s&%7$h%2$s%8$s%<s%s%s%<ssuffix", "1",
995                "2", "3", "4", "5", "6", 7, "8", "9", "10", "11");
996        assertEquals("xx12221155&7288233suffix", formatter.toString());
997
998        try {
999            formatter.format("%<s", "hello");
1000            fail("should throw MissingFormatArgumentException");
1001        } catch (MissingFormatArgumentException e) {
1002            // expected
1003        }
1004
1005        formatter = new Formatter(Locale.US);
1006        try {
1007            formatter.format("%123$s", "hello");
1008            fail("should throw MissingFormatArgumentException");
1009        } catch (MissingFormatArgumentException e) {
1010            // expected
1011        }
1012
1013        formatter = new Formatter(Locale.US);
1014        try {
1015            // 2147483648 is the value of Integer.MAX_VALUE + 1
1016            formatter.format("%2147483648$s", "hello");
1017            fail("should throw MissingFormatArgumentException");
1018        } catch (MissingFormatArgumentException e) {
1019            // expected
1020        }
1021
1022        try {
1023            // 2147483647 is the value of Integer.MAX_VALUE
1024            formatter.format("%2147483647$s", "hello");
1025            fail("should throw MissingFormatArgumentException");
1026        } catch (MissingFormatArgumentException e) {
1027            // expected
1028        }
1029
1030        formatter = new Formatter(Locale.US);
1031        try {
1032            formatter.format("%s%s", "hello");
1033            fail("should throw MissingFormatArgumentException");
1034        } catch (MissingFormatArgumentException e) {
1035            // expected
1036        }
1037
1038        formatter = new Formatter(Locale.US);
1039        formatter.format("$100", 100);
1040        assertEquals("$100", formatter.toString());
1041
1042        formatter = new Formatter(Locale.UK);
1043        formatter.format("%01$s", "string");
1044        assertEquals("string", formatter.toString());
1045    }
1046
1047    /**
1048     * @tests java.util.Formatter#format(String, Object...) for width
1049     */
1050    @TestTargetNew(
1051        level = TestLevel.PARTIAL_COMPLETE,
1052        notes = "",
1053        method = "format",
1054        args = {java.lang.String.class, java.lang.Object[].class}
1055    )
1056    public void test_formatLjava_lang_String$Ljava_lang_Object_Width() {
1057        Formatter f = new Formatter(Locale.US);
1058        f.format("%1$8s", "1");
1059        assertEquals("       1", f.toString());
1060
1061        f = new Formatter(Locale.US);
1062        f.format("%1$-1%", "string");
1063        assertEquals("%", f.toString());
1064
1065        f = new Formatter(Locale.ITALY);
1066        // 2147483648 is the value of Integer.MAX_VALUE + 1
1067        f.format("%2147483648s", "string");
1068        assertEquals("string", f.toString());
1069
1070        // the value of Integer.MAX_VALUE will allocate about 4G bytes of
1071        // memory.
1072        // It may cause OutOfMemoryError, so this value is not tested
1073    }
1074
1075    /**
1076     * @tests java.util.Formatter#format(String, Object...) for precision
1077     */
1078    @TestTargetNew(
1079        level = TestLevel.PARTIAL_COMPLETE,
1080        notes = "",
1081        method = "format",
1082        args = {java.lang.String.class, java.lang.Object[].class}
1083    )
1084    public void test_formatLjava_lang_String$Ljava_lang_Object_Precision() {
1085        Formatter f = new Formatter(Locale.US);
1086        f.format("%.5s", "123456");
1087        assertEquals("12345", f.toString());
1088
1089        f = new Formatter(Locale.US);
1090        // 2147483648 is the value of Integer.MAX_VALUE + 1
1091        f.format("%.2147483648s", "...");
1092        assertEquals("...", f.toString());
1093
1094        // the value of Integer.MAX_VALUE will allocate about 4G bytes of
1095        // memory.
1096        // It may cause OutOfMemoryError, so this value is not tested
1097
1098        f = new Formatter(Locale.US);
1099        f.format("%10.0b", Boolean.TRUE);
1100        assertEquals("          ", f.toString());
1101
1102        f = new Formatter(Locale.US);
1103        f.format("%10.01s", "hello");
1104        assertEquals("         h", f.toString());
1105
1106        try {
1107            f = new Formatter(Locale.US);
1108            f.format("%.s", "hello", "2");
1109            fail("should throw UnknownFormatConversionException");
1110        } catch (UnknownFormatConversionException e) {
1111            // expected
1112        }
1113
1114        try {
1115            f = new Formatter(Locale.US);
1116            f.format("%.-5s", "123456");
1117            fail("should throw UnknownFormatConversionException");
1118        } catch (UnknownFormatConversionException e) {
1119            // expected
1120        }
1121
1122        try {
1123            f = new Formatter(Locale.US);
1124            f.format("%1.s", "hello", "2");
1125            fail("should throw UnknownFormatConversionException");
1126        } catch (UnknownFormatConversionException e) {
1127            // expected
1128        }
1129
1130        f = new Formatter(Locale.US);
1131        f.format("%5.1s", "hello");
1132        assertEquals("    h", f.toString());
1133
1134        f = new Formatter(Locale.FRANCE);
1135        f.format("%.0s", "hello", "2");
1136        assertEquals("", f.toString());
1137    }
1138
1139    /**
1140     * @tests java.util.Formatter#format(String, Object...) for line sperator
1141     */
1142    @TestTargetNew(
1143        level = TestLevel.PARTIAL_COMPLETE,
1144        notes = "Verifies IllegalFormatFlagsException.",
1145        method = "format",
1146        args = {java.lang.String.class, java.lang.Object[].class}
1147    )
1148    public void test_formatLjava_lang_String$Ljava_lang_Object_LineSeparator() {
1149        Formatter f = null;
1150
1151        String oldSeparator = System.getProperty("line.separator");
1152        System.setProperty("line.separator", "!\n");
1153
1154        f = new Formatter(Locale.US);
1155        f.format("%1$n", 1);
1156        assertEquals("!\n", f.toString());
1157
1158        f = new Formatter(Locale.KOREAN);
1159        f.format("head%1$n%2$n", 1, new Date());
1160        assertEquals("head!\n!\n", f.toString());
1161
1162        f = new Formatter(Locale.US);
1163        f.format("%n%s", "hello");
1164        assertEquals("!\nhello", f.toString());
1165
1166        System.setProperty("line.separator", oldSeparator);
1167
1168        f = new Formatter(Locale.US);
1169        try {
1170            f.format("%-n");
1171            fail("should throw IllegalFormatFlagsException: %-n");
1172        } catch (IllegalFormatFlagsException e) {
1173            // expected
1174        }
1175        try {
1176            f.format("%+n");
1177            fail("should throw IllegalFormatFlagsException: %+n");
1178        } catch (IllegalFormatFlagsException e) {
1179            // expected
1180        }
1181        try {
1182            f.format("%#n");
1183            fail("should throw IllegalFormatFlagsException: %#n");
1184        } catch (IllegalFormatFlagsException e) {
1185            // expected
1186        }
1187        try {
1188            f.format("% n");
1189            fail("should throw IllegalFormatFlagsException: % n");
1190        } catch (IllegalFormatFlagsException e) {
1191            // expected
1192        }
1193        try {
1194            f.format("%0n");
1195            fail("should throw IllegalFormatFlagsException: %0n");
1196        } catch (IllegalFormatFlagsException e) {
1197            // expected
1198        }
1199        try {
1200            f.format("%,n");
1201            fail("should throw IllegalFormatFlagsException: %,n");
1202        } catch (IllegalFormatFlagsException e) {
1203            // expected
1204        }
1205        try {
1206            f.format("%(n");
1207            fail("should throw IllegalFormatFlagsException: %(n");
1208        } catch (IllegalFormatFlagsException e) {
1209            // expected
1210        }
1211
1212        f = new Formatter(Locale.US);
1213        try {
1214            f.format("%4n");
1215            fail("should throw IllegalFormatWidthException");
1216        } catch (IllegalFormatWidthException e) {
1217            // expected
1218        }
1219
1220        f = new Formatter(Locale.US);
1221        try {
1222            f.format("%-4n");
1223            fail("should throw IllegalFormatWidthException");
1224        } catch (IllegalFormatWidthException e) {
1225            // expected
1226        }
1227
1228        f = new Formatter(Locale.US);
1229        try {
1230            f.format("%.9n");
1231            fail("should throw IllegalFormatPrecisionException");
1232        } catch (IllegalFormatPrecisionException e) {
1233            // expected
1234        }
1235
1236        f = new Formatter(Locale.US);
1237        try {
1238            f.format("%5.9n");
1239            fail("should throw IllegalFormatPrecisionException");
1240        } catch (IllegalFormatPrecisionException e) {
1241            // expected
1242        }
1243
1244        System.setProperty("line.separator", oldSeparator);
1245    }
1246
1247    /**
1248     * @tests java.util.Formatter#format(String, Object...) for percent
1249     */
1250    @TestTargetNew(
1251        level = TestLevel.PARTIAL_COMPLETE,
1252        notes = "Verifies IllegalFormatPrecisionException.",
1253        method = "format",
1254        args = {java.lang.String.class, java.lang.Object[].class}
1255    )
1256    @AndroidOnly("fails on RI. See comment below")
1257    public void test_formatLjava_lang_String$Ljava_lang_Object_Percent() {
1258        Formatter f = null;
1259
1260        f = new Formatter(Locale.ENGLISH);
1261        f.format("%1$%", 100);
1262        assertEquals("%", f.toString());
1263
1264        f = new Formatter(Locale.CHINA);
1265        f.format("%1$%%%", "hello", new Object());
1266        assertEquals("%%", f.toString());
1267
1268        f = new Formatter(Locale.CHINA);
1269        f.format("%%%s", "hello");
1270        assertEquals("%hello", f.toString());
1271
1272        f = new Formatter(Locale.US);
1273        try {
1274            f.format("%.9%");
1275            fail("should throw IllegalFormatPrecisionException");
1276        } catch (IllegalFormatPrecisionException e) {
1277            // expected
1278        }
1279
1280        f = new Formatter(Locale.US);
1281        try {
1282            f.format("%5.9%");
1283            fail("should throw IllegalFormatPrecisionException");
1284        } catch (IllegalFormatPrecisionException e) {
1285            // expected
1286        }
1287
1288        f = new Formatter(Locale.US);
1289    /*
1290     * fail on RI. The only flag supported for '%' is '-'. If any other flags are
1291     * provide, FormatFlagsConversionMismatchException shall be thrown
1292     */
1293        assertFormatFlagsConversionMismatchException(f, "%+%");
1294        assertFormatFlagsConversionMismatchException(f, "%#%");
1295        assertFormatFlagsConversionMismatchException(f, "% %");
1296        assertFormatFlagsConversionMismatchException(f, "%0%");
1297        assertFormatFlagsConversionMismatchException(f, "%,%");
1298        assertFormatFlagsConversionMismatchException(f, "%(%");
1299
1300
1301        f = new Formatter(Locale.KOREAN);
1302        f.format("%4%", 1);
1303
1304        /* fail on RI the output string should be right justified by appending
1305         * spaces till the whole string is 4 chars width.
1306         */
1307        assertEquals("   %", f.toString());
1308
1309        f = new Formatter(Locale.US);
1310        f.format("%-4%", 100);
1311        /*
1312         * fail on RI, throw UnknownFormatConversionException the output string
1313         * should be left justified by appending spaces till the whole string is
1314         * 4 chars width.
1315         */
1316        assertEquals("%   ", f.toString());
1317    }
1318
1319    private void assertFormatFlagsConversionMismatchException(Formatter f, String str) {
1320        try {
1321            f.format(str);
1322            fail("should throw FormatFlagsConversionMismatchException: "
1323                    + str);
1324             /*
1325             * error on RI, throw IllegalFormatFlagsException specification
1326             * says FormatFlagsConversionMismatchException should be thrown
1327             */
1328        } catch (FormatFlagsConversionMismatchException e) {
1329           // expected
1330        }
1331    }
1332
1333    /**
1334     * @tests java.util.Formatter#format(String, Object...) for flag
1335     */
1336    @TestTargetNew(
1337        level = TestLevel.PARTIAL_COMPLETE,
1338        notes = "Verifies UnknownFormatConversionException, DuplicateFormatFlagsException.",
1339        method = "format",
1340        args = {java.lang.String.class, java.lang.Object[].class}
1341    )
1342    public void test_formatLjava_lang_String$Ljava_lang_Object_Flag() {
1343        Formatter f = new Formatter(Locale.US);
1344        try {
1345            f.format("%1$-#-8s", "something");
1346            fail("should throw DuplicateFormatFlagsException");
1347        } catch (DuplicateFormatFlagsException e) {
1348            // expected
1349        }
1350
1351        final char[] chars = { '-', '#', '+', ' ', '0', ',', '(', '%', '<' };
1352        Arrays.sort(chars);
1353        f = new Formatter(Locale.US);
1354        for (char i = 0; i <= 256; i++) {
1355            // test 8 bit character
1356            if (Arrays.binarySearch(chars, i) >= 0 || Character.isDigit(i)
1357                    || Character.isLetter(i)) {
1358                // Do not test 0-9, a-z, A-Z and characters in the chars array.
1359                // They are characters used as flags, width or conversions
1360                continue;
1361            }
1362            try {
1363                f.format("%" + i + "s", 1);
1364                fail("should throw UnknownFormatConversionException");
1365            } catch (UnknownFormatConversionException e) {
1366                // expected
1367            }
1368        }
1369    }
1370
1371    /**
1372     * @tests java.util.Formatter#format(String, Object...) for general
1373     *        conversion b/B
1374     */
1375    @TestTargetNew(
1376        level = TestLevel.PARTIAL_COMPLETE,
1377        notes = "Doesn't verify IllegalFormatException, FormatterClosedException.",
1378        method = "format",
1379        args = {java.lang.String.class, java.lang.Object[].class}
1380    )
1381    public void test_format_LString$LObject_GeneralConversionB() {
1382        final Object[][] triple = {
1383                { Boolean.FALSE,                "%3.2b",  " fa", },
1384                { Boolean.FALSE,                "%-4.6b", "false", },
1385                { Boolean.FALSE,                "%.2b",   "fa", },
1386                { Boolean.TRUE,                 "%3.2b",  " tr", },
1387                { Boolean.TRUE,                 "%-4.6b", "true", },
1388                { Boolean.TRUE,                 "%.2b",   "tr", },
1389                { new Character('c'),           "%3.2b",  " tr", },
1390                { new Character('c'),           "%-4.6b", "true", },
1391                { new Character('c'),           "%.2b",   "tr", },
1392                { new Byte((byte) 0x01),        "%3.2b",  " tr", },
1393                { new Byte((byte) 0x01),        "%-4.6b", "true", },
1394                { new Byte((byte) 0x01),        "%.2b",   "tr", },
1395                { new Short((short) 0x0001),    "%3.2b",  " tr", },
1396                { new Short((short) 0x0001),    "%-4.6b", "true", },
1397                { new Short((short) 0x0001),    "%.2b",   "tr", },
1398                { new Integer(1),               "%3.2b",  " tr", },
1399                { new Integer(1),               "%-4.6b", "true", },
1400                { new Integer(1),               "%.2b",   "tr", },
1401                { new Float(1.1f),              "%3.2b",  " tr", },
1402                { new Float(1.1f),              "%-4.6b", "true", },
1403                { new Float(1.1f),              "%.2b",   "tr", },
1404                { new Double(1.1d),             "%3.2b",  " tr", },
1405                { new Double(1.1d),             "%-4.6b", "true", },
1406                { new Double(1.1d),             "%.2b",   "tr", },
1407                { "",                           "%3.2b",  " tr", },
1408                { "",                           "%-4.6b", "true", },
1409                { "",                           "%.2b",   "tr", },
1410                { "string content",             "%3.2b",  " tr", },
1411                { "string content",             "%-4.6b", "true", },
1412                { "string content",             "%.2b",   "tr", },
1413                { new MockFormattable(),        "%3.2b",  " tr", },
1414                { new MockFormattable(),        "%-4.6b", "true", },
1415                { new MockFormattable(),        "%.2b",   "tr", },
1416                { (Object) null,                "%3.2b",  " fa", },
1417                { (Object) null,                "%-4.6b", "false", },
1418                { (Object) null,                "%.2b",   "fa", },
1419                };
1420
1421
1422        final int input   = 0;
1423        final int pattern = 1;
1424        final int output  = 2;
1425        Formatter f = null;
1426        for (int i = 0; i < triple.length; i++) {
1427            f = new Formatter(Locale.FRANCE);
1428            f.format((String)triple[i][pattern], triple[i][input]);
1429            assertEquals("triple[" + i + "]:" + triple[i][input]
1430                          + ",pattern[" + i + "]:" + triple[i][pattern], triple[i][output], f.toString());
1431
1432            f = new Formatter(Locale.GERMAN);
1433            f.format(((String)triple[i][pattern]).toUpperCase(Locale.US), triple[i][input]);
1434            assertEquals("triple[" + i + "]:" + triple[i][input]
1435                          + ",pattern[" + i + "]:" + triple[i][pattern], ((String)triple[i][output])
1436                    .toUpperCase(Locale.US), f.toString());
1437        }
1438    }
1439
1440    /**
1441     * @tests java.util.Formatter#format(String, Object...) for general
1442     *        conversion type 's' and 'S'
1443     */
1444    @TestTargetNew(
1445        level = TestLevel.PARTIAL_COMPLETE,
1446        notes = "Doesn't verify IllegalFormatException, FormatterClosedException.",
1447        method = "format",
1448        args = {java.lang.String.class, java.lang.Object[].class}
1449    )
1450    public void test_format_LString$LObject_GeneralConversionS() {
1451
1452        final Object[][] triple = {
1453                { Boolean.FALSE,                "%2.3s",  "fal", },
1454                { Boolean.FALSE,                "%-6.4s", "fals  ", },
1455                { Boolean.FALSE,                "%.5s",   "false", },
1456                { Boolean.TRUE,                 "%2.3s",  "tru", },
1457                { Boolean.TRUE,                 "%-6.4s", "true  ", },
1458                { Boolean.TRUE,                 "%.5s",   "true", },
1459                { new Character('c'),           "%2.3s",  " c", },
1460                { new Character('c'),           "%-6.4s", "c     ", },
1461                { new Character('c'),           "%.5s",   "c", },
1462                { new Byte((byte) 0x01),        "%2.3s",  " 1", },
1463                { new Byte((byte) 0x01),        "%-6.4s", "1     ", },
1464                { new Byte((byte) 0x01),        "%.5s",   "1", },
1465                { new Short((short) 0x0001),    "%2.3s",  " 1", },
1466                { new Short((short) 0x0001),    "%-6.4s", "1     ", },
1467                { new Short((short) 0x0001),    "%.5s",   "1", },
1468                { new Integer(1),               "%2.3s",  " 1", },
1469                { new Integer(1),               "%-6.4s", "1     ", },
1470                { new Integer(1),               "%.5s",   "1", },
1471                { new Float(1.1f),              "%2.3s",  "1.1", },
1472                { new Float(1.1f),              "%-6.4s", "1.1   ", },
1473                { new Float(1.1f),              "%.5s",   "1.1", },
1474                { new Double(1.1d),             "%2.3s",  "1.1", },
1475                { new Double(1.1d),             "%-6.4s", "1.1   ", },
1476                { new Double(1.1d),             "%.5s",   "1.1", },
1477                { "",                           "%2.3s",  "  ", },
1478                { "",                           "%-6.4s", "      ", },
1479                { "",                           "%.5s",   "", },
1480                { "string content",             "%2.3s",  "str", },
1481                { "string content",             "%-6.4s", "stri  ", },
1482                { "string content",             "%.5s",   "strin", },
1483                { new MockFormattable(),        "%2.3s",  "customized format function width: 2 precision: 3", },
1484                { new MockFormattable(),        "%-6.4s", "customized format function width: 6 precision: 4", },
1485                { new MockFormattable(),        "%.5s",   "customized format function width: -1 precision: 5", },
1486                { (Object) null,                "%2.3s",  "nul", },
1487                { (Object) null,                "%-6.4s", "null  ", },
1488                { (Object) null,                "%.5s",   "null", },
1489                };
1490
1491
1492        final int input   = 0;
1493        final int pattern = 1;
1494        final int output  = 2;
1495        Formatter f = null;
1496        for (int i = 0; i < triple.length; i++) {
1497            f = new Formatter(Locale.FRANCE);
1498            f.format((String)triple[i][pattern], triple[i][input]);
1499            assertEquals("triple[" + i + "]:" + triple[i][input]
1500                          + ",pattern[" + i + "]:" + triple[i][pattern], triple[i][output], f.toString());
1501
1502            f = new Formatter(Locale.GERMAN);
1503            f.format(((String)triple[i][pattern]).toUpperCase(Locale.US), triple[i][input]);
1504            assertEquals("triple[" + i + "]:" + triple[i][input]
1505                          + ",pattern[" + i + "]:" + triple[i][pattern], ((String)triple[i][output])
1506                    .toUpperCase(Locale.US), f.toString());
1507        }
1508    }
1509
1510    /**
1511     * @tests java.util.Formatter#format(String, Object...) for general
1512     *        conversion type 'h' and 'H'
1513     */
1514    @TestTargetNew(
1515        level = TestLevel.PARTIAL_COMPLETE,
1516        notes = "Doesn't verify IllegalFormatException, FormatterClosedException.",
1517        method = "format",
1518        args = {java.lang.String.class, java.lang.Object[].class}
1519    )
1520    public void test_format_LString$LObject_GeneralConversionH() {
1521
1522        final Object[] input = {
1523                 Boolean.FALSE,
1524                 Boolean.TRUE,
1525                 new Character('c'),
1526                 new Byte((byte) 0x01),
1527                 new Short((short) 0x0001),
1528                 new Integer(1),
1529                 new Float(1.1f),
1530                 new Double(1.1d),
1531                 "",
1532                 "string content",
1533                 new MockFormattable(),
1534                 (Object) null,
1535                };
1536
1537        Formatter f = null;
1538        for (int i = 0; i < input.length - 1; i++) {
1539            f = new Formatter(Locale.FRANCE);
1540            f.format("%h", input[i]);
1541            assertEquals("triple[" + i + "]:" + input[i],
1542                    Integer.toHexString(input[i].hashCode()), f.toString());
1543
1544            f = new Formatter(Locale.GERMAN);
1545            f.format("%H", input[i]);
1546            assertEquals("triple[" + i + "]:" + input[i],
1547                    Integer.toHexString(input[i].hashCode()).toUpperCase(Locale.US), f.toString());
1548        }
1549    }
1550
1551    /**
1552     * @tests java.util.Formatter#format(String, Object...) for general
1553     *        conversion other cases
1554     */
1555    @TestTargetNew(
1556        level = TestLevel.PARTIAL_COMPLETE,
1557        notes = "Verifies FormatFlagsConversionMismatchException.",
1558        method = "format",
1559        args = {java.lang.String.class, java.lang.Object[].class}
1560    )
1561    @AndroidOnly("fails on RI. See comments below")
1562    public void test_formatLjava_lang_String$Ljava_lang_Object_GeneralConversionOther() {
1563        /*
1564         * In Turkish locale, the upper case of '\u0069' is '\u0130'. The
1565         * following test indicate that '\u0069' is coverted to upper case
1566         * without using the turkish locale.
1567         */
1568        Formatter f = new Formatter(new Locale("tr"));
1569        f.format("%S", "\u0069");
1570        assertEquals("\u0049", f.toString());
1571
1572        final Object[] input = {
1573                Boolean.FALSE,
1574                Boolean.TRUE,
1575                new Character('c'),
1576                new Byte((byte) 0x01),
1577                new Short((short) 0x0001),
1578                new Integer(1),
1579                new Float(1.1f),
1580                new Double(1.1d),
1581                "",
1582                "string content",
1583                new MockFormattable(),
1584                (Object) null,
1585               };
1586        f = new Formatter(Locale.GERMAN);
1587        for (int i = 0; i < input.length; i++) {
1588            if (!(input[i] instanceof Formattable)) {
1589                try {
1590                    f.format("%#s", input[i]);
1591                    /*
1592                     * fail on RI, spec says if the '#' flag is present and the
1593                     * argument is not a Formattable , then a
1594                     * FormatFlagsConversionMismatchException will be thrown.
1595                     */
1596                    fail("should throw FormatFlagsConversionMismatchException");
1597                } catch (FormatFlagsConversionMismatchException e) {
1598                    // expected
1599                }
1600            } else {
1601                f.format("%#s%<-#8s", input[i]);
1602                assertEquals(
1603                        "customized format function width: -1 precision: -1customized format function width: 8 precision: -1",
1604                        f.toString());
1605            }
1606        }
1607    }
1608
1609    /**
1610     * @tests java.util.Formatter#format(String, Object...) for general
1611     *        conversion exception
1612     */
1613    @TestTargetNew(
1614        level = TestLevel.PARTIAL_COMPLETE,
1615        notes = "Verifies exceptions.",
1616        method = "format",
1617        args = {java.lang.String.class, java.lang.Object[].class}
1618    )
1619    public void test_formatLjava_lang_String$Ljava_lang_Object_GeneralConversionException() {
1620        final String[] flagMismatch = { "%#b", "%+b", "% b", "%0b", "%,b",
1621                "%(b", "%#B", "%+B", "% B", "%0B", "%,B", "%(B", "%#h", "%+h",
1622                "% h", "%0h", "%,h", "%(h", "%#H", "%+H", "% H", "%0H", "%,H",
1623                "%(H", "%+s", "% s", "%0s", "%,s", "%(s", "%+S", "% S", "%0S",
1624                "%,S", "%(S" };
1625
1626        Formatter f = new Formatter(Locale.US);
1627
1628        for (int i = 0; i < flagMismatch.length; i++) {
1629            try {
1630                f.format(flagMismatch[i], "something");
1631                fail("should throw FormatFlagsConversionMismatchException");
1632            } catch (FormatFlagsConversionMismatchException e) {
1633                // expected
1634            }
1635        }
1636
1637        final String[] missingWidth = { "%-b", "%-B", "%-h", "%-H", "%-s",
1638                "%-S", };
1639        for (int i = 0; i < missingWidth.length; i++) {
1640            try {
1641                f.format(missingWidth[i], "something");
1642                fail("should throw MissingFormatWidthException");
1643            } catch (MissingFormatWidthException e) {
1644                // expected
1645            }
1646        }
1647
1648        // Regression test
1649        f = new Formatter();
1650        try {
1651            f.format("%c", (byte)-0x0001);
1652            fail("Should throw IllegalFormatCodePointException");
1653        } catch (IllegalFormatCodePointException e) {
1654            // expected
1655        }
1656
1657        f = new Formatter();
1658        try {
1659            f.format("%c", (short)-0x0001);
1660            fail("Should throw IllegalFormatCodePointException");
1661        } catch (IllegalFormatCodePointException e) {
1662            // expected
1663        }
1664
1665        f = new Formatter();
1666        try {
1667            f.format("%c", -0x0001);
1668            fail("Should throw IllegalFormatCodePointException");
1669        } catch (IllegalFormatCodePointException e) {
1670            // expected
1671        }
1672    }
1673
1674    /**
1675     * @tests java.util.Formatter#format(String, Object...) for Character
1676     *        conversion
1677     */
1678    @TestTargetNew(
1679        level = TestLevel.PARTIAL_COMPLETE,
1680        notes = "Verifies IllegalFormatConversionException, FormatFlagsConversionMismatchException, functionality.",
1681        method = "format",
1682        args = {java.lang.String.class, java.lang.Object[].class}
1683    )
1684    @AndroidOnly("RI doesn't support 'C' format")
1685    public void test_formatLjava_lang_String$Ljava_lang_Object_CharacterConversion() {
1686        Formatter f = new Formatter(Locale.US);
1687        final Object[] illArgs = { Boolean.TRUE, new Float(1.1f),
1688                new Double(1.1d), "string content", new Float(1.1f), new Date() };
1689        for (int i = 0; i < illArgs.length; i++) {
1690            try {
1691                f.format("%c", illArgs[i]);
1692                fail("should throw IllegalFormatConversionException");
1693            } catch (IllegalFormatConversionException e) {
1694                // expected
1695            }
1696        }
1697
1698        try {
1699            f.format("%c", Integer.MAX_VALUE);
1700            fail("should throw IllegalFormatCodePointException");
1701        } catch (IllegalFormatCodePointException e) {
1702            // expected
1703        }
1704
1705        try {
1706            f.format("%#c", 'c');
1707            fail("should throw FormatFlagsConversionMismatchException");
1708        } catch (FormatFlagsConversionMismatchException e) {
1709            // expected
1710        }
1711
1712        final Object[][] triple = {
1713                {'c',               "%c",   "c"},
1714                {'c',               "%-2c", "c "},
1715                {'\u0123',          "%c",   "\u0123"},
1716                {'\u0123',          "%-2c", "\u0123 "},
1717                {(byte) 0x11,       "%c",   "\u0011"},
1718                {(byte) 0x11,       "%-2c", "\u0011 "},
1719                {(short) 0x1111,    "%c",   "\u1111"},
1720                {(short) 0x1111,    "%-2c", "\u1111 "},
1721                {0x11,              "%c",   "\u0011"},
1722                {0x11,              "%-2c", "\u0011 "},
1723        };
1724
1725        final int input   = 0;
1726        final int pattern = 1;
1727        final int output  = 2;
1728        for (int i = 0; i < triple.length; i++) {
1729                f = new Formatter(Locale.US);
1730                f.format((String)triple[i][pattern], triple[i][input]);
1731                assertEquals(triple[i][output], f.toString());
1732        }
1733
1734        f = new Formatter(Locale.US);
1735        f.format("%c", 0x10000);
1736        assertEquals(0x10000, f.toString().codePointAt(0));
1737
1738        try {
1739            f.format("%2.2c", 'c');
1740            fail("should throw IllegalFormatPrecisionException");
1741        } catch (IllegalFormatPrecisionException e) {
1742            // expected
1743        }
1744
1745        f = new Formatter(Locale.US);
1746        f.format("%C", 'w');
1747        // error on RI, throw UnknownFormatConversionException
1748        // RI do not support converter 'C'
1749        assertEquals("W", f.toString());
1750
1751        f = new Formatter(Locale.JAPAN);
1752        f.format("%Ced", 0x1111);
1753        // error on RI, throw UnknownFormatConversionException
1754        // RI do not support converter 'C'
1755        assertEquals("\u1111ed", f.toString());
1756    }
1757
1758
1759
1760    /**
1761     * @tests java.util.Formatter#format(String, Object...) for legal
1762     *        Byte/Short/Integer/Long conversion type 'd'
1763     */
1764    @TestTargetNew(
1765        level = TestLevel.PARTIAL_COMPLETE,
1766        notes = "Doesn't verify exceptions.",
1767        method = "format",
1768        args = {java.lang.String.class, java.lang.Object[].class}
1769    )
1770    public void test_formatLjava_lang_String$Ljava_lang_Object_ByteShortIntegerLongConversionD() {
1771        Locale[] requiredLocales = {Locale.GERMAN};
1772        if (!Support_Locale.areLocalesAvailable(requiredLocales)) {
1773            // locale dependent test, bug 1943269
1774            return;
1775        }
1776        final Object[][] triple = {
1777                { 0,                "%d",                  "0" },
1778                { 0,                "%10d",       "         0" },
1779                { 0,                "%-1d",                "0" },
1780                { 0,                "%+d",                "+0" },
1781                { 0,                "% d",                " 0" },
1782                { 0,                "%,d",                 "0" },
1783                { 0,                "%(d",                 "0" },
1784                { 0,                "%08d",         "00000000" },
1785                { 0,                "%-+,(11d",  "+0         " },
1786                { 0,                "%0 ,(11d",  " 0000000000" },
1787
1788                { (byte) 0xff,      "%d",                 "-1" },
1789                { (byte) 0xff,      "%10d",       "        -1" },
1790                { (byte) 0xff,      "%-1d",               "-1" },
1791                { (byte) 0xff,      "%+d",                "-1" },
1792                { (byte) 0xff,      "% d",                "-1" },
1793                { (byte) 0xff,      "%,d",                "-1" },
1794                { (byte) 0xff,      "%(d",               "(1)" },
1795                { (byte) 0xff,      "%08d",         "-0000001" },
1796                { (byte) 0xff,      "%-+,(11d",  "(1)        " },
1797                { (byte) 0xff,      "%0 ,(11d",  "(000000001)" },
1798
1799                { (short) 0xf123,   "%d",              "-3805" },
1800                { (short) 0xf123,   "%10d",       "     -3805" },
1801                { (short) 0xf123,   "%-1d",            "-3805" },
1802                { (short) 0xf123,   "%+d",             "-3805" },
1803                { (short) 0xf123,   "% d",             "-3805" },
1804                { (short) 0xf123,   "%,d",            "-3.805" },
1805                { (short) 0xf123,   "%(d",            "(3805)" },
1806                { (short) 0xf123,   "%08d",         "-0003805" },
1807                { (short) 0xf123,   "%-+,(11d",  "(3.805)    " },
1808                { (short) 0xf123,   "%0 ,(11d",  "(00003.805)" },
1809
1810                {  0x123456,        "%d",            "1193046" },
1811                {  0x123456,        "%10d",       "   1193046" },
1812                {  0x123456,        "%-1d",          "1193046" },
1813                {  0x123456,        "%+d",          "+1193046" },
1814                {  0x123456,        "% d",          " 1193046" },
1815                {  0x123456,        "%,d",         "1.193.046" },
1816                {  0x123456,        "%(d",           "1193046" },
1817                {  0x123456,        "%08d",         "01193046" },
1818                {  0x123456,        "%-+,(11d",  "+1.193.046 " },
1819                {  0x123456,        "%0 ,(11d",  " 01.193.046" },
1820
1821                { -3,               "%d",                 "-3" },
1822                { -3,               "%10d",       "        -3" },
1823                { -3,               "%-1d",               "-3" },
1824                { -3,               "%+d",                "-3" },
1825                { -3,               "% d",                "-3" },
1826                { -3,               "%,d",                "-3" },
1827                { -3,               "%(d",               "(3)" },
1828                { -3,               "%08d",         "-0000003" },
1829                { -3,               "%-+,(11d",  "(3)        " },
1830                { -3,               "%0 ,(11d",  "(000000003)" },
1831
1832                { 0x7654321L,       "%d",          "124076833" },
1833                { 0x7654321L,       "%10d",       " 124076833" },
1834                { 0x7654321L,       "%-1d",        "124076833" },
1835                { 0x7654321L,       "%+d",        "+124076833" },
1836                { 0x7654321L,       "% d",        " 124076833" },
1837                { 0x7654321L,       "%,d",       "124.076.833" },
1838                { 0x7654321L,       "%(d",         "124076833" },
1839                { 0x7654321L,       "%08d",        "124076833" },
1840                { 0x7654321L,       "%-+,(11d", "+124.076.833" },
1841                { 0x7654321L,       "%0 ,(11d", " 124.076.833" },
1842
1843                { -1L,              "%d",                 "-1" },
1844                { -1L,              "%10d",       "        -1" },
1845                { -1L,              "%-1d",               "-1" },
1846                { -1L,              "%+d",                "-1" },
1847                { -1L,              "% d",                "-1" },
1848                { -1L,              "%,d",                "-1" },
1849                { -1L,              "%(d",               "(1)" },
1850                { -1L,              "%08d",         "-0000001" },
1851                { -1L,              "%-+,(11d",  "(1)        " },
1852                { -1L,              "%0 ,(11d",  "(000000001)" },
1853                };
1854
1855        final int input = 0;
1856        final int pattern = 1;
1857        final int output = 2;
1858        Formatter f;
1859        for (int i = 0; i < triple.length; i++) {
1860            f = new Formatter(Locale.GERMAN);
1861            f.format((String) triple[i][pattern],
1862                    triple[i][input]);
1863            assertEquals("triple[" + i + "]:" + triple[i][input] + ",pattern["
1864                    + i + "]:" + triple[i][pattern], triple[i][output], f
1865                    .toString());
1866        }
1867    }
1868
1869    /**
1870     * @tests java.util.Formatter#format(String, Object...) for legal
1871     *        Byte/Short/Integer/Long conversion type 'o'
1872     */
1873    @TestTargetNew(
1874        level = TestLevel.PARTIAL_COMPLETE,
1875        notes = "Doesn't verify exceptions.",
1876        method = "format",
1877        args = {java.lang.String.class, java.lang.Object[].class}
1878    )
1879    public void test_formatLjava_lang_String$Ljava_lang_Object_ByteShortIntegerLongConversionO() {
1880        final Object[][] triple = {
1881                { 0,                "%o",                 "0" },
1882                { 0,                "%-6o",          "0     " },
1883                { 0,                "%08o",        "00000000" },
1884                { 0,                "%#o",               "00" },
1885                { 0,                "%0#11o",   "00000000000" },
1886                { 0,                "%-#9o",      "00       " },
1887
1888                { (byte) 0xff,      "%o",               "377" },
1889                { (byte) 0xff,      "%-6o",          "377   " },
1890                { (byte) 0xff,      "%08o",        "00000377" },
1891                { (byte) 0xff,      "%#o",             "0377" },
1892                { (byte) 0xff,      "%0#11o",   "00000000377" },
1893                { (byte) 0xff,      "%-#9o",      "0377     " },
1894
1895                { (short) 0xf123,   "%o",            "170443" },
1896                { (short) 0xf123,   "%-6o",          "170443" },
1897                { (short) 0xf123,   "%08o",        "00170443" },
1898                { (short) 0xf123,   "%#o",          "0170443" },
1899                { (short) 0xf123,   "%0#11o",   "00000170443" },
1900                { (short) 0xf123,   "%-#9o",      "0170443  " },
1901
1902                {  0x123456,        "%o",           "4432126" },
1903                {  0x123456,        "%-6o",         "4432126" },
1904                {  0x123456,        "%08o",        "04432126" },
1905                {  0x123456,        "%#o",         "04432126" },
1906                {  0x123456,        "%0#11o",   "00004432126" },
1907                {  0x123456,        "%-#9o",      "04432126 " },
1908
1909                { -3,               "%o",       "37777777775" },
1910                { -3,               "%-6o",     "37777777775" },
1911                { -3,               "%08o",     "37777777775" },
1912                { -3,               "%#o",     "037777777775" },
1913                { -3,               "%0#11o",  "037777777775" },
1914                { -3,               "%-#9o",   "037777777775" },
1915
1916                { 0x7654321L,       "%o",          "731241441" },
1917                { 0x7654321L,       "%-6o",        "731241441" },
1918                { 0x7654321L,       "%08o",        "731241441" },
1919                { 0x7654321L,       "%#o",        "0731241441" },
1920                { 0x7654321L,       "%0#11o",    "00731241441" },
1921                { 0x7654321L,       "%-#9o",      "0731241441" },
1922
1923                { -1L,              "%o",       "1777777777777777777777" },
1924                { -1L,              "%-6o",     "1777777777777777777777" },
1925                { -1L,              "%08o",     "1777777777777777777777" },
1926                { -1L,              "%#o",     "01777777777777777777777" },
1927                { -1L,              "%0#11o",  "01777777777777777777777" },
1928                { -1L,              "%-#9o",   "01777777777777777777777" },
1929                };
1930
1931        final int input = 0;
1932        final int pattern = 1;
1933        final int output = 2;
1934        Formatter f;
1935        for (int i = 0; i < triple.length; i++) {
1936            f = new Formatter(Locale.ITALY);
1937            f.format((String) triple[i][pattern],
1938                    triple[i][input]);
1939            assertEquals("triple[" + i + "]:" + triple[i][input] + ",pattern["
1940                    + i + "]:" + triple[i][pattern], triple[i][output], f
1941                    .toString());
1942        }
1943    }
1944
1945    /**
1946     * @tests java.util.Formatter#format(String, Object...) for legal
1947     *        Byte/Short/Integer/Long conversion type 'x' and 'X'
1948     */
1949    @TestTargetNew(
1950        level = TestLevel.PARTIAL_COMPLETE,
1951        notes = "Doesn't verify exceptions.",
1952        method = "format",
1953        args = {java.lang.String.class, java.lang.Object[].class}
1954    )
1955    public void test_formatLjava_lang_String$Ljava_lang_Object_ByteShortIntegerLongConversionX() {
1956        final Object[][] triple = {
1957                { 0,                "%x",                 "0" },
1958                { 0,                "%-8x",        "0       " },
1959                { 0,                "%06x",          "000000" },
1960                { 0,                "%#x",              "0x0" },
1961                { 0,                "%0#12x",  "0x0000000000" },
1962                { 0,                "%-#9x",      "0x0      " },
1963
1964                { (byte) 0xff,      "%x",                "ff" },
1965                { (byte) 0xff,      "%-8x",        "ff      " },
1966                { (byte) 0xff,      "%06x",          "0000ff" },
1967                { (byte) 0xff,      "%#x",             "0xff" },
1968                { (byte) 0xff,      "%0#12x",  "0x00000000ff" },
1969                { (byte) 0xff,      "%-#9x",      "0xff     " },
1970
1971                { (short) 0xf123,   "%x",              "f123" },
1972                { (short) 0xf123,   "%-8x",        "f123    " },
1973                { (short) 0xf123,   "%06x",          "00f123" },
1974                { (short) 0xf123,   "%#x",           "0xf123" },
1975                { (short) 0xf123,   "%0#12x",  "0x000000f123" },
1976                { (short) 0xf123,   "%-#9x",      "0xf123   " },
1977
1978                {  0x123456,        "%x",            "123456" },
1979                {  0x123456,        "%-8x",        "123456  " },
1980                {  0x123456,        "%06x",          "123456" },
1981                {  0x123456,        "%#x",         "0x123456" },
1982                {  0x123456,        "%0#12x",  "0x0000123456" },
1983                {  0x123456,        "%-#9x",      "0x123456 " },
1984
1985                { -3,               "%x",          "fffffffd" },
1986                { -3,               "%-8x",        "fffffffd" },
1987                { -3,               "%06x",        "fffffffd" },
1988                { -3,               "%#x",       "0xfffffffd" },
1989                { -3,               "%0#12x",  "0x00fffffffd" },
1990                { -3,               "%-#9x",     "0xfffffffd" },
1991
1992                { 0x7654321L,       "%x",          "7654321" },
1993                { 0x7654321L,       "%-8x",       "7654321 " },
1994                { 0x7654321L,       "%06x",        "7654321" },
1995                { 0x7654321L,       "%#x",       "0x7654321" },
1996                { 0x7654321L,       "%0#12x", "0x0007654321" },
1997                { 0x7654321L,       "%-#9x",     "0x7654321" },
1998
1999                { -1L,              "%x",       "ffffffffffffffff" },
2000                { -1L,              "%-8x",     "ffffffffffffffff" },
2001                { -1L,              "%06x",     "ffffffffffffffff" },
2002                { -1L,              "%#x",    "0xffffffffffffffff" },
2003                { -1L,              "%0#12x", "0xffffffffffffffff" },
2004                { -1L,              "%-#9x",  "0xffffffffffffffff" },
2005                };
2006
2007        final int input = 0;
2008        final int pattern = 1;
2009        final int output = 2;
2010        Formatter f;
2011        for (int i = 0; i < triple.length; i++) {
2012            f = new Formatter(Locale.FRANCE);
2013            f.format((String) triple[i][pattern],
2014                    triple[i][input]);
2015            assertEquals("triple[" + i + "]:" + triple[i][input] + ",pattern["
2016                    + i + "]:" + triple[i][pattern], triple[i][output], f
2017                    .toString());
2018
2019            f = new Formatter(Locale.FRANCE);
2020            f.format((String) triple[i][pattern],
2021                    triple[i][input]);
2022            assertEquals("triple[" + i + "]:" + triple[i][input] + ",pattern["
2023                    + i + "]:" + triple[i][pattern], triple[i][output], f
2024                    .toString());
2025        }
2026    }
2027
2028    /**
2029     * @tests java.util.Formatter#format(String, Object...) for Date/Time
2030     *        conversion
2031     */
2032    @TestTargetNew(
2033        level = TestLevel.PARTIAL_COMPLETE,
2034        notes = "",
2035        method = "format",
2036        args = {java.lang.String.class, java.lang.Object[].class}
2037    )
2038    @AndroidOnly("ICU data is different from RI data")
2039    public void test_formatLjava_lang_String$Ljava_lang_Object_DateTimeConversion() {
2040        Locale[] requiredLocales = {Locale.FRANCE, Locale.CHINA, Locale.GERMAN, Locale.US};
2041        if (!Support_Locale.areLocalesAvailable(requiredLocales)) {
2042            // locale dependent test, bug 1943269
2043            return;
2044        }
2045        /*
2046         * Implementation note: For a millisecond date based on Long.MAX_VALUE,
2047         * the RI produces different formatted minutes and seconds than we do.
2048         * Our Calendar does handle these values in the same way (see
2049         * CalendardTest#test_EdgeCases()), so we obviously inherit this
2050         * difference from ICU and simply accept it. We have left the original
2051         * RI values in comments in the following tables. Apart from that, there
2052         * are numerous other differences due to ICU locale data.
2053         */
2054        Formatter f = null;
2055        Date now = new Date(1147327147578L);
2056
2057        Calendar paris = Calendar.getInstance(TimeZone
2058                .getTimeZone("Europe/Paris"), Locale.FRANCE);
2059        paris.set(2006, 4, 8, 12, 0, 0);
2060        paris.set(Calendar.MILLISECOND, 453);
2061        Calendar china = Calendar.getInstance(
2062                TimeZone.getTimeZone("GMT-08:00"), Locale.CHINA);
2063        china.set(2006, 4, 8, 12, 0, 0);
2064        china.set(Calendar.MILLISECOND, 609);
2065
2066        final Object[][] lowerCaseGermanTriple = {
2067                {0L,                        'a', "Do"},  //$NON-NLS-2$
2068                {Long.MAX_VALUE,            'a', "So"},  //$NON-NLS-2$
2069                {-1000L,                    'a', "Do"},  //$NON-NLS-2$
2070                {new Date(1147327147578L),  'a', "Do"},  //$NON-NLS-2$
2071                {paris,                     'a', "Mo"},  //$NON-NLS-2$
2072                {china,                     'a', "Mo"},  //$NON-NLS-2$
2073                {0L,                        'b', "Jan"},  //$NON-NLS-2$
2074                {Long.MAX_VALUE,            'b', "Aug"},  //$NON-NLS-2$
2075                {-1000L,                    'b', "Jan"},  //$NON-NLS-2$
2076                {new Date(1147327147578L),  'b', "Mai"},  //$NON-NLS-2$
2077                {paris,                     'b', "Mai"},  //$NON-NLS-2$
2078                {china,                     'b', "Mai"},  //$NON-NLS-2$
2079                {0L,                        'c', "Do Jan 01 08:00:00 GMT+08:00 1970"},  //$NON-NLS-2$
2080                // {Long.MAX_VALUE,            'c', "So Aug 17 15:12:55 GMT+08:00 292278994"},  //$NON-NLS-2$
2081                {Long.MAX_VALUE,            'c', "So Aug 17 15:18:47 GMT+08:00 292278994"},  //$NON-NLS-2$
2082                {-1000L,                    'c', "Do Jan 01 07:59:59 GMT+08:00 1970"},  //$NON-NLS-2$
2083                {new Date(1147327147578L),  'c', "Do Mai 11 13:59:07 GMT+08:00 2006"},  //$NON-NLS-2$
2084                {paris,                     'c', "Mo Mai 08 12:00:00 MESZ 2006"},  //$NON-NLS-2$
2085                {china,                     'c', "Mo Mai 08 12:00:00 GMT-08:00 2006"},  //$NON-NLS-2$
2086                {0L,                        'd', "01"},  //$NON-NLS-2$
2087                {Long.MAX_VALUE,            'd', "17"},  //$NON-NLS-2$
2088                {-1000L,                    'd', "01"},  //$NON-NLS-2$
2089                {new Date(1147327147578L),  'd', "11"},  //$NON-NLS-2$
2090                {paris,                     'd', "08"},  //$NON-NLS-2$
2091                {china,                     'd', "08"},  //$NON-NLS-2$
2092                {0L,                        'e', "1"},  //$NON-NLS-2$
2093                {Long.MAX_VALUE,            'e', "17"},  //$NON-NLS-2$
2094                {-1000L,                    'e', "1"},  //$NON-NLS-2$
2095                {new Date(1147327147578L),  'e', "11"},  //$NON-NLS-2$
2096                {paris,                     'e', "8"},  //$NON-NLS-2$
2097                {china,                     'e', "8"},  //$NON-NLS-2$
2098                {0L,                        'h', "Jan"},  //$NON-NLS-2$
2099                {Long.MAX_VALUE,            'h', "Aug"},  //$NON-NLS-2$
2100                {-1000L,                    'h', "Jan"},  //$NON-NLS-2$
2101                {new Date(1147327147578L),  'h', "Mai"},  //$NON-NLS-2$
2102                {paris,                     'h', "Mai"},  //$NON-NLS-2$
2103                {china,                     'h', "Mai"},  //$NON-NLS-2$
2104                {0L,                        'j', "001"},  //$NON-NLS-2$
2105                {Long.MAX_VALUE,            'j', "229"},  //$NON-NLS-2$
2106                {-1000L,                    'j', "001"},  //$NON-NLS-2$
2107                {new Date(1147327147578L),  'j', "131"},  //$NON-NLS-2$
2108                {paris,                     'j', "128"},  //$NON-NLS-2$
2109                {china,                     'j', "128"},  //$NON-NLS-2$
2110                {0L,                        'k', "8"},  //$NON-NLS-2$
2111                {Long.MAX_VALUE,            'k', "15"},  //$NON-NLS-2$
2112                {-1000L,                    'k', "7"},  //$NON-NLS-2$
2113                {new Date(1147327147578L),  'k', "13"},  //$NON-NLS-2$
2114                {paris,                     'k', "12"},  //$NON-NLS-2$
2115                {china,                     'k', "12"},  //$NON-NLS-2$
2116                {0L,                        'l', "8"}, //$NON-NLS-2$
2117                {Long.MAX_VALUE,            'l', "3"}, //$NON-NLS-2$
2118                {-1000L,                    'l', "7"}, //$NON-NLS-2$
2119                {new Date(1147327147578L),  'l', "1"}, //$NON-NLS-2$
2120                {paris,                     'l', "12"}, //$NON-NLS-2$
2121                {china,                     'l', "12"}, //$NON-NLS-2$
2122                {0L,                        'm', "01"}, //$NON-NLS-2$
2123                {Long.MAX_VALUE,            'm', "08"}, //$NON-NLS-2$
2124                {-1000L,                    'm', "01"}, //$NON-NLS-2$
2125                {new Date(1147327147578L),  'm', "05"}, //$NON-NLS-2$
2126                {paris,                     'm', "05"}, //$NON-NLS-2$
2127                {china,                     'm', "05"}, //$NON-NLS-2$
2128                {0L,                        'p', "vorm."}, //$NON-NLS-2$
2129                {Long.MAX_VALUE,            'p', "nachm."}, //$NON-NLS-2$
2130                {-1000L,                    'p', "vorm."}, //$NON-NLS-2$
2131                {new Date(1147327147578L),  'p', "nachm."}, //$NON-NLS-2$
2132                {paris,                     'p', "nachm."}, //$NON-NLS-2$
2133                {china,                     'p', "nachm."}, //$NON-NLS-2$
2134                {0L,                        'r', "08:00:00 vorm."}, //$NON-NLS-2$
2135                // {Long.MAX_VALUE,            'r', "03:12:55 PM"}, //$NON-NLS-2$
2136                {Long.MAX_VALUE,            'r', "03:18:47 nachm."}, //$NON-NLS-2$
2137                {-1000L,                    'r', "07:59:59 vorm."}, //$NON-NLS-2$
2138                {new Date(1147327147578L),  'r', "01:59:07 nachm."}, //$NON-NLS-2$
2139                {paris,                     'r', "12:00:00 nachm."}, //$NON-NLS-2$
2140                {china,                     'r', "12:00:00 nachm."}, //$NON-NLS-2$
2141                {0L,                        's', "0"}, //$NON-NLS-2$
2142                {Long.MAX_VALUE,            's', "9223372036854775"}, //$NON-NLS-2$
2143                {-1000L,                    's', "-1"}, //$NON-NLS-2$
2144                {new Date(1147327147578L),  's', "1147327147"}, //$NON-NLS-2$
2145                {paris,                     's', "1147082400"}, //$NON-NLS-2$
2146                {china,                     's', "1147118400"}, //$NON-NLS-2$
2147                {0L,                        'y', "70"}, //$NON-NLS-2$
2148                {Long.MAX_VALUE,            'y', "94"}, //$NON-NLS-2$
2149                {-1000L,                    'y', "70"}, //$NON-NLS-2$
2150                {new Date(1147327147578L),  'y', "06"}, //$NON-NLS-2$
2151                {paris,                     'y', "06"}, //$NON-NLS-2$
2152                {china,                     'y', "06"}, //$NON-NLS-2$
2153                {0L,                        'z', "+0800"}, //$NON-NLS-2$
2154                {Long.MAX_VALUE,            'z', "+0800"}, //$NON-NLS-2$
2155                {-1000L,                    'z', "+0800"}, //$NON-NLS-2$
2156                {new Date(1147327147578L),  'z', "+0800"}, //$NON-NLS-2$
2157                {paris,                     'z', "+0100"}, //$NON-NLS-2$
2158                {china,                     'z', "-0800"}, //$NON-NLS-2$
2159
2160        };
2161
2162        final Object[][] lowerCaseFranceTriple = {
2163                {0L,                        'a', "jeu."}, //$NON-NLS-2$
2164                {Long.MAX_VALUE,            'a', "dim."}, //$NON-NLS-2$
2165                {-1000L,                    'a', "jeu."}, //$NON-NLS-2$
2166                {new Date(1147327147578L),  'a', "jeu."}, //$NON-NLS-2$
2167                {paris,                     'a', "lun."}, //$NON-NLS-2$
2168                {china,                     'a', "lun."}, //$NON-NLS-2$
2169                {0L,                        'b', "janv."}, //$NON-NLS-2$
2170                {Long.MAX_VALUE,            'b', "ao\u00fbt"}, //$NON-NLS-2$
2171                {-1000L,                    'b', "janv."}, //$NON-NLS-2$
2172                {new Date(1147327147578L),  'b', "mai"}, //$NON-NLS-2$
2173                {paris,                     'b', "mai"}, //$NON-NLS-2$
2174                {china,                     'b', "mai"}, //$NON-NLS-2$
2175                {0L,                        'c', "jeu. janv. 01 08:00:00 HMG+08:00 1970"}, //$NON-NLS-2$
2176                // {Long.MAX_VALUE,            'c', "dim. ao\u00fbt 17 15:12:55 HMG+08:00 292278994"}, //$NON-NLS-2$
2177                {Long.MAX_VALUE,            'c', "dim. ao\u00fbt 17 15:18:47 HMG+08:00 292278994"}, //$NON-NLS-2$
2178                {-1000L,                    'c', "jeu. janv. 01 07:59:59 HMG+08:00 1970"}, //$NON-NLS-2$
2179                {new Date(1147327147578L),  'c', "jeu. mai 11 13:59:07 HMG+08:00 2006"}, //$NON-NLS-2$
2180                {paris,                     'c', "lun. mai 08 12:00:00 HAEC 2006"}, //$NON-NLS-2$
2181                {china,                     'c', "lun. mai 08 12:00:00 HMG-08:00 2006"}, //$NON-NLS-2$
2182                {0L,                        'd', "01"}, //$NON-NLS-2$
2183                {Long.MAX_VALUE,            'd', "17"}, //$NON-NLS-2$
2184                {-1000L,                    'd', "01"}, //$NON-NLS-2$
2185                {new Date(1147327147578L),  'd', "11"}, //$NON-NLS-2$
2186                {paris,                     'd', "08"}, //$NON-NLS-2$
2187                {china,                     'd', "08"}, //$NON-NLS-2$
2188                {0L,                        'e', "1"}, //$NON-NLS-2$
2189                {Long.MAX_VALUE,            'e', "17"}, //$NON-NLS-2$
2190                {-1000L,                    'e', "1"}, //$NON-NLS-2$
2191                {new Date(1147327147578L),  'e', "11"}, //$NON-NLS-2$
2192                {paris,                     'e', "8"}, //$NON-NLS-2$
2193                {china,                     'e', "8"}, //$NON-NLS-2$
2194                {0L,                        'h', "janv."}, //$NON-NLS-2$
2195                {Long.MAX_VALUE,            'h', "ao\u00fbt"}, //$NON-NLS-2$
2196                {-1000L,                    'h', "janv."}, //$NON-NLS-2$
2197                {new Date(1147327147578L),  'h', "mai"}, //$NON-NLS-2$
2198                {paris,                     'h', "mai"}, //$NON-NLS-2$
2199                {china,                     'h', "mai"}, //$NON-NLS-2$
2200                {0L,                        'j', "001"}, //$NON-NLS-2$
2201                {Long.MAX_VALUE,            'j', "229"}, //$NON-NLS-2$
2202                {-1000L,                    'j', "001"}, //$NON-NLS-2$
2203                {new Date(1147327147578L),  'j', "131"}, //$NON-NLS-2$
2204                {paris,                     'j', "128"}, //$NON-NLS-2$
2205                {china,                     'j', "128"}, //$NON-NLS-2$
2206                {0L,                        'k', "8"}, //$NON-NLS-2$
2207                {Long.MAX_VALUE,            'k', "15"}, //$NON-NLS-2$
2208                {-1000L,                    'k', "7"}, //$NON-NLS-2$
2209                {new Date(1147327147578L),  'k', "13"}, //$NON-NLS-2$
2210                {paris,                     'k', "12"}, //$NON-NLS-2$
2211                {china,                     'k', "12"}, //$NON-NLS-2$
2212                {0L,                        'l', "8"}, //$NON-NLS-2$
2213                {Long.MAX_VALUE,            'l', "3"}, //$NON-NLS-2$
2214                {-1000L,                    'l', "7"}, //$NON-NLS-2$
2215                {new Date(1147327147578L),  'l', "1"}, //$NON-NLS-2$
2216                {paris,                     'l', "12"}, //$NON-NLS-2$
2217                {china,                     'l', "12"}, //$NON-NLS-2$
2218                {0L,                        'm', "01"}, //$NON-NLS-2$
2219                {Long.MAX_VALUE,            'm', "08"}, //$NON-NLS-2$
2220                {-1000L,                    'm', "01"}, //$NON-NLS-2$
2221                {new Date(1147327147578L),  'm', "05"}, //$NON-NLS-2$
2222                {paris,                     'm', "05"}, //$NON-NLS-2$
2223                {china,                     'm', "05"}, //$NON-NLS-2$
2224                {0L,                        'p', "am"}, //$NON-NLS-2$
2225                {Long.MAX_VALUE,            'p', "pm"}, //$NON-NLS-2$
2226                {-1000L,                    'p', "am"}, //$NON-NLS-2$
2227                {new Date(1147327147578L),  'p', "pm"}, //$NON-NLS-2$
2228                {paris,                     'p', "pm"}, //$NON-NLS-2$
2229                {china,                     'p', "pm"}, //$NON-NLS-2$
2230                {0L,                        'r', "08:00:00 AM"}, //$NON-NLS-2$
2231                // {Long.MAX_VALUE,            'r', "03:12:55 PM"}, //$NON-NLS-2$
2232                {Long.MAX_VALUE,            'r', "03:18:47 PM"}, //$NON-NLS-2$
2233                {-1000L,                    'r', "07:59:59 AM"}, //$NON-NLS-2$
2234                {new Date(1147327147578L),  'r', "01:59:07 PM"}, //$NON-NLS-2$
2235                {paris,                     'r', "12:00:00 PM"}, //$NON-NLS-2$
2236                {china,                     'r', "12:00:00 PM"}, //$NON-NLS-2$
2237                {0L,                        's', "0"}, //$NON-NLS-2$
2238                {Long.MAX_VALUE,            's', "9223372036854775"}, //$NON-NLS-2$
2239                {-1000L,                    's', "-1"}, //$NON-NLS-2$
2240                {new Date(1147327147578L),  's', "1147327147"}, //$NON-NLS-2$
2241                {paris,                     's', "1147082400"}, //$NON-NLS-2$
2242                {china,                     's', "1147118400"}, //$NON-NLS-2$
2243                {0L,                        'y', "70"}, //$NON-NLS-2$
2244                {Long.MAX_VALUE,            'y', "94"}, //$NON-NLS-2$
2245                {-1000L,                    'y', "70"}, //$NON-NLS-2$
2246                {new Date(1147327147578L),  'y', "06"}, //$NON-NLS-2$
2247                {paris,                     'y', "06"}, //$NON-NLS-2$
2248                {china,                     'y', "06"}, //$NON-NLS-2$
2249                {0L,                        'z', "+0800"}, //$NON-NLS-2$
2250                {Long.MAX_VALUE,            'z', "+0800"}, //$NON-NLS-2$
2251                {-1000L,                    'z', "+0800"}, //$NON-NLS-2$
2252                {new Date(1147327147578L),  'z', "+0800"}, //$NON-NLS-2$
2253                {paris,                     'z', "+0100"}, //$NON-NLS-2$
2254                {china,                     'z', "-0800"}, //$NON-NLS-2$
2255
2256        };
2257
2258        final Object[][] lowerCaseCzechTriple = {
2259                {0L,                        'a', "\u010dt"}, //$NON-NLS-2$
2260                {Long.MAX_VALUE,            'a', "ne"}, //$NON-NLS-2$
2261                {-1000L,                    'a', "\u010dt"}, //$NON-NLS-2$
2262                {new Date(1147327147578L),  'a', "\u010dt"}, //$NON-NLS-2$
2263                {paris,                     'a', "po"}, //$NON-NLS-2$
2264                {china,                     'a', "po"}, //$NON-NLS-2$
2265                {0L,                        'b', "1"}, //$NON-NLS-2$
2266                {Long.MAX_VALUE,            'b', "8"}, //$NON-NLS-2$
2267                {-1000L,                    'b', "1"}, //$NON-NLS-2$
2268                {new Date(1147327147578L),  'b', "5"}, //$NON-NLS-2$
2269                {paris,                     'b', "5"}, //$NON-NLS-2$
2270                {china,                     'b', "5"}, //$NON-NLS-2$
2271                {0L,                        'c', "\u010dt 1 01 08:00:00 GMT+08:00 1970"}, //$NON-NLS-2$
2272                // {Long.MAX_VALUE,            'c', "ne 8 17 15:12:55 GMT+08:00 292278994"}, //$NON-NLS-2$
2273                {Long.MAX_VALUE,            'c', "ne 8 17 15:18:47 GMT+08:00 292278994"}, //$NON-NLS-2$
2274                {-1000L,                    'c', "\u010dt 1 01 07:59:59 GMT+08:00 1970"}, //$NON-NLS-2$
2275                {new Date(1147327147578L),  'c', "\u010dt 5 11 13:59:07 GMT+08:00 2006"}, //$NON-NLS-2$
2276                {paris,                     'c', "po 5 08 12:00:00 GMT+02:00 2006"}, //$NON-NLS-2$
2277                {china,                     'c', "po 5 08 12:00:00 GMT-08:00 2006"}, //$NON-NLS-2$
2278                {0L,                        'd', "01"}, //$NON-NLS-2$
2279                {Long.MAX_VALUE,            'd', "17"}, //$NON-NLS-2$
2280                {-1000L,                    'd', "01"}, //$NON-NLS-2$
2281                {new Date(1147327147578L),  'd', "11"}, //$NON-NLS-2$
2282                {paris,                     'd', "08"}, //$NON-NLS-2$
2283                {china,                     'd', "08"}, //$NON-NLS-2$
2284                {0L,                        'e', "1"}, //$NON-NLS-2$
2285                {Long.MAX_VALUE,            'e', "17"}, //$NON-NLS-2$
2286                {-1000L,                    'e', "1"}, //$NON-NLS-2$
2287                {new Date(1147327147578L),  'e', "11"}, //$NON-NLS-2$
2288                {paris,                     'e', "8"}, //$NON-NLS-2$
2289                {china,                     'e', "8"}, //$NON-NLS-2$
2290                {0L,                        'h', "1"}, //$NON-NLS-2$
2291                {Long.MAX_VALUE,            'h', "8"}, //$NON-NLS-2$
2292                {-1000L,                    'h', "1"}, //$NON-NLS-2$
2293                {new Date(1147327147578L),  'h', "5"}, //$NON-NLS-2$
2294                {paris,                     'h', "5"}, //$NON-NLS-2$
2295                {china,                     'h', "5"}, //$NON-NLS-2$
2296                {0L,                        'j', "001"}, //$NON-NLS-2$
2297                {Long.MAX_VALUE,            'j', "229"}, //$NON-NLS-2$
2298                {-1000L,                    'j', "001"}, //$NON-NLS-2$
2299                {new Date(1147327147578L),  'j', "131"}, //$NON-NLS-2$
2300                {paris,                     'j', "128"}, //$NON-NLS-2$
2301                {china,                     'j', "128"}, //$NON-NLS-2$
2302                {0L,                        'k', "8"}, //$NON-NLS-2$
2303                {Long.MAX_VALUE,            'k', "15"}, //$NON-NLS-2$
2304                {-1000L,                    'k', "7"}, //$NON-NLS-2$
2305                {new Date(1147327147578L),  'k', "13"}, //$NON-NLS-2$
2306                {paris,                     'k', "12"}, //$NON-NLS-2$
2307                {china,                     'k', "12"}, //$NON-NLS-2$
2308                {0L,                        'l', "8"}, //$NON-NLS-2$
2309                {Long.MAX_VALUE,            'l', "3"}, //$NON-NLS-2$
2310                {-1000L,                    'l', "7"}, //$NON-NLS-2$
2311                {new Date(1147327147578L),  'l', "1"}, //$NON-NLS-2$
2312                {paris,                     'l', "12"}, //$NON-NLS-2$
2313                {china,                     'l', "12"}, //$NON-NLS-2$
2314                {0L,                        'm', "01"}, //$NON-NLS-2$
2315                {Long.MAX_VALUE,            'm', "08"}, //$NON-NLS-2$
2316                {-1000L,                    'm', "01"}, //$NON-NLS-2$
2317                {new Date(1147327147578L),  'm', "05"}, //$NON-NLS-2$
2318                {paris,                     'm', "05"}, //$NON-NLS-2$
2319                {china,                     'm', "05"}, //$NON-NLS-2$
2320                {0L,                        'p', "dop."}, //$NON-NLS-2$
2321                {Long.MAX_VALUE,            'p', "odp."}, //$NON-NLS-2$
2322                {-1000L,                    'p', "dop."}, //$NON-NLS-2$
2323                {new Date(1147327147578L),  'p', "odp."}, //$NON-NLS-2$
2324                {paris,                     'p', "odp."}, //$NON-NLS-2$
2325                {china,                     'p', "odp."}, //$NON-NLS-2$
2326                {0L,                        'r', "08:00:00 dop."}, //$NON-NLS-2$
2327                // {Long.MAX_VALUE,            'r', "03:12:55 ODP."}, //$NON-NLS-2$
2328                {Long.MAX_VALUE,            'r', "03:18:47 odp."}, //$NON-NLS-2$
2329                {-1000L,                    'r', "07:59:59 dop."}, //$NON-NLS-2$
2330                {new Date(1147327147578L),  'r', "01:59:07 odp."}, //$NON-NLS-2$
2331                {paris,                     'r', "12:00:00 odp."}, //$NON-NLS-2$
2332                {china,                     'r', "12:00:00 odp."}, //$NON-NLS-2$
2333                {0L,                        's', "0"}, //$NON-NLS-2$
2334                {Long.MAX_VALUE,            's', "9223372036854775"}, //$NON-NLS-2$
2335                {-1000L,                    's', "-1"}, //$NON-NLS-2$
2336                {new Date(1147327147578L),  's', "1147327147"}, //$NON-NLS-2$
2337                {paris,                     's', "1147082400"}, //$NON-NLS-2$
2338                {china,                     's', "1147118400"}, //$NON-NLS-2$
2339                {0L,                        'y', "70"}, //$NON-NLS-2$
2340                {Long.MAX_VALUE,            'y', "94"}, //$NON-NLS-2$
2341                {-1000L,                    'y', "70"}, //$NON-NLS-2$
2342                {new Date(1147327147578L),  'y', "06"}, //$NON-NLS-2$
2343                {paris,                     'y', "06"}, //$NON-NLS-2$
2344                {china,                     'y', "06"}, //$NON-NLS-2$
2345                {0L,                        'z', "+0800"}, //$NON-NLS-2$
2346                {Long.MAX_VALUE,            'z', "+0800"}, //$NON-NLS-2$
2347                {-1000L,                    'z', "+0800"}, //$NON-NLS-2$
2348                {new Date(1147327147578L),  'z', "+0800"}, //$NON-NLS-2$
2349                {paris,                     'z', "+0100"}, //$NON-NLS-2$
2350                {china,                     'z', "-0800"}, //$NON-NLS-2$
2351        };
2352
2353        assertEquals(lowerCaseGermanTriple.length, lowerCaseFranceTriple.length);
2354        assertEquals(lowerCaseGermanTriple.length, lowerCaseCzechTriple.length);
2355
2356        final int input   = 0;
2357        final int pattern = 1;
2358        final int output  = 2;
2359        for (int i = 0; i < lowerCaseGermanTriple.length; i++) {
2360            // go through legal conversion
2361            String formatSpecifier = "%t" + lowerCaseGermanTriple[i][pattern]; //$NON-NLS-2$
2362            String formatSpecifierUpper = "%T" + lowerCaseGermanTriple[i][pattern]; //$NON-NLS-2$
2363            // test '%t'
2364            f = new Formatter(Locale.GERMAN);
2365            f.format(formatSpecifier, lowerCaseGermanTriple[i][input]);
2366            assertEquals("Format pattern: " + formatSpecifier //$NON-NLS-2$
2367                            + " Argument: " + lowerCaseGermanTriple[i][pattern], //$NON-NLS-2$
2368                            lowerCaseGermanTriple[i][output], f.toString());
2369
2370            f = new Formatter(Locale.GERMAN);
2371            f.format(Locale.FRANCE, formatSpecifier, lowerCaseFranceTriple[i][input]);
2372            assertEquals("Format pattern: " + formatSpecifier //$NON-NLS-2$
2373                            + " Argument: " + lowerCaseFranceTriple[i][input], //$NON-NLS-2$
2374                            lowerCaseFranceTriple[i][output], f.toString());
2375
2376            f = new Formatter(Locale.GERMAN);
2377            f.format(new Locale("cs", "CZ"), formatSpecifier, lowerCaseCzechTriple[i][input]);
2378            assertEquals("Format pattern: " + formatSpecifier //$NON-NLS-2$
2379                            + " Argument: " + lowerCaseCzechTriple[i][input], //$NON-NLS-2$
2380                            lowerCaseCzechTriple[i][output], f.toString());
2381
2382            // test '%T'
2383            f = new Formatter(Locale.GERMAN);
2384            f.format(formatSpecifierUpper, lowerCaseGermanTriple[i][input]);
2385            assertEquals("Format pattern: " + formatSpecifierUpper //$NON-NLS-2$
2386                            + " Argument: " + lowerCaseGermanTriple[i][input], //$NON-NLS-2$
2387                            ((String)lowerCaseGermanTriple[i][output])
2388                                    .toUpperCase(Locale.US), f.toString());
2389
2390            f = new Formatter(Locale.GERMAN);
2391            f.format(Locale.FRANCE, formatSpecifierUpper, lowerCaseFranceTriple[i][input]);
2392            assertEquals("Format pattern: " + formatSpecifierUpper //$NON-NLS-2$
2393                            + " Argument: " + lowerCaseFranceTriple[i][input], //$NON-NLS-2$
2394                            ((String)lowerCaseFranceTriple[i][output])
2395                                    .toUpperCase(Locale.US), f.toString());
2396
2397            f = new Formatter(Locale.GERMAN);
2398            f.format(new Locale("cs", "CZ"), formatSpecifierUpper, lowerCaseCzechTriple[i][input]);
2399            assertEquals("Format pattern: " + formatSpecifierUpper //$NON-NLS-2$
2400                            + " Argument: " + lowerCaseCzechTriple[i][input], //$NON-NLS-2$
2401                            ((String)lowerCaseCzechTriple[i][output])
2402                                    .toUpperCase(Locale.US), f.toString());
2403        }
2404
2405        final Object[][] upperCaseGermanTriple = {
2406                {0L,                        'A', "Donnerstag"}, //$NON-NLS-2$
2407                {Long.MAX_VALUE,            'A', "Sonntag"}, //$NON-NLS-2$
2408                {-1000L,                    'A', "Donnerstag"}, //$NON-NLS-2$
2409                {new Date(1147327147578L),  'A', "Donnerstag"}, //$NON-NLS-2$
2410                {paris,                     'A', "Montag"}, //$NON-NLS-2$
2411                {china,                     'A', "Montag"}, //$NON-NLS-2$
2412                {0L,                        'B', "Januar"}, //$NON-NLS-2$
2413                {Long.MAX_VALUE,            'B', "August"}, //$NON-NLS-2$
2414                {-1000L,                    'B', "Januar"}, //$NON-NLS-2$
2415                {new Date(1147327147578L),  'B', "Mai"}, //$NON-NLS-2$
2416                {paris,                     'B', "Mai"}, //$NON-NLS-2$
2417                {china,                     'B', "Mai"}, //$NON-NLS-2$
2418                {0L,                        'C', "19"}, //$NON-NLS-2$
2419                {Long.MAX_VALUE,            'C', "2922789"}, //$NON-NLS-2$
2420                {-1000L,                    'C', "19"}, //$NON-NLS-2$
2421                {new Date(1147327147578L),  'C', "20"}, //$NON-NLS-2$
2422                {paris,                     'C', "20"}, //$NON-NLS-2$
2423                {china,                     'C', "20"}, //$NON-NLS-2$
2424                {0L,                        'D', "01/01/70"}, //$NON-NLS-2$
2425                {Long.MAX_VALUE,            'D', "08/17/94"}, //$NON-NLS-2$
2426                {-1000L,                    'D', "01/01/70"}, //$NON-NLS-2$
2427                {new Date(1147327147578L),  'D', "05/11/06"}, //$NON-NLS-2$
2428                {paris,                     'D', "05/08/06"}, //$NON-NLS-2$
2429                {china,                     'D', "05/08/06"}, //$NON-NLS-2$
2430                {0L,                        'F', "1970-01-01"}, //$NON-NLS-2$
2431                {Long.MAX_VALUE,            'F', "292278994-08-17"}, //$NON-NLS-2$
2432                {-1000L,                    'F', "1970-01-01"}, //$NON-NLS-2$
2433                {new Date(1147327147578L),  'F', "2006-05-11"}, //$NON-NLS-2$
2434                {paris,                     'F', "2006-05-08"}, //$NON-NLS-2$
2435                {china,                     'F', "2006-05-08"}, //$NON-NLS-2$
2436                {0L,                        'H', "08"}, //$NON-NLS-2$
2437                {Long.MAX_VALUE,            'H', "15"}, //$NON-NLS-2$
2438                {-1000L,                    'H', "07"}, //$NON-NLS-2$
2439                {new Date(1147327147578L),  'H', "13"}, //$NON-NLS-2$
2440                {paris,                     'H', "12"}, //$NON-NLS-2$
2441                {china,                     'H', "12"}, //$NON-NLS-2$
2442                {0L,                        'I', "08"}, //$NON-NLS-2$
2443                {Long.MAX_VALUE,            'I', "03"}, //$NON-NLS-2$
2444                {-1000L,                    'I', "07"}, //$NON-NLS-2$
2445                {new Date(1147327147578L),  'I', "01"}, //$NON-NLS-2$
2446                {paris,                     'I', "12"}, //$NON-NLS-2$
2447                {china,                     'I', "12"}, //$NON-NLS-2$
2448                {0L,                        'L', "000"}, //$NON-NLS-2$
2449                {Long.MAX_VALUE,            'L', "807"}, //$NON-NLS-2$
2450                {-1000L,                    'L', "000"}, //$NON-NLS-2$
2451                {new Date(1147327147578L),  'L', "578"}, //$NON-NLS-2$
2452                {paris,                     'L', "453"}, //$NON-NLS-2$
2453                {china,                     'L', "609"}, //$NON-NLS-2$
2454                {0L,                        'M', "00"}, //$NON-NLS-2$
2455                // {Long.MAX_VALUE,            'M', "12"}, //$NON-NLS-2$
2456                {Long.MAX_VALUE,            'M', "18"}, //$NON-NLS-2$
2457                {-1000L,                    'M', "59"}, //$NON-NLS-2$
2458                {new Date(1147327147578L),  'M', "59"}, //$NON-NLS-2$
2459                {paris,                     'M', "00"}, //$NON-NLS-2$
2460                {china,                     'M', "00"}, //$NON-NLS-2$
2461                {0L,                        'N', "000000000"}, //$NON-NLS-2$
2462                {Long.MAX_VALUE,            'N', "807000000"}, //$NON-NLS-2$
2463                {-1000L,                    'N', "000000000"}, //$NON-NLS-2$
2464                {new Date(1147327147578L),  'N', "578000000"}, //$NON-NLS-2$
2465                {paris,                     'N', "609000000"}, //$NON-NLS-2$
2466                {china,                     'N', "609000000"}, //$NON-NLS-2$
2467                {0L,                        'Q', "0"}, //$NON-NLS-2$
2468                {Long.MAX_VALUE,            'Q', "9223372036854775807"}, //$NON-NLS-2$
2469                {-1000L,                    'Q', "-1000"}, //$NON-NLS-2$
2470                {new Date(1147327147578L),  'Q', "1147327147578"}, //$NON-NLS-2$
2471                {paris,                     'Q', "1147082400453"}, //$NON-NLS-2$
2472                {china,                     'Q', "1147118400609"}, //$NON-NLS-2$
2473                {0L,                        'R', "08:00"}, //$NON-NLS-2$
2474                // {Long.MAX_VALUE,            'R', "15:12"}, //$NON-NLS-2$
2475                {Long.MAX_VALUE,            'R', "15:18"}, //$NON-NLS-2$
2476                {-1000L,                    'R', "07:59"}, //$NON-NLS-2$
2477                {new Date(1147327147578L),  'R', "13:59"}, //$NON-NLS-2$
2478                {paris,                     'R', "12:00"}, //$NON-NLS-2$
2479                {china,                     'R', "12:00"}, //$NON-NLS-2$
2480                {0L,                        'S', "00"}, //$NON-NLS-2$
2481                // {Long.MAX_VALUE,            'S', "55"}, //$NON-NLS-2$
2482                {Long.MAX_VALUE,            'S', "47"}, //$NON-NLS-2$
2483                {-1000L,                    'S', "59"}, //$NON-NLS-2$
2484                {new Date(1147327147578L),  'S', "07"}, //$NON-NLS-2$
2485                {paris,                     'S', "00"}, //$NON-NLS-2$
2486                {china,                     'S', "00"}, //$NON-NLS-2$
2487                {0L,                        'T', "08:00:00"}, //$NON-NLS-2$
2488                // {Long.MAX_VALUE,            'T', "15:12:55"}, //$NON-NLS-2$
2489                {Long.MAX_VALUE,            'T', "15:18:47"}, //$NON-NLS-2$
2490                {-1000L,                    'T', "07:59:59"}, //$NON-NLS-2$
2491                {new Date(1147327147578L),  'T', "13:59:07"}, //$NON-NLS-2$
2492                {paris,                     'T', "12:00:00"}, //$NON-NLS-2$
2493                {china,                     'T', "12:00:00"}, //$NON-NLS-2$
2494                {0L,                        'Y', "1970"}, //$NON-NLS-2$
2495                {Long.MAX_VALUE,            'Y', "292278994"}, //$NON-NLS-2$
2496                {-1000L,                    'Y', "1970"}, //$NON-NLS-2$
2497                {new Date(1147327147578L),  'Y', "2006"}, //$NON-NLS-2$
2498                {paris,                     'Y', "2006"}, //$NON-NLS-2$
2499                {china,                     'Y', "2006"}, //$NON-NLS-2$
2500                {0L,                        'Z', "GMT+08:00"}, //$NON-NLS-2$
2501                {Long.MAX_VALUE,            'Z', "GMT+08:00"}, //$NON-NLS-2$
2502                {-1000L,                    'Z', "GMT+08:00"}, //$NON-NLS-2$
2503                {new Date(1147327147578L),  'Z', "GMT+08:00"}, //$NON-NLS-2$
2504                {paris,                     'Z', "MESZ"}, //$NON-NLS-2$
2505                {china,                     'Z', "GMT-08:00"}, //$NON-NLS-2$
2506
2507        };
2508
2509        final Object[][] upperCaseFranceTriple = {
2510                {0L,                        'A', "jeudi"}, //$NON-NLS-2$
2511                {Long.MAX_VALUE,            'A', "dimanche"}, //$NON-NLS-2$
2512                {-1000L,                    'A', "jeudi"}, //$NON-NLS-2$
2513                {new Date(1147327147578L),  'A', "jeudi"}, //$NON-NLS-2$
2514                {paris,                     'A', "lundi"}, //$NON-NLS-2$
2515                {china,                     'A', "lundi"}, //$NON-NLS-2$
2516                {0L,                        'B', "janvier"}, //$NON-NLS-2$
2517                {Long.MAX_VALUE,            'B', "ao\u00fbt"}, //$NON-NLS-2$
2518                {-1000L,                    'B', "janvier"}, //$NON-NLS-2$
2519                {new Date(1147327147578L),  'B', "mai"}, //$NON-NLS-2$
2520                {paris,                     'B', "mai"}, //$NON-NLS-2$
2521                {china,                     'B', "mai"}, //$NON-NLS-2$
2522                {0L,                        'C', "19"}, //$NON-NLS-2$
2523                {Long.MAX_VALUE,            'C', "2922789"}, //$NON-NLS-2$
2524                {-1000L,                    'C', "19"}, //$NON-NLS-2$
2525                {new Date(1147327147578L),  'C', "20"}, //$NON-NLS-2$
2526                {paris,                     'C', "20"}, //$NON-NLS-2$
2527                {china,                     'C', "20"}, //$NON-NLS-2$
2528                {0L,                        'D', "01/01/70"}, //$NON-NLS-2$
2529                {Long.MAX_VALUE,            'D', "08/17/94"}, //$NON-NLS-2$
2530                {-1000L,                    'D', "01/01/70"}, //$NON-NLS-2$
2531                {new Date(1147327147578L),  'D', "05/11/06"}, //$NON-NLS-2$
2532                {paris,                     'D', "05/08/06"}, //$NON-NLS-2$
2533                {china,                     'D', "05/08/06"}, //$NON-NLS-2$
2534                {0L,                        'F', "1970-01-01"}, //$NON-NLS-2$
2535                {Long.MAX_VALUE,            'F', "292278994-08-17"}, //$NON-NLS-2$
2536                {-1000L,                    'F', "1970-01-01"}, //$NON-NLS-2$
2537                {new Date(1147327147578L),  'F', "2006-05-11"}, //$NON-NLS-2$
2538                {paris,                     'F', "2006-05-08"}, //$NON-NLS-2$
2539                {china,                     'F', "2006-05-08"}, //$NON-NLS-2$
2540                {0L,                        'H', "08"}, //$NON-NLS-2$
2541                {Long.MAX_VALUE,            'H', "15"}, //$NON-NLS-2$
2542                {-1000L,                    'H', "07"}, //$NON-NLS-2$
2543                {new Date(1147327147578L),  'H', "13"}, //$NON-NLS-2$
2544                {paris,                     'H', "12"}, //$NON-NLS-2$
2545                {china,                     'H', "12"}, //$NON-NLS-2$
2546                {0L,                        'I', "08"}, //$NON-NLS-2$
2547                {Long.MAX_VALUE,            'I', "03"}, //$NON-NLS-2$
2548                {-1000L,                    'I', "07"}, //$NON-NLS-2$
2549                {new Date(1147327147578L),  'I', "01"}, //$NON-NLS-2$
2550                {paris,                     'I', "12"}, //$NON-NLS-2$
2551                {china,                     'I', "12"}, //$NON-NLS-2$
2552                {0L,                        'L', "000"}, //$NON-NLS-2$
2553                {Long.MAX_VALUE,            'L', "807"}, //$NON-NLS-2$
2554                {-1000L,                    'L', "000"}, //$NON-NLS-2$
2555                {new Date(1147327147578L),  'L', "578"}, //$NON-NLS-2$
2556                {paris,                     'L', "453"}, //$NON-NLS-2$
2557                {china,                     'L', "609"}, //$NON-NLS-2$
2558                {0L,                        'M', "00"}, //$NON-NLS-2$
2559                // {Long.MAX_VALUE,            'M', "12"}, //$NON-NLS-2$
2560                {Long.MAX_VALUE,            'M', "18"}, //$NON-NLS-2$
2561                {-1000L,                    'M', "59"}, //$NON-NLS-2$
2562                {new Date(1147327147578L),  'M', "59"}, //$NON-NLS-2$
2563                {paris,                     'M', "00"}, //$NON-NLS-2$
2564                {china,                     'M', "00"}, //$NON-NLS-2$
2565                {0L,                        'N', "000000000"}, //$NON-NLS-2$
2566                {Long.MAX_VALUE,            'N', "807000000"}, //$NON-NLS-2$
2567                {-1000L,                    'N', "000000000"}, //$NON-NLS-2$
2568                {new Date(1147327147578L),  'N', "578000000"}, //$NON-NLS-2$
2569                {paris,                     'N', "453000000"}, //$NON-NLS-2$
2570                {china,                     'N', "468000000"}, //$NON-NLS-2$
2571                {0L,                        'Q', "0"}, //$NON-NLS-2$
2572                {Long.MAX_VALUE,            'Q', "9223372036854775807"}, //$NON-NLS-2$
2573                {-1000L,                    'Q', "-1000"}, //$NON-NLS-2$
2574                {new Date(1147327147578L),  'Q', "1147327147578"}, //$NON-NLS-2$
2575                {paris,                     'Q', "1147082400453"}, //$NON-NLS-2$
2576                {china,                     'Q', "1147118400609"}, //$NON-NLS-2$
2577                {0L,                        'R', "08:00"}, //$NON-NLS-2$
2578                // {Long.MAX_VALUE,            'R', "15:12"}, //$NON-NLS-2$
2579                {Long.MAX_VALUE,            'R', "15:18"}, //$NON-NLS-2$
2580                {-1000L,                    'R', "07:59"}, //$NON-NLS-2$
2581                {new Date(1147327147578L),  'R', "13:59"}, //$NON-NLS-2$
2582                {paris,                     'R', "12:00"}, //$NON-NLS-2$
2583                {china,                     'R', "12:00"}, //$NON-NLS-2$
2584                {0L,                        'S', "00"}, //$NON-NLS-2$
2585                // {Long.MAX_VALUE,            'S', "55"}, //$NON-NLS-2$
2586                {Long.MAX_VALUE,            'S', "47"}, //$NON-NLS-2$
2587                {-1000L,                    'S', "59"}, //$NON-NLS-2$
2588                {new Date(1147327147578L),  'S', "07"}, //$NON-NLS-2$
2589                {paris,                     'S', "00"}, //$NON-NLS-2$
2590                {china,                     'S', "00"}, //$NON-NLS-2$
2591                {0L,                        'T', "08:00:00"}, //$NON-NLS-2$
2592                // {Long.MAX_VALUE,            'T', "15:12:55"}, //$NON-NLS-2$
2593                {Long.MAX_VALUE,            'T', "15:18:47"}, //$NON-NLS-2$
2594                {-1000L,                    'T', "07:59:59"}, //$NON-NLS-2$
2595                {new Date(1147327147578L),  'T', "13:59:07"}, //$NON-NLS-2$
2596                {paris,                     'T', "12:00:00"}, //$NON-NLS-2$
2597                {china,                     'T', "12:00:00"}, //$NON-NLS-2$
2598                {0L,                        'Y', "1970"}, //$NON-NLS-2$
2599                {Long.MAX_VALUE,            'Y', "292278994"}, //$NON-NLS-2$
2600                {-1000L,                    'Y', "1970"}, //$NON-NLS-2$
2601                {new Date(1147327147578L),  'Y', "2006"}, //$NON-NLS-2$
2602                {paris,                     'Y', "2006"}, //$NON-NLS-2$
2603                {china,                     'Y', "2006"}, //$NON-NLS-2$
2604                {0L,                        'Z', "HMG+08:00"}, //$NON-NLS-2$
2605                {Long.MAX_VALUE,            'Z', "HMG+08:00"}, //$NON-NLS-2$
2606                {-1000L,                    'Z', "HMG+08:00"}, //$NON-NLS-2$
2607                {new Date(1147327147578L),  'Z', "HMG+08:00"}, //$NON-NLS-2$
2608                {paris,                     'Z', "HAEC"}, //$NON-NLS-2$
2609                {china,                     'Z', "HMG-08:00"}, //$NON-NLS-2$
2610
2611        };
2612
2613        final Object[][] upperCaseCzechTriple = {
2614                {0L,                        'A', "\u010dtvrtek"}, //$NON-NLS-2$
2615                {Long.MAX_VALUE,            'A', "ned\u011ble"}, //$NON-NLS-2$
2616                {-1000L,                    'A', "\u010dtvrtek"}, //$NON-NLS-2$
2617                {new Date(1147327147578L),  'A', "\u010dtvrtek"}, //$NON-NLS-2$
2618                {paris,                     'A', "pond\u011bl\u00ed"}, //$NON-NLS-2$
2619                {china,                     'A', "pond\u011bl\u00ed"}, //$NON-NLS-2$
2620                {0L,                        'B', "ledna"}, //$NON-NLS-2$
2621                {Long.MAX_VALUE,            'B', "srpna"}, //$NON-NLS-2$
2622                {-1000L,                    'B', "ledna"}, //$NON-NLS-2$
2623                {new Date(1147327147578L),  'B', "kv\u011btna"}, //$NON-NLS-2$
2624                {paris,                     'B', "kv\u011btna"}, //$NON-NLS-2$
2625                {china,                     'B', "kv\u011btna"}, //$NON-NLS-2$
2626                {0L,                        'C', "19"}, //$NON-NLS-2$
2627                {Long.MAX_VALUE,            'C', "2922789"}, //$NON-NLS-2$
2628                {-1000L,                    'C', "19"}, //$NON-NLS-2$
2629                {new Date(1147327147578L),  'C', "20"}, //$NON-NLS-2$
2630                {paris,                     'C', "20"}, //$NON-NLS-2$
2631                {china,                     'C', "20"}, //$NON-NLS-2$
2632                {0L,                        'D', "01/01/70"}, //$NON-NLS-2$
2633                {Long.MAX_VALUE,            'D', "08/17/94"}, //$NON-NLS-2$
2634                {-1000L,                    'D', "01/01/70"}, //$NON-NLS-2$
2635                {new Date(1147327147578L),  'D', "05/11/06"}, //$NON-NLS-2$
2636                {paris,                     'D', "05/08/06"}, //$NON-NLS-2$
2637                {china,                     'D', "05/08/06"}, //$NON-NLS-2$
2638                {0L,                        'F', "1970-01-01"}, //$NON-NLS-2$
2639                {Long.MAX_VALUE,            'F', "292278994-08-17"}, //$NON-NLS-2$
2640                {-1000L,                    'F', "1970-01-01"}, //$NON-NLS-2$
2641                {new Date(1147327147578L),  'F', "2006-05-11"}, //$NON-NLS-2$
2642                {paris,                     'F', "2006-05-08"}, //$NON-NLS-2$
2643                {china,                     'F', "2006-05-08"}, //$NON-NLS-2$
2644                {0L,                        'H', "08"}, //$NON-NLS-2$
2645                {Long.MAX_VALUE,            'H', "15"}, //$NON-NLS-2$
2646                {-1000L,                    'H', "07"}, //$NON-NLS-2$
2647                {new Date(1147327147578L),  'H', "13"}, //$NON-NLS-2$
2648                {paris,                     'H', "12"}, //$NON-NLS-2$
2649                {china,                     'H', "12"}, //$NON-NLS-2$
2650                {0L,                        'I', "08"}, //$NON-NLS-2$
2651                {Long.MAX_VALUE,            'I', "03"}, //$NON-NLS-2$
2652                {-1000L,                    'I', "07"}, //$NON-NLS-2$
2653                {new Date(1147327147578L),  'I', "01"}, //$NON-NLS-2$
2654                {paris,                     'I', "12"}, //$NON-NLS-2$
2655                {china,                     'I', "12"}, //$NON-NLS-2$
2656                {0L,                        'L', "000"}, //$NON-NLS-2$
2657                {Long.MAX_VALUE,            'L', "807"}, //$NON-NLS-2$
2658                {-1000L,                    'L', "000"}, //$NON-NLS-2$
2659                {new Date(1147327147578L),  'L', "578"}, //$NON-NLS-2$
2660                {paris,                     'L', "453"}, //$NON-NLS-2$
2661                {china,                     'L', "609"}, //$NON-NLS-2$
2662                {0L,                        'M', "00"}, //$NON-NLS-2$
2663                // {Long.MAX_VALUE,            'M', "12"}, //$NON-NLS-2$
2664                {Long.MAX_VALUE,            'M', "18"}, //$NON-NLS-2$
2665                {-1000L,                    'M', "59"}, //$NON-NLS-2$
2666                {new Date(1147327147578L),  'M', "59"}, //$NON-NLS-2$
2667                {paris,                     'M', "00"}, //$NON-NLS-2$
2668                {china,                     'M', "00"}, //$NON-NLS-2$
2669                {0L,                        'N', "000000000"}, //$NON-NLS-2$
2670                {Long.MAX_VALUE,            'N', "807000000"}, //$NON-NLS-2$
2671                {-1000L,                    'N', "000000000"}, //$NON-NLS-2$
2672                {new Date(1147327147578L),  'N', "578000000"}, //$NON-NLS-2$
2673                {paris,                     'N', "453000000"}, //$NON-NLS-2$
2674                {china,                     'N', "468000000"}, //$NON-NLS-2$
2675                {0L,                        'Q', "0"}, //$NON-NLS-2$
2676                {Long.MAX_VALUE,            'Q', "9223372036854775807"}, //$NON-NLS-2$
2677                {-1000L,                    'Q', "-1000"}, //$NON-NLS-2$
2678                {new Date(1147327147578L),  'Q', "1147327147578"}, //$NON-NLS-2$
2679                {paris,                     'Q', "1147082400453"}, //$NON-NLS-2$
2680                {china,                     'Q', "1147118400609"}, //$NON-NLS-2$
2681                {0L,                        'R', "08:00"}, //$NON-NLS-2$
2682                // {Long.MAX_VALUE,            'R', "15:12"}, //$NON-NLS-2$
2683                {Long.MAX_VALUE,            'R', "15:18"}, //$NON-NLS-2$
2684                {-1000L,                    'R', "07:59"}, //$NON-NLS-2$
2685                {new Date(1147327147578L),  'R', "13:59"}, //$NON-NLS-2$
2686                {paris,                     'R', "12:00"}, //$NON-NLS-2$
2687                {china,                     'R', "12:00"}, //$NON-NLS-2$
2688                {0L,                        'S', "00"}, //$NON-NLS-2$
2689                // {Long.MAX_VALUE,            'S', "55"}, //$NON-NLS-2$
2690                {Long.MAX_VALUE,            'S', "47"}, //$NON-NLS-2$
2691                {-1000L,                    'S', "59"}, //$NON-NLS-2$
2692                {new Date(1147327147578L),  'S', "07"}, //$NON-NLS-2$
2693                {paris,                     'S', "00"}, //$NON-NLS-2$
2694                {china,                     'S', "00"}, //$NON-NLS-2$
2695                {0L,                        'T', "08:00:00"}, //$NON-NLS-2$
2696                // {Long.MAX_VALUE,            'T', "15:12:55"}, //$NON-NLS-2$
2697                {Long.MAX_VALUE,            'T', "15:18:47"}, //$NON-NLS-2$
2698                {-1000L,                    'T', "07:59:59"}, //$NON-NLS-2$
2699                {new Date(1147327147578L),  'T', "13:59:07"}, //$NON-NLS-2$
2700                {paris,                     'T', "12:00:00"}, //$NON-NLS-2$
2701                {china,                     'T', "12:00:00"}, //$NON-NLS-2$
2702                {0L,                        'Y', "1970"}, //$NON-NLS-2$
2703                {Long.MAX_VALUE,            'Y', "292278994"}, //$NON-NLS-2$
2704                {-1000L,                    'Y', "1970"}, //$NON-NLS-2$
2705                {new Date(1147327147578L),  'Y', "2006"}, //$NON-NLS-2$
2706                {paris,                     'Y', "2006"}, //$NON-NLS-2$
2707                {china,                     'Y', "2006"}, //$NON-NLS-2$
2708                {0L,                        'Z', "GMT+08:00"}, //$NON-NLS-2$
2709                {Long.MAX_VALUE,            'Z', "GMT+08:00"}, //$NON-NLS-2$
2710                {-1000L,                    'Z', "GMT+08:00"}, //$NON-NLS-2$
2711                {new Date(1147327147578L),  'Z', "GMT+08:00"}, //$NON-NLS-2$
2712                {paris,                     'Z', "GMT+02:00"}, //$NON-NLS-2$
2713                {china,                     'Z', "GMT-08:00"}, //$NON-NLS-2$
2714        };
2715
2716        assertEquals(upperCaseGermanTriple.length, upperCaseFranceTriple.length);
2717        assertEquals(upperCaseGermanTriple.length, upperCaseCzechTriple.length);
2718
2719        for (int i = 0; i < upperCaseGermanTriple.length; i++) {
2720            String formatSpecifier = "%t" + upperCaseGermanTriple[i][pattern]; //$NON-NLS-2$
2721            String formatSpecifierUpper = "%T" + upperCaseGermanTriple[i][pattern]; //$NON-NLS-2$
2722                    if ((Character)upperCaseGermanTriple[i][pattern] == 'N') {
2723                        // result can't be predicted on RI, so skip this test
2724                        continue;
2725                    }
2726                    // test '%t'
2727                    f = new Formatter(new Locale("cs", "CZ"));
2728                    f.format(formatSpecifier, upperCaseCzechTriple[i][input]);
2729                    assertEquals("Format pattern: " + formatSpecifier //$NON-NLS-2$
2730                            + " Argument: " + upperCaseCzechTriple[i][input], //$NON-NLS-2$
2731                            upperCaseCzechTriple[i][output], f.toString());
2732
2733                    f = new Formatter(new Locale("cs", "CZ"));
2734                    f.format(Locale.GERMAN, formatSpecifier, upperCaseGermanTriple[i][input]);
2735                    assertEquals("Format pattern: " + formatSpecifier //$NON-NLS-2$
2736                            + " Argument: " + upperCaseGermanTriple[i][input], //$NON-NLS-2$
2737                            upperCaseGermanTriple[i][output], f.toString());
2738
2739                    f = new Formatter(new Locale("cs", "CZ"));
2740                    f.format(Locale.FRANCE, formatSpecifier, upperCaseFranceTriple[i][input]);
2741                    assertEquals("Format pattern: " + formatSpecifier //$NON-NLS-2$
2742                            + " Argument: " + upperCaseFranceTriple[i][input], //$NON-NLS-2$
2743                            upperCaseFranceTriple[i][output], f.toString());
2744
2745                    // test '%T'
2746                    f = new Formatter(Locale.GERMAN);
2747                    f.format(formatSpecifierUpper, upperCaseGermanTriple[i][input]);
2748                    assertEquals("Format pattern: " + formatSpecifierUpper //$NON-NLS-2$
2749                            + " Argument: " + upperCaseGermanTriple[i][input], //$NON-NLS-2$
2750                            ((String)upperCaseGermanTriple[i][output])
2751                                    .toUpperCase(Locale.US), f.toString());
2752
2753                    f = new Formatter(Locale.GERMAN);
2754                    f.format(new Locale("cs", "CZ"), formatSpecifierUpper, upperCaseCzechTriple[i][input]);
2755                    assertEquals("Format pattern: " + formatSpecifierUpper //$NON-NLS-2$
2756                            + " Argument: " + upperCaseCzechTriple[i][input], //$NON-NLS-2$
2757                            ((String)upperCaseCzechTriple[i][output])
2758                                    .toUpperCase(Locale.US), f.toString());
2759
2760                    f = new Formatter(Locale.GERMAN);
2761                    f.format(Locale.FRANCE, formatSpecifierUpper, upperCaseFranceTriple[i][input]);
2762                    assertEquals("Format pattern: " + formatSpecifierUpper //$NON-NLS-2$
2763                            + " Argument: " + upperCaseFranceTriple[i][input], //$NON-NLS-2$
2764                            ((String)upperCaseFranceTriple[i][output])
2765                                    .toUpperCase(Locale.US), f.toString());
2766        }
2767
2768        f = new Formatter(Locale.US);
2769        f.format("%-10ta", now); //$NON-NLS-2$
2770        assertEquals("Thu       ", f.toString()); //$NON-NLS-2$
2771
2772        f = new Formatter(Locale.US);
2773        f.format("%10000000000000000000000000000000001ta", now); //$NON-NLS-2$
2774        assertEquals("Thu", f.toString().trim()); //$NON-NLS-2$
2775    }
2776
2777    /**
2778     * @tests java.util.Formatter#format(String, Object...) for null argment for
2779     *        Byte/Short/Integer/Long/BigInteger conversion
2780     */
2781    @TestTargetNew(
2782        level = TestLevel.PARTIAL_COMPLETE,
2783        notes = "Verifies null as the second parameter.",
2784        method = "format",
2785        args = {java.lang.String.class, java.lang.Object[].class}
2786    )
2787    public void test_formatLjava_lang_String$Ljava_lang_Object_ByteShortIntegerLongNullConversion() {
2788
2789        Formatter f = new Formatter(Locale.FRANCE);
2790        f.format("%d%<o%<x%<5X", (Integer) null);
2791        assertEquals("nullnullnull NULL", f.toString());
2792
2793        f = new Formatter(Locale.GERMAN);
2794        f.format("%d%<#03o %<0#4x%<6X", (Long) null);
2795        assertEquals("nullnull null  NULL", f.toString());
2796
2797        f = new Formatter(Locale.GERMAN);
2798        f.format("%(+,07d%<o %<x%<6X", (Byte) null);
2799        assertEquals("   nullnull null  NULL", f.toString());
2800
2801        f = new Formatter(Locale.ITALY);
2802        f.format("%(+,07d%<o %<x%<0#6X", (Short) null);
2803        assertEquals("   nullnull null  NULL", f.toString());
2804
2805        f = new Formatter(Locale.GERMAN);
2806        f.format("%(+,-7d%<( o%<+(x %<( 06X", (BigInteger) null);
2807        assertEquals("null   nullnull   NULL", f.toString());
2808    }
2809
2810    /**
2811     * @tests java.util.Formatter#format(String, Object...) for legal
2812     *        BigInteger conversion type 'd'
2813     */
2814    @TestTargetNew(
2815        level = TestLevel.PARTIAL_COMPLETE,
2816        notes = "Flaky! results. differs if debugger is attached." +
2817                "Unchecked IllegalFormatException, FormatterClosedException.",
2818        method = "format",
2819        args = {java.lang.String.class, java.lang.Object[].class}
2820    )
2821    public void test_formatLjava_lang_String$LBigInteger() {
2822        Locale[] requiredLocales = {Locale.ITALY, Locale.GERMAN, Locale.FRANCE};
2823        if (!Support_Locale.areLocalesAvailable(requiredLocales)) {
2824            // locale dependent test, bug 1943269
2825            return;
2826        }
2827        final Object[][] tripleD = {
2828                {new BigInteger("123456789012345678901234567890"),          "%d",       "123456789012345678901234567890"}, //$NON-NLS-2$
2829                {new BigInteger("123456789012345678901234567890"),          "%10d",     "123456789012345678901234567890"}, //$NON-NLS-2$
2830                {new BigInteger("123456789012345678901234567890"),          "%-1d",     "123456789012345678901234567890"}, //$NON-NLS-2$
2831                {new BigInteger("123456789012345678901234567890"),          "%+d",      "+123456789012345678901234567890"}, //$NON-NLS-2$
2832                {new BigInteger("123456789012345678901234567890"),          "% d",      " 123456789012345678901234567890"}, //$NON-NLS-2$
2833                {new BigInteger("123456789012345678901234567890"),          "%,d",      "123.456.789.012.345.678.901.234.567.890"}, //$NON-NLS-2$
2834                {new BigInteger("123456789012345678901234567890"),          "%(d",      "123456789012345678901234567890"}, //$NON-NLS-2$
2835                {new BigInteger("123456789012345678901234567890"),          "%08d",     "123456789012345678901234567890"}, //$NON-NLS-2$
2836                {new BigInteger("123456789012345678901234567890"),          "%-+,(11d", "+123.456.789.012.345.678.901.234.567.890"}, //$NON-NLS-2$
2837                {new BigInteger("123456789012345678901234567890"),          "%0 ,(11d", " 123.456.789.012.345.678.901.234.567.890"}, //$NON-NLS-2$
2838                {new BigInteger("-9876543210987654321098765432100000"),     "%d",       "-9876543210987654321098765432100000"}, //$NON-NLS-2$
2839                {new BigInteger("-9876543210987654321098765432100000"),     "%10d",     "-9876543210987654321098765432100000"}, //$NON-NLS-2$
2840                {new BigInteger("-9876543210987654321098765432100000"),     "%-1d",     "-9876543210987654321098765432100000"}, //$NON-NLS-2$
2841                {new BigInteger("-9876543210987654321098765432100000"),     "%+d",      "-9876543210987654321098765432100000"}, //$NON-NLS-2$
2842                {new BigInteger("-9876543210987654321098765432100000"),     "% d",      "-9876543210987654321098765432100000"}, //$NON-NLS-2$
2843                {new BigInteger("-9876543210987654321098765432100000"),     "%,d",      "-9.876.543.210.987.654.321.098.765.432.100.000"}, //$NON-NLS-2$
2844                {new BigInteger("-9876543210987654321098765432100000"),     "%(d",      "(9876543210987654321098765432100000)"}, //$NON-NLS-2$
2845                {new BigInteger("-9876543210987654321098765432100000"),     "%08d",     "-9876543210987654321098765432100000"}, //$NON-NLS-2$
2846                {new BigInteger("-9876543210987654321098765432100000"),     "%-+,(11d", "(9.876.543.210.987.654.321.098.765.432.100.000)"}, //$NON-NLS-2$
2847                {new BigInteger("-9876543210987654321098765432100000"),     "%0 ,(11d", "(9.876.543.210.987.654.321.098.765.432.100.000)"}, //$NON-NLS-2$
2848        };
2849
2850        final int input = 0;
2851        final int pattern = 1;
2852        final int output = 2;
2853        Formatter f;
2854        for (int i = 0; i < tripleD.length; i++) {
2855            f = new Formatter(Locale.GERMAN);
2856            f.format((String) tripleD[i][pattern],
2857                    tripleD[i][input]);
2858            assertEquals("triple[" + i + "]:" + tripleD[i][input] + ",pattern["
2859                    + i + "]:" + tripleD[i][pattern], tripleD[i][output], f
2860                    .toString());
2861
2862        }
2863
2864        final Object[][] tripleO = {
2865                {new BigInteger("123456789012345678901234567890"),          "%o",       "143564417755415637016711617605322"}, //$NON-NLS-2$
2866                {new BigInteger("123456789012345678901234567890"),          "%-6o",     "143564417755415637016711617605322"}, //$NON-NLS-2$
2867                {new BigInteger("123456789012345678901234567890"),          "%08o",     "143564417755415637016711617605322"}, //$NON-NLS-2$
2868                {new BigInteger("123456789012345678901234567890"),          "%#o",      "0143564417755415637016711617605322"}, //$NON-NLS-2$
2869                {new BigInteger("123456789012345678901234567890"),          "%0#11o",   "0143564417755415637016711617605322"}, //$NON-NLS-2$
2870                {new BigInteger("123456789012345678901234567890"),          "%-#9o",    "0143564417755415637016711617605322"}, //$NON-NLS-2$
2871                {new BigInteger("-9876543210987654321098765432100000"),     "%o",       "-36336340043453651353467270113157312240"}, //$NON-NLS-2$
2872                {new BigInteger("-9876543210987654321098765432100000"),     "%-6o",     "-36336340043453651353467270113157312240"}, //$NON-NLS-2$
2873                {new BigInteger("-9876543210987654321098765432100000"),     "%08o",     "-36336340043453651353467270113157312240"}, //$NON-NLS-2$
2874                {new BigInteger("-9876543210987654321098765432100000"),     "%#o",      "-036336340043453651353467270113157312240"}, //$NON-NLS-2$
2875                {new BigInteger("-9876543210987654321098765432100000"),     "%0#11o",   "-036336340043453651353467270113157312240"}, //$NON-NLS-2$
2876                {new BigInteger("-9876543210987654321098765432100000"),     "%-#9o",    "-036336340043453651353467270113157312240"}, //$NON-NLS-2$
2877        };
2878        for (int i = 0; i < tripleO.length; i++) {
2879            f = new Formatter(Locale.ITALY);
2880            f.format((String) tripleO[i][pattern],
2881                    tripleO[i][input]);
2882            assertEquals("triple[" + i + "]:" + tripleO[i][input] + ",pattern["
2883                    + i + "]:" + tripleO[i][pattern], tripleO[i][output], f
2884                    .toString());
2885
2886        }
2887
2888        final Object[][] tripleX = {
2889                {new BigInteger("123456789012345678901234567890"),          "%x",       "18ee90ff6c373e0ee4e3f0ad2"}, //$NON-NLS-2$
2890                {new BigInteger("123456789012345678901234567890"),          "%-8x",     "18ee90ff6c373e0ee4e3f0ad2"}, //$NON-NLS-2$
2891                {new BigInteger("123456789012345678901234567890"),          "%06x",     "18ee90ff6c373e0ee4e3f0ad2"}, //$NON-NLS-2$
2892                {new BigInteger("123456789012345678901234567890"),          "%#x",      "0x18ee90ff6c373e0ee4e3f0ad2"}, //$NON-NLS-2$
2893                {new BigInteger("123456789012345678901234567890"),          "%0#12x",   "0x18ee90ff6c373e0ee4e3f0ad2"}, //$NON-NLS-2$
2894                {new BigInteger("123456789012345678901234567890"),          "%-#9x",    "0x18ee90ff6c373e0ee4e3f0ad2"}, //$NON-NLS-2$
2895                {new BigInteger("-9876543210987654321098765432100000"),     "%x",       "-1e6f380472bd4bae6eb8259bd94a0"}, //$NON-NLS-2$
2896                {new BigInteger("-9876543210987654321098765432100000"),     "%-8x",     "-1e6f380472bd4bae6eb8259bd94a0"}, //$NON-NLS-2$
2897                {new BigInteger("-9876543210987654321098765432100000"),     "%06x",     "-1e6f380472bd4bae6eb8259bd94a0"}, //$NON-NLS-2$
2898                {new BigInteger("-9876543210987654321098765432100000"),     "%#x",      "-0x1e6f380472bd4bae6eb8259bd94a0"}, //$NON-NLS-2$
2899                {new BigInteger("-9876543210987654321098765432100000"),     "%0#12x",   "-0x1e6f380472bd4bae6eb8259bd94a0"}, //$NON-NLS-2$
2900                {new BigInteger("-9876543210987654321098765432100000"),     "%-#9x",    "-0x1e6f380472bd4bae6eb8259bd94a0"}, //$NON-NLS-2$
2901        };
2902
2903        for (int i = 0; i < tripleX.length; i++) {
2904            f = new Formatter(Locale.FRANCE);
2905            f.format((String) tripleX[i][pattern],
2906                    tripleX[i][input]);
2907            assertEquals("triple[" + i + "]:" + tripleX[i][input] + ",pattern["
2908                    + i + "]:" + tripleX[i][pattern], tripleX[i][output], f
2909                    .toString());
2910
2911        }
2912
2913        f = new Formatter(Locale.GERMAN);
2914        f.format("%(+,-7d%<( o%<+(x %<( 06X", (BigInteger) null);
2915        assertEquals("null   nullnull   NULL", f.toString());
2916    }
2917
2918    /**
2919     * @tests java.util.Formatter#format(String, Object...) for padding of
2920     *        BigInteger conversion
2921     */
2922    @TestTargetNew(
2923        level = TestLevel.PARTIAL_COMPLETE,
2924        notes = "Verifies boundary conditions.",
2925        method = "format",
2926        args = {java.lang.String.class, java.lang.Object[].class}
2927    )
2928    public void test_formatLjava_lang_String$Ljava_lang_Object_BigIntegerPaddingConversion() {
2929        Locale[] requiredLocales = {Locale.GERMAN};
2930        if (!Support_Locale.areLocalesAvailable(requiredLocales)) {
2931            // locale dependent test, bug 1943269
2932            return;
2933        }
2934        Formatter f = null;
2935
2936        BigInteger bigInt = new BigInteger("123456789012345678901234567890");
2937        f = new Formatter(Locale.GERMAN);
2938        f.format("%32d", bigInt);
2939        assertEquals("  123456789012345678901234567890", f.toString());
2940
2941        f = new Formatter(Locale.GERMAN);
2942        f.format("%+32x", bigInt);
2943        assertEquals("      +18ee90ff6c373e0ee4e3f0ad2", f.toString());
2944
2945        f = new Formatter(Locale.GERMAN);
2946        f.format("% 32o", bigInt);
2947        assertEquals(" 143564417755415637016711617605322", f.toString());
2948
2949        BigInteger negBigInt = new BigInteger(
2950                "-1234567890123456789012345678901234567890");
2951        f = new Formatter(Locale.GERMAN);
2952        f.format("%( 040X", negBigInt);
2953        assertEquals("(000003A0C92075C0DBF3B8ACBC5F96CE3F0AD2)", f.toString());
2954
2955        f = new Formatter(Locale.GERMAN);
2956        f.format("%+(045d", negBigInt);
2957        assertEquals("(0001234567890123456789012345678901234567890)", f
2958                .toString());
2959
2960        f = new Formatter(Locale.GERMAN);
2961        f.format("%+,-(60d", negBigInt);
2962        assertEquals(
2963                "(1.234.567.890.123.456.789.012.345.678.901.234.567.890)     ",
2964                f.toString());
2965    }
2966
2967    /**
2968     * @tests java.util.Formatter#format(String, Object...) for BigInteger
2969     *        conversion exception
2970     */
2971    @TestTargetNew(
2972        level = TestLevel.PARTIAL_COMPLETE,
2973        notes = "Verifies exceptions.",
2974        method = "format",
2975        args = {java.lang.String.class, java.lang.Object[].class}
2976    )
2977    public void test_formatLjava_lang_String$Ljava_lang_Object_BigIntegerConversionException() {
2978        Formatter f = null;
2979
2980        final String[] flagsConversionMismatches = { "%#d", "%,o", "%,x", "%,X" };
2981        for (int i = 0; i < flagsConversionMismatches.length; i++) {
2982            try {
2983                f = new Formatter(Locale.CHINA);
2984                f.format(flagsConversionMismatches[i], new BigInteger("1"));
2985                fail("should throw FormatFlagsConversionMismatchException");
2986            } catch (FormatFlagsConversionMismatchException e) {
2987                // expected
2988            }
2989        }
2990
2991        final String[] missingFormatWidths = { "%-0d", "%0d", "%-d", "%-0o",
2992                "%0o", "%-o", "%-0x", "%0x", "%-x", "%-0X", "%0X", "%-X" };
2993        for (int i = 0; i < missingFormatWidths.length; i++) {
2994            try {
2995                f = new Formatter(Locale.KOREA);
2996                f.format(missingFormatWidths[i], new BigInteger("1"));
2997                fail("should throw MissingFormatWidthException");
2998            } catch (MissingFormatWidthException e) {
2999                // expected
3000            }
3001        }
3002
3003        final String[] illFlags = { "%+ d", "%-08d", "%+ o", "%-08o", "%+ x",
3004                "%-08x", "%+ X", "%-08X" };
3005        for (int i = 0; i < illFlags.length; i++) {
3006            try {
3007                f = new Formatter(Locale.CANADA);
3008                f.format(illFlags[i], new BigInteger("1"));
3009                fail("should throw IllegalFormatFlagsException");
3010            } catch (IllegalFormatFlagsException e) {
3011                // expected
3012            }
3013        }
3014
3015        final String[] precisionExceptions = { "%.4d", "%2.5o", "%8.6x",
3016                "%11.17X" };
3017        for (int i = 0; i < precisionExceptions.length; i++) {
3018            try {
3019                f = new Formatter(Locale.US);
3020                f.format(precisionExceptions[i], new BigInteger("1"));
3021                fail("should throw IllegalFormatPrecisionException");
3022            } catch (IllegalFormatPrecisionException e) {
3023                // expected
3024            }
3025        }
3026
3027        f = new Formatter(Locale.US);
3028        try {
3029            f.format("%D", new BigInteger("1"));
3030            fail("should throw UnknownFormatConversionException");
3031        } catch (UnknownFormatConversionException e) {
3032            // expected
3033        }
3034
3035        f = new Formatter(Locale.US);
3036        try {
3037            f.format("%O", new BigInteger("1"));
3038            fail("should throw UnknownFormatConversionException");
3039        } catch (UnknownFormatConversionException e) {
3040            // expected
3041        }
3042
3043        try {
3044            f = new Formatter();
3045            f.format("%010000000000000000000000000000000001d", new BigInteger(
3046                    "1"));
3047            fail("should throw MissingFormatWidthException");
3048        } catch (MissingFormatWidthException e) {
3049            // expected
3050        }
3051    }
3052
3053    /**
3054     * @tests java.util.Formatter#format(String, Object...) for BigInteger
3055     *        exception throwing order
3056     */
3057    @TestTargetNew(
3058        level = TestLevel.PARTIAL_COMPLETE,
3059        notes = "Verifies exceptions.",
3060        method = "format",
3061        args = {java.lang.String.class, java.lang.Object[].class}
3062    )
3063    public void test_formatLjava_lang_String$Ljava_lang_Object_BigIntegerExceptionOrder() {
3064        Formatter f = null;
3065        BigInteger big = new BigInteger("100");
3066
3067        /*
3068         * Order summary: UnknownFormatConversionException >
3069         * MissingFormatWidthException > IllegalFormatFlagsException >
3070         * IllegalFormatPrecisionException > IllegalFormatConversionException >
3071         * FormatFlagsConversionMismatchException
3072         *
3073         */
3074        f = new Formatter(Locale.US);
3075        try {
3076            // compare IllegalFormatConversionException and
3077            // FormatFlagsConversionMismatchException
3078            f.format("%(o", false);
3079            fail("should throw IllegalFormatConversionException");
3080        } catch (IllegalFormatConversionException e) {
3081            // expected
3082        }
3083
3084        try {
3085            // compare IllegalFormatPrecisionException and
3086            // IllegalFormatConversionException
3087            f.format("%.4o", false);
3088            fail("should throw IllegalFormatPrecisionException");
3089        } catch (IllegalFormatPrecisionException e) {
3090            // expected
3091        }
3092
3093        try {
3094            // compare IllegalFormatFlagsException and
3095            // IllegalFormatPrecisionException
3096            f.format("%+ .4o", big);
3097            fail("should throw IllegalFormatFlagsException");
3098        } catch (IllegalFormatFlagsException e) {
3099            // expected
3100        }
3101
3102        try {
3103            // compare MissingFormatWidthException and
3104            // IllegalFormatFlagsException
3105            f.format("%+ -o", big);
3106            fail("should throw MissingFormatWidthException");
3107        } catch (MissingFormatWidthException e) {
3108            // expected
3109        }
3110
3111        try {
3112            // compare UnknownFormatConversionException and
3113            // MissingFormatWidthException
3114            f.format("%-O", big);
3115            fail("should throw UnknownFormatConversionException");
3116        } catch (UnknownFormatConversionException e) {
3117            // expected
3118        }
3119    }
3120
3121    /**
3122     * @tests java.util.Formatter#format(String, Object...) for Float/Double
3123     *        conversion type 'e' and 'E'
3124     */
3125    @TestTargetNew(
3126        level = TestLevel.PARTIAL_COMPLETE,
3127        notes = "Doesn't verify exceptions.",
3128        method = "format",
3129        args = {java.lang.String.class, java.lang.Object[].class}
3130    )
3131    @KnownFailure("Formatting of Double.MIN_VALUE works improperly")
3132    @AndroidOnly("last case fails on RI. See comment below")
3133    public void test_formatLjava_lang_String$Ljava_lang_Object_FloatConversionE() {
3134        Formatter f = null;
3135        final Object[][] tripleE = {
3136                {0f, "%e",          "0.000000e+00"},
3137                {0f, "%#.0e",       "0.e+00"},
3138                {0f, "%#- (9.8e",   " 0.00000000e+00"},
3139                {0f, "%#+0(8.4e",   "+0.0000e+00"},
3140                {0f, "%-+(1.6e",    "+0.000000e+00"},
3141                {0f, "% 0(12e",     " 0.000000e+00"},
3142
3143                {101f, "%e",          "1.010000e+02"},
3144                {101f, "%#.0e",       "1.e+02"},
3145                {101f, "%#- (9.8e",   " 1.01000000e+02"},
3146                {101f, "%#+0(8.4e",   "+1.0100e+02"},
3147                {101f, "%-+(1.6e",    "+1.010000e+02"},
3148                {101f, "% 0(12e",     " 1.010000e+02"},
3149
3150                {1.f, "%e",          "1.000000e+00"},
3151                {1.f, "%#.0e",       "1.e+00"},
3152                {1.f, "%#- (9.8e",   " 1.00000000e+00"},
3153                {1.f, "%#+0(8.4e",   "+1.0000e+00"},
3154                {1.f, "%-+(1.6e",    "+1.000000e+00"},
3155                {1.f, "% 0(12e",     " 1.000000e+00"},
3156
3157                {-98f, "%e",          "-9.800000e+01"},
3158                {-98f, "%#.0e",       "-1.e+02"},
3159                {-98f, "%#- (9.8e",   "(9.80000000e+01)"},
3160                {-98f, "%#+0(8.4e",   "(9.8000e+01)"},
3161                {-98f, "%-+(1.6e",    "(9.800000e+01)"},
3162                {-98f, "% 0(12e",     "(9.800000e+01)"},
3163
3164                {1.23f, "%e",          "1.230000e+00"},
3165                {1.23f, "%#.0e",       "1.e+00"},
3166                {1.23f, "%#- (9.8e",   " 1.23000002e+00"},
3167                {1.23f, "%#+0(8.4e",   "+1.2300e+00"},
3168                {1.23f, "%-+(1.6e",    "+1.230000e+00"},
3169                {1.23f, "% 0(12e",     " 1.230000e+00"},
3170
3171                {34.1234567f, "%e",          "3.412346e+01"},
3172                {34.1234567f, "%#.0e",       "3.e+01"},
3173                {34.1234567f, "%#- (9.8e",   " 3.41234550e+01"},
3174                {34.1234567f, "%#+0(8.4e",   "+3.4123e+01"},
3175                {34.1234567f, "%-+(1.6e",    "+3.412346e+01"},
3176                {34.1234567f, "% 0(12e",     " 3.412346e+01"},
3177
3178                {-.12345f, "%e",          "-1.234500e-01"},
3179                {-.12345f, "%#.0e",       "-1.e-01"},
3180                {-.12345f, "%#- (9.8e",   "(1.23450004e-01)"},
3181                {-.12345f, "%#+0(8.4e",   "(1.2345e-01)"},
3182                {-.12345f, "%-+(1.6e",    "(1.234500e-01)"},
3183                {-.12345f, "% 0(12e",     "(1.234500e-01)"},
3184
3185                {-9876.1234567f, "%e",          "-9.876123e+03"},
3186                {-9876.1234567f, "%#.0e",       "-1.e+04"},
3187                {-9876.1234567f, "%#- (9.8e",   "(9.87612305e+03)"},
3188                {-9876.1234567f, "%#+0(8.4e",   "(9.8761e+03)"},
3189                {-9876.1234567f, "%-+(1.6e",    "(9.876123e+03)"},
3190                {-9876.1234567f, "% 0(12e",     "(9.876123e+03)"},
3191
3192                {Float.MAX_VALUE, "%e",          "3.402823e+38"},
3193                {Float.MAX_VALUE, "%#.0e",       "3.e+38"},
3194                {Float.MAX_VALUE, "%#- (9.8e",   " 3.40282347e+38"},
3195                {Float.MAX_VALUE, "%#+0(8.4e",   "+3.4028e+38"},
3196                {Float.MAX_VALUE, "%-+(1.6e",    "+3.402823e+38"},
3197                {Float.MAX_VALUE, "% 0(12e",     " 3.402823e+38"},
3198
3199                {Float.MIN_VALUE, "%e",          "1.401298e-45"},
3200                {Float.MIN_VALUE, "%#.0e",       "1.e-45"},
3201                {Float.MIN_VALUE, "%#- (9.8e",   " 1.40129846e-45"},
3202                {Float.MIN_VALUE, "%#+0(8.4e",   "+1.4013e-45"},
3203                {Float.MIN_VALUE, "%-+(1.6e",    "+1.401298e-45"},
3204                {Float.MIN_VALUE, "% 0(12e",     " 1.401298e-45"},
3205
3206                {Float.NaN, "%e",          "NaN"},
3207                {Float.NaN, "%#.0e",       "NaN"},
3208                {Float.NaN, "%#- (9.8e",   "NaN      "},
3209                {Float.NaN, "%#+0(8.4e",   "     NaN"},
3210                {Float.NaN, "%-+(1.6e",    "NaN"},
3211                {Float.NaN, "% 0(12e",     "         NaN"},
3212
3213
3214                {Float.NEGATIVE_INFINITY, "%e",          "-Infinity"},
3215                {Float.NEGATIVE_INFINITY, "%#.0e",       "-Infinity"},
3216                {Float.NEGATIVE_INFINITY, "%#- (9.8e",   "(Infinity)"},
3217                {Float.NEGATIVE_INFINITY, "%#+0(8.4e",   "(Infinity)"},
3218                {Float.NEGATIVE_INFINITY, "%-+(1.6e",    "(Infinity)"},
3219                {Float.NEGATIVE_INFINITY, "% 0(12e",     "  (Infinity)"},
3220
3221                {Float.NEGATIVE_INFINITY, "%e",          "-Infinity"},
3222                {Float.NEGATIVE_INFINITY, "%#.0e",       "-Infinity"},
3223                {Float.NEGATIVE_INFINITY, "%#- (9.8e",   "(Infinity)"},
3224                {Float.NEGATIVE_INFINITY, "%#+0(8.4e",   "(Infinity)"},
3225                {Float.NEGATIVE_INFINITY, "%-+(1.6e",    "(Infinity)"},
3226                {Float.NEGATIVE_INFINITY, "% 0(12e",     "  (Infinity)"},
3227
3228                {0d, "%e",          "0.000000e+00"},
3229                {0d, "%#.0e",       "0.e+00"},
3230                {0d, "%#- (9.8e",   " 0.00000000e+00"},
3231                {0d, "%#+0(8.4e",   "+0.0000e+00"},
3232                {0d, "%-+(1.6e",    "+0.000000e+00"},
3233                {0d, "% 0(12e",     " 0.000000e+00"},
3234
3235                {1d, "%e",          "1.000000e+00"},
3236                {1d, "%#.0e",       "1.e+00"},
3237                {1d, "%#- (9.8e",   " 1.00000000e+00"},
3238                {1d, "%#+0(8.4e",   "+1.0000e+00"},
3239                {1d, "%-+(1.6e",    "+1.000000e+00"},
3240                {1d, "% 0(12e",     " 1.000000e+00"},
3241
3242                {-1d, "%e",          "-1.000000e+00"},
3243                {-1d, "%#.0e",       "-1.e+00"},
3244                {-1d, "%#- (9.8e",   "(1.00000000e+00)"},
3245                {-1d, "%#+0(8.4e",   "(1.0000e+00)"},
3246                {-1d, "%-+(1.6e",    "(1.000000e+00)"},
3247                {-1d, "% 0(12e",     "(1.000000e+00)"},
3248
3249
3250                {.00000001d, "%e",          "1.000000e-08"},
3251                {.00000001d, "%#.0e",       "1.e-08"},
3252                {.00000001d, "%#- (9.8e",   " 1.00000000e-08"},
3253                {.00000001d, "%#+0(8.4e",   "+1.0000e-08"},
3254                {.00000001d, "%-+(1.6e",    "+1.000000e-08"},
3255                {.00000001d, "% 0(12e",     " 1.000000e-08"},
3256
3257                {9122.10d, "%e",          "9.122100e+03"},
3258                {9122.10d, "%#.0e",       "9.e+03"},
3259                {9122.10d, "%#- (9.8e",   " 9.12210000e+03"},
3260                {9122.10d, "%#+0(8.4e",   "+9.1221e+03"},
3261                {9122.10d, "%-+(1.6e",    "+9.122100e+03"},
3262                {9122.10d, "% 0(12e",     " 9.122100e+03"},
3263
3264                {0.1d, "%e",          "1.000000e-01"},
3265                {0.1d, "%#.0e",       "1.e-01"},
3266                {0.1d, "%#- (9.8e",   " 1.00000000e-01"},
3267                {0.1d, "%#+0(8.4e",   "+1.0000e-01"},
3268                {0.1d, "%-+(1.6e",    "+1.000000e-01"},
3269                {0.1d, "% 0(12e",     " 1.000000e-01"},
3270
3271                {-2.d, "%e",          "-2.000000e+00"},
3272                {-2.d, "%#.0e",       "-2.e+00"},
3273                {-2.d, "%#- (9.8e",   "(2.00000000e+00)"},
3274                {-2.d, "%#+0(8.4e",   "(2.0000e+00)"},
3275                {-2.d, "%-+(1.6e",    "(2.000000e+00)"},
3276                {-2.d, "% 0(12e",     "(2.000000e+00)"},
3277
3278                {-.39d, "%e",          "-3.900000e-01"},
3279                {-.39d, "%#.0e",       "-4.e-01"},
3280                {-.39d, "%#- (9.8e",   "(3.90000000e-01)"},
3281                {-.39d, "%#+0(8.4e",   "(3.9000e-01)"},
3282                {-.39d, "%-+(1.6e",    "(3.900000e-01)"},
3283                {-.39d, "% 0(12e",     "(3.900000e-01)"},
3284
3285                {-1234567890.012345678d, "%e",          "-1.234568e+09"},
3286                {-1234567890.012345678d, "%#.0e",       "-1.e+09"},
3287                {-1234567890.012345678d, "%#- (9.8e",   "(1.23456789e+09)"},
3288                {-1234567890.012345678d, "%#+0(8.4e",   "(1.2346e+09)"},
3289                {-1234567890.012345678d, "%-+(1.6e",    "(1.234568e+09)"},
3290                {-1234567890.012345678d, "% 0(12e",     "(1.234568e+09)"},
3291
3292                {Double.MAX_VALUE, "%e",          "1.797693e+308"},
3293                {Double.MAX_VALUE, "%#.0e",       "2.e+308"},
3294                {Double.MAX_VALUE, "%#- (9.8e",   " 1.79769313e+308"},
3295                {Double.MAX_VALUE, "%#+0(8.4e",   "+1.7977e+308"},
3296                {Double.MAX_VALUE, "%-+(1.6e",    "+1.797693e+308"},
3297                {Double.MAX_VALUE, "% 0(12e",     " 1.797693e+308"},
3298
3299                {Double.MIN_VALUE, "%e",          "4.900000e-324"},
3300                {Double.MIN_VALUE, "%#.0e",       "5.e-324"},
3301                {Double.MIN_VALUE, "%#- (9.8e",   " 4.90000000e-324"},
3302                {Double.MIN_VALUE, "%#+0(8.4e",   "+4.9000e-324"},
3303                {Double.MIN_VALUE, "%-+(1.6e",    "+4.900000e-324"},
3304                {Double.MIN_VALUE, "% 0(12e",     " 4.900000e-324"},
3305
3306                {Double.NaN, "%e",          "NaN"},
3307                {Double.NaN, "%#.0e",       "NaN"},
3308                {Double.NaN, "%#- (9.8e",   "NaN      "},
3309                {Double.NaN, "%#+0(8.4e",   "     NaN"},
3310                {Double.NaN, "%-+(1.6e",    "NaN"},
3311                {Double.NaN, "% 0(12e",     "         NaN"},
3312
3313                {Double.NEGATIVE_INFINITY, "%e",          "-Infinity"},
3314                {Double.NEGATIVE_INFINITY, "%#.0e",       "-Infinity"},
3315                {Double.NEGATIVE_INFINITY, "%#- (9.8e",   "(Infinity)"},
3316                {Double.NEGATIVE_INFINITY, "%#+0(8.4e",   "(Infinity)"},
3317                {Double.NEGATIVE_INFINITY, "%-+(1.6e",    "(Infinity)"},
3318                {Double.NEGATIVE_INFINITY, "% 0(12e",     "  (Infinity)"},
3319
3320                {Double.POSITIVE_INFINITY, "%e",          "Infinity"},
3321                {Double.POSITIVE_INFINITY, "%#.0e",       "Infinity"},
3322                {Double.POSITIVE_INFINITY, "%#- (9.8e",   " Infinity"},
3323                {Double.POSITIVE_INFINITY, "%#+0(8.4e",   "+Infinity"},
3324                {Double.POSITIVE_INFINITY, "%-+(1.6e",    "+Infinity"},
3325                {Double.POSITIVE_INFINITY, "% 0(12e",     "    Infinity"},
3326        };
3327        final int input   = 0;
3328        final int pattern = 1;
3329        final int output  = 2;
3330            for (int i = 0; i < tripleE.length; i++) {
3331                f = new Formatter(Locale.US);
3332                f.format((String)tripleE[i][pattern], tripleE[i][input]);
3333                assertEquals("triple[" + i + "]:" + tripleE[i][input] + ",pattern["
3334                        + i + "]:" + tripleE[i][pattern],
3335                        tripleE[i][output], f.toString());
3336
3337                // test for conversion type 'E'
3338                f = new Formatter(Locale.US);
3339                f.format(((String)tripleE[i][pattern]).toUpperCase(), tripleE[i][input]);
3340                assertEquals("triple[" + i + "]:" + tripleE[i][input] + ",pattern["
3341                        + i + "]:" + tripleE[i][pattern], ((String)tripleE[i][output])
3342                        .toUpperCase(Locale.UK), f.toString());
3343            }
3344
3345        f = new Formatter(Locale.GERMAN);
3346        f.format("%e", 1001f);
3347        /*
3348         * fail on RI, spec says 'e' requires the output to be formatted in
3349         * general scientific notation and the localization algorithm is
3350         * applied. But RI format this case to 1.001000e+03, which does not
3351         * conform to the German Locale
3352         */
3353        assertEquals("1,001000e+03", f.toString());
3354    }
3355
3356    /**
3357     * @tests java.util.Formatter#format(String, Object...) for Float/Double
3358     *        conversion type 'g' and 'G'
3359     */
3360    @TestTargetNew(
3361        level = TestLevel.PARTIAL_COMPLETE,
3362        notes = "Doesn't verify exceptions.",
3363        method = "format",
3364        args = {java.lang.String.class, java.lang.Object[].class}
3365    )
3366    @KnownFailure("Formatting of Double.MIN_VALUE works improperly")
3367    @AndroidOnly("last case fails on RI. See comment below")
3368    public void test_formatLjava_lang_String$Ljava_lang_Object_FloatConversionG() {
3369        Formatter f = null;
3370        final Object[][] tripleG = {
3371                {1001f, "%g",           "1001.00"},
3372                {1001f, "%- (,9.8g",    " 1,001.0000"},
3373                {1001f, "%+0(,8.4g",    "+001,001"},
3374                {1001f, "%-+(,1.6g",    "+1,001.00"},
3375                {1001f, "% 0(,12.0g",   " 0000001e+03"},
3376
3377                {1.f, "%g",           "1.00000"},
3378                {1.f, "%- (,9.8g",    " 1.0000000"},
3379                {1.f, "%+0(,8.4g",    "+001.000"},
3380                {1.f, "%-+(,1.6g",    "+1.00000"},
3381                {1.f, "% 0(,12.0g",   " 00000000001"},
3382
3383                {-98f, "%g",           "-98.0000"},
3384                {-98f, "%- (,9.8g",    "(98.000000)"},
3385                {-98f, "%+0(,8.4g",    "(098.00)"},
3386                {-98f, "%-+(,1.6g",    "(98.0000)"},
3387                {-98f, "% 0(,12.0g",   "(000001e+02)"},
3388
3389                {0.000001f, "%g",           "1.00000e-06"},
3390                {0.000001f, "%- (,9.8g",    " 1.0000000e-06"},
3391                {0.000001f, "%+0(,8.4g",    "+1.000e-06"},
3392                {0.000001f, "%-+(,1.6g",    "+1.00000e-06"},
3393                {0.000001f, "% 0(,12.0g",   " 0000001e-06"},
3394
3395                {345.1234567f, "%g",           "345.123"},
3396                {345.1234567f, "%- (,9.8g",    " 345.12344"},
3397                {345.1234567f, "%+0(,8.4g",    "+00345.1"},
3398                {345.1234567f, "%-+(,1.6g",    "+345.123"},
3399                {345.1234567f, "% 0(,12.0g",   " 0000003e+02"},
3400
3401                {-.00000012345f, "%g",           "-1.23450e-07"},
3402                {-.00000012345f, "%- (,9.8g",    "(1.2344999e-07)"},
3403                {-.00000012345f, "%+0(,8.4g",    "(1.234e-07)"},
3404                {-.00000012345f, "%-+(,1.6g",    "(1.23450e-07)"},
3405                {-.00000012345f, "% 0(,12.0g",   "(000001e-07)"},
3406
3407                {-987.1234567f, "%g",           "-987.123"},
3408                {-987.1234567f, "%- (,9.8g",    "(987.12347)"},
3409                {-987.1234567f, "%+0(,8.4g",    "(0987.1)"},
3410                {-987.1234567f, "%-+(,1.6g",    "(987.123)"},
3411                {-987.1234567f, "% 0(,12.0g",   "(000001e+03)"},
3412
3413                {Float.MAX_VALUE, "%g",           "3.40282e+38"},
3414                {Float.MAX_VALUE, "%- (,9.8g",    " 3.4028235e+38"},
3415                {Float.MAX_VALUE, "%+0(,8.4g",    "+3.403e+38"},
3416                {Float.MAX_VALUE, "%-+(,1.6g",    "+3.40282e+38"},
3417                {Float.MAX_VALUE, "% 0(,12.0g",   " 0000003e+38"},
3418
3419                {Float.MIN_VALUE, "%g",           "1.40130e-45"},
3420                {Float.MIN_VALUE, "%- (,9.8g",    " 1.4012985e-45"},
3421                {Float.MIN_VALUE, "%+0(,8.4g",    "+1.401e-45"},
3422                {Float.MIN_VALUE, "%-+(,1.6g",    "+1.40130e-45"},
3423                {Float.MIN_VALUE, "% 0(,12.0g",   " 0000001e-45"},
3424
3425                {Float.NaN, "%g",           "NaN"},
3426                {Float.NaN, "%- (,9.8g",    "NaN      "},
3427                {Float.NaN, "%+0(,8.4g",    "     NaN"},
3428                {Float.NaN, "%-+(,1.6g",    "NaN"},
3429                {Float.NaN, "% 0(,12.0g",   "         NaN"},
3430
3431                {Float.NEGATIVE_INFINITY, "%g",           "-Infinity"},
3432                {Float.NEGATIVE_INFINITY, "%- (,9.8g",    "(Infinity)"},
3433                {Float.NEGATIVE_INFINITY, "%+0(,8.4g",    "(Infinity)"},
3434                {Float.NEGATIVE_INFINITY, "%-+(,1.6g",    "(Infinity)"},
3435                {Float.NEGATIVE_INFINITY, "% 0(,12.0g",   "  (Infinity)"},
3436
3437                {Float.POSITIVE_INFINITY, "%g",           "Infinity"},
3438                {Float.POSITIVE_INFINITY, "%- (,9.8g",    " Infinity"},
3439                {Float.POSITIVE_INFINITY, "%+0(,8.4g",    "+Infinity"},
3440                {Float.POSITIVE_INFINITY, "%-+(,1.6g",    "+Infinity"},
3441                {Float.POSITIVE_INFINITY, "% 0(,12.0g",   "    Infinity"},
3442
3443                {1d, "%g",           "1.00000"},
3444                {1d, "%- (,9.8g",    " 1.0000000"},
3445                {1d, "%+0(,8.4g",    "+001.000"},
3446                {1d, "%-+(,1.6g",    "+1.00000"},
3447                {1d, "% 0(,12.0g",   " 00000000001"},
3448
3449                {-1d, "%g",           "-1.00000"},
3450                {-1d, "%- (,9.8g",    "(1.0000000)"},
3451                {-1d, "%+0(,8.4g",    "(01.000)"},
3452                {-1d, "%-+(,1.6g",    "(1.00000)"},
3453                {-1d, "% 0(,12.0g",   "(0000000001)"},
3454
3455                {.00000001d, "%g",           "1.00000e-08"},
3456                {.00000001d, "%- (,9.8g",    " 1.0000000e-08"},
3457                {.00000001d, "%+0(,8.4g",    "+1.000e-08"},
3458                {.00000001d, "%-+(,1.6g",    "+1.00000e-08"},
3459                {.00000001d, "% 0(,12.0g",   " 0000001e-08"},
3460
3461                {1912.10d, "%g",           "1912.10"},
3462                {1912.10d, "%- (,9.8g",    " 1,912.1000"},
3463                {1912.10d, "%+0(,8.4g",    "+001,912"},
3464                {1912.10d, "%-+(,1.6g",    "+1,912.10"},
3465                {1912.10d, "% 0(,12.0g",   " 0000002e+03"},
3466
3467                {0.1d, "%g",           "0.100000"},
3468                {0.1d, "%- (,9.8g",    " 0.10000000"},
3469                {0.1d, "%+0(,8.4g",    "+00.1000"},
3470                {0.1d, "%-+(,1.6g",    "+0.100000"},
3471                {0.1d, "% 0(,12.0g",   " 000000000.1"},
3472
3473                {-2.d, "%g",           "-2.00000"},
3474                {-2.d, "%- (,9.8g",    "(2.0000000)"},
3475                {-2.d, "%+0(,8.4g",    "(02.000)"},
3476                {-2.d, "%-+(,1.6g",    "(2.00000)"},
3477                {-2.d, "% 0(,12.0g",   "(0000000002)"},
3478
3479                {-.00039d, "%g",           "-0.000390000"},
3480                {-.00039d, "%- (,9.8g",    "(0.00039000000)"},
3481                {-.00039d, "%+0(,8.4g",    "(0.0003900)"},
3482                {-.00039d, "%-+(,1.6g",    "(0.000390000)"},
3483                {-.00039d, "% 0(,12.0g",   "(00000.0004)"},
3484
3485                {-1234567890.012345678d, "%g",           "-1.23457e+09"},
3486                {-1234567890.012345678d, "%- (,9.8g",    "(1.2345679e+09)"},
3487                {-1234567890.012345678d, "%+0(,8.4g",    "(1.235e+09)"},
3488                {-1234567890.012345678d, "%-+(,1.6g",    "(1.23457e+09)"},
3489                {-1234567890.012345678d, "% 0(,12.0g",   "(000001e+09)"},
3490
3491                {Double.MAX_VALUE, "%g",           "1.79769e+308"},
3492                {Double.MAX_VALUE, "%- (,9.8g",    " 1.7976931e+308"},
3493                {Double.MAX_VALUE, "%+0(,8.4g",    "+1.798e+308"},
3494                {Double.MAX_VALUE, "%-+(,1.6g",    "+1.79769e+308"},
3495                {Double.MAX_VALUE, "% 0(,12.0g",   " 000002e+308"},
3496
3497                {Double.MIN_VALUE, "%g",           "4.90000e-324"},
3498                {Double.MIN_VALUE, "%- (,9.8g",    " 4.9000000e-324"},
3499                {Double.MIN_VALUE, "%+0(,8.4g",    "+4.900e-324"},
3500                {Double.MIN_VALUE, "%-+(,1.6g",    "+4.90000e-324"},
3501                {Double.MIN_VALUE, "% 0(,12.0g",   " 000005e-324"},
3502
3503                {Double.NaN, "%g",           "NaN"},
3504                {Double.NaN, "%- (,9.8g",    "NaN      "},
3505                {Double.NaN, "%+0(,8.4g",    "     NaN"},
3506                {Double.NaN, "%-+(,1.6g",    "NaN"},
3507                {Double.NaN, "% 0(,12.0g",   "         NaN"},
3508
3509                {Double.NEGATIVE_INFINITY, "%g",           "-Infinity"},
3510                {Double.NEGATIVE_INFINITY, "%- (,9.8g",    "(Infinity)"},
3511                {Double.NEGATIVE_INFINITY, "%+0(,8.4g",    "(Infinity)"},
3512                {Double.NEGATIVE_INFINITY, "%-+(,1.6g",    "(Infinity)"},
3513                {Double.NEGATIVE_INFINITY, "% 0(,12.0g",   "  (Infinity)"},
3514
3515                {Double.POSITIVE_INFINITY, "%g",           "Infinity"},
3516                {Double.POSITIVE_INFINITY, "%- (,9.8g",    " Infinity"},
3517                {Double.POSITIVE_INFINITY, "%+0(,8.4g",    "+Infinity"},
3518                {Double.POSITIVE_INFINITY, "%-+(,1.6g",    "+Infinity"},
3519                {Double.POSITIVE_INFINITY, "% 0(,12.0g",   "    Infinity"},
3520
3521        };
3522        final int input   = 0;
3523        final int pattern = 1;
3524        final int output  = 2;
3525            for (int i = 0; i < tripleG.length; i++) {
3526
3527                f = new Formatter(Locale.US);
3528                f.format((String)tripleG[i][pattern], tripleG[i][input]);
3529                assertEquals("triple[" + i + "]:" + tripleG[i][input] + ",pattern["
3530                        + i + "]:" + tripleG[i][pattern],
3531                        tripleG[i][output], f.toString());
3532
3533                // test for conversion type 'G'
3534                f = new Formatter(Locale.US);
3535                f.format(((String)tripleG[i][pattern]).toUpperCase(), tripleG[i][input]);
3536                assertEquals("triple[" + i + "]:" + tripleG[i][input] + ",pattern["
3537                        + i + "]:" + tripleG[i][pattern], ((String)tripleG[i][output])
3538                        .toUpperCase(Locale.UK), f.toString());
3539            }
3540
3541        f = new Formatter(Locale.US);
3542        f.format("%.5g", 0f);
3543        assertEquals("0.0000", f.toString());
3544
3545        f = new Formatter(Locale.US);
3546        f.format("%.0g", 0f);
3547        /*
3548         * fail on RI, spec says if the precision is 0, then it is taken to be
3549         * 1. but RI throws ArrayIndexOutOfBoundsException.
3550         */
3551        assertEquals("0", f.toString());
3552
3553        f = new Formatter(Locale.GERMAN);
3554        f.format("%g", 1001f);
3555        /*
3556         * fail on RI, spec says 'g' requires the output to be formatted in
3557         * general scientific notation and the localization algorithm is
3558         * applied. But RI format this case to 1001.00, which does not conform
3559         * to the German Locale
3560         */
3561        assertEquals("1001,00", f.toString());
3562    }
3563
3564    /**
3565     * @tests java.util.Formatter#format(String, Object...) for Float/Double
3566     *        conversion type 'g' and 'G' overflow
3567     */
3568    @TestTargetNew(
3569        level = TestLevel.PARTIAL_COMPLETE,
3570        notes = "Doesn't verify exceptions.",
3571        method = "format",
3572        args = {java.lang.String.class, java.lang.Object[].class}
3573    )
3574    public void test_formatLjava_lang_String$Ljava_lang_Object_FloatConversionG_Overflow() {
3575        Formatter f = new Formatter();
3576        f.format("%g", 999999.5);
3577        assertEquals("1.00000e+06", f.toString());
3578
3579        f = new Formatter();
3580        f.format("%g", 99999.5);
3581        assertEquals("99999.5", f.toString());
3582
3583        f = new Formatter();
3584        f.format("%.4g", 99.95);
3585        assertEquals("99.95", f.toString());
3586
3587        f = new Formatter();
3588        f.format("%g", 99.95);
3589        assertEquals("99.9500", f.toString());
3590
3591        f = new Formatter();
3592        f.format("%g", 0.9);
3593        assertEquals("0.900000", f.toString());
3594
3595        f = new Formatter();
3596        f.format("%.0g", 0.000095);
3597        assertEquals("0.0001", f.toString());
3598
3599        f = new Formatter();
3600        f.format("%g", 0.0999999);
3601        assertEquals("0.0999999", f.toString());
3602
3603        f = new Formatter();
3604        f.format("%g", 0.00009);
3605        assertEquals("9.00000e-05", f.toString());
3606    }
3607
3608    /**
3609     * @tests java.util.Formatter#format(String, Object...) for Float/Double
3610     *        conversion type 'f'
3611     */
3612    @TestTargetNew(
3613        level = TestLevel.PARTIAL_COMPLETE,
3614        notes = "Doesn't verify exceptions.",
3615        method = "format",
3616        args = {java.lang.String.class, java.lang.Object[].class}
3617    )
3618    @KnownFailure("Formatting of Float.MAX_VALUE works improperly")
3619    public void test_formatLjava_lang_String$Ljava_lang_Object_FloatConversionF() {
3620        Formatter f = null;
3621
3622        final Object[][] tripleF = {
3623                {0f, "%f",          "0,000000"},
3624                {0f, "%#.3f",       "0,000"},
3625                {0f, "%,5f",        "0,000000"},
3626                {0f, "%- (12.0f",   " 0          "},
3627                {0f, "%#+0(1.6f",   "+0,000000"},
3628                {0f, "%-+(8.4f",    "+0,0000 "},
3629                {0f, "% 0#(9.8f",   " 0,00000000"},
3630
3631                {1234f, "%f",          "1234,000000"},
3632                {1234f, "%#.3f",       "1234,000"},
3633                {1234f, "%,5f",        "1.234,000000"},
3634                {1234f, "%- (12.0f",   " 1234       "},
3635                {1234f, "%#+0(1.6f",   "+1234,000000"},
3636                {1234f, "%-+(8.4f",    "+1234,0000"},
3637                {1234f, "% 0#(9.8f",   " 1234,00000000"},
3638
3639                {1.f, "%f",          "1,000000"},
3640                {1.f, "%#.3f",       "1,000"},
3641                {1.f, "%,5f",        "1,000000"},
3642                {1.f, "%- (12.0f",   " 1          "},
3643                {1.f, "%#+0(1.6f",   "+1,000000"},
3644                {1.f, "%-+(8.4f",    "+1,0000 "},
3645                {1.f, "% 0#(9.8f",   " 1,00000000"},
3646
3647                {-98f, "%f",          "-98,000000"},
3648                {-98f, "%#.3f",       "-98,000"},
3649                {-98f, "%,5f",        "-98,000000"},
3650                {-98f, "%- (12.0f",   "(98)        "},
3651                {-98f, "%#+0(1.6f",   "(98,000000)"},
3652                {-98f, "%-+(8.4f",    "(98,0000)"},
3653                {-98f, "% 0#(9.8f",   "(98,00000000)"},
3654
3655                {0.000001f, "%f",          "0,000001"},
3656                {0.000001f, "%#.3f",       "0,000"},
3657                {0.000001f, "%,5f",        "0,000001"},
3658                {0.000001f, "%- (12.0f",   " 0          "},
3659                {0.000001f, "%#+0(1.6f",   "+0,000001"},
3660                {0.000001f, "%-+(8.4f",    "+0,0000 "},
3661                {0.000001f, "% 0#(9.8f",   " 0,00000100"},
3662
3663                {345.1234567f, "%f",          "345,123444"},
3664                {345.1234567f, "%#.3f",       "345,123"},
3665                {345.1234567f, "%,5f",        "345,123444"},
3666                {345.1234567f, "%- (12.0f",   " 345        "},
3667                {345.1234567f, "%#+0(1.6f",   "+345,123444"},
3668                {345.1234567f, "%-+(8.4f",    "+345,1234"},
3669                {345.1234567f, "% 0#(9.8f",   " 345,12344360"},
3670
3671                {-.00000012345f, "%f",          "-0,000000"},
3672                {-.00000012345f, "%#.3f",       "-0,000"},
3673                {-.00000012345f, "%,5f",        "-0,000000"},
3674                {-.00000012345f, "%- (12.0f",   "(0)         "},
3675                {-.00000012345f, "%#+0(1.6f",   "(0,000000)"},
3676                {-.00000012345f, "%-+(8.4f",    "(0,0000)"},
3677                {-.00000012345f, "% 0#(9.8f",   "(0,00000012)"},
3678
3679                {-987654321.1234567f, "%f",          "-987654336,000000"},
3680                {-987654321.1234567f, "%#.3f",       "-987654336,000"},
3681                {-987654321.1234567f, "%,5f",        "-987.654.336,000000"},
3682                {-987654321.1234567f, "%- (12.0f",   "(987654336) "},
3683                {-987654321.1234567f, "%#+0(1.6f",   "(987654336,000000)"},
3684                {-987654321.1234567f, "%-+(8.4f",    "(987654336,0000)"},
3685                {-987654321.1234567f, "% 0#(9.8f",   "(987654336,00000000)"},
3686
3687                {Float.MAX_VALUE, "%f",          "340282346638528860000000000000000000000,000000"},
3688                {Float.MAX_VALUE, "%#.3f",       "340282346638528860000000000000000000000,000"},
3689                {Float.MAX_VALUE, "%,5f",        "340.282.346.638.528.860.000.000.000.000.000.000.000,000000"},
3690                {Float.MAX_VALUE, "%- (12.0f",   " 340282346638528860000000000000000000000"},
3691                {Float.MAX_VALUE, "%#+0(1.6f",   "+340282346638528860000000000000000000000,000000"},
3692                {Float.MAX_VALUE, "%-+(8.4f",    "+340282346638528860000000000000000000000,0000"},
3693                {Float.MAX_VALUE, "% 0#(9.8f",   " 340282346638528860000000000000000000000,00000000"},
3694
3695                {Float.MIN_VALUE, "%f",          "0,000000"},
3696                {Float.MIN_VALUE, "%#.3f",       "0,000"},
3697                {Float.MIN_VALUE, "%,5f",        "0,000000"},
3698                {Float.MIN_VALUE, "%- (12.0f",   " 0          "},
3699                {Float.MIN_VALUE, "%#+0(1.6f",   "+0,000000"},
3700                {Float.MIN_VALUE, "%-+(8.4f",    "+0,0000 "},
3701                {Float.MIN_VALUE, "% 0#(9.8f",   " 0,00000000"},
3702
3703                {Float.NaN, "%f",          "NaN"},
3704                {Float.NaN, "%#.3f",       "NaN"},
3705                {Float.NaN, "%,5f",        "  NaN"},
3706                {Float.NaN, "%- (12.0f",   "NaN         "},
3707                {Float.NaN, "%#+0(1.6f",   "NaN"},
3708                {Float.NaN, "%-+(8.4f",    "NaN     "},
3709                {Float.NaN, "% 0#(9.8f",   "      NaN"},
3710
3711                {Float.NEGATIVE_INFINITY, "%f",          "-Infinity"},
3712                {Float.NEGATIVE_INFINITY, "%#.3f",       "-Infinity"},
3713                {Float.NEGATIVE_INFINITY, "%,5f",        "-Infinity"},
3714                {Float.NEGATIVE_INFINITY, "%- (12.0f",   "(Infinity)  "},
3715                {Float.NEGATIVE_INFINITY, "%#+0(1.6f",   "(Infinity)"},
3716                {Float.NEGATIVE_INFINITY, "%-+(8.4f",    "(Infinity)"},
3717                {Float.NEGATIVE_INFINITY, "% 0#(9.8f",   "(Infinity)"},
3718
3719                {Float.POSITIVE_INFINITY, "%f",          "Infinity"},
3720                {Float.POSITIVE_INFINITY, "%#.3f",       "Infinity"},
3721                {Float.POSITIVE_INFINITY, "%,5f",        "Infinity"},
3722                {Float.POSITIVE_INFINITY, "%- (12.0f",   " Infinity   "},
3723                {Float.POSITIVE_INFINITY, "%#+0(1.6f",   "+Infinity"},
3724                {Float.POSITIVE_INFINITY, "%-+(8.4f",    "+Infinity"},
3725                {Float.POSITIVE_INFINITY, "% 0#(9.8f",   " Infinity"},
3726
3727
3728                {0d, "%f",          "0,000000"},
3729                {0d, "%#.3f",       "0,000"},
3730                {0d, "%,5f",        "0,000000"},
3731                {0d, "%- (12.0f",   " 0          "},
3732                {0d, "%#+0(1.6f",   "+0,000000"},
3733                {0d, "%-+(8.4f",    "+0,0000 "},
3734                {0d, "% 0#(9.8f",   " 0,00000000"},
3735
3736                {1d, "%f",          "1,000000"},
3737                {1d, "%#.3f",       "1,000"},
3738                {1d, "%,5f",        "1,000000"},
3739                {1d, "%- (12.0f",   " 1          "},
3740                {1d, "%#+0(1.6f",   "+1,000000"},
3741                {1d, "%-+(8.4f",    "+1,0000 "},
3742                {1d, "% 0#(9.8f",   " 1,00000000"},
3743
3744                {-1d, "%f",          "-1,000000"},
3745                {-1d, "%#.3f",       "-1,000"},
3746                {-1d, "%,5f",        "-1,000000"},
3747                {-1d, "%- (12.0f",   "(1)         "},
3748                {-1d, "%#+0(1.6f",   "(1,000000)"},
3749                {-1d, "%-+(8.4f",    "(1,0000)"},
3750                {-1d, "% 0#(9.8f",   "(1,00000000)"},
3751
3752                {.00000001d, "%f",          "0,000000"},
3753                {.00000001d, "%#.3f",       "0,000"},
3754                {.00000001d, "%,5f",        "0,000000"},
3755                {.00000001d, "%- (12.0f",   " 0          "},
3756                {.00000001d, "%#+0(1.6f",   "+0,000000"},
3757                {.00000001d, "%-+(8.4f",    "+0,0000 "},
3758                {.00000001d, "% 0#(9.8f",   " 0,00000001"},
3759
3760                {1000.10d, "%f",          "1000,100000"},
3761                {1000.10d, "%#.3f",       "1000,100"},
3762                {1000.10d, "%,5f",        "1.000,100000"},
3763                {1000.10d, "%- (12.0f",   " 1000       "},
3764                {1000.10d, "%#+0(1.6f",   "+1000,100000"},
3765                {1000.10d, "%-+(8.4f",    "+1000,1000"},
3766                {1000.10d, "% 0#(9.8f",   " 1000,10000000"},
3767
3768                {0.1d, "%f",          "0,100000"},
3769                {0.1d, "%#.3f",       "0,100"},
3770                {0.1d, "%,5f",        "0,100000"},
3771                {0.1d, "%- (12.0f",   " 0          "},
3772                {0.1d, "%#+0(1.6f",   "+0,100000"},
3773                {0.1d, "%-+(8.4f",    "+0,1000 "},
3774                {0.1d, "% 0#(9.8f",   " 0,10000000"},
3775
3776                {-2.d, "%f",          "-2,000000"},
3777                {-2.d, "%#.3f",       "-2,000"},
3778                {-2.d, "%,5f",        "-2,000000"},
3779                {-2.d, "%- (12.0f",   "(2)         "},
3780                {-2.d, "%#+0(1.6f",   "(2,000000)"},
3781                {-2.d, "%-+(8.4f",    "(2,0000)"},
3782                {-2.d, "% 0#(9.8f",   "(2,00000000)"},
3783
3784                {-.00009d, "%f",          "-0,000090"},
3785                {-.00009d, "%#.3f",       "-0,000"},
3786                {-.00009d, "%,5f",        "-0,000090"},
3787                {-.00009d, "%- (12.0f",   "(0)         "},
3788                {-.00009d, "%#+0(1.6f",   "(0,000090)"},
3789                {-.00009d, "%-+(8.4f",    "(0,0001)"},
3790                {-.00009d, "% 0#(9.8f",   "(0,00009000)"},
3791
3792                {-1234567890.012345678d, "%f",          "-1234567890,012346"},
3793                {-1234567890.012345678d, "%#.3f",       "-1234567890,012"},
3794                {-1234567890.012345678d, "%,5f",        "-1.234.567.890,012346"},
3795                {-1234567890.012345678d, "%- (12.0f",   "(1234567890)"},
3796                {-1234567890.012345678d, "%#+0(1.6f",   "(1234567890,012346)"},
3797                {-1234567890.012345678d, "%-+(8.4f",    "(1234567890,0123)"},
3798                {-1234567890.012345678d, "% 0#(9.8f",   "(1234567890,01234580)"},
3799
3800                {Double.MAX_VALUE, "%f",          "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,000000"},
3801                {Double.MAX_VALUE, "%#.3f",       "179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,000"},
3802                {Double.MAX_VALUE, "%,5f",        "179.769.313.486.231.570.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000.000,000000"},
3803                {Double.MAX_VALUE, "%- (12.0f",   " 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"},
3804                {Double.MAX_VALUE, "%#+0(1.6f",   "+179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,000000"},
3805                {Double.MAX_VALUE, "%-+(8.4f",    "+179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,0000"},
3806                {Double.MAX_VALUE, "% 0#(9.8f",   " 179769313486231570000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000,00000000"},
3807
3808                {Double.MIN_VALUE, "%f",          "0,000000"},
3809                {Double.MIN_VALUE, "%#.3f",       "0,000"},
3810                {Double.MIN_VALUE, "%,5f",        "0,000000"},
3811                {Double.MIN_VALUE, "%- (12.0f",   " 0          "},
3812                {Double.MIN_VALUE, "%#+0(1.6f",   "+0,000000"},
3813                {Double.MIN_VALUE, "%-+(8.4f",    "+0,0000 "},
3814                {Double.MIN_VALUE, "% 0#(9.8f",   " 0,00000000"},
3815
3816                {Double.NaN, "%f",          "NaN"},
3817                {Double.NaN, "%#.3f",       "NaN"},
3818                {Double.NaN, "%,5f",        "  NaN"},
3819                {Double.NaN, "%- (12.0f",   "NaN         "},
3820                {Double.NaN, "%#+0(1.6f",   "NaN"},
3821                {Double.NaN, "%-+(8.4f",    "NaN     "},
3822                {Double.NaN, "% 0#(9.8f",   "      NaN"},
3823
3824                {Double.POSITIVE_INFINITY, "%f",          "Infinity"},
3825                {Double.POSITIVE_INFINITY, "%#.3f",       "Infinity"},
3826                {Double.POSITIVE_INFINITY, "%,5f",        "Infinity"},
3827                {Double.POSITIVE_INFINITY, "%- (12.0f",   " Infinity   "},
3828                {Double.POSITIVE_INFINITY, "%#+0(1.6f",   "+Infinity"},
3829                {Double.POSITIVE_INFINITY, "%-+(8.4f",    "+Infinity"},
3830                {Double.POSITIVE_INFINITY, "% 0#(9.8f",   " Infinity"},
3831
3832                {Double.NEGATIVE_INFINITY, "%f",          "-Infinity"},
3833                {Double.NEGATIVE_INFINITY, "%#.3f",       "-Infinity"},
3834                {Double.NEGATIVE_INFINITY, "%,5f",        "-Infinity"},
3835                {Double.NEGATIVE_INFINITY, "%- (12.0f",   "(Infinity)  "},
3836                {Double.NEGATIVE_INFINITY, "%#+0(1.6f",   "(Infinity)"},
3837                {Double.NEGATIVE_INFINITY, "%-+(8.4f",    "(Infinity)"},
3838                {Double.NEGATIVE_INFINITY, "% 0#(9.8f",   "(Infinity)"},
3839        };
3840        final int input   = 0;
3841        final int pattern = 1;
3842        final int output  = 2;
3843            for (int i = 0; i < tripleF.length; i++) {
3844                f = new Formatter(Locale.GERMAN);
3845                f.format((String)tripleF[i][pattern], tripleF[i][input]);
3846                assertEquals("triple[" + i + "]:" + tripleF[i][input] + ",pattern["
3847                        + i + "]:" + tripleF[i][pattern],
3848                        tripleF[i][output], f.toString());
3849            }
3850    }
3851
3852    /**
3853     * @tests java.util.Formatter#format(String, Object...) for Float/Double
3854     *        conversion type 'a' and 'A'
3855     */
3856    @TestTargetNew(
3857        level = TestLevel.PARTIAL_COMPLETE,
3858        notes = "Doesn't verify exceptions.",
3859        method = "format",
3860        args = {java.lang.String.class, java.lang.Object[].class}
3861    )
3862    public void test_formatLjava_lang_String$Ljava_lang_Object_FloatConversionA() {
3863        Formatter f = null;
3864        final Object[][] tripleA = {
3865                {-0f, "%a",         "-0x0.0p0"},
3866                {-0f, "%#.3a",      "-0x0.000p0"},
3867                {-0f, "%5a",        "-0x0.0p0"},
3868                {-0f, "%- 12.0a",   "-0x0.0p0    "},
3869                {-0f, "%#+01.6a",   "-0x0.000000p0"},
3870                {-0f, "%-+8.4a",    "-0x0.0000p0"},
3871
3872                {0f, "%a",         "0x0.0p0"},
3873                {0f, "%#.3a",      "0x0.000p0"},
3874                {0f, "%5a",        "0x0.0p0"},
3875                {0f, "%- 12.0a",   " 0x0.0p0    "},
3876                {0f, "%#+01.6a",   "+0x0.000000p0"},
3877                {0f, "%-+8.4a",    "+0x0.0000p0"},
3878
3879                {1234f, "%a",         "0x1.348p10"},
3880                {1234f, "%#.3a",      "0x1.348p10"},
3881                {1234f, "%5a",        "0x1.348p10"},
3882                {1234f, "%- 12.0a",   " 0x1.3p10   "},
3883                {1234f, "%#+01.6a",   "+0x1.348000p10"},
3884                {1234f, "%-+8.4a",    "+0x1.3480p10"},
3885
3886                {1.f, "%a",         "0x1.0p0"},
3887                {1.f, "%#.3a",      "0x1.000p0"},
3888                {1.f, "%5a",        "0x1.0p0"},
3889                {1.f, "%- 12.0a",   " 0x1.0p0    "},
3890                {1.f, "%#+01.6a",   "+0x1.000000p0"},
3891                {1.f, "%-+8.4a",    "+0x1.0000p0"},
3892
3893                {-98f, "%a",         "-0x1.88p6"},
3894                {-98f, "%#.3a",      "-0x1.880p6"},
3895                {-98f, "%5a",        "-0x1.88p6"},
3896                {-98f, "%- 12.0a",   "-0x1.8p6    "},
3897                {-98f, "%#+01.6a",   "-0x1.880000p6"},
3898                {-98f, "%-+8.4a",    "-0x1.8800p6"},
3899
3900                {345.1234567f, "%a",         "0x1.591f9ap8"},
3901                {345.1234567f, "%5a",        "0x1.591f9ap8"},
3902                {345.1234567f, "%#+01.6a",   "+0x1.591f9ap8"},
3903
3904                {-987654321.1234567f, "%a",         "-0x1.d6f346p29"},
3905                {-987654321.1234567f, "%#.3a",      "-0x1.d6fp29"},
3906                {-987654321.1234567f, "%5a",        "-0x1.d6f346p29"},
3907                {-987654321.1234567f, "%- 12.0a",   "-0x1.dp29   "},
3908                {-987654321.1234567f, "%#+01.6a",   "-0x1.d6f346p29"},
3909                {-987654321.1234567f, "%-+8.4a",    "-0x1.d6f3p29"},
3910
3911                {Float.MAX_VALUE, "%a",         "0x1.fffffep127"},
3912                {Float.MAX_VALUE, "%5a",        "0x1.fffffep127"},
3913                {Float.MAX_VALUE, "%#+01.6a",   "+0x1.fffffep127"},
3914
3915                {Float.NaN, "%a",         "NaN"},
3916                {Float.NaN, "%#.3a",      "NaN"},
3917                {Float.NaN, "%5a",        "  NaN"},
3918                {Float.NaN, "%- 12.0a",   "NaN         "},
3919                {Float.NaN, "%#+01.6a",   "NaN"},
3920                {Float.NaN, "%-+8.4a",    "NaN     "},
3921
3922                {Float.NEGATIVE_INFINITY, "%a",         "-Infinity"},
3923                {Float.NEGATIVE_INFINITY, "%#.3a",      "-Infinity"},
3924                {Float.NEGATIVE_INFINITY, "%5a",        "-Infinity"},
3925                {Float.NEGATIVE_INFINITY, "%- 12.0a",   "-Infinity   "},
3926                {Float.NEGATIVE_INFINITY, "%#+01.6a",   "-Infinity"},
3927                {Float.NEGATIVE_INFINITY, "%-+8.4a",    "-Infinity"},
3928
3929                {Float.POSITIVE_INFINITY, "%a",         "Infinity"},
3930                {Float.POSITIVE_INFINITY, "%#.3a",      "Infinity"},
3931                {Float.POSITIVE_INFINITY, "%5a",        "Infinity"},
3932                {Float.POSITIVE_INFINITY, "%- 12.0a",   " Infinity   "},
3933                {Float.POSITIVE_INFINITY, "%#+01.6a",   "+Infinity"},
3934                {Float.POSITIVE_INFINITY, "%-+8.4a",    "+Infinity"},
3935
3936                {-0d, "%a",         "-0x0.0p0"},
3937                {-0d, "%#.3a",      "-0x0.000p0"},
3938                {-0d, "%5a",        "-0x0.0p0"},
3939                {-0d, "%- 12.0a",   "-0x0.0p0    "},
3940                {-0d, "%#+01.6a",   "-0x0.000000p0"},
3941                {-0d, "%-+8.4a",    "-0x0.0000p0"},
3942
3943                {0d, "%a",         "0x0.0p0"},
3944                {0d, "%#.3a",      "0x0.000p0"},
3945                {0d, "%5a",        "0x0.0p0"},
3946                {0d, "%- 12.0a",   " 0x0.0p0    "},
3947                {0d, "%#+01.6a",   "+0x0.000000p0"},
3948                {0d, "%-+8.4a",    "+0x0.0000p0"},
3949
3950                {1d, "%a",         "0x1.0p0"},
3951                {1d, "%#.3a",      "0x1.000p0"},
3952                {1d, "%5a",        "0x1.0p0"},
3953                {1d, "%- 12.0a",   " 0x1.0p0    "},
3954                {1d, "%#+01.6a",   "+0x1.000000p0"},
3955                {1d, "%-+8.4a",    "+0x1.0000p0"},
3956
3957                {-1d, "%a",         "-0x1.0p0"},
3958                {-1d, "%#.3a",      "-0x1.000p0"},
3959                {-1d, "%5a",        "-0x1.0p0"},
3960                {-1d, "%- 12.0a",   "-0x1.0p0    "},
3961                {-1d, "%#+01.6a",   "-0x1.000000p0"},
3962                {-1d, "%-+8.4a",    "-0x1.0000p0"},
3963
3964                {.00000001d, "%a",         "0x1.5798ee2308c3ap-27"},
3965                {.00000001d, "%5a",        "0x1.5798ee2308c3ap-27"},
3966                {.00000001d, "%- 12.0a",   " 0x1.5p-27  "},
3967                {.00000001d, "%#+01.6a",   "+0x1.5798eep-27"},
3968
3969                {1000.10d, "%a",         "0x1.f40cccccccccdp9"},
3970                {1000.10d, "%5a",        "0x1.f40cccccccccdp9"},
3971                {1000.10d, "%- 12.0a",   " 0x1.fp9    "},
3972
3973                {0.1d, "%a",         "0x1.999999999999ap-4"},
3974                {0.1d, "%5a",        "0x1.999999999999ap-4"},
3975
3976                {-2.d, "%a",         "-0x1.0p1"},
3977                {-2.d, "%#.3a",      "-0x1.000p1"},
3978                {-2.d, "%5a",        "-0x1.0p1"},
3979                {-2.d, "%- 12.0a",   "-0x1.0p1    "},
3980                {-2.d, "%#+01.6a",   "-0x1.000000p1"},
3981                {-2.d, "%-+8.4a",    "-0x1.0000p1"},
3982
3983                {-.00009d, "%a",         "-0x1.797cc39ffd60fp-14"},
3984                {-.00009d, "%5a",        "-0x1.797cc39ffd60fp-14"},
3985
3986                {-1234567890.012345678d, "%a",         "-0x1.26580b480ca46p30"},
3987                {-1234567890.012345678d, "%5a",        "-0x1.26580b480ca46p30"},
3988                {-1234567890.012345678d, "%- 12.0a",   "-0x1.2p30   "},
3989                {-1234567890.012345678d, "%#+01.6a",   "-0x1.26580bp30"},
3990                {-1234567890.012345678d, "%-+8.4a",    "-0x1.2658p30"},
3991
3992                {Double.MAX_VALUE, "%a",         "0x1.fffffffffffffp1023"},
3993                {Double.MAX_VALUE, "%5a",        "0x1.fffffffffffffp1023"},
3994
3995                {Double.MIN_VALUE, "%a",         "0x0.0000000000001p-1022"},
3996                {Double.MIN_VALUE, "%5a",        "0x0.0000000000001p-1022"},
3997
3998                {Double.NaN, "%a",         "NaN"},
3999                {Double.NaN, "%#.3a",      "NaN"},
4000                {Double.NaN, "%5a",        "  NaN"},
4001                {Double.NaN, "%- 12.0a",   "NaN         "},
4002                {Double.NaN, "%#+01.6a",   "NaN"},
4003                {Double.NaN, "%-+8.4a",    "NaN     "},
4004
4005                {Double.NEGATIVE_INFINITY, "%a",         "-Infinity"},
4006                {Double.NEGATIVE_INFINITY, "%#.3a",      "-Infinity"},
4007                {Double.NEGATIVE_INFINITY, "%5a",        "-Infinity"},
4008                {Double.NEGATIVE_INFINITY, "%- 12.0a",   "-Infinity   "},
4009                {Double.NEGATIVE_INFINITY, "%#+01.6a",   "-Infinity"},
4010                {Double.NEGATIVE_INFINITY, "%-+8.4a",    "-Infinity"},
4011
4012                {Double.POSITIVE_INFINITY, "%a",         "Infinity"},
4013                {Double.POSITIVE_INFINITY, "%#.3a",      "Infinity"},
4014                {Double.POSITIVE_INFINITY, "%5a",        "Infinity"},
4015                {Double.POSITIVE_INFINITY, "%- 12.0a",   " Infinity   "},
4016                {Double.POSITIVE_INFINITY, "%#+01.6a",   "+Infinity"},
4017                {Double.POSITIVE_INFINITY, "%-+8.4a",    "+Infinity"},
4018
4019        };
4020        final int input   = 0;
4021        final int pattern = 1;
4022        final int output  = 2;
4023            for (int i = 0; i < tripleA.length; i++) {
4024                f = new Formatter(Locale.UK);
4025                f.format((String)tripleA[i][pattern], tripleA[i][input]);
4026                assertEquals("triple[" + i + "]:" + tripleA[i][input] + ",pattern["
4027                        + i + "]:" + tripleA[i][pattern],
4028                        tripleA[i][output], f.toString());
4029
4030                // test for conversion type 'A'
4031                f = new Formatter(Locale.UK);
4032                f.format(((String)tripleA[i][pattern]).toUpperCase(), tripleA[i][input]);
4033                assertEquals("triple[" + i + "]:" + tripleA[i][input] + ",pattern["
4034                        + i + "]:" + tripleA[i][pattern], ((String)tripleA[i][output])
4035                        .toUpperCase(Locale.UK), f.toString());
4036            }
4037    }
4038
4039    /**
4040     * @tests java.util.Formatter#format(String, Object...) for BigDecimal
4041     *        conversion type 'e' and 'E'
4042     */
4043    @TestTargetNew(
4044        level = TestLevel.PARTIAL_COMPLETE,
4045        notes = "Doesn't verify IllegalFormatException, FormatterClosedException.",
4046        method = "format",
4047        args = {java.lang.String.class, java.lang.Object[].class}
4048    )
4049    @KnownFailure("Formatting of BigDecimal lost precission sometimes")
4050    public void test_formatLjava_lang_String$Ljava_lang_Object_BigDecimalConversionE() {
4051        Formatter f = null;
4052        final Object[][] tripleE = {
4053                {BigDecimal.ZERO, "%e",         "0.000000e+00"},
4054                {BigDecimal.ZERO, "%#.0e",      "0.e+00"},
4055                {BigDecimal.ZERO, "%# 9.8e",    " 0.00000000e+00"},
4056                {BigDecimal.ZERO, "%#+0(8.4e",  "+0.0000e+00"},
4057                {BigDecimal.ZERO, "%-+17.6e",   "+0.000000e+00    "},
4058                {BigDecimal.ZERO, "% 0(20e",    " 00000000.000000e+00"},
4059
4060                {BigDecimal.ONE, "%e",         "1.000000e+00"},
4061                {BigDecimal.ONE, "%#.0e",      "1.e+00"},
4062                {BigDecimal.ONE, "%# 9.8e",    " 1.00000000e+00"},
4063                {BigDecimal.ONE, "%#+0(8.4e",  "+1.0000e+00"},
4064                {BigDecimal.ONE, "%-+17.6e",   "+1.000000e+00    "},
4065                {BigDecimal.ONE, "% 0(20e",    " 00000001.000000e+00"},
4066
4067                {BigDecimal.TEN, "%e",         "1.000000e+01"},
4068                {BigDecimal.TEN, "%#.0e",      "1.e+01"},
4069                {BigDecimal.TEN, "%# 9.8e",    " 1.00000000e+01"},
4070                {BigDecimal.TEN, "%#+0(8.4e",  "+1.0000e+01"},
4071                {BigDecimal.TEN, "%-+17.6e",   "+1.000000e+01    "},
4072                {BigDecimal.TEN, "% 0(20e",    " 00000001.000000e+01"},
4073
4074                {new BigDecimal(-1), "%e",         "-1.000000e+00"},
4075                {new BigDecimal(-1), "%#.0e",      "-1.e+00"},
4076                {new BigDecimal(-1), "%# 9.8e",    "-1.00000000e+00"},
4077                {new BigDecimal(-1), "%#+0(8.4e",  "(1.0000e+00)"},
4078                {new BigDecimal(-1), "%-+17.6e",   "-1.000000e+00    "},
4079                {new BigDecimal(-1), "% 0(20e",    "(0000001.000000e+00)"},
4080
4081                {new BigDecimal("5.000E999"), "%e",         "5.000000e+999"},
4082                {new BigDecimal("5.000E999"), "%#.0e",      "5.e+999"},
4083                {new BigDecimal("5.000E999"), "%# 9.8e",    " 5.00000000e+999"},
4084                {new BigDecimal("5.000E999"), "%#+0(8.4e",  "+5.0000e+999"},
4085                {new BigDecimal("5.000E999"), "%-+17.6e",   "+5.000000e+999   "},
4086                {new BigDecimal("5.000E999"), "% 0(20e",    " 0000005.000000e+999"},
4087
4088                {new BigDecimal("-5.000E999"), "%e",         "-5.000000e+999"},
4089                {new BigDecimal("-5.000E999"), "%#.0e",      "-5.e+999"},
4090                {new BigDecimal("-5.000E999"), "%# 9.8e",    "-5.00000000e+999"},
4091                {new BigDecimal("-5.000E999"), "%#+0(8.4e",  "(5.0000e+999)"},
4092                {new BigDecimal("-5.000E999"), "%-+17.6e",   "-5.000000e+999   "},
4093                {new BigDecimal("-5.000E999"), "% 0(20e",    "(000005.000000e+999)"},
4094        };
4095        final int input   = 0;
4096        final int pattern = 1;
4097        final int output  = 2;
4098            for (int i = 0; i < tripleE.length; i++) {
4099                f = new Formatter(Locale.US);
4100                f.format((String)tripleE[i][pattern], tripleE[i][input]);
4101                assertEquals("triple[" + i + "]:" + tripleE[i][input] + ",pattern["
4102                        + i + "]:" + tripleE[i][pattern],
4103                        tripleE[i][output], f.toString());
4104
4105                // test for conversion type 'E'
4106                f = new Formatter(Locale.US);
4107                f.format(((String)tripleE[i][pattern]).toUpperCase(), tripleE[i][input]);
4108                assertEquals("triple[" + i + "]:" + tripleE[i][input] + ",pattern["
4109                        + i + "]:" + tripleE[i][pattern], ((String)tripleE[i][output])
4110                        .toUpperCase(Locale.US), f.toString());
4111            }
4112    }
4113
4114    /**
4115     * @tests java.util.Formatter#format(String, Object...) for BigDecimal
4116     *        conversion type 'g' and 'G'
4117     */
4118    @TestTargetNew(
4119        level = TestLevel.PARTIAL_COMPLETE,
4120        notes = "Doesn't verify IllegalFormatException, FormatterClosedException.",
4121        method = "format",
4122        args = {java.lang.String.class, java.lang.Object[].class}
4123    )
4124    @KnownFailure("Formatting of BigDecimal lost precission sometimes")
4125    @AndroidOnly("last case fails on RI. See comment below")
4126    public void test_formatLjava_lang_String$Ljava_lang_Object_BigDecimalConversionG() {
4127        Formatter f = null;
4128        final Object[][] tripleG = {
4129                {BigDecimal.ZERO, "%g",         "0.00000"},
4130                {BigDecimal.ZERO, "%.5g",       "0.0000"},
4131                {BigDecimal.ZERO, "%- (,9.8g",  " 0.0000000"},
4132                {BigDecimal.ZERO, "%+0(,8.4g",  "+000.000"},
4133                {BigDecimal.ZERO, "%-+10.6g",   "+0.00000  "},
4134                {BigDecimal.ZERO, "% 0(,12.0g", " 00000000000"},
4135                {BigDecimal.ONE, "%g",          "1.00000"},
4136                {BigDecimal.ONE, "%.5g",        "1.0000"},
4137                {BigDecimal.ONE, "%- (,9.8g",   " 1.0000000"},
4138                {BigDecimal.ONE, "%+0(,8.4g",   "+001.000"},
4139                {BigDecimal.ONE, "%-+10.6g",    "+1.00000  "},
4140                {BigDecimal.ONE, "% 0(,12.0g",  " 00000000001"},
4141
4142                {new BigDecimal(-1), "%g",          "-1.00000"},
4143                {new BigDecimal(-1), "%.5g",        "-1.0000"},
4144                {new BigDecimal(-1), "%- (,9.8g",   "(1.0000000)"},
4145                {new BigDecimal(-1), "%+0(,8.4g",   "(01.000)"},
4146                {new BigDecimal(-1), "%-+10.6g",    "-1.00000  "},
4147                {new BigDecimal(-1), "% 0(,12.0g",  "(0000000001)"},
4148
4149                {new BigDecimal(-0.000001), "%g",           "-1.00000e-06"},
4150                {new BigDecimal(-0.000001), "%.5g",         "-1.0000e-06"},
4151                {new BigDecimal(-0.000001), "%- (,9.8g",    "(1.0000000e-06)"},
4152                {new BigDecimal(-0.000001), "%+0(,8.4g",    "(1.000e-06)"},
4153                {new BigDecimal(-0.000001), "%-+10.6g",     "-1.00000e-06"},
4154                {new BigDecimal(-0.000001), "% 0(,12.0g",   "(000001e-06)"},
4155
4156                {new BigDecimal(0.0002), "%g",          "0.000200000"},
4157                {new BigDecimal(0.0002), "%.5g",        "0.00020000"},
4158                {new BigDecimal(0.0002), "%- (,9.8g",   " 0.00020000000"},
4159                {new BigDecimal(0.0002), "%+0(,8.4g",   "+0.0002000"},
4160                {new BigDecimal(0.0002), "%-+10.6g",    "+0.000200000"},
4161                {new BigDecimal(0.0002), "% 0(,12.0g",  " 000000.0002"},
4162
4163                {new BigDecimal(-0.003), "%g",          "-0.00300000"},
4164                {new BigDecimal(-0.003), "%.5g",        "-0.0030000"},
4165                {new BigDecimal(-0.003), "%- (,9.8g",   "(0.0030000000)"},
4166                {new BigDecimal(-0.003), "%+0(,8.4g",   "(0.003000)"},
4167                {new BigDecimal(-0.003), "%-+10.6g",    "-0.00300000"},
4168                {new BigDecimal(-0.003), "% 0(,12.0g",  "(000000.003)"},
4169
4170                {new BigDecimal("5.000E999"), "%g",             "5.00000e+999"},
4171                {new BigDecimal("5.000E999"), "%.5g",           "5.0000e+999"},
4172                {new BigDecimal("5.000E999"), "%- (,9.8g",      " 5.0000000e+999"},
4173                {new BigDecimal("5.000E999"), "%+0(,8.4g",      "+5.000e+999"},
4174                {new BigDecimal("5.000E999"), "%-+10.6g",       "+5.00000e+999"},
4175                {new BigDecimal("5.000E999"), "% 0(,12.0g",     " 000005e+999"},
4176
4177                {new BigDecimal("-5.000E999"), "%g",            "-5.00000e+999"},
4178                {new BigDecimal("-5.000E999"), "%.5g",          "-5.0000e+999"},
4179                {new BigDecimal("-5.000E999"), "%- (,9.8g",     "(5.0000000e+999)"},
4180                {new BigDecimal("-5.000E999"), "%+0(,8.4g",     "(5.000e+999)"},
4181                {new BigDecimal("-5.000E999"), "%-+10.6g",      "-5.00000e+999"},
4182                {new BigDecimal("-5.000E999"), "% 0(,12.0g",    "(00005e+999)"},
4183        };
4184        final int input   = 0;
4185        final int pattern = 1;
4186        final int output  = 2;
4187            for (int i = 0; i < tripleG.length; i++) {
4188                f = new Formatter(Locale.US);
4189                f.format((String)tripleG[i][pattern], tripleG[i][input]);
4190                assertEquals("triple[" + i + "]:" + tripleG[i][input] + ",pattern["
4191                        + i + "]:" + tripleG[i][pattern],
4192                        tripleG[i][output], f.toString());
4193
4194                // test for conversion type 'G'
4195                f = new Formatter(Locale.US);
4196                f.format(((String)tripleG[i][pattern]).toUpperCase(), tripleG[i][input]);
4197                assertEquals("triple[" + i + "]:" + tripleG[i][input] + ",pattern["
4198                        + i + "]:" + tripleG[i][pattern], ((String)tripleG[i][output])
4199                        .toUpperCase(Locale.US), f.toString());
4200            }
4201
4202        f = new Formatter(Locale.GERMAN);
4203        f.format("%- (,9.6g", new BigDecimal("4E6"));
4204        /*
4205         * fail on RI, spec says 'g' requires the output to be formatted in
4206         * general scientific notation and the localization algorithm is
4207         * applied. But RI format this case to 4.00000e+06, which does not
4208         * conform to the German Locale
4209         */
4210        assertEquals(" 4,00000e+06", f.toString());
4211    }
4212
4213    /**
4214     * @tests java.util.Formatter#format(String, Object...) for BigDecimal
4215     *        conversion type 'f'
4216     */
4217    @TestTargetNew(
4218        level = TestLevel.PARTIAL_COMPLETE,
4219        notes = "Doesn't verify IllegalFormatException, FormatterClosedException.",
4220        method = "format",
4221        args = {java.lang.String.class, java.lang.Object[].class}
4222    )
4223    @AndroidOnly("last case fails on RI. See comment below")
4224    public void test_formatLjava_lang_String$Ljava_lang_Object_BigDecimalConversionF() {
4225
4226        Formatter f = null;
4227        final int input   = 0;
4228        final int pattern = 1;
4229        final int output  = 2;
4230        final Object[][] tripleF = {
4231                {BigDecimal.ZERO,                                               "%f",           "0.000000"},
4232                {BigDecimal.ZERO,                                               "%#.3f",        "0.000"},
4233                {BigDecimal.ZERO,                                               "%#,5f",        "0.000000"},
4234                {BigDecimal.ZERO,                                               "%- #(12.0f",   " 0.         "},
4235                {BigDecimal.ZERO,                                               "%#+0(1.6f",    "+0.000000"},
4236                {BigDecimal.ZERO,                                               "%-+(8.4f",     "+0.0000 "},
4237                {BigDecimal.ZERO,                                               "% 0#(9.8f",    " 0.00000000"},
4238                {BigDecimal.ONE,                                                "%f",           "1.000000"},
4239                {BigDecimal.ONE,                                                "%#.3f",        "1.000"},
4240                {BigDecimal.ONE,                                                "%#,5f",        "1.000000"},
4241                {BigDecimal.ONE,                                                "%- #(12.0f",   " 1.         "},
4242                {BigDecimal.ONE,                                                "%#+0(1.6f",    "+1.000000"},
4243                {BigDecimal.ONE,                                                "%-+(8.4f",     "+1.0000 "},
4244                {BigDecimal.ONE,                                                "% 0#(9.8f",    " 1.00000000"},
4245                {BigDecimal.TEN,                                                "%f",           "10.000000"},
4246                {BigDecimal.TEN,                                                "%#.3f",        "10.000"},
4247                {BigDecimal.TEN,                                                "%#,5f",        "10.000000"},
4248                {BigDecimal.TEN,                                                "%- #(12.0f",   " 10.        "},
4249                {BigDecimal.TEN,                                                "%#+0(1.6f",    "+10.000000"},
4250                {BigDecimal.TEN,                                                "%-+(8.4f",     "+10.0000"},
4251                {BigDecimal.TEN,                                                "% 0#(9.8f",    " 10.00000000"},
4252                {new BigDecimal(-1),                                            "%f",           "-1.000000"},
4253                {new BigDecimal(-1),                                            "%#.3f",        "-1.000"},
4254                {new BigDecimal(-1),                                            "%#,5f",        "-1.000000"},
4255                {new BigDecimal(-1),                                            "%- #(12.0f",   "(1.)        "},
4256                {new BigDecimal(-1),                                            "%#+0(1.6f",    "(1.000000)"},
4257                {new BigDecimal(-1),                                            "%-+(8.4f",     "(1.0000)"},
4258                {new BigDecimal(-1),                                            "% 0#(9.8f",    "(1.00000000)"},
4259                {new BigDecimal("9999999999999999999999999999999999999999999"), "%f",           "9999999999999999999999999999999999999999999.000000"},
4260                {new BigDecimal("9999999999999999999999999999999999999999999"), "%#.3f",        "9999999999999999999999999999999999999999999.000"},
4261                {new BigDecimal("9999999999999999999999999999999999999999999"), "%#,5f",        "9,999,999,999,999,999,999,999,999,999,999,999,999,999,999.000000"},
4262                {new BigDecimal("9999999999999999999999999999999999999999999"), "%- #(12.0f",   " 9999999999999999999999999999999999999999999."},
4263                {new BigDecimal("9999999999999999999999999999999999999999999"), "%#+0(1.6f",    "+9999999999999999999999999999999999999999999.000000"},
4264                {new BigDecimal("9999999999999999999999999999999999999999999"), "%-+(8.4f",     "+9999999999999999999999999999999999999999999.0000"},
4265                {new BigDecimal("9999999999999999999999999999999999999999999"), "% 0#(9.8f",    " 9999999999999999999999999999999999999999999.00000000"},
4266                {new BigDecimal("-9999999999999999999999999999999999999999999"), "%f",          "-9999999999999999999999999999999999999999999.000000"},
4267                {new BigDecimal("-9999999999999999999999999999999999999999999"), "%#.3f",       "-9999999999999999999999999999999999999999999.000"},
4268                {new BigDecimal("-9999999999999999999999999999999999999999999"), "%#,5f",       "-9,999,999,999,999,999,999,999,999,999,999,999,999,999,999.000000"},
4269                {new BigDecimal("-9999999999999999999999999999999999999999999"), "%- #(12.0f",  "(9999999999999999999999999999999999999999999.)"},
4270                {new BigDecimal("-9999999999999999999999999999999999999999999"), "%#+0(1.6f",   "(9999999999999999999999999999999999999999999.000000)"},
4271                {new BigDecimal("-9999999999999999999999999999999999999999999"), "%-+(8.4f",    "(9999999999999999999999999999999999999999999.0000)"},
4272                {new BigDecimal("-9999999999999999999999999999999999999999999"), "% 0#(9.8f",   "(9999999999999999999999999999999999999999999.00000000)"},
4273        };
4274        for (int i = 0; i < tripleF.length; i++) {
4275            f = new Formatter(Locale.US);
4276            f.format((String)tripleF[i][pattern], tripleF[i][input]);
4277            assertEquals("triple[" + i + "]:" + tripleF[i][input] + ",pattern["
4278                    + i + "]:" + tripleF[i][pattern], tripleF[i][output], f.toString());
4279        }
4280
4281        f = new Formatter(Locale.US);
4282        f.format("%f", new BigDecimal("5.0E9"));
4283        // error on RI
4284        // RI throw ArrayIndexOutOfBoundsException
4285        assertEquals("5000000000.000000", f.toString());
4286    }
4287
4288    /**
4289     * @tests java.util.Formatter#format(String, Object...) for exceptions in
4290     *        Float/Double/BigDecimal conversion type 'e', 'E', 'g', 'G', 'f', 'a', 'A'
4291     */
4292    @TestTargetNew(
4293        level = TestLevel.PARTIAL_COMPLETE,
4294        notes = "Verifies exceptions.",
4295        method = "format",
4296        args = {java.lang.String.class, java.lang.Object[].class}
4297    )
4298    public void test_formatLjava_lang_String$Ljava_lang_Object_FloatDoubleBigDecimalConversionException() {
4299        Formatter f = null;
4300
4301        final char[] conversions = { 'e', 'E', 'g', 'G', 'f', 'a', 'A' };
4302        final Object[] illArgs = { false, (byte) 1, (short) 2, 3, (long) 4,
4303                new BigInteger("5"), new Character('c'), new Object(),
4304                new Date() };
4305        for (int i = 0; i < illArgs.length; i++) {
4306            for (int j = 0; j < conversions.length; j++) {
4307                try {
4308                    f = new Formatter(Locale.UK);
4309                    f.format("%" + conversions[j], illArgs[i]);
4310                    fail("should throw IllegalFormatConversionException");
4311                } catch (IllegalFormatConversionException e) {
4312                    // expected
4313                }
4314            }
4315        }
4316
4317        try {
4318            f = new Formatter(Locale.UK);
4319            f.format("%a", new BigDecimal(1));
4320            fail("should throw IllegalFormatConversionException");
4321        } catch (IllegalFormatConversionException e) {
4322            // expected
4323        }
4324
4325        try {
4326            f = new Formatter(Locale.UK);
4327            f.format("%A", new BigDecimal(1));
4328            fail("should throw IllegalFormatConversionException");
4329        } catch (IllegalFormatConversionException e) {
4330            // expected
4331        }
4332
4333        final String[] flagsConversionMismatches = { "%,e", "%,E", "%#g",
4334                "%#G", "%,a", "%,A", "%(a", "%(A" };
4335        for (int i = 0; i < flagsConversionMismatches.length; i++) {
4336            try {
4337                f = new Formatter(Locale.CHINA);
4338                f.format(flagsConversionMismatches[i], new BigDecimal(1));
4339                fail("should throw FormatFlagsConversionMismatchException");
4340            } catch (FormatFlagsConversionMismatchException e) {
4341                // expected
4342            }
4343            try {
4344                f = new Formatter(Locale.JAPAN);
4345                f.format(flagsConversionMismatches[i], (BigDecimal) null);
4346                fail("should throw FormatFlagsConversionMismatchException");
4347            } catch (FormatFlagsConversionMismatchException e) {
4348                // expected
4349            }
4350        }
4351
4352        final String[] missingFormatWidths = { "%-0e", "%0e", "%-e", "%-0E",
4353                "%0E", "%-E", "%-0g", "%0g", "%-g", "%-0G", "%0G", "%-G",
4354                "%-0f", "%0f", "%-f", "%-0a", "%0a", "%-a", "%-0A", "%0A",
4355                "%-A" };
4356        for (int i = 0; i < missingFormatWidths.length; i++) {
4357            try {
4358                f = new Formatter(Locale.KOREA);
4359                f.format(missingFormatWidths[i], 1f);
4360                fail("should throw MissingFormatWidthException");
4361            } catch (MissingFormatWidthException e) {
4362                // expected
4363            }
4364
4365            try {
4366                f = new Formatter(Locale.KOREA);
4367                f.format(missingFormatWidths[i], (Float) null);
4368                fail("should throw MissingFormatWidthException");
4369            } catch (MissingFormatWidthException e) {
4370                // expected
4371            }
4372        }
4373
4374        final String[] illFlags = { "%+ e", "%+ E", "%+ g", "%+ G", "%+ f",
4375                "%+ a", "%+ A", "%-03e", "%-03E", "%-03g", "%-03G", "%-03f",
4376                "%-03a", "%-03A" };
4377        for (int i = 0; i < illFlags.length; i++) {
4378            try {
4379                f = new Formatter(Locale.CANADA);
4380                f.format(illFlags[i], 1.23d);
4381                fail("should throw IllegalFormatFlagsException");
4382            } catch (IllegalFormatFlagsException e) {
4383                // expected
4384            }
4385
4386            try {
4387                f = new Formatter(Locale.CANADA);
4388                f.format(illFlags[i], (Double) null);
4389                fail("should throw IllegalFormatFlagsException");
4390            } catch (IllegalFormatFlagsException e) {
4391                // expected
4392            }
4393        }
4394
4395        f = new Formatter(Locale.US);
4396        try {
4397            f.format("%F", 1);
4398            fail("should throw UnknownFormatConversionException");
4399        } catch (UnknownFormatConversionException e) {
4400            // expected
4401        }
4402    }
4403
4404    /**
4405     * @tests java.util.Formatter#format(String, Object...) for
4406     *        Float/Double/BigDecimal exception throwing order
4407     */
4408    @TestTargetNew(
4409        level = TestLevel.PARTIAL_COMPLETE,
4410        notes = "Verifies exceptions.",
4411        method = "format",
4412        args = {java.lang.String.class, java.lang.Object[].class}
4413    )
4414    public void test_formatLjava_lang_String$Ljava_lang_Object_FloatDoubleBigDecimalExceptionOrder() {
4415        Formatter f = null;
4416
4417        /*
4418         * Summary: UnknownFormatConversionException >
4419         * MissingFormatWidthException > IllegalFormatFlagsException >
4420         * FormatFlagsConversionMismatchException >
4421         * IllegalFormatConversionException
4422         *
4423         */
4424        try {
4425            // compare FormatFlagsConversionMismatchException and
4426            // IllegalFormatConversionException
4427            f = new Formatter(Locale.US);
4428            f.format("%,e", (byte) 1);
4429            fail("should throw FormatFlagsConversionMismatchException");
4430        } catch (FormatFlagsConversionMismatchException e) {
4431            // expected
4432        }
4433
4434        try {
4435            // compare IllegalFormatFlagsException and
4436            // FormatFlagsConversionMismatchException
4437            f = new Formatter(Locale.US);
4438            f.format("%+ ,e", 1f);
4439            fail("should throw IllegalFormatFlagsException");
4440        } catch (IllegalFormatFlagsException e) {
4441            // expected
4442        }
4443
4444        try {
4445            // compare MissingFormatWidthException and
4446            // IllegalFormatFlagsException
4447            f = new Formatter(Locale.US);
4448            f.format("%+ -e", 1f);
4449            fail("should throw MissingFormatWidthException");
4450        } catch (MissingFormatWidthException e) {
4451            // expected
4452        }
4453
4454        try {
4455            // compare UnknownFormatConversionException and
4456            // MissingFormatWidthException
4457            f = new Formatter(Locale.US);
4458            f.format("%-F", 1f);
4459            fail("should throw UnknownFormatConversionException");
4460        } catch (UnknownFormatConversionException e) {
4461            // expected
4462        }
4463    }
4464
4465    /**
4466     * @tests java.util.Formatter#format(String, Object...) for BigDecimal
4467     *        exception throwing order
4468     */
4469    @TestTargetNew(
4470        level = TestLevel.PARTIAL_COMPLETE,
4471        notes = "Verifies exceptions.",
4472        method = "format",
4473        args = {java.lang.String.class, java.lang.Object[].class}
4474    )
4475    public void test_formatLjava_lang_String$Ljava_lang_Object_BigDecimalExceptionOrder() {
4476        Formatter f = null;
4477        BigDecimal bd = new BigDecimal("1.0");
4478
4479        /*
4480         * Summary: UnknownFormatConversionException >
4481         * MissingFormatWidthException > IllegalFormatFlagsException >
4482         * FormatFlagsConversionMismatchException >
4483         * IllegalFormatConversionException
4484         *
4485         */
4486        try {
4487            // compare FormatFlagsConversionMismatchException and
4488            // IllegalFormatConversionException
4489            f = new Formatter(Locale.US);
4490            f.format("%,e", (byte) 1);
4491            fail("should throw FormatFlagsConversionMismatchException");
4492        } catch (FormatFlagsConversionMismatchException e) {
4493            // expected
4494        }
4495
4496        try {
4497            // compare IllegalFormatFlagsException and
4498            // FormatFlagsConversionMismatchException
4499            f = new Formatter(Locale.US);
4500            f.format("%+ ,e", bd);
4501            fail("should throw IllegalFormatFlagsException");
4502        } catch (IllegalFormatFlagsException e) {
4503            // expected
4504        }
4505
4506        try {
4507            // compare MissingFormatWidthException and
4508            // IllegalFormatFlagsException
4509            f = new Formatter(Locale.US);
4510            f.format("%+ -e", bd);
4511            fail("should throw MissingFormatWidthException");
4512        } catch (MissingFormatWidthException e) {
4513            // expected
4514        }
4515
4516        // compare UnknownFormatConversionException and
4517        // MissingFormatWidthException
4518        try {
4519            f = new Formatter(Locale.US);
4520            f.format("%-F", bd);
4521            fail("should throw UnknownFormatConversionException");
4522        } catch (UnknownFormatConversionException e) {
4523            // expected
4524        }
4525    }
4526
4527    /**
4528     * @tests java.util.Formatter#format(String, Object...) for null argment for
4529     *        Float/Double/BigDecimal conversion
4530     */
4531    @TestTargetNew(
4532        level = TestLevel.PARTIAL_COMPLETE,
4533        notes = "Verifies null as the second parameter.",
4534        method = "format",
4535        args = {java.lang.String.class, java.lang.Object[].class}
4536    )
4537    public void test_formatLjava_lang_String$Ljava_lang_Object_FloatDoubleBigDecimalNullConversion() {
4538        Formatter f = null;
4539
4540        // test (Float)null
4541        f = new Formatter(Locale.FRANCE);
4542        f.format("%#- (9.0e", (Float) null);
4543        assertEquals("         ", f.toString());
4544
4545        f = new Formatter(Locale.GERMAN);
4546        f.format("%-+(1.6E", (Float) null);
4547        assertEquals("NULL", f.toString());
4548
4549        f = new Formatter(Locale.UK);
4550        f.format("%+0(,8.4g", (Float) null);
4551        assertEquals("    null", f.toString());
4552
4553        f = new Formatter(Locale.FRANCE);
4554        f.format("%- (9.8G", (Float) null);
4555        assertEquals("NULL     ", f.toString());
4556
4557        f = new Formatter(Locale.FRANCE);
4558        f.format("%- (12.1f", (Float) null);
4559        assertEquals("n           ", f.toString());
4560
4561        f = new Formatter(Locale.FRANCE);
4562        f.format("% .4a", (Float) null);
4563        assertEquals("null", f.toString());
4564
4565        f = new Formatter(Locale.FRANCE);
4566        f.format("%06A", (Float) null);
4567        assertEquals("  NULL", f.toString());
4568
4569        // test (Double)null
4570        f = new Formatter(Locale.GERMAN);
4571        f.format("%- (9e", (Double) null);
4572        assertEquals("null     ", f.toString());
4573
4574        f = new Formatter(Locale.GERMAN);
4575        f.format("%#-+(1.6E", (Double) null);
4576        assertEquals("NULL", f.toString());
4577
4578        f = new Formatter(Locale.GERMAN);
4579        f.format("%+0(6.4g", (Double) null);
4580        assertEquals("  null", f.toString());
4581
4582        f = new Formatter(Locale.GERMAN);
4583        f.format("%- (,5.8G", (Double) null);
4584        assertEquals("NULL ", f.toString());
4585
4586        f = new Formatter(Locale.GERMAN);
4587        f.format("% (.4f", (Double) null);
4588        assertEquals("null", f.toString());
4589
4590        f = new Formatter(Locale.GERMAN);
4591        f.format("%#.6a", (Double) null);
4592        assertEquals("null", f.toString());
4593
4594        f = new Formatter(Locale.GERMAN);
4595        f.format("% 2.5A", (Double) null);
4596        assertEquals("NULL", f.toString());
4597
4598        // test (BigDecimal)null
4599        f = new Formatter(Locale.UK);
4600        f.format("%#- (6.2e", (BigDecimal) null);
4601        assertEquals("nu    ", f.toString());
4602
4603        f = new Formatter(Locale.UK);
4604        f.format("%-+(1.6E", (BigDecimal) null);
4605        assertEquals("NULL", f.toString());
4606
4607        f = new Formatter(Locale.UK);
4608        f.format("%+-(,5.3g", (BigDecimal) null);
4609        assertEquals("nul  ", f.toString());
4610
4611        f = new Formatter(Locale.UK);
4612        f.format("%0 3G", (BigDecimal) null);
4613        assertEquals("NULL", f.toString());
4614
4615        f = new Formatter(Locale.UK);
4616        f.format("%0 (9.0G", (BigDecimal) null);
4617        assertEquals("         ", f.toString());
4618
4619        f = new Formatter(Locale.UK);
4620        f.format("% (.5f", (BigDecimal) null);
4621        assertEquals("null", f.toString());
4622
4623        f = new Formatter(Locale.UK);
4624        f.format("%06a", (BigDecimal) null);
4625        assertEquals("  null", f.toString());
4626
4627        f = new Formatter(Locale.UK);
4628        f.format("% .5A", (BigDecimal) null);
4629        assertEquals("NULL", f.toString());
4630    }
4631
4632    @TestTargetNew(
4633        level = TestLevel.COMPLETE,
4634        notes = "Check locale specific functionality, not all formatter functional.Formatter functionality well checked under above tests.",
4635        method = "format",
4636        args = {java.util.Locale.class, java.lang.String.class, java.lang.Object[].class}
4637    )
4638    public void test_formatLjava_util_LocaleLjava_lang_StringLjava_lang_Object() {
4639        Locale[] requiredLocales = {Locale.GERMAN, Locale.FRANCE, Locale.US, Locale.CANADA};
4640        if (!Support_Locale.areLocalesAvailable(requiredLocales)) {
4641            // locale dependent test, bug 1943269
4642            return;
4643        }
4644        Double val = new Double(3.14);
4645        Calendar cal = Calendar.getInstance();
4646        Formatter fLoc = null;
4647        Formatter fNoL = null;
4648        cal.set(2008, Calendar.SEPTEMBER, 23, 18, 30);
4649
4650        fLoc = new Formatter(Locale.GERMAN);
4651        fNoL = new Formatter(Locale.GERMAN);
4652        fLoc.format(Locale.US, "%f", val);
4653        fNoL.format("%f", val);
4654        assertFalse(fLoc.toString().equals(fNoL.toString()));
4655
4656        fLoc = new Formatter(Locale.FRANCE);
4657        fNoL = new Formatter(Locale.FRANCE);
4658        fLoc.format(Locale.US, "%f", val);
4659        fNoL.format("%f", val);
4660        assertFalse(fLoc.toString().equals(fNoL.toString()));
4661
4662        fLoc = new Formatter(Locale.US);
4663        fNoL = new Formatter(Locale.US);
4664        fLoc.format(Locale.US, "%f", val);
4665        fNoL.format("%f", val);
4666        assertTrue(fLoc.toString().equals(fNoL.toString()));
4667
4668        fLoc = new Formatter(Locale.GERMAN);
4669        fNoL = new Formatter(Locale.GERMAN);
4670        fLoc.format(Locale.US, "%tA %tB %td %tT", cal, cal,  cal, cal);
4671        fNoL.format("%tA %tB %td %tT", cal, cal,  cal, cal);
4672        assertFalse(fLoc.toString().equals(fNoL.toString()));
4673
4674        fLoc = new Formatter(Locale.FRANCE);
4675        fNoL = new Formatter(Locale.FRANCE);
4676        fLoc.format(Locale.US, "%tA %tB %td %tT", cal, cal,  cal, cal);
4677        fNoL.format("%tA %tB %td %tT", cal, cal,  cal, cal);
4678        assertFalse(fLoc.toString().equals(fNoL.toString()));
4679
4680        fLoc = new Formatter(Locale.US);
4681        fNoL = new Formatter(Locale.US);
4682        fLoc.format(Locale.US, "%tA %tB %td %tT", cal, cal,  cal, cal);
4683        fNoL.format("%tA %tB %td %tT", cal, cal,  cal, cal);
4684        assertTrue(fLoc.toString().equals(fNoL.toString()));
4685
4686        final String[] illFlags = { "%+ e", "%+ E", "%+ g", "%+ G", "%+ f",
4687                "%+ a", "%+ A", "%-03e", "%-03E", "%-03g", "%-03G", "%-03f",
4688                "%-03a", "%-03A" };
4689        for (int i = 0; i < illFlags.length; i++) {
4690            try {
4691                fLoc = new Formatter(Locale.US);
4692                fLoc.format(Locale.FRANCE, illFlags[i], 1.23d);
4693                fail("should throw IllegalFormatFlagsException");
4694            } catch (IllegalFormatFlagsException e) {
4695                // expected
4696            }
4697
4698            try {
4699                fLoc = new Formatter(Locale.CANADA);
4700                fLoc.format(Locale.GERMAN, illFlags[i], (Double) null);
4701                fail("should throw IllegalFormatFlagsException");
4702            } catch (IllegalFormatFlagsException e) {
4703                // expected
4704            }
4705        }
4706
4707        fLoc.close();
4708
4709        try {
4710            fLoc.format(Locale.GERMAN, "%f", val);
4711        } catch (FormatterClosedException e) {
4712            //expected
4713        }
4714    }
4715
4716    /**
4717     * Setup resource files for testing
4718     */
4719    protected void setUp() throws IOException {
4720        notExist = File.createTempFile("notexist", null);
4721        notExist.delete();
4722
4723        fileWithContent = File.createTempFile("filewithcontent", null);
4724        BufferedOutputStream bw = new BufferedOutputStream(
4725                new FileOutputStream(fileWithContent));
4726        bw.write(1);// write something into the file
4727        bw.close();
4728
4729        readOnly = File.createTempFile("readonly", null);
4730        assertTrue(readOnly.setReadOnly());
4731
4732        secret = File.createTempFile("secret", null);
4733
4734        defaultTimeZone = TimeZone.getDefault();
4735        TimeZone cst = TimeZone.getTimeZone("Asia/Shanghai");
4736        TimeZone.setDefault(cst);
4737    }
4738
4739    /**
4740     * Delete the resource files if they exist
4741     */
4742    protected void tearDown() {
4743        if (notExist.exists()) {
4744            notExist.delete();
4745        }
4746
4747        if (fileWithContent.exists()) {
4748            fileWithContent.delete();
4749        }
4750        if (readOnly.exists()) {
4751            readOnly.delete();
4752        }
4753        if (secret.exists()) {
4754            secret.delete();
4755        }
4756
4757        TimeZone.setDefault(defaultTimeZone);
4758    }
4759}
4760