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