jdk/test/java/text/Format/NumberFormat/BigDecimalParse.java
changeset 38581 e761c1ccd13e
child 40948 5869c625afaa
equal deleted inserted replaced
38580:0f5cf0999399 38581:e761c1ccd13e
       
     1 /*
       
     2  * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4018937 8008577
       
    27  * @summary Confirm that methods which are newly added to support BigDecimal and BigInteger work as expected.
       
    28  * @library /java/text/testlib
       
    29  * @run main/othervm -Djava.locale.providers=COMPAT,SPI BigDecimalParse
       
    30  */
       
    31 
       
    32 import java.math.BigDecimal;
       
    33 import java.text.*;
       
    34 import java.util.*;
       
    35 
       
    36 public class BigDecimalParse extends IntlTest {
       
    37 
       
    38     public static void main(String[] args) throws Exception {
       
    39         Locale loc = Locale.getDefault();
       
    40         try {
       
    41             Locale.setDefault(Locale.US);
       
    42             new BigDecimalParse().run(args);
       
    43         } finally {
       
    44             // restore the reserved locale
       
    45             Locale.setDefault(loc);
       
    46         }
       
    47     }
       
    48 
       
    49     static final String nonsep_int =
       
    50         "123456789012345678901234567890123456789012345678901234567890" +
       
    51         "123456789012345678901234567890123456789012345678901234567890" +
       
    52         "123456789012345678901234567890123456789012345678901234567890" +
       
    53         "123456789012345678901234567890123456789012345678901234567890" +
       
    54         "123456789012345678901234567890123456789012345678901234567890" +
       
    55         "123456789012345678901234567890123456789012345678901234567890";
       
    56 
       
    57     static final String sep_int =
       
    58         "123,456,789,012,345,678,901,234,567,890," +
       
    59         "123,456,789,012,345,678,901,234,567,890," +
       
    60         "123,456,789,012,345,678,901,234,567,890," +
       
    61         "123,456,789,012,345,678,901,234,567,890," +
       
    62         "123,456,789,012,345,678,901,234,567,890," +
       
    63         "123,456,789,012,345,678,901,234,567,890," +
       
    64         "123,456,789,012,345,678,901,234,567,890," +
       
    65         "123,456,789,012,345,678,901,234,567,890," +
       
    66         "123,456,789,012,345,678,901,234,567,890," +
       
    67         "123,456,789,012,345,678,901,234,567,890," +
       
    68         "123,456,789,012,345,678,901,234,567,890," +
       
    69         "123,456,789,012,345,678,901,234,567,890";
       
    70 
       
    71     static final String nonsep_zero =
       
    72         "000000000000000000000000000000000000000000000000000000000000" +
       
    73         "000000000000000000000000000000000000000000000000000000000000" +
       
    74         "000000000000000000000000000000000000000000000000000000000000" +
       
    75         "000000000000000000000000000000000000000000000000000000000000" +
       
    76         "000000000000000000000000000000000000000000000000000000000000" +
       
    77         "000000000000000000000000000000000000000000000000000000000000";
       
    78 
       
    79     static final String sep_zero =
       
    80         "000,000,000,000,000,000,000,000,000,000," +
       
    81         "000,000,000,000,000,000,000,000,000,000," +
       
    82         "000,000,000,000,000,000,000,000,000,000," +
       
    83         "000,000,000,000,000,000,000,000,000,000," +
       
    84         "000,000,000,000,000,000,000,000,000,000," +
       
    85         "000,000,000,000,000,000,000,000,000,000," +
       
    86         "000,000,000,000,000,000,000,000,000,000," +
       
    87         "000,000,000,000,000,000,000,000,000,000," +
       
    88         "000,000,000,000,000,000,000,000,000,000," +
       
    89         "000,000,000,000,000,000,000,000,000,000," +
       
    90         "000,000,000,000,000,000,000,000,000,000," +
       
    91         "000,000,000,000,000,000,000,000,000,000";
       
    92 
       
    93     static final String fra =
       
    94         "012345678901234567890123456789012345678901234567890123456789" +
       
    95         "012345678901234567890123456789012345678901234567890123456789" +
       
    96         "012345678901234567890123456789012345678901234567890123456789" +
       
    97         "012345678901234567890123456789012345678901234567890123456789" +
       
    98         "012345678901234567890123456789012345678901234567890123456789" +
       
    99         "012345678901234567890123456789012345678901234567890123456789";
       
   100 
       
   101 
       
   102     Number parsed = null;
       
   103     ParsePosition pp;
       
   104     boolean exceptionOccurred;
       
   105     String msg;
       
   106     DecimalFormat df;
       
   107 
       
   108     /**
       
   109      * Test for normal big numbers which have the fraction part
       
   110      */
       
   111     void test_Parse_in_DecimalFormat_BigDecimal() {
       
   112         df = new DecimalFormat();
       
   113         df.setParseBigDecimal(true);
       
   114 
       
   115         // From: 1234...7890.012...789
       
   116         // To:   BigDecimal 1234...7890.012...789
       
   117         check(nonsep_int + "." + fra, new BigDecimal(nonsep_int + "." + fra));
       
   118 
       
   119         // From: -1,234...7,890.012...789
       
   120         // To:   BigDecimal -1234...7890.012...789
       
   121         check("-" + sep_int    + "." + fra,
       
   122               new BigDecimal("-" + nonsep_int + "." + fra));
       
   123 
       
   124         // From: 000...0000.0...0
       
   125         // To:   BigDecimal 0E-360
       
   126         check(nonsep_zero + "." + nonsep_zero,
       
   127               new BigDecimal(nonsep_zero + "." + nonsep_zero));
       
   128 
       
   129         // From: 0.000...0000123...789E370
       
   130         // To:   BigDecimal 0.0123...789
       
   131         check("0.0000000000" + nonsep_zero + fra + "E370",
       
   132               new BigDecimal("0.0000000000" + nonsep_zero + fra + "E370"));
       
   133 
       
   134         // From: 0.1123...890E-360
       
   135         // To:   BigDecimal 1.123...890E-361
       
   136         check("0.1" + nonsep_int + "E-360",
       
   137               new BigDecimal("0.1" + nonsep_int + "E-360"));
       
   138 
       
   139         // From: 000...0000.0...0123...7890
       
   140         // To:   BigDecimal 1.234...890E-361
       
   141         check(nonsep_zero + "." + nonsep_zero + nonsep_int,
       
   142               new BigDecimal(nonsep_zero + "." + nonsep_zero + nonsep_int));
       
   143 
       
   144         // From: 0.123...890E360
       
   145         // To:   BigDecimal 123...890
       
   146         check("0." + nonsep_int + "E360",
       
   147               new BigDecimal("0." + nonsep_int + "E360"));
       
   148     }
       
   149 
       
   150     /**
       
   151      * Test for normal big numbers which have the fraction part with multiplier
       
   152      */
       
   153     void test_Parse_in_DecimalFormat_BigDecimal_usingMultiplier() {
       
   154         df = new DecimalFormat();
       
   155         df.setParseBigDecimal(true);
       
   156 
       
   157         // From: 250,0...0,000.000...000
       
   158         // To:   1000...0000.000...000
       
   159         df.setMultiplier(250000000);
       
   160         check("250,000,000," + sep_zero + "." + nonsep_zero,
       
   161               new BigDecimal("1" + nonsep_zero + "." + nonsep_zero));
       
   162 
       
   163         // From: -250,0...0,000.000...000
       
   164         // To:   -1000...0000.000...000
       
   165         check("-250,000,000," + sep_zero + "." + nonsep_zero,
       
   166               new BigDecimal("-1" + nonsep_zero + "." + nonsep_zero));
       
   167 
       
   168         // From: 250,0...0,000.000...000
       
   169         // To:   -1000...0000.000...000
       
   170         df.setMultiplier(-250000000);
       
   171         check("250,000,000," + sep_zero + "." + nonsep_zero,
       
   172               new BigDecimal("-1" + nonsep_zero + "." + nonsep_zero));
       
   173 
       
   174         // From: -250,0...0,000.000...000
       
   175         // To:   1000...0000.000...000
       
   176         check("-250,000,000," + sep_zero + "." + nonsep_zero,
       
   177               new BigDecimal("1" + nonsep_zero + "." + nonsep_zero));
       
   178 
       
   179         // Confirm that ArithmeticException is handled properly
       
   180         // From: 1000.000
       
   181         // To:   333.333
       
   182         df.setMultiplier(3);
       
   183         check("1000.000", new BigDecimal("333.333"));
       
   184 
       
   185         // Confirm that ArithmeticException is handled properly
       
   186         // From: 10000.0000
       
   187         // To:   303.0303
       
   188         df.setMultiplier(33);
       
   189         check("10000.0000", new BigDecimal("303.0303"));
       
   190     }
       
   191 
       
   192     /**
       
   193      * Test for division by zero (BigDecimal)
       
   194      */
       
   195     void test_Parse_in_DecimalFormat_BigDecimal_DivisionByZero() {
       
   196         df = new DecimalFormat();
       
   197         df.setParseBigDecimal(true);
       
   198         df.setMultiplier(0);
       
   199 
       
   200         // From: 1000.000
       
   201         // To:   Double.POSITIVE_INFINITY
       
   202         check("1000.000", new Double(Double.POSITIVE_INFINITY));
       
   203 
       
   204         // From: -1000
       
   205         // To:   Double.NEGATIVE_INFINITY
       
   206         check("-1000", new Double(Double.NEGATIVE_INFINITY));
       
   207 
       
   208         // From: -0.00
       
   209         // To:   Double.NaN
       
   210         check("-0.00", new Double(Double.NaN));
       
   211     }
       
   212 
       
   213     /**
       
   214      * Test for division by zero (Double)
       
   215      */
       
   216     void test_Parse_in_DecimalFormat_Double_DivisionByZero() {
       
   217         df = new DecimalFormat();
       
   218         df.setParseBigDecimal(false);
       
   219         df.setMultiplier(0);
       
   220 
       
   221         // From: 1000.000
       
   222         // To:   Double.POSITIVE_INFINITY
       
   223         check("1000.000", new Double(Double.POSITIVE_INFINITY));
       
   224 
       
   225         // From: -1000.000
       
   226         // To:   Double.NEGATIVE_INFINITY
       
   227         check("-1000.000", new Double(Double.NEGATIVE_INFINITY));
       
   228 
       
   229         // From: 0.0
       
   230         // To:   Double.NaN
       
   231         check("0.0", new Double(Double.NaN));
       
   232 
       
   233         // From: -0.0 (Double)
       
   234         // To:   Double.NaN
       
   235         check("-0.0", new Double(Double.NaN));
       
   236 
       
   237         // From: Double.NaN
       
   238         // To:   Double.NaN
       
   239         check("\ufffd", new Double(Double.NaN));
       
   240 
       
   241         // From: Double.POSITIVE_INFINITY
       
   242         // To:   Double.NaN
       
   243         check("\u221e", new Double(Double.POSITIVE_INFINITY));
       
   244 
       
   245         // From: Double.NEGATIVE_INFINITY
       
   246         // To:   Double.NaN
       
   247         check("-\u221e", new Double(Double.NEGATIVE_INFINITY));
       
   248     }
       
   249 
       
   250     /**
       
   251      * Test for division by zero (Long)
       
   252      */
       
   253     void test_Parse_in_DecimalFormat_Long_DivisionByZero() {
       
   254         df = new DecimalFormat();
       
   255         df.setParseBigDecimal(false);
       
   256         df.setMultiplier(0);
       
   257 
       
   258         // From: 1000
       
   259         // To:   Double.POSITIVE_INFINITY
       
   260         check("1000", new Double(Double.POSITIVE_INFINITY));
       
   261 
       
   262         // From: -1000
       
   263         // To:   Double.NEGATIVE_INFINITY
       
   264         check("-1000", new Double(Double.NEGATIVE_INFINITY));
       
   265 
       
   266         // From: -000 (Long)
       
   267         // To:   Double.NaN
       
   268         check("-000", new Double(Double.NaN));
       
   269     }
       
   270 
       
   271     /**
       
   272      * Test for normal big numbers which don't have the fraction part
       
   273      */
       
   274     void test_Parse_in_DecimalFormat_BigInteger() {
       
   275         df = new DecimalFormat();
       
   276         df.setParseBigDecimal(true);
       
   277 
       
   278         // From: 123...890
       
   279         // To:   BigDecimal 123...890
       
   280         check(nonsep_int + nonsep_int, new BigDecimal(nonsep_int + nonsep_int));
       
   281 
       
   282         // From: 123,4...7,890
       
   283         // To:   BigDecimal 1234...7890
       
   284         check(sep_int + "," + sep_int, new BigDecimal(nonsep_int + nonsep_int));
       
   285 
       
   286         // From: -000...000123...890
       
   287         // To:   BigDecimal -123...890
       
   288         check("-" + nonsep_zero + nonsep_int, new BigDecimal("-" + nonsep_int));
       
   289 
       
   290         // From: -000,0...0,000,123,4...7,890
       
   291         // To:   BigDecimal -123...890
       
   292         check("-" + sep_zero + "," + sep_int, new BigDecimal("-" + nonsep_int));
       
   293     }
       
   294 
       
   295     /**
       
   296      * Test for normal big numbers which don't have the fraction part with
       
   297      * multiplier
       
   298      */
       
   299     void test_Parse_in_DecimalFormat_BigInteger_usingMultiplier() {
       
   300         df = new DecimalFormat();
       
   301         df.setParseBigDecimal(true);
       
   302 
       
   303         // From: 250,0...0,000
       
   304         // To:   1000...0000
       
   305         df.setMultiplier(250000000);
       
   306         check("250,000,000," + sep_zero, new BigDecimal("1" + nonsep_zero));
       
   307 
       
   308         // From: -250,0...0,000
       
   309         // To:   -1000...0000
       
   310         check("-250,000,000," + sep_zero, new BigDecimal("-1" + nonsep_zero));
       
   311 
       
   312         // From: 250,0...0,000
       
   313         // To:   -1000...0000
       
   314         df.setMultiplier(-250000000);
       
   315         check("250,000,000," + sep_zero, new BigDecimal("-1" + nonsep_zero));
       
   316 
       
   317         // From: -250,0...0,000
       
   318         // To:   1000...0000
       
   319         check("-250,000,000," + sep_zero, new BigDecimal("1" + nonsep_zero));
       
   320 
       
   321         // From: 250,0...0,000E-360
       
   322         // To:   -1000...0000.000...000
       
   323         check("250,000,000," + sep_zero + "," + sep_zero + "E-360",
       
   324               new BigDecimal("-1" + nonsep_zero + "." + nonsep_zero));
       
   325 
       
   326         // Confirm that a division which results in a irrational number is done
       
   327         // properly
       
   328         // From: 1000
       
   329         // To:   333
       
   330         df.setMultiplier(3);
       
   331         check("1000", new BigDecimal("333"));
       
   332     }
       
   333 
       
   334     /**
       
   335      * Test for special numbers
       
   336      *    Double.NaN
       
   337      *    Double.POSITIVE_INFINITY
       
   338      *    Double.NEGATIVE_INFINITY
       
   339      */
       
   340     void test_Parse_in_DecimalFormat_SpecialNumber() {
       
   341         df = new DecimalFormat();
       
   342         df.setParseBigDecimal(true);
       
   343 
       
   344         String[] numbers = {
       
   345             "0", "0.0", "25", "25.0", "25.5", "\u221e", "\ufffd",
       
   346             "-0", "-0.0", "-25", "-25.0", "-25.5", "-\u221e",
       
   347         };
       
   348         int multipliers[] = {5, -5};
       
   349         Number[][] expected = {
       
   350             {
       
   351                 new BigDecimal("0"), new BigDecimal("0.0"), new BigDecimal("5"),
       
   352                 new BigDecimal("5.0"), new BigDecimal("5.1"),
       
   353                 new Double(Double.POSITIVE_INFINITY), new Double(Double.NaN),
       
   354                 new BigDecimal("0"), new BigDecimal("0.0"),
       
   355                 new BigDecimal("-5"), new BigDecimal("-5.0"),
       
   356                 new BigDecimal("-5.1"),
       
   357                 new Double(Double.NEGATIVE_INFINITY), new Double(Double.NaN),
       
   358             },
       
   359             {
       
   360                 new BigDecimal("0"), new BigDecimal("0.0"),
       
   361                 new BigDecimal("-5"), new BigDecimal("-5.0"),
       
   362                 new BigDecimal("-5.1"),
       
   363                 new Double(Double.NEGATIVE_INFINITY), new Double(Double.NaN),
       
   364                 new BigDecimal("0"), new BigDecimal("0.0"), new BigDecimal("5"),
       
   365                 new BigDecimal("5.0"), new BigDecimal("5.1"),
       
   366                 new Double(Double.POSITIVE_INFINITY),
       
   367             },
       
   368         };
       
   369 
       
   370         for (int i = 0; i < multipliers.length; i++) {
       
   371             df.setMultiplier(multipliers[i]);
       
   372             for (int j = 0; j < numbers.length; j++) {
       
   373                 check(String.valueOf(numbers[j]), expected[i][j]);
       
   374             }
       
   375         }
       
   376     }
       
   377 
       
   378     /**
       
   379      * Test for special numbers
       
   380      */
       
   381     void test_Parse_in_DecimalFormat_Other() {
       
   382         df = new DecimalFormat();
       
   383         df.setParseBigDecimal(true);
       
   384 
       
   385         String[] numbers = {
       
   386             "-9223372036854775808",     // Long.MIN_VALUE
       
   387         };
       
   388         int multipliers[] = {1, -1};
       
   389         String[][] expected = {
       
   390             {"-9223372036854775808"},   // Long.MIN_VALUE
       
   391             {"9223372036854775808"},    // Long.MAX_VALUE+1 = abs(MIN_VALUE)
       
   392         };
       
   393 
       
   394         for (int i = 0; i < multipliers.length; i++) {
       
   395             df.setMultiplier(multipliers[i]);
       
   396             for (int j = 0; j < numbers.length; j++) {
       
   397                 check(String.valueOf(numbers[j]),
       
   398                       new BigDecimal(expected[i][j]));
       
   399             }
       
   400         }
       
   401     }
       
   402 
       
   403     static final String[] patterns = {
       
   404         "  {0, number}  ",
       
   405         "  {0, number}  ",
       
   406         "  {0, number, currency}  ",
       
   407         "  {0, number, currency}  ",
       
   408         "  {0, number, percent}  ",
       
   409         "  {0, number, percent}  ",
       
   410         "  {0, number,#,##0.###E0}  ",
       
   411         "  {0, number,#,##0.###E0}  ",
       
   412 
       
   413         "  {0, number}  ",
       
   414         "  {0, number}  ",
       
   415         "  {0, number, integer}  ",
       
   416         "  {0, number, integer}  ",
       
   417         "  {0, number, currency}  ",
       
   418         "  {0, number, currency}  ",
       
   419         "  {0, number, percent}  ",
       
   420         "  {0, number, percent}  ",
       
   421         "  {0, number,#,##0.###E0}  ",
       
   422         "  {0, number,#,##0.###E0}  ",
       
   423     };
       
   424     static final String[] from = {
       
   425         "  12,345,678,901,234,567,890.98765432109876543210987654321  ",
       
   426         "  -12,345,678,901,234,567,890.98765432109876543210987654321  ",
       
   427         "  $12,345,678,901,234,567,890.98765432109876543210987654321  ",
       
   428         "  ($12,345,678,901,234,567,890.98765432109876543210987654321)  ",
       
   429         "  1,234,567,890,123,456,789,098.76543210987654321098765432100%  ",
       
   430         "  -1,234,567,890,123,456,789,098.76543210987654321098765432100%  ",
       
   431         "  12,345,678,901,234,567,890.98765432109876543210987654321E-20  ",
       
   432         "  -12,345,678,901,234,567,890.98765432109876543210987654321E-20  ",
       
   433 
       
   434         "  9,876,543,210,987,654,321,098,765,432,109,876,543,210  ",
       
   435         "  -9,876,543,210,987,654,321,098,765,432,109,876,543,210  ",
       
   436         "  9,876,543,210,987,654,321,098,765,432,109,876,543,210E5  ",
       
   437         "  -9,876,543,210,987,654,321,098,765,432,109,876,543,210E-5  ",
       
   438         "  $9,876,543,210,987,654,321,098,765,432,109,876,543,210.00  ",
       
   439         "  ($9,876,543,210,987,654,321,098,765,432,109,876,543,210.00)  ",
       
   440         "  987,654,321,098,765,432,109,876,543,210,987,654,321,012%  ",
       
   441         "  -987,654,321,098,765,432,109,876,543,210,987,654,321,012%  ",
       
   442         "  98,765,432,109,876,543,210.98765432109876543210E20  ",
       
   443         "  -987,654,321,098,765,432,109,876,543,210,987,654,321,000,000,000,000,000,000,000E-20  ",
       
   444     };
       
   445 
       
   446     static final String[] expected1 = { // isParseIntegerOnly() == false
       
   447         "12345678901234567890.98765432109876543210987654321",
       
   448         "-12345678901234567890.98765432109876543210987654321",
       
   449         "12345678901234567890.98765432109876543210987654321",
       
   450         "-12345678901234567890.98765432109876543210987654321",
       
   451         "12345678901234567890.98765432109876543210987654321",
       
   452         "-12345678901234567890.98765432109876543210987654321",
       
   453         "0.1234567890123456789098765432109876543210987654321",
       
   454         "-0.1234567890123456789098765432109876543210987654321",
       
   455 
       
   456         "9876543210987654321098765432109876543210",
       
   457         "-9876543210987654321098765432109876543210",
       
   458         "9.876543210987654321098765432109876543210E44",
       
   459         "-98765432109876543210987654321098765.43210",
       
   460         "9876543210987654321098765432109876543210.00",
       
   461         "-9876543210987654321098765432109876543210.00",
       
   462         "9876543210987654321098765432109876543210.12",
       
   463         "-9876543210987654321098765432109876543210.12",
       
   464         "9876543210987654321098765432109876543210",
       
   465         "-9876543210987654321098765432109876543210.00000000000000000000",
       
   466     };
       
   467     static final int[] parsePosition1 = {
       
   468         60, 61, 61, 63, 64, 65, 64, 65,
       
   469         57, 58, 59, 61, 61, 63, 60, 61, 54, 88,
       
   470     };
       
   471 
       
   472     /**
       
   473      * Test for MessageFormat: setParseIntegerOnly(false)
       
   474      */
       
   475     void test_Parse_in_MessageFormat_NotParseIntegerOnly() {
       
   476         for (int i=0; i < patterns.length; i++) {
       
   477             pp = new ParsePosition(0);
       
   478             Object[] parsed = null;
       
   479 
       
   480             try {
       
   481                 MessageFormat mf = new MessageFormat(patterns[i]);
       
   482                 Format[] formats = mf.getFormats();
       
   483                 for (int j=0; j < formats.length; j++) {
       
   484                     ((DecimalFormat)formats[j]).setParseBigDecimal(true);
       
   485                 }
       
   486 
       
   487                 parsed = mf.parse(from[i], pp);
       
   488 
       
   489                 if (pp.getErrorIndex() != -1) {
       
   490                     errln("Case" + (i+1) +
       
   491                           ": getErrorIndex() returns wrong value. expected:-1, got:"+
       
   492                           pp.getErrorIndex() + " for " + from[i]);
       
   493                 }
       
   494                 if (pp.getIndex() != parsePosition1[i]) {
       
   495                     errln("Case" + (i+1) +
       
   496                           ": getIndex() returns wrong value. expected:" +
       
   497                           parsePosition1[i] + ", got:"+ pp.getIndex() +
       
   498                           " for " + from[i]);
       
   499                 }
       
   500             }
       
   501             catch(Exception e) {
       
   502                 errln("Unexpected exception: " + e.getMessage());
       
   503             }
       
   504 
       
   505             checkType(from[i], getType(new BigDecimal(expected1[i])),
       
   506                       getType((Number)parsed[0]));
       
   507             checkParse(from[i], new BigDecimal(expected1[i]),
       
   508                        (Number)parsed[0]);
       
   509         }
       
   510     }
       
   511 
       
   512     static final String[] expected2 = { // isParseIntegerOnly() == true
       
   513         "12345678901234567890",
       
   514         "-12345678901234567890",
       
   515         "12345678901234567890",
       
   516         "-12345678901234567890",
       
   517         "12345678901234567890",
       
   518         "-12345678901234567890",
       
   519         "0",
       
   520         "0",
       
   521 
       
   522         "9876543210987654321098765432109876543210",
       
   523         "-9876543210987654321098765432109876543210",
       
   524         "9.876543210987654321098765432109876543210E44",
       
   525         "-98765432109876543210987654321098765.43210",
       
   526         "9876543210987654321098765432109876543210",
       
   527         "-9876543210987654321098765432109876543210",
       
   528         "9876543210987654321098765432109876543210.12",
       
   529         "-9876543210987654321098765432109876543210.12",
       
   530         "9876543210987654321098765432109876543210",
       
   531         "-9876543210987654321098765432109876543210.00000000000000000000",
       
   532     };
       
   533     static final int[][] parsePosition2 = {     // {errorIndex, index}
       
   534         /*
       
   535          * Should keep in mind that the expected result is different from
       
   536          * DecimalFormat.parse() for some cases.
       
   537          */
       
   538         {28, 0},        // parsing stopped at '.'
       
   539         {29, 0},        // parsing stopped at '.'
       
   540         {29, 0},        // parsing stopped at '.'
       
   541         {2, 0},         // parsing stopped at '(' because cannot find ')'
       
   542         {2, 0},         // parsing stopped at the first numeric
       
   543                         // because cannot find '%'
       
   544         {2, 0},         // parsing stopped at the first numeric
       
   545                         // because cannot find '%'
       
   546         {28, 0},        // parsing stopped at '.'
       
   547         {29, 0},        // parsing stopped at '.'
       
   548 
       
   549         {-1, 57}, {-1, 58}, {-1, 59}, {-1, 61},
       
   550         {56, 0},        // parsing stopped at '.'
       
   551                         // because cannot find '%'
       
   552         {2, 0},         // parsing stopped at '(' because cannot find ')'
       
   553         {-1, 60}, {-1, 61},
       
   554         {28, 0},        // parsing stopped at '.'
       
   555         {-1, 88},
       
   556     };
       
   557 
       
   558     /**
       
   559      * Test for MessageFormat: setParseIntegerOnly(true)
       
   560      */
       
   561     void test_Parse_in_MessageFormat_ParseIntegerOnly() {
       
   562         for (int i=0; i < patterns.length; i++) {
       
   563             pp = new ParsePosition(0);
       
   564             Object[] parsed = null;
       
   565 
       
   566             try {
       
   567                 MessageFormat mf = new MessageFormat(patterns[i]);
       
   568                 Format[] formats = mf.getFormats();
       
   569                 for (int j=0; j < formats.length; j++) {
       
   570                     ((DecimalFormat)formats[j]).setParseBigDecimal(true);
       
   571                     ((DecimalFormat)formats[j]).setParseIntegerOnly(true);
       
   572                 }
       
   573 
       
   574                 parsed = mf.parse(from[i], pp);
       
   575 
       
   576                 if (pp.getErrorIndex() != parsePosition2[i][0]) {
       
   577                     errln("Case" + (i+1) +
       
   578                           ": getErrorIndex() returns wrong value. expected:" +
       
   579                           parsePosition2[i][0] + ", got:"+ pp.getErrorIndex() +
       
   580                           " for " + from[i]);
       
   581                 }
       
   582                 if (pp.getIndex() != parsePosition2[i][1]) {
       
   583                     errln("Case" + (i+1) +
       
   584                           ": getIndex() returns wrong value. expected:" +
       
   585                           parsePosition2[i][1] + ", got:"+ pp.getIndex() +
       
   586                           " for " + from[i]);
       
   587                 }
       
   588             }
       
   589             catch(Exception e) {
       
   590                 errln("Unexpected exception: " + e.getMessage());
       
   591             }
       
   592 
       
   593             if (parsePosition2[i][0] == -1) {
       
   594                 checkType(from[i], getType(new BigDecimal(expected2[i])),
       
   595                           getType((Number)parsed[0]));
       
   596                 checkParse(from[i], new BigDecimal(expected2[i]),
       
   597                            (Number)parsed[0]);
       
   598             }
       
   599         }
       
   600     }
       
   601 
       
   602     static final String[] from3 = {
       
   603         "12,345,678,901,234,567,890.98765432109876543210987654321",
       
   604         "-12,345,678,901,234,567,890.98765432109876543210987654321",
       
   605         "9,876,543,210,987,654,321,098,765,432,109,876,543,210",
       
   606         "-9,876,543,210,987,654,321,098,765,432,109,876,543,210",
       
   607         "1234556790000E-8",
       
   608     };
       
   609     static final String[] expected3 = {
       
   610         "12345678901234567890",
       
   611         "-12345678901234567890",
       
   612         "9876543210987654321098765432109876543210",
       
   613         "-9876543210987654321098765432109876543210",
       
   614         "12345.56790000",
       
   615     };
       
   616     static final int[][] parsePosition3 = {     // {errorIndex, index}
       
   617         {-1, 26},
       
   618         {-1, 27},
       
   619         {-1, 53},
       
   620         {-1, 54},
       
   621         {-1, 16},
       
   622     };
       
   623 
       
   624     /**
       
   625      * Test for DecimalFormat: setParseIntegerOnly(true)
       
   626      */
       
   627     void test_Parse_in_DecimalFormat_ParseIntegerOnly() {
       
   628         DecimalFormat df = (DecimalFormat)NumberFormat.getIntegerInstance();
       
   629         df.setParseBigDecimal(true);
       
   630 
       
   631         for (int i=0; i < from3.length; i++) {
       
   632             pp = new ParsePosition(0);
       
   633             Number parsed = null;
       
   634 
       
   635             try {
       
   636                 parsed = df.parse(from3[i], pp);
       
   637 
       
   638                 if (pp.getErrorIndex() != parsePosition3[i][0]) {
       
   639                     errln("Case" + (i+1) +
       
   640                           ": getErrorIndex() returns wrong value. expected:" +
       
   641                           parsePosition3[i][0] + ", got:"+ pp.getErrorIndex() +
       
   642                           " for " + from3[i]);
       
   643                 }
       
   644                 if (pp.getIndex() != parsePosition3[i][1]) {
       
   645                     errln("Case" + (i+1) +
       
   646                           ": getIndex() returns wrong value. expected:" +
       
   647                           parsePosition3[i][1] + ", got:"+ pp.getIndex() +
       
   648                           " for " + from3[i]);
       
   649                 }
       
   650             }
       
   651             catch(Exception e) {
       
   652                 errln("Unexpected exception: " + e.getMessage());
       
   653             }
       
   654 
       
   655             if (parsePosition3[i][0] == -1) {
       
   656                 checkType(from3[i], getType(new BigDecimal(expected3[i])),
       
   657                           getType(parsed));
       
   658                 checkParse(from3[i], new BigDecimal(expected3[i]), parsed);
       
   659             }
       
   660         }
       
   661     }
       
   662 
       
   663     protected void check(String from, Number to) {
       
   664         pp = new ParsePosition(0);
       
   665         try {
       
   666             parsed = df.parse(from, pp);
       
   667         }
       
   668         catch(Exception e) {
       
   669             exceptionOccurred = true;
       
   670             errln(e.getMessage());
       
   671         }
       
   672         if (!exceptionOccurred) {
       
   673             checkParse(from, to, parsed);
       
   674             checkType(from, getType(to), getType(parsed));
       
   675             checkParsePosition(from, from.length(), pp.getIndex());
       
   676         }
       
   677     }
       
   678 
       
   679     private void checkParse(String orig, Number expected, Number got) {
       
   680         if (!expected.equals(got)) {
       
   681             errln("Parsing... failed." +
       
   682                   "\n   original: " + orig +
       
   683                   "\n   parsed:   " + got +
       
   684                   "\n   expected: " + expected + "\n");
       
   685         }
       
   686     }
       
   687 
       
   688     private void checkType(String orig, String expected, String got) {
       
   689         if (!expected.equals(got)) {
       
   690             errln("Parsing... unexpected Class returned." +
       
   691                   "\n   original: " + orig +
       
   692                   "\n   got:      " + got +
       
   693                   "\n   expected: " + expected + "\n");
       
   694         }
       
   695     }
       
   696 
       
   697     private void checkParsePosition(String orig, int expected, int got) {
       
   698         if (expected != got) {
       
   699             errln("Parsing... wrong ParsePosition returned." +
       
   700                   "\n   original: " + orig +
       
   701                   "\n   got:      " + got +
       
   702                   "\n   expected: " + expected + "\n");
       
   703         }
       
   704     }
       
   705 
       
   706     private String getType(Number number) {
       
   707         return number.getClass().getName();
       
   708     }
       
   709 }