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