1 /*
   2  * Copyright (c) 2018, 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 import java.math.BigDecimal;
  25 import java.math.BigInteger;
  26 import java.text.NumberFormat;
  27 import java.text.ParseException;
  28 import java.text.ParsePosition;
  29 import static org.testng.Assert.*;
  30 
  31 class CompactFormatAndParse {
  32 
  33     public static void testFormat(NumberFormat cnf, Object number,
  34             String expected) {
  35         String result = cnf.format(number);
  36         assertEquals(result, expected, "Incorrect formatting of the number '"
  37                 + number + "'");
  38     }
  39 
  40     public static void testParse(NumberFormat cnf, String parseString,
  41             Number expected, ParsePosition position, Class<? extends Number> returnType) throws ParseException {
  42 
  43         Number number;
  44         if (position == null) {
  45             number = cnf.parse(parseString);
  46         } else {
  47             number = cnf.parse(parseString, position);
  48         }
  49 
  50         if (returnType != null) {
  51             assertEquals(number.getClass(), returnType, "Incorrect return type for string" + parseString);
  52         }
  53 
  54         if (number instanceof Double) {
  55             assertEquals(number.doubleValue(), (double) expected,
  56                     "Incorrect parsing of the string '" + parseString + "'");
  57         } else if (number instanceof Long) {
  58             assertEquals(number.longValue(), (long) expected,
  59                     "Incorrect parsing of the string '" + parseString + "'");
  60         } else if (number instanceof BigDecimal) {
  61             BigDecimal num = (BigDecimal) number;
  62             assertEquals(num, (BigDecimal) expected,
  63                     "Incorrect parsing of the string '" + parseString + "'");
  64         } else {
  65             assertEquals(number, expected, "Incorrect parsing of the string '"
  66                     + parseString + "'");
  67         }
  68     }
  69 }