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