Path: blob/master/test/jdk/java/text/Format/DecimalFormat/Bug6609740.java
41152 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 660974025* @summary Checks the formatting and parsing of a number based26* on the positive and negative sub-patterns, also27* checks few invalid number patterns28*/29import java.text.DecimalFormat;30import java.text.NumberFormat;31import java.text.ParseException;32import java.util.Locale;3334public class Bug6609740 {3536public static void main(String[] args) {3738double dNumber = -3456.349347;39String fOutput = "(3,456.35)";40String[] validCases = {"#,##0.0#;(#,##0.0#)", "#,##0.0#;(#)",41"#,##0.0#;(#,##0)"};4243// formatting with the valid cases44NumberFormat nf = NumberFormat.getInstance(Locale.US);45for (String pattern : validCases) {46formatOnPattern(nf, pattern, dNumber, fOutput);47}4849// parsing with the valid cases50String parseString = "(3,456.35)";51Number pOutput = -3456.35;52for (String pattern : validCases) {53parseOnPattern(nf, pattern, parseString, pOutput);54}5556// should throw parse exception57String[] invalidParseCases = {"#,##0.0#;0", "#,##0.0#;()"};58for (String pattern : invalidParseCases) {59if (nf instanceof DecimalFormat) {60((DecimalFormat) nf).applyPattern(pattern);61}6263try {64nf.parse(parseString);65} catch (ParseException ex) {66continue;67}68throw new RuntimeException("[FAILED: Should throw"69+ " ParseException for pattern: "70+ pattern + " and input: " + parseString + "]");71}7273// should throw exception on invalid patterns74// invalid patterns: no positive subpattern, zero after non-zero in75// the decimal part i.e. 0#0, multiple decimal separators,76// multiple percent, malformed pattern77String[] invalidPatterns = {";(#,##0.0#)", "#,##0.0#0;(#)",78"#,##0.0.#", "#,##0%%", ".#,##0"};79for (String pattern : invalidPatterns) {80if (nf instanceof DecimalFormat) {81try {82((DecimalFormat) nf).applyPattern(pattern);83} catch (IllegalArgumentException ex) {84continue;85}86throw new RuntimeException("[FAILED: Should throw"87+ " IllegalArgumentException for invalid pattern: "88+ pattern + "]");89}90}91}9293private static void formatOnPattern(NumberFormat nf, String pattern,94double number, String expected) {9596if (nf instanceof DecimalFormat) {97((DecimalFormat) nf).applyPattern(pattern);98}99100String formatted = nf.format(number);101if (!formatted.equals(expected)) {102throw new RuntimeException("[FAILED: Unable to format the number"103+ " based on the pattern: '" + pattern + "', Expected : '"104+ expected + "', Found: '" + formatted + "']");105}106}107108private static void parseOnPattern(NumberFormat nf, String pattern,109String parseString, Number expected) {110111if (nf instanceof DecimalFormat) {112((DecimalFormat) nf).applyPattern(pattern);113}114115try {116Number output = nf.parse(parseString);117if (expected.doubleValue() != output.doubleValue()) {118throw new RuntimeException("[FAILED: Unable to parse the number"119+ " based on the pattern: '" + pattern + "', Expected : '"120+ expected + "', Found: '" + output + "']");121}122} catch (ParseException ex) {123throw new RuntimeException("[FAILED: Unable to parse the pattern:"124+ " '" + pattern + "']", ex);125}126}127128}129130131