1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
|
/*
* Copyright (c) 2008, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @bug 4823811 8008577
* @summary Confirm that text which includes numbers with a trailing minus sign is parsed correctly.
* @modules jdk.localedata
* @run main/othervm -Duser.timezone=GMT+09:00 -Djava.locale.providers=JRE,SPI Bug4823811
*/
import java.text.*;
import java.util.*;
public class Bug4823811 {
private static Locale localeEG = new Locale("ar", "EG");
private static Locale localeUS = Locale.US;
private static String JuneInArabic = "\u064a\u0648\u0646\u064a\u0648";
private static String JulyInArabic = "\u064a\u0648\u0644\u064a\u0648";
private static String JuneInEnglish = "June";
private static String JulyInEnglish = "July";
private static String BORDER =
"============================================================";
/*
* I don't use static import here intentionally so that this test program
* can be run on JDK 1.4.2.
*/
private static int ERA = Calendar.ERA;
private static int BC = GregorianCalendar.BC;
// private static int JAN = Calendar.JANUARY;
// private static int FEB = Calendar.FEBRUARY;
// private static int MAR = Calendar.MARCH;
private static int APR = Calendar.APRIL;
private static int MAY = Calendar.MAY;
private static int JUN = Calendar.JUNE;
private static int JUL = Calendar.JULY;
// private static int AUG = Calendar.AUGUST;
// private static int SEP = Calendar.SEPTEMBER;
// private static int OCT = Calendar.OCTOBER;
// private static int NOV = Calendar.NOVEMBER;
// private static int DEC = Calendar.DECEMBER;
private static String[] patterns = {
"yyyy MMMM d H m s",
"yyyy MM dd hh mm ss",
/*
* Because 1-based HOUR_OF_DAY, 1-based HOUR, MONTH, and YEAR fields
* are parsed using different code from the code for other numeric
* fields, I prepared YEAR-preceding patterns and SECOND-preceding
* patterns.
*/
"yyyy M d h m s",
" yyyy M d h m s",
"yyyy M d h m s ",
"s m h d M yyyy",
" s m h d M yyyy",
"s m h d M yyyy ",
};
private static char originalMinusSign1 = ':';
private static char originalMinusSign2 = '\uff0d'; // fullwidth minus
private static String[] delimiters = {"-", "/", ":", "/", "\uff0d", "/"};
private static String[][] specialDelimiters = {
// for Arabic formatter and modified English formatter
{"--", "-/", "::", ":/", "\uff0d\uff0d", "\uff0d/"},
// for English formatter and modified Arabic formatter
{"--", "/-", "::", "/:", "\uff0d\uff0d", "/\uff0d"},
};
/*
* Format:
* +-------------------------------------------------------------------+
* | Input | Output |
* +---------------------+---------------------------------------------|
* | datesEG & datesUS | formattedDatesEG & formattedDatesUS |
* +-------------------------------------------------------------------+
*
* Parse:
* +-------------------------------------------------------------------+
* | Input | Output |
* |---------------------+---------------------------------------------|
* | datesToParse | datesEG & datesUS |
* +-------------------------------------------------------------------+
*/
private static String[][] datesToParse = {
// "JUNE" and "JULY" are replaced with a localized month name later.
{"2008 JULY 20 3 12 83",
"2008 JULY 20 3 12 83",
"2008 JULY 20 3 12 83"},
{"2008 07 20 03 12 83",
"2008 07 20 03 12 83",
"2008 07 20 03 12 83"},
{"2008 7 20 3 12 83",
"2008 7 20 3 12 83",
"2008 7 20 3 12 83"},
{" 2008 7 20 3 12 83",
" 2008 7 20 3 12 83",
" 2008 7 20 3 12 83",
"2008 7 20 3 12 83"},
{"2008 7 20 3 12 83 ",
"2008 7 20 3 12 83 ",
"2008 7 20 3 12 83"},
{"83 12 3 20 7 2008",
"83 12 3 20 7 2008",
"83 12 3 20 7 2008"},
{" 83 12 3 20 7 2008",
" 83 12 3 20 7 2008",
" 83 12 3 20 7 2008",
"83 12 3 20 7 2008"},
{"83 12 3 20 7 2008 ",
"83 12 3 20 7 2008 ",
"83 12 3 20 7 2008"},
};
// For formatting
private static String[][] formattedDatesEG = {
{"2008 JULY 20 3 13 23",
"2009 JULY 20 3 13 23",
null},
{"2008 07 20 03 13 23",
"2009 07 20 03 13 23",
"2007 05 20 03 13 23"},
{"2008 7 20 3 13 23",
"2009 6 10 3 13 23",
"2007 4 10 3 13 23"},
{" 2008 7 20 3 13 23",
null,
" 2009 7 20 3 13 23",
null},
{"2008 7 20 3 13 23 ",
"2008 7 20 3 10 37 ",
null},
{"23 13 3 20 7 2008",
"37 10 9 19 7 2008",
"23 49 8 19 7 2008"},
{" 23 13 3 20 7 2008",
null,
" 37 10 3 20 7 2008",
null},
{"23 13 3 20 7 2008 ",
"23 13 3 20 7 2009 ",
null},
};
private static String[][] formattedDatesUS = {
{"2008 JULY 20 3 13 23",
null,
"2008 JUNE 10 3 13 23"},
{"2008 07 20 03 13 23",
"2007 05 20 03 13 23",
"2008 06 10 03 13 23"},
{"2008 7 20 3 13 23",
"2007 5 19 9 13 23",
"2008 6 9 9 13 23"},
{" 2008 7 20 3 13 23",
" 2009 7 20 3 13 23",
" 2007 5 20 3 13 23",
null},
{"2008 7 20 3 13 23 ",
"2008 7 20 3 13 23 ",
null},
{"23 13 3 20 7 2008",
"23 49 2 10 6 2008",
"23 13 9 9 6 2008"},
{" 23 13 3 20 7 2008",
" 37 10 3 20 7 2008",
" 23 49 2 20 7 2008",
null},
{"23 13 3 20 7 2008 ",
"23 13 3 20 7 2008 ",
null},
};
private static GregorianCalendar[][] datesEG = {
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar(-2008, JUL, 20, 3, 12, 83),
null},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar(-2008, JUL, 20, 3, 12, 83),
new GregorianCalendar( 2007, MAY, 20, 3, 12, 83)},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar(-2008, JUL, -20, 3, 12, 83),
new GregorianCalendar( 2007, APR, 10, 3, 12, 83)},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
null,
new GregorianCalendar(-2008, JUL, 20, 3, 12, 83),
null},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar( 2008, JUL, 20, 3, 12, -83),
null},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar( 2008, JUL, 20, -3, 12, -83),
new GregorianCalendar( 2008, JUL, 20, -3, -12, 83)},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
null,
new GregorianCalendar( 2008, JUL, 20, 3, 12, -83),
null},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar(-2008, JUL, 20, 3, 12, 83),
null},
};
private static GregorianCalendar[][] datesUS = {
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
null,
new GregorianCalendar( 2008, JUN, 10, 3, 12, 83)},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar( 2007, MAY, 20, 3, 12, 83),
new GregorianCalendar( 2008, JUN, 10, 3, 12, 83)},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar( 2007, MAY, 20, -3, 12, 83),
new GregorianCalendar( 2008, JUL, -20, -3, 12, 83)},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar(-2008, JUL, 20, 3, 12, 83),
new GregorianCalendar( 2007, MAY, 20, 3, 12, 83),
null},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
null},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar( 2008, JUL, -20, 3, -12, 83),
new GregorianCalendar( 2008, JUL, -20, -3, 12, 83)},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar( 2008, JUL, 20, 3, 12, -83),
new GregorianCalendar( 2008, JUL, 20, 3, -12, 83),
null},
{new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
null},
};
/* flags */
private static boolean err = false;
private static boolean verbose = false;
public static void main(String[] args) {
if (args.length == 1 && args[0].equals("-v")) {
verbose = true;
}
Locale defaultLocale = Locale.getDefault();
TimeZone defaultTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("Asia/Tokyo"));
try {
/*
* Test SimpleDateFormat.parse() and format() for original
* SimpleDateFormat instances
*/
testDateFormat1();
/*
* Test SimpleDateFormat.parse() and format() for modified
* SimpleDateFormat instances using an original minus sign,
* pattern, and diffenrent month names in DecimalFormat
*/
testDateFormat2();
/*
* Test SimpleDateFormat.parse() and format() for modified
* SimpleDateFormat instances using a fullwidth minus sign
*/
testDateFormat3();
/*
* Just to confirm that regressions aren't introduced in
* DecimalFormat. This cannot happen, though. Because I didn't
* change DecimalFormat at all.
*/
testNumberFormat();
}
catch (Exception e) {
err = true;
System.err.println("Unexpected exception: " + e);
}
finally {
Locale.setDefault(defaultLocale);
TimeZone.setDefault(defaultTimeZone);
if (err) {
System.err.println(BORDER + " Test failed.");
throw new RuntimeException("Date/Number formatting/parsing error.");
} else {
System.out.println(BORDER + " Test passed.");
}
}
}
//
// DateFormat test
//
private static void testDateFormat1() {
for (int i = 0; i < patterns.length; i++) {
System.out.println(BORDER);
for (int j = 0; j <= 1; j++) {
// Generate a pattern
String pattern = patterns[i].replaceAll(" ", delimiters[j]);
System.out.println("Pattern=\"" + pattern + "\"");
System.out.println("*** DateFormat.format test in ar_EG");
testDateFormatFormattingInRTL(pattern, i, j, null, localeEG, false);
System.out.println("*** DateFormat.parse test in ar_EG");
testDateFormatParsingInRTL(pattern, i, j, null, localeEG, false);
System.out.println("*** DateFormat.format test in en_US");
testDateFormatFormattingInLTR(pattern, i, j, null, localeUS, true);
System.out.println("*** DateFormat.parse test in en_US");
testDateFormatParsingInLTR(pattern, i, j, null, localeUS, true);
}
}
}
private static void testDateFormat2() {
/*
* modified ar_EG Date&Time formatter :
* minus sign: ':'
* pattern: "#,##0.###"
* month names: In Arabic
*
* modified en_US Date&Time formatter :
* minus sign: ':'
* pattern: "#,##0.###;#,##0.###-"
* month names: In English
*/
DecimalFormat dfEG = (DecimalFormat)NumberFormat.getInstance(localeEG);
DecimalFormat dfUS = (DecimalFormat)NumberFormat.getInstance(localeUS);
DecimalFormatSymbols dfsEG = dfEG.getDecimalFormatSymbols();
DecimalFormatSymbols dfsUS = dfUS.getDecimalFormatSymbols();
dfsEG.setMinusSign(originalMinusSign1);
dfsUS.setMinusSign(originalMinusSign1);
dfEG.setDecimalFormatSymbols(dfsUS);
dfUS.setDecimalFormatSymbols(dfsEG);
String patternEG = dfEG.toPattern();
String patternUS = dfUS.toPattern();
dfEG.applyPattern(patternUS);
dfUS.applyPattern(patternEG);
for (int i = 0; i < patterns.length; i++) {
System.out.println(BORDER);
for (int j = 2; j <= 3; j++) {
// Generate a pattern
String pattern = patterns[i].replaceAll(" ", delimiters[j]);
System.out.println("Pattern=\"" + pattern + "\"");
System.out.println("*** DateFormat.format test in modified en_US");
testDateFormatFormattingInRTL(pattern, i, j, dfUS, localeUS, true);
System.out.println("*** DateFormat.parse test in modified en_US");
testDateFormatParsingInRTL(pattern, i, j, dfUS, localeUS, true);
System.out.println("*** DateFormat.format test in modified ar_EG");
testDateFormatFormattingInLTR(pattern, i, j, dfEG, localeEG, false);
System.out.println("*** DateFormat.parse test in modified ar_EG");
testDateFormatParsingInLTR(pattern, i, j, dfEG, localeEG, false);
}
}
}
private static void testDateFormat3() {
/*
* modified ar_EG Date&Time formatter :
* minus sign: '\uff0d' // fullwidth minus
* pattern: "#,##0.###;#,##0.###-"
* month names: In Arabic
*
* modified en_US Date&Time formatter :
* minus sign: '\uff0d' // fullwidth minus
* pattern: "#,##0.###"
* month names: In English
*/
DecimalFormat dfEG = (DecimalFormat)NumberFormat.getInstance(localeEG);
DecimalFormat dfUS = (DecimalFormat)NumberFormat.getInstance(localeUS);
DecimalFormatSymbols dfsEG = dfEG.getDecimalFormatSymbols();
DecimalFormatSymbols dfsUS = dfUS.getDecimalFormatSymbols();
dfsEG.setMinusSign(originalMinusSign2);
dfsUS.setMinusSign(originalMinusSign2);
dfEG.setDecimalFormatSymbols(dfsEG);
dfUS.setDecimalFormatSymbols(dfsUS);
for (int i = 0; i < patterns.length; i++) {
System.out.println(BORDER);
for (int j = 4; j <= 5; j++) {
// Generate a pattern
String pattern = patterns[i].replaceAll(" ", delimiters[j]);
System.out.println("Pattern=\"" + pattern + "\"");
System.out.println("*** DateFormat.format test in modified ar_EG");
testDateFormatFormattingInRTL(pattern, i, j, dfEG, localeEG, false);
System.out.println("*** DateFormat.parse test in modified ar_EG");
testDateFormatParsingInRTL(pattern, i, j, dfEG, localeEG, false);
System.out.println("*** DateFormat.format test in modified en_US");
testDateFormatFormattingInLTR(pattern, i, j, dfUS, localeUS, true);
System.out.println("*** DateFormat.parse test in modified en_US");
testDateFormatParsingInLTR(pattern, i, j, dfUS, localeUS, true);
}
}
}
private static void testDateFormatFormattingInRTL(String pattern,
int basePattern,
int delimiter,
NumberFormat nf,
Locale locale,
boolean useEnglishMonthName) {
Locale.setDefault(locale);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
if (nf != null) {
sdf.setNumberFormat(nf);
}
for (int i = 0; i < datesToParse[basePattern].length; i++) {
if (datesEG[basePattern][i] == null) {
continue;
}
String expected = formattedDatesEG[basePattern][i]
.replaceAll("JUNE", (useEnglishMonthName ?
JuneInEnglish : JuneInArabic))
.replaceAll("JULY", (useEnglishMonthName ?
JulyInEnglish : JulyInArabic))
.replaceAll(" ", delimiters[delimiter]);
testDateFormatFormatting(sdf, pattern, datesEG[basePattern][i],
expected, locale.toString());
}
}
private static void testDateFormatFormattingInLTR(String pattern,
int basePattern,
int delimiter,
NumberFormat nf,
Locale locale,
boolean useEnglishMonthName) {
Locale.setDefault(locale);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
if (nf != null) {
sdf.setNumberFormat(nf);
}
for (int i = 0; i < datesToParse[basePattern].length; i++) {
if (datesUS[basePattern][i] == null) {
continue;
}
String expected = formattedDatesUS[basePattern][i]
.replaceAll("JUNE", (useEnglishMonthName ?
JuneInEnglish : JuneInArabic))
.replaceAll("JULY", (useEnglishMonthName ?
JulyInEnglish : JulyInArabic))
.replaceAll(" ", delimiters[delimiter]);
testDateFormatFormatting(sdf, pattern, datesUS[basePattern][i],
expected, locale.toString());
}
}
private static void testDateFormatFormatting(SimpleDateFormat sdf,
String pattern,
GregorianCalendar givenGC,
String expected,
String locale) {
Date given = givenGC.getTime();
String str = sdf.format(given);
if (expected.equals(str)) {
if (verbose) {
System.out.print(" Passed: SimpleDateFormat(");
System.out.print(locale + ", \"" + pattern + "\").format(");
System.out.println(given + ")");
System.out.print(" ---> \"" + str + "\" ");
System.out.println((givenGC.get(ERA) == BC) ? "(BC)" : "(AD)");
}
} else {
err = true;
System.err.print(" Failed: Unexpected SimpleDateFormat(");
System.out.print(locale + ", \"" + pattern + "\").format(");
System.out.println(given + ") result.");
System.out.println(" Expected: \"" + expected + "\"");
System.out.print(" Got: \"" + str + "\" ");
System.out.println((givenGC.get(ERA) == BC) ? "(BC)" : "(AD)");
}
}
private static void testDateFormatParsingInRTL(String pattern,
int basePattern,
int delimiter,
NumberFormat nf,
Locale locale,
boolean useEnglishMonthName) {
Locale.setDefault(locale);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
if (nf != null) {
sdf.setNumberFormat(nf);
}
for (int i = 0; i < datesToParse[basePattern].length; i++) {
String given = datesToParse[basePattern][i]
.replaceAll(" ", specialDelimiters[0][delimiter])
.replaceAll(" ", delimiters[delimiter]);
testDateFormatParsing(sdf, pattern,
given.replaceAll("JULY", (useEnglishMonthName ?
JulyInEnglish : JulyInArabic)),
datesEG[basePattern][i], locale.toString());
}
}
private static void testDateFormatParsingInLTR(String pattern,
int basePattern,
int delimiter,
NumberFormat nf,
Locale locale,
boolean useEnglishMonthName) {
Locale.setDefault(locale);
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
if (nf != null) {
sdf.setNumberFormat(nf);
}
for (int i = 0; i < datesToParse[basePattern].length; i++) {
String given = datesToParse[basePattern][i]
.replaceAll(" ", specialDelimiters[1][delimiter])
.replaceAll(" ", delimiters[delimiter]);
testDateFormatParsing(sdf, pattern,
given.replaceAll("JULY", (useEnglishMonthName ?
JulyInEnglish : JulyInArabic)),
datesUS[basePattern][i], locale.toString());
}
}
private static void testDateFormatParsing(SimpleDateFormat sdf,
String pattern,
String given,
GregorianCalendar expectedGC,
String locale) {
try {
Date d = sdf.parse(given);
if (expectedGC == null) {
err = true;
System.err.print(" Failed: SimpleDateFormat(" + locale);
System.err.print(", \"" + pattern + "\").parse(\"" + given);
System.err.println("\") should have thrown ParseException");
} else if (expectedGC.getTime().equals(d)) {
if (verbose) {
System.out.print(" Passed: SimpleDateFormat(" + locale);
System.out.print(", \"" + pattern + "\").parse(\"" + given);
System.out.println("\")");
System.out.print(" ---> " + d + " (" + d.getTime());
System.out.println(")");
}
} else {
err = true;
System.err.print(" Failed: SimpleDateFormat(" + locale);
System.err.print(", \"" + pattern + "\").parse(\"" + given);
System.err.println("\")");
System.err.print(" Expected: " + expectedGC.getTime());
System.err.println(" (" + d.getTime() + ")");
System.err.print(" Got: " + d + " (" + d.getTime());
System.err.println(")");
System.err.print(" Pattern: \"");
System.err.print(((DecimalFormat)sdf.getNumberFormat()).toPattern());
System.err.println("\"");
}
}
catch (ParseException pe) {
if (expectedGC == null) {
if (verbose) {
System.out.print(" Passed: SimpleDateFormat(" + locale);
System.out.print(", \"" + pattern + "\").parse(\"" + given);
System.out.println("\")");
System.out.println(" threw ParseException as expected");
}
} else {
err = true;
System.err.println(" Failed: Unexpected exception with");
System.err.print(" SimpleDateFormat(" + locale);
System.err.print(", \"" + pattern + "\").parse(\"");
System.err.println(given + "\"):");
System.err.println(" " + pe);
System.err.print(" Pattern: \"");
System.err.print(((DecimalFormat)sdf.getNumberFormat()).toPattern());
System.err.println("\"");
System.err.print(" Month 0: ");
System.err.println(sdf.getDateFormatSymbols().getMonths()[0]);
}
}
}
//
// NumberFormat test
//
private static void testNumberFormat() {
NumberFormat nfEG = NumberFormat.getInstance(localeEG);
NumberFormat nfUS = NumberFormat.getInstance(localeUS);
System.out.println("*** DecimalFormat.format test in ar_EG");
testNumberFormatFormatting(nfEG, -123456789, "123,456,789-", "ar_EG");
testNumberFormatFormatting(nfEG, -456, "456-", "ar_EG");
System.out.println("*** DecimalFormat.parse test in ar_EG");
testNumberFormatParsing(nfEG, "123-", -123L, "ar_EG");
testNumberFormatParsing(nfEG, "123--",-123L, "ar_EG");
testNumberFormatParsingCheckException(nfEG, "-123", 0, "ar_EG");
System.out.println("*** DecimalFormat.format test in en_US");
testNumberFormatFormatting(nfUS, -123456789, "-123,456,789", "en_US");
testNumberFormatFormatting(nfUS, -456, "-456", "en_US");
System.out.println("*** DecimalFormat.parse test in en_US");
testNumberFormatParsing(nfUS, "123-", 123L, "en_US");
testNumberFormatParsing(nfUS, "-123",-123L, "en_US");
testNumberFormatParsingCheckException(nfUS, "--123", 0, "en_US");
}
private static void testNumberFormatFormatting(NumberFormat nf,
int given,
String expected,
String locale) {
String str = nf.format(given);
if (expected.equals(str)) {
if (verbose) {
System.out.print(" Passed: NumberFormat(" + locale);
System.out.println(").format(" + given + ")");
System.out.println(" ---> \"" + str + "\"");
}
} else {
err = true;
System.err.print(" Failed: Unexpected NumberFormat(" + locale);
System.err.println(").format(" + given + ") result.");
System.err.println(" Expected: \"" + expected + "\"");
System.err.println(" Got: \"" + str + "\"");
}
}
private static void testNumberFormatParsing(NumberFormat nf,
String given,
Number expected,
String locale) {
try {
Number n = nf.parse(given);
if (n.equals(expected)) {
if (verbose) {
System.out.print(" Passed: NumberFormat(" + locale);
System.out.println(").parse(\"" + given + "\")");
System.out.println(" ---> " + n);
}
} else {
err = true;
System.err.print(" Failed: Unexpected NumberFormat(" + locale);
System.err.println(").parse(\"" + given + "\") result.");
System.err.println(" Expected: " + expected);
System.err.println(" Got: " + n);
}
}
catch (ParseException pe) {
err = true;
System.err.print(" Failed: Unexpected exception with NumberFormat(");
System.err.println(locale + ").parse(\"" + given + "\") :");
System.err.println(" " + pe);
}
}
private static void testNumberFormatParsingCheckException(NumberFormat nf,
String given,
int expected,
String locale) {
try {
Number n = nf.parse(given);
err = true;
System.err.print(" Failed: NumberFormat(" + locale);
System.err.println(").parse(\"" + given + "\")");
System.err.println(" should have thrown ParseException");
}
catch (ParseException pe) {
int errorOffset = pe.getErrorOffset();
if (errorOffset == expected) {
if (verbose) {
System.out.print(" Passed: NumberFormat(" + locale);
System.out.println(").parse(\"" + given + "\")");
System.out.print(" threw ParseException as expected, and its errorOffset was correct: ");
System.out.println(errorOffset);
}
} else {
err = true;
System.err.print(" Failed: NumberFormat(" + locale);
System.err.println(").parse(\"" + given + "\")");
System.err.print(" threw ParseException as expected, but its errorOffset was incorrect: ");
System.err.println(errorOffset);
}
}
}
}
|