637N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
637N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
637N/A *
637N/A * This code is free software; you can redistribute it and/or modify it
637N/A * under the terms of the GNU General Public License version 2 only, as
637N/A * published by the Free Software Foundation.
637N/A *
637N/A * This code is distributed in the hope that it will be useful, but WITHOUT
637N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
637N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
637N/A * version 2 for more details (a copy is included in the LICENSE file that
637N/A * accompanied this code).
637N/A *
637N/A * You should have received a copy of the GNU General Public License version
637N/A * 2 along with this work; if not, write to the Free Software Foundation,
637N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
637N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
637N/A */
637N/A
637N/A/**
637N/A * @test
637N/A * @bug 4823811
637N/A * @summary Confirm that text which includes numbers with a trailing minus sign is parsed correctly.
822N/A * @run main/othervm -Duser.timezone=GMT+09:00 Bug4823811
637N/A */
637N/A
637N/Aimport java.text.*;
637N/Aimport java.util.*;
637N/A
637N/Apublic class Bug4823811 {
637N/A
637N/A private static Locale localeEG = new Locale("ar", "EG");
637N/A private static Locale localeUS = Locale.US;
637N/A
637N/A private static String JuneInArabic = "\u064a\u0648\u0646\u064a\u0648";
637N/A private static String JulyInArabic = "\u064a\u0648\u0644\u064a\u0648";
637N/A private static String JuneInEnglish = "June";
637N/A private static String JulyInEnglish = "July";
637N/A
637N/A private static String BORDER =
637N/A "============================================================";
637N/A
637N/A /*
637N/A * I don't use static import here intentionally so that this test program
637N/A * can be run on JDK 1.4.2.
637N/A */
637N/A private static int ERA = Calendar.ERA;
637N/A private static int BC = GregorianCalendar.BC;
637N/A// private static int JAN = Calendar.JANUARY;
637N/A// private static int FEB = Calendar.FEBRUARY;
637N/A// private static int MAR = Calendar.MARCH;
637N/A private static int APR = Calendar.APRIL;
637N/A private static int MAY = Calendar.MAY;
637N/A private static int JUN = Calendar.JUNE;
637N/A private static int JUL = Calendar.JULY;
637N/A// private static int AUG = Calendar.AUGUST;
637N/A// private static int SEP = Calendar.SEPTEMBER;
637N/A// private static int OCT = Calendar.OCTOBER;
637N/A// private static int NOV = Calendar.NOVEMBER;
637N/A// private static int DEC = Calendar.DECEMBER;
637N/A
637N/A private static String[] patterns = {
637N/A "yyyy MMMM d H m s",
637N/A "yyyy MM dd hh mm ss",
637N/A
637N/A /*
637N/A * Because 1-based HOUR_OF_DAY, 1-based HOUR, MONTH, and YEAR fields
637N/A * are parsed using different code from the code for other numeric
637N/A * fields, I prepared YEAR-preceding patterns and SECOND-preceding
637N/A * patterns.
637N/A */
637N/A "yyyy M d h m s",
637N/A " yyyy M d h m s",
637N/A "yyyy M d h m s ",
637N/A
637N/A "s m h d M yyyy",
637N/A " s m h d M yyyy",
637N/A "s m h d M yyyy ",
637N/A };
637N/A
637N/A private static char originalMinusSign1 = ':';
637N/A private static char originalMinusSign2 = '\uff0d'; // fullwidth minus
637N/A private static String[] delimiters = {"-", "/", ":", "/", "\uff0d", "/"};
637N/A private static String[][] specialDelimiters = {
637N/A // for Arabic formatter and modified English formatter
637N/A {"--", "-/", "::", ":/", "\uff0d\uff0d", "\uff0d/"},
637N/A
637N/A // for English formatter and modified Arabic formatter
637N/A {"--", "/-", "::", "/:", "\uff0d\uff0d", "/\uff0d"},
637N/A };
637N/A
637N/A /*
637N/A * Format:
637N/A * +-------------------------------------------------------------------+
637N/A * | Input | Output |
637N/A * +---------------------+---------------------------------------------|
637N/A * | datesEG & datesUS | formattedDatesEG & formattedDatesUS |
637N/A * +-------------------------------------------------------------------+
637N/A *
637N/A * Parse:
637N/A * +-------------------------------------------------------------------+
637N/A * | Input | Output |
637N/A * |---------------------+---------------------------------------------|
637N/A * | datesToParse | datesEG & datesUS |
637N/A * +-------------------------------------------------------------------+
637N/A */
637N/A private static String[][] datesToParse = {
637N/A // "JUNE" and "JULY" are replaced with a localized month name later.
637N/A {"2008 JULY 20 3 12 83",
637N/A "2008 JULY 20 3 12 83",
637N/A "2008 JULY 20 3 12 83"},
637N/A
637N/A {"2008 07 20 03 12 83",
637N/A "2008 07 20 03 12 83",
637N/A "2008 07 20 03 12 83"},
637N/A
637N/A {"2008 7 20 3 12 83",
637N/A "2008 7 20 3 12 83",
637N/A "2008 7 20 3 12 83"},
637N/A
637N/A {" 2008 7 20 3 12 83",
637N/A " 2008 7 20 3 12 83",
637N/A " 2008 7 20 3 12 83",
637N/A "2008 7 20 3 12 83"},
637N/A
637N/A {"2008 7 20 3 12 83 ",
637N/A "2008 7 20 3 12 83 ",
637N/A "2008 7 20 3 12 83"},
637N/A
637N/A {"83 12 3 20 7 2008",
637N/A "83 12 3 20 7 2008",
637N/A "83 12 3 20 7 2008"},
637N/A
637N/A {" 83 12 3 20 7 2008",
637N/A " 83 12 3 20 7 2008",
637N/A " 83 12 3 20 7 2008",
637N/A "83 12 3 20 7 2008"},
637N/A
637N/A {"83 12 3 20 7 2008 ",
637N/A "83 12 3 20 7 2008 ",
637N/A "83 12 3 20 7 2008"},
637N/A };
637N/A
637N/A // For formatting
637N/A private static String[][] formattedDatesEG = {
637N/A {"2008 JULY 20 3 13 23",
637N/A "2009 JULY 20 3 13 23",
637N/A null},
637N/A
637N/A {"2008 07 20 03 13 23",
637N/A "2009 07 20 03 13 23",
637N/A "2007 05 20 03 13 23"},
637N/A
637N/A {"2008 7 20 3 13 23",
637N/A "2009 6 10 3 13 23",
637N/A "2007 4 10 3 13 23"},
637N/A
637N/A {" 2008 7 20 3 13 23",
637N/A null,
637N/A " 2009 7 20 3 13 23",
637N/A null},
637N/A
637N/A {"2008 7 20 3 13 23 ",
637N/A "2008 7 20 3 10 37 ",
637N/A null},
637N/A
637N/A {"23 13 3 20 7 2008",
637N/A "37 10 9 19 7 2008",
637N/A "23 49 8 19 7 2008"},
637N/A
637N/A {" 23 13 3 20 7 2008",
637N/A null,
637N/A " 37 10 3 20 7 2008",
637N/A null},
637N/A
637N/A {"23 13 3 20 7 2008 ",
637N/A "23 13 3 20 7 2009 ",
637N/A null},
637N/A };
637N/A
637N/A private static String[][] formattedDatesUS = {
637N/A {"2008 JULY 20 3 13 23",
637N/A null,
637N/A "2008 JUNE 10 3 13 23"},
637N/A
637N/A {"2008 07 20 03 13 23",
637N/A "2007 05 20 03 13 23",
637N/A "2008 06 10 03 13 23"},
637N/A
637N/A {"2008 7 20 3 13 23",
637N/A "2007 5 19 9 13 23",
637N/A "2008 6 9 9 13 23"},
637N/A
637N/A {" 2008 7 20 3 13 23",
637N/A " 2009 7 20 3 13 23",
637N/A " 2007 5 20 3 13 23",
637N/A null},
637N/A
637N/A {"2008 7 20 3 13 23 ",
637N/A "2008 7 20 3 13 23 ",
637N/A null},
637N/A
637N/A {"23 13 3 20 7 2008",
637N/A "23 49 2 10 6 2008",
637N/A "23 13 9 9 6 2008"},
637N/A
637N/A {" 23 13 3 20 7 2008",
637N/A " 37 10 3 20 7 2008",
637N/A " 23 49 2 20 7 2008",
637N/A null},
637N/A
637N/A {"23 13 3 20 7 2008 ",
637N/A "23 13 3 20 7 2008 ",
637N/A null},
637N/A };
637N/A
637N/A private static GregorianCalendar[][] datesEG = {
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar(-2008, JUL, 20, 3, 12, 83),
637N/A null},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar(-2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar( 2007, MAY, 20, 3, 12, 83)},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar(-2008, JUL, -20, 3, 12, 83),
637N/A new GregorianCalendar( 2007, APR, 10, 3, 12, 83)},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A null,
637N/A new GregorianCalendar(-2008, JUL, 20, 3, 12, 83),
637N/A null},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar( 2008, JUL, 20, 3, 12, -83),
637N/A null},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar( 2008, JUL, 20, -3, 12, -83),
637N/A new GregorianCalendar( 2008, JUL, 20, -3, -12, 83)},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A null,
637N/A new GregorianCalendar( 2008, JUL, 20, 3, 12, -83),
637N/A null},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar(-2008, JUL, 20, 3, 12, 83),
637N/A null},
637N/A };
637N/A
637N/A private static GregorianCalendar[][] datesUS = {
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A null,
637N/A new GregorianCalendar( 2008, JUN, 10, 3, 12, 83)},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar( 2007, MAY, 20, 3, 12, 83),
637N/A new GregorianCalendar( 2008, JUN, 10, 3, 12, 83)},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar( 2007, MAY, 20, -3, 12, 83),
637N/A new GregorianCalendar( 2008, JUL, -20, -3, 12, 83)},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar(-2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar( 2007, MAY, 20, 3, 12, 83),
637N/A null},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A null},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar( 2008, JUL, -20, 3, -12, 83),
637N/A new GregorianCalendar( 2008, JUL, -20, -3, 12, 83)},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar( 2008, JUL, 20, 3, 12, -83),
637N/A new GregorianCalendar( 2008, JUL, 20, 3, -12, 83),
637N/A null},
637N/A
637N/A {new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A new GregorianCalendar( 2008, JUL, 20, 3, 12, 83),
637N/A null},
637N/A };
637N/A
637N/A /* flags */
637N/A private static boolean err = false;
637N/A private static boolean verbose = false;
637N/A
637N/A
637N/A public static void main(String[] args) {
637N/A if (args.length == 1 && args[0].equals("-v")) {
637N/A verbose = true;
637N/A }
637N/A
637N/A Locale defaultLocale = Locale.getDefault();
637N/A TimeZone defaultTimeZone = TimeZone.getDefault();
637N/A
637N/A TimeZone.setDefault(TimeZone.getTimeZone("Asia/Tokyo"));
637N/A
637N/A try {
637N/A /*
637N/A * Test SimpleDateFormat.parse() and format() for original
637N/A * SimpleDateFormat instances
637N/A */
637N/A testDateFormat1();
637N/A
637N/A /*
637N/A * Test SimpleDateFormat.parse() and format() for modified
637N/A * SimpleDateFormat instances using an original minus sign,
637N/A * pattern, and diffenrent month names in DecimalFormat
637N/A */
637N/A testDateFormat2();
637N/A
637N/A /*
637N/A * Test SimpleDateFormat.parse() and format() for modified
637N/A * SimpleDateFormat instances using a fullwidth minus sign
637N/A */
637N/A testDateFormat3();
637N/A
637N/A /*
637N/A * Just to confirm that regressions aren't introduced in
637N/A * DecimalFormat. This cannot happen, though. Because I didn't
637N/A * change DecimalFormat at all.
637N/A */
637N/A testNumberFormat();
637N/A }
637N/A catch (Exception e) {
637N/A err = true;
637N/A System.err.println("Unexpected exception: " + e);
637N/A }
637N/A finally {
637N/A Locale.setDefault(defaultLocale);
637N/A TimeZone.setDefault(defaultTimeZone);
637N/A
637N/A if (err) {
637N/A System.err.println(BORDER + " Test failed.");
637N/A throw new RuntimeException("Date/Number formatting/parsing error.");
637N/A } else {
637N/A System.out.println(BORDER + " Test passed.");
637N/A }
637N/A }
637N/A }
637N/A
637N/A
637N/A //
637N/A // DateFormat test
637N/A //
637N/A private static void testDateFormat1() {
637N/A for (int i = 0; i < patterns.length; i++) {
637N/A System.out.println(BORDER);
637N/A for (int j = 0; j <= 1; j++) {
637N/A // Generate a pattern
637N/A String pattern = patterns[i].replaceAll(" ", delimiters[j]);
637N/A System.out.println("Pattern=\"" + pattern + "\"");
637N/A
637N/A System.out.println("*** DateFormat.format test in ar_EG");
637N/A testDateFormatFormattingInRTL(pattern, i, j, null, localeEG, false);
637N/A
637N/A System.out.println("*** DateFormat.parse test in ar_EG");
637N/A testDateFormatParsingInRTL(pattern, i, j, null, localeEG, false);
637N/A
637N/A System.out.println("*** DateFormat.format test in en_US");
637N/A testDateFormatFormattingInLTR(pattern, i, j, null, localeUS, true);
637N/A
637N/A System.out.println("*** DateFormat.parse test in en_US");
637N/A testDateFormatParsingInLTR(pattern, i, j, null, localeUS, true);
637N/A }
637N/A }
637N/A }
637N/A
637N/A private static void testDateFormat2() {
637N/A /*
637N/A * modified ar_EG Date&Time formatter :
637N/A * minus sign: ':'
637N/A * pattern: "#,##0.###"
637N/A * month names: In Arabic
637N/A *
637N/A * modified en_US Date&Time formatter :
637N/A * minus sign: ':'
637N/A * pattern: "#,##0.###;#,##0.###-"
637N/A * month names: In English
637N/A */
637N/A DecimalFormat dfEG = (DecimalFormat)NumberFormat.getInstance(localeEG);
637N/A DecimalFormat dfUS = (DecimalFormat)NumberFormat.getInstance(localeUS);
637N/A
637N/A DecimalFormatSymbols dfsEG = dfEG.getDecimalFormatSymbols();
637N/A DecimalFormatSymbols dfsUS = dfUS.getDecimalFormatSymbols();
637N/A dfsEG.setMinusSign(originalMinusSign1);
637N/A dfsUS.setMinusSign(originalMinusSign1);
637N/A dfEG.setDecimalFormatSymbols(dfsUS);
637N/A dfUS.setDecimalFormatSymbols(dfsEG);
637N/A
637N/A String patternEG = dfEG.toPattern();
637N/A String patternUS = dfUS.toPattern();
637N/A
637N/A dfEG.applyPattern(patternUS);
637N/A dfUS.applyPattern(patternEG);
637N/A
637N/A for (int i = 0; i < patterns.length; i++) {
637N/A System.out.println(BORDER);
637N/A for (int j = 2; j <= 3; j++) {
637N/A // Generate a pattern
637N/A String pattern = patterns[i].replaceAll(" ", delimiters[j]);
637N/A System.out.println("Pattern=\"" + pattern + "\"");
637N/A
637N/A System.out.println("*** DateFormat.format test in modified en_US");
637N/A testDateFormatFormattingInRTL(pattern, i, j, dfUS, localeUS, true);
637N/A
637N/A System.out.println("*** DateFormat.parse test in modified en_US");
637N/A testDateFormatParsingInRTL(pattern, i, j, dfUS, localeUS, true);
637N/A
637N/A System.out.println("*** DateFormat.format test in modified ar_EG");
637N/A testDateFormatFormattingInLTR(pattern, i, j, dfEG, localeEG, false);
637N/A
637N/A System.out.println("*** DateFormat.parse test in modified ar_EG");
637N/A testDateFormatParsingInLTR(pattern, i, j, dfEG, localeEG, false);
637N/A }
637N/A }
637N/A }
637N/A
637N/A private static void testDateFormat3() {
637N/A /*
637N/A * modified ar_EG Date&Time formatter :
637N/A * minus sign: '\uff0d' // fullwidth minus
637N/A * pattern: "#,##0.###;#,##0.###-"
637N/A * month names: In Arabic
637N/A *
637N/A * modified en_US Date&Time formatter :
637N/A * minus sign: '\uff0d' // fullwidth minus
637N/A * pattern: "#,##0.###"
637N/A * month names: In English
637N/A */
637N/A DecimalFormat dfEG = (DecimalFormat)NumberFormat.getInstance(localeEG);
637N/A DecimalFormat dfUS = (DecimalFormat)NumberFormat.getInstance(localeUS);
637N/A
637N/A DecimalFormatSymbols dfsEG = dfEG.getDecimalFormatSymbols();
637N/A DecimalFormatSymbols dfsUS = dfUS.getDecimalFormatSymbols();
637N/A dfsEG.setMinusSign(originalMinusSign2);
637N/A dfsUS.setMinusSign(originalMinusSign2);
637N/A dfEG.setDecimalFormatSymbols(dfsEG);
637N/A dfUS.setDecimalFormatSymbols(dfsUS);
637N/A
637N/A for (int i = 0; i < patterns.length; i++) {
637N/A System.out.println(BORDER);
637N/A for (int j = 4; j <= 5; j++) {
637N/A // Generate a pattern
637N/A String pattern = patterns[i].replaceAll(" ", delimiters[j]);
637N/A System.out.println("Pattern=\"" + pattern + "\"");
637N/A
637N/A System.out.println("*** DateFormat.format test in modified ar_EG");
637N/A testDateFormatFormattingInRTL(pattern, i, j, dfEG, localeEG, false);
637N/A
637N/A System.out.println("*** DateFormat.parse test in modified ar_EG");
637N/A testDateFormatParsingInRTL(pattern, i, j, dfEG, localeEG, false);
637N/A
637N/A System.out.println("*** DateFormat.format test in modified en_US");
637N/A testDateFormatFormattingInLTR(pattern, i, j, dfUS, localeUS, true);
637N/A
637N/A System.out.println("*** DateFormat.parse test in modified en_US");
637N/A testDateFormatParsingInLTR(pattern, i, j, dfUS, localeUS, true);
637N/A }
637N/A }
637N/A }
637N/A
637N/A private static void testDateFormatFormattingInRTL(String pattern,
637N/A int basePattern,
637N/A int delimiter,
637N/A NumberFormat nf,
637N/A Locale locale,
637N/A boolean useEnglishMonthName) {
637N/A Locale.setDefault(locale);
637N/A
637N/A SimpleDateFormat sdf = new SimpleDateFormat(pattern);
637N/A if (nf != null) {
637N/A sdf.setNumberFormat(nf);
637N/A }
637N/A for (int i = 0; i < datesToParse[basePattern].length; i++) {
637N/A if (datesEG[basePattern][i] == null) {
637N/A continue;
637N/A }
637N/A
637N/A String expected = formattedDatesEG[basePattern][i]
637N/A .replaceAll("JUNE", (useEnglishMonthName ?
637N/A JuneInEnglish : JuneInArabic))
637N/A .replaceAll("JULY", (useEnglishMonthName ?
637N/A JulyInEnglish : JulyInArabic))
637N/A .replaceAll(" ", delimiters[delimiter]);
637N/A testDateFormatFormatting(sdf, pattern, datesEG[basePattern][i],
637N/A expected, locale.toString());
637N/A }
637N/A }
637N/A
637N/A private static void testDateFormatFormattingInLTR(String pattern,
637N/A int basePattern,
637N/A int delimiter,
637N/A NumberFormat nf,
637N/A Locale locale,
637N/A boolean useEnglishMonthName) {
637N/A Locale.setDefault(locale);
637N/A
637N/A SimpleDateFormat sdf = new SimpleDateFormat(pattern);
637N/A if (nf != null) {
637N/A sdf.setNumberFormat(nf);
637N/A }
637N/A for (int i = 0; i < datesToParse[basePattern].length; i++) {
637N/A if (datesUS[basePattern][i] == null) {
637N/A continue;
637N/A }
637N/A
637N/A String expected = formattedDatesUS[basePattern][i]
637N/A .replaceAll("JUNE", (useEnglishMonthName ?
637N/A JuneInEnglish : JuneInArabic))
637N/A .replaceAll("JULY", (useEnglishMonthName ?
637N/A JulyInEnglish : JulyInArabic))
637N/A .replaceAll(" ", delimiters[delimiter]);
637N/A testDateFormatFormatting(sdf, pattern, datesUS[basePattern][i],
637N/A expected, locale.toString());
637N/A }
637N/A }
637N/A
637N/A private static void testDateFormatFormatting(SimpleDateFormat sdf,
637N/A String pattern,
637N/A GregorianCalendar givenGC,
637N/A String expected,
637N/A String locale) {
637N/A Date given = givenGC.getTime();
637N/A String str = sdf.format(given);
637N/A if (expected.equals(str)) {
637N/A if (verbose) {
637N/A System.out.print(" Passed: SimpleDateFormat(");
637N/A System.out.print(locale + ", \"" + pattern + "\").format(");
637N/A System.out.println(given + ")");
637N/A
637N/A System.out.print(" ---> \"" + str + "\" ");
637N/A System.out.println((givenGC.get(ERA) == BC) ? "(BC)" : "(AD)");
637N/A }
637N/A } else {
637N/A err = true;
637N/A
637N/A System.err.print(" Failed: Unexpected SimpleDateFormat(");
637N/A System.out.print(locale + ", \"" + pattern + "\").format(");
637N/A System.out.println(given + ") result.");
637N/A
637N/A System.out.println(" Expected: \"" + expected + "\"");
637N/A
637N/A System.out.print(" Got: \"" + str + "\" ");
637N/A System.out.println((givenGC.get(ERA) == BC) ? "(BC)" : "(AD)");
637N/A }
637N/A }
637N/A
637N/A private static void testDateFormatParsingInRTL(String pattern,
637N/A int basePattern,
637N/A int delimiter,
637N/A NumberFormat nf,
637N/A Locale locale,
637N/A boolean useEnglishMonthName) {
637N/A Locale.setDefault(locale);
637N/A
637N/A SimpleDateFormat sdf = new SimpleDateFormat(pattern);
637N/A if (nf != null) {
637N/A sdf.setNumberFormat(nf);
637N/A }
637N/A for (int i = 0; i < datesToParse[basePattern].length; i++) {
637N/A String given = datesToParse[basePattern][i]
637N/A .replaceAll(" ", specialDelimiters[0][delimiter])
637N/A .replaceAll(" ", delimiters[delimiter]);
637N/A
637N/A testDateFormatParsing(sdf, pattern,
637N/A given.replaceAll("JULY", (useEnglishMonthName ?
637N/A JulyInEnglish : JulyInArabic)),
637N/A datesEG[basePattern][i], locale.toString());
637N/A }
637N/A }
637N/A
637N/A private static void testDateFormatParsingInLTR(String pattern,
637N/A int basePattern,
637N/A int delimiter,
637N/A NumberFormat nf,
637N/A Locale locale,
637N/A boolean useEnglishMonthName) {
637N/A Locale.setDefault(locale);
637N/A
637N/A SimpleDateFormat sdf = new SimpleDateFormat(pattern);
637N/A if (nf != null) {
637N/A sdf.setNumberFormat(nf);
637N/A }
637N/A for (int i = 0; i < datesToParse[basePattern].length; i++) {
637N/A String given = datesToParse[basePattern][i]
637N/A .replaceAll(" ", specialDelimiters[1][delimiter])
637N/A .replaceAll(" ", delimiters[delimiter]);
637N/A
637N/A testDateFormatParsing(sdf, pattern,
637N/A given.replaceAll("JULY", (useEnglishMonthName ?
637N/A JulyInEnglish : JulyInArabic)),
637N/A datesUS[basePattern][i], locale.toString());
637N/A }
637N/A }
637N/A
637N/A private static void testDateFormatParsing(SimpleDateFormat sdf,
637N/A String pattern,
637N/A String given,
637N/A GregorianCalendar expectedGC,
637N/A String locale) {
637N/A try {
637N/A Date d = sdf.parse(given);
637N/A if (expectedGC == null) {
637N/A err = true;
637N/A System.err.print(" Failed: SimpleDateFormat(" + locale);
637N/A System.err.print(", \"" + pattern + "\").parse(\"" + given);
637N/A System.err.println("\") should have thrown ParseException");
637N/A } else if (expectedGC.getTime().equals(d)) {
637N/A if (verbose) {
637N/A System.out.print(" Passed: SimpleDateFormat(" + locale);
637N/A System.out.print(", \"" + pattern + "\").parse(\"" + given);
637N/A System.out.println("\")");
637N/A
637N/A System.out.print(" ---> " + d + " (" + d.getTime());
637N/A System.out.println(")");
637N/A }
637N/A } else {
637N/A err = true;
637N/A System.err.print(" Failed: SimpleDateFormat(" + locale);
637N/A System.err.print(", \"" + pattern + "\").parse(\"" + given);
637N/A System.err.println("\")");
637N/A
637N/A System.err.print(" Expected: " + expectedGC.getTime());
637N/A System.err.println(" (" + d.getTime() + ")");
637N/A
637N/A System.err.print(" Got: " + d + " (" + d.getTime());
637N/A System.err.println(")");
637N/A
637N/A System.err.print(" Pattern: \"");
637N/A System.err.print(((DecimalFormat)sdf.getNumberFormat()).toPattern());
637N/A System.err.println("\"");
637N/A }
637N/A }
637N/A catch (ParseException pe) {
637N/A if (expectedGC == null) {
637N/A if (verbose) {
637N/A System.out.print(" Passed: SimpleDateFormat(" + locale);
637N/A System.out.print(", \"" + pattern + "\").parse(\"" + given);
637N/A System.out.println("\")");
637N/A
637N/A System.out.println(" threw ParseException as expected");
637N/A }
637N/A } else {
637N/A err = true;
637N/A System.err.println(" Failed: Unexpected exception with");
637N/A
637N/A System.err.print(" SimpleDateFormat(" + locale);
637N/A System.err.print(", \"" + pattern + "\").parse(\"");
637N/A System.err.println(given + "\"):");
637N/A
637N/A System.err.println(" " + pe);
637N/A
637N/A System.err.print(" Pattern: \"");
637N/A System.err.print(((DecimalFormat)sdf.getNumberFormat()).toPattern());
637N/A System.err.println("\"");
637N/A
637N/A System.err.print(" Month 0: ");
637N/A System.err.println(sdf.getDateFormatSymbols().getMonths()[0]);
637N/A }
637N/A }
637N/A }
637N/A
637N/A
637N/A //
637N/A // NumberFormat test
637N/A //
637N/A private static void testNumberFormat() {
637N/A NumberFormat nfEG = NumberFormat.getInstance(localeEG);
637N/A NumberFormat nfUS = NumberFormat.getInstance(localeUS);
637N/A
637N/A System.out.println("*** DecimalFormat.format test in ar_EG");
637N/A testNumberFormatFormatting(nfEG, -123456789, "123,456,789-", "ar_EG");
637N/A testNumberFormatFormatting(nfEG, -456, "456-", "ar_EG");
637N/A
637N/A System.out.println("*** DecimalFormat.parse test in ar_EG");
637N/A testNumberFormatParsing(nfEG, "123-", new Long(-123), "ar_EG");
637N/A testNumberFormatParsing(nfEG, "123--", new Long(-123), "ar_EG");
637N/A testNumberFormatParsingCheckException(nfEG, "-123", 0, "ar_EG");
637N/A
637N/A System.out.println("*** DecimalFormat.format test in en_US");
637N/A testNumberFormatFormatting(nfUS, -123456789, "-123,456,789", "en_US");
637N/A testNumberFormatFormatting(nfUS, -456, "-456", "en_US");
637N/A
637N/A System.out.println("*** DecimalFormat.parse test in en_US");
637N/A testNumberFormatParsing(nfUS, "123-", new Long(123), "en_US");
637N/A testNumberFormatParsing(nfUS, "-123", new Long(-123), "en_US");
637N/A testNumberFormatParsingCheckException(nfUS, "--123", 0, "en_US");
637N/A }
637N/A
637N/A private static void testNumberFormatFormatting(NumberFormat nf,
637N/A int given,
637N/A String expected,
637N/A String locale) {
637N/A String str = nf.format(given);
637N/A if (expected.equals(str)) {
637N/A if (verbose) {
637N/A System.out.print(" Passed: NumberFormat(" + locale);
637N/A System.out.println(").format(" + given + ")");
637N/A
637N/A System.out.println(" ---> \"" + str + "\"");
637N/A }
637N/A } else {
637N/A err = true;
637N/A System.err.print(" Failed: Unexpected NumberFormat(" + locale);
637N/A System.err.println(").format(" + given + ") result.");
637N/A
637N/A System.err.println(" Expected: \"" + expected + "\"");
637N/A
637N/A System.err.println(" Got: \"" + str + "\"");
637N/A }
637N/A }
637N/A
637N/A private static void testNumberFormatParsing(NumberFormat nf,
637N/A String given,
637N/A Number expected,
637N/A String locale) {
637N/A try {
637N/A Number n = nf.parse(given);
637N/A if (n.equals(expected)) {
637N/A if (verbose) {
637N/A System.out.print(" Passed: NumberFormat(" + locale);
637N/A System.out.println(").parse(\"" + given + "\")");
637N/A
637N/A System.out.println(" ---> " + n);
637N/A }
637N/A } else {
637N/A err = true;
637N/A System.err.print(" Failed: Unexpected NumberFormat(" + locale);
637N/A System.err.println(").parse(\"" + given + "\") result.");
637N/A
637N/A System.err.println(" Expected: " + expected);
637N/A
637N/A System.err.println(" Got: " + n);
637N/A }
637N/A }
637N/A catch (ParseException pe) {
637N/A err = true;
637N/A System.err.print(" Failed: Unexpected exception with NumberFormat(");
637N/A System.err.println(locale + ").parse(\"" + given + "\") :");
637N/A
637N/A System.err.println(" " + pe);
637N/A }
637N/A }
637N/A
637N/A private static void testNumberFormatParsingCheckException(NumberFormat nf,
637N/A String given,
637N/A int expected,
637N/A String locale) {
637N/A try {
637N/A Number n = nf.parse(given);
637N/A err = true;
637N/A
637N/A System.err.print(" Failed: NumberFormat(" + locale);
637N/A System.err.println(").parse(\"" + given + "\")");
637N/A
637N/A System.err.println(" should have thrown ParseException");
637N/A }
637N/A catch (ParseException pe) {
637N/A int errorOffset = pe.getErrorOffset();
637N/A if (errorOffset == expected) {
637N/A if (verbose) {
637N/A System.out.print(" Passed: NumberFormat(" + locale);
637N/A System.out.println(").parse(\"" + given + "\")");
637N/A
637N/A System.out.print(" threw ParseException as expected, and its errorOffset was correct: ");
637N/A System.out.println(errorOffset);
637N/A }
637N/A } else {
637N/A err = true;
637N/A System.err.print(" Failed: NumberFormat(" + locale);
637N/A System.err.println(").parse(\"" + given + "\")");
637N/A
637N/A System.err.print(" threw ParseException as expected, but its errorOffset was incorrect: ");
637N/A System.err.println(errorOffset);
637N/A }
637N/A }
637N/A }
637N/A
637N/A}