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