Path: blob/master/test/jdk/java/text/Format/DecimalFormat/RoundingAndPropertyTest.java
41152 views
/*1* Copyright (c) 2012, 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*/2223/* @test24* @bug 705052825* @summary Test java.text.DecimalFormat fast-path for format(double...)26* @author Olivier Lagneau27* @build GoldenDoubleValues GoldenFormattedValues28* @run main RoundingAndPropertyTest29*30*/3132/* -----------------------------------------------------------------------------33* Note :34* Since fast-path algorithm does not modify any feature of DecimalFormat,35* some tests or values in this program may have to be adapted/added/removed36* when any change has been done in the fast-path source code, because the37* conditions for exercising fast-path may change.38*39* This is specially true if the set of constraints to fall in the fast-path40* case is relaxed in any manner.41*42* Usage :43* - Run main without any argument to test against a set of golden values and44* associated results hard-coded in the source code. That will do the tests45* described below46* See below comment section named "Description".47*48* or49*50* - Run main with string argument "-gengold" to output source code of51* GoldenFormattedValues.java class file with the jdk version used while52* generating the code.53* See below comment section named : "Modifying Golden Values".54*55* In case of error while running the test, a Runtime exception is generated56* providing the numbers of errors detected (format of golden values checks and57* property changes checks), and the program exit.58*59* Description :60*61* This test first checks that localization of digits is done correctly when62* calling DecimalFormat.format() on the array of values DecimalLocalizationValues63* found in GoldenDoubleValues, using the locale FullLocalizationTestLocale64* (from GoldenDoubleValues) that implies localization of digits. it checks the65* the results against expected returned string. In case of formatting error,66* it provides a message informing which value was wrongly formatted.67*68* Then it checks the results of calling NumberFormat.format(double) on a set69* of predefined golden values and checks results against expected returned70* string. It does this both for the decimal case, with an instance returned71* NumberFormat.getInstance() call and for the currency case, with an instance72* returned by NumberFormat.getCurrencyInstance(). Almost all the tested double73* values satisfy the constraints assumed by the fast-path algorithm for74* format(double ...). Some are voluntarily outside the scope of fast-path to75* check that the algorithm correctly eliminate them. In case of formatting76* error a message provides information on the golden value raising the error77* (value, exact decimal value (using BidDecimal), expected result, formatted result).78*79* Last the test checks the status and behavior of a DecimalFormat instance80* when changing properties that make this instance satisfy/invalidate its81* fast-path status, depending on the predefined set of fast-path constraints.82*83* The golden results are predefined arrays of int[] containing the unicode84* ints of the chars in the expected formatted string, when using locale85* provided in GoldenDoubleValues class. The results are those obtained by86* using a reference jdk version (for example one that does not contains the87* DecimalFormat fast-path algorithm, like jdk80-b25).88*89* The double values from which we get golden results are stored inside two90* arrays of double values:91* - DecimalGoldenValues for testing NumberFormat.getInstance().92* - CurrencyGoldenValues for testing NumberFormat.getCurrencyInstance().93* These arrays are located in GoldenDoubleValues.java source file.94*95* For each double value in the arrays above, there is an associated golden96* result. These results are stored in arrays of int[]:97* - DecimalGoldenFormattedValues for expected decimal golden results.98* - CurrencyGoldenFormattedValues for expected currency golden results.99* - DecimalDigitsLocalizedFormattedValues for expected localized digit results.100*101* We store the results in int[] arrays containing the expected unicode values102* because the compiler that will compile the containing java file may use a103* different locale than the one registered in GoldenDoubleValues.java. These104* arrays are located in a separate GoldenFormattedValues.java source file105* that is generated by RoundingAndPropertyTest using "-gengold" parameter.106* See below "Modifying Golden Values".107*108* The golden value arrays can be expanded, modified ... to test additional109* or different double values. In that case, the source file of class110* GoldenFormattedValues must be regenerated to replace the existing one..111*112* Modifying Golden Values :113*114* In order to ease further modification of the list of double values checked115* and associated golden results, the test includes the method116* generatesGoldenFormattedValuesClass() that writes on standard output stream117* the source code for GoldenFormattedValues class that includes the expected118* results arrays.119*120* Here are the steps to follow for updating/modifying golden values and results:121* 1- Edit GoldenDoubleValues.java to add/remove/modify golden or localization122* values.123* 2- Run main with "-gengold" string argument with a target jdk.124* (at the creation of this test file, the target jdk used was jdk1.8.0-ea).125* 2- Copy this java code that has been writen on standard output and replace126* GoldenFormattedValues.java contents by the generated output.127* 3- Check that this updated code compiles.128* [4]- If needed replaces existing GoldenDoubleValues and GoldenFormattedValues129* files in jdk/test section, respectively by the one modified at step 1 and130* generated at step 2.131* -----------------------------------------------------------------------------132*/133134import java.util.*;135import java.text.NumberFormat;136import java.text.DecimalFormat;137import java.text.DecimalFormatSymbols;138import java.math.RoundingMode;139import java.math.BigDecimal;140141142public class RoundingAndPropertyTest {143144145// Prints on standard output stream the unicode values of chars as a146// comma-separated list of int values147private static void printUnicodeValuesArray(char[] chars) {148for (int i = 0; i < chars.length; i++) {149System.out.print((int) chars[i]);150if (i != (chars.length - 1))151System.out.print(", ");152}153}154155// Converts given array of unicode values as an array of chars.156// Returns this converted array.157private static char[] getCharsFromUnicodeArray(int[] unicodeValues) {158char[] chars = new char[unicodeValues.length];159160for (int i = 0; i < unicodeValues.length; i++) {161chars[i] = (char) unicodeValues[i];162}163return chars;164}165166/* Prints on standard output stream the java code of resulting167* GoldenFormattedValues class for the golden values found in168* class GoldenDoubleValues.169*/170private static void generatesGoldenFormattedValuesClass() {171172String fourWhiteSpaces = " ";173String eightWhiteSpaces = " ";174175// Prints header without Copyright header.176System.out.println("/* This is a machine generated file - Please DO NOT EDIT !");177System.out.println(" * Change RoundingAndPropertyTest instead,");178System.out.println(" * and run with \"-gengold\" argument to regenerate (without copyright header).");179System.out.println(" */");180System.out.println();181182System.out.println("/* This file contains the set of result Strings expected from calling inside");183System.out.println(" * RoundingAndPropertyTest the method NumberFormat.format() upon the set of");184System.out.println(" * double values provided in GoldenDoubleValues.java. It contains three arrays,");185System.out.println(" * each containing arrays of unicode values representing the expected string");186System.out.println(" * result when calling format() on the corresponding (i.e. same index) double");187System.out.println(" * value found in GoldenDoubleValues arrays :");188System.out.println(" * - DecimalDigitsLocalizedFormattedValues corresponds to DecimalLocalizationValues,");189System.out.println(" * when using FullLocalizationTestLocale to format.");190System.out.println(" * - DecimalGoldenFormattedValues corresponds to DecimalGoldenValues, when used");191System.out.println(" * in the decimal pattern case together with TestLocale.");192System.out.println(" * - CurrencyGoldenFormattedValues corresponds to CurrencyGoldenValues. when used");193System.out.println(" * in the currency pattern case together with TestLocale.");194System.out.println(" * Please see documentation in RoundingAndPropertyTest.java for more details.");195System.out.println(" *");196System.out.println(" * This file generated by running RoundingAndPropertyTest with \"-gengold\" argument.");197System.out.println(" */");198System.out.println();199200// Prints beginning of class GoldenFormattedValues.201System.out.println("class GoldenFormattedValues {");202System.out.println();203System.out.println(204fourWhiteSpaces +205"// The formatted values below were generated from golden values");206System.out.print(207fourWhiteSpaces +208"// listed in GoldenDoubleValues.java,");209System.out.println(" using the following jvm version :");210System.out.println(211fourWhiteSpaces + "// " +212System.getProperty("java.vendor") +213" " +214System.getProperty("java.vm.name") +215" " +216System.getProperty("java.version"));217System.out.println(218fourWhiteSpaces +219"// locale for golden double values : " + GoldenDoubleValues.TestLocale);220System.out.println(221fourWhiteSpaces +222"// locale for testing digit localization : " + GoldenDoubleValues.FullLocalizationTestLocale);223System.out.println();224225// Prints the expected results when digit localization happens226System.out.println(227fourWhiteSpaces +228"// The array of int[] unicode values storing the expected results");229System.out.print(230fourWhiteSpaces +231"// when experiencing full localization of digits");232System.out.println(" on DecimalLocalizationValues.");233System.out.println(234fourWhiteSpaces +235"static int[][] DecimalDigitsLocalizedFormattedValues = {");236NumberFormat df =237NumberFormat.getInstance(GoldenDoubleValues.FullLocalizationTestLocale);238for (int i = 0;239i < GoldenDoubleValues.DecimalLocalizationValues.length;240i++) {241double d = GoldenDoubleValues.DecimalLocalizationValues[i];242String formatted = df.format(d);243char[] decFmtChars = formatted.toCharArray();244245System.out.print(eightWhiteSpaces + "{ ");246printUnicodeValuesArray(decFmtChars);247System.out.println(" },");248}249System.out.println(fourWhiteSpaces + "};");250System.out.println();251252// Prints the golden expected results for the decimal pattern case253System.out.println(254fourWhiteSpaces +255"// The array of int[] unicode values storing the expected results");256System.out.print(257fourWhiteSpaces +258"// when calling Decimal.format(double)");259System.out.println(" on the decimal GoldenDoubleValues.");260System.out.println(261fourWhiteSpaces +262"static int[][] DecimalGoldenFormattedValues = {");263df = NumberFormat.getInstance(GoldenDoubleValues.TestLocale);264for (int i = 0;265i < GoldenDoubleValues.DecimalGoldenValues.length;266i++) {267double d = GoldenDoubleValues.DecimalGoldenValues[i];268String formatted = df.format(d);269char[] decFmtChars = formatted.toCharArray();270271System.out.print(eightWhiteSpaces + "{ ");272printUnicodeValuesArray(decFmtChars);273System.out.println(" },");274}275System.out.println(fourWhiteSpaces + "};");276System.out.println();277278// Prints the golden expected results for the currency pattern case279System.out.println(280fourWhiteSpaces +281"// The array of int[] unicode values storing the expected results");282System.out.print(283fourWhiteSpaces +284"// when calling Decimal.format(double)");285System.out.println(" on the currency GoldenDoubleValues.");286System.out.println(287fourWhiteSpaces +288"static int[][] CurrencyGoldenFormattedValues = {");289NumberFormat cf =290NumberFormat.getCurrencyInstance(GoldenDoubleValues.TestLocale);291for (int i = 0;292i < GoldenDoubleValues.CurrencyGoldenValues.length;293i++) {294double d = GoldenDoubleValues.CurrencyGoldenValues[i];295String formatted = cf.format(d);296char[] decFmtChars = formatted.toCharArray();297298System.out.print(eightWhiteSpaces + "{ ");299printUnicodeValuesArray(decFmtChars);300System.out.println(" },");301}302System.out.println(fourWhiteSpaces + "};");303System.out.println();304305// Prints end of GoldenFormattedValues class.306System.out.println("}");307}308309private static int testLocalizationValues() {310311DecimalFormat df = (DecimalFormat)312NumberFormat.getInstance(GoldenDoubleValues.FullLocalizationTestLocale);313314double[] localizationValues = GoldenDoubleValues.DecimalLocalizationValues;315int size = localizationValues.length;316int successCounter = 0;317int failureCounter = 0;318for (int i = 0; i < size; i++) {319320double d = localizationValues[i];321String formatted = df.format(d);322323char[] expectedUnicodeArray =324getCharsFromUnicodeArray(325GoldenFormattedValues.DecimalDigitsLocalizedFormattedValues[i]);326String expected = new String(expectedUnicodeArray);327328if (!formatted.equals(expected)) {329failureCounter++;330System.out.println(331"--- Localization error for value d = " + d +332". Exact value = " + new BigDecimal(d).toString() +333". Expected result = " + expected +334". Output result = " + formatted);335} else successCounter++;336}337System.out.println("Checked positively " + successCounter +338" golden decimal values out of " + size +339" tests. There were " + failureCounter +340" format failure");341342return failureCounter;343}344345private static int testGoldenValues(java.text.DecimalFormat df,346java.text.DecimalFormat cf) {347348double[] goldenDecimalValues = GoldenDoubleValues.DecimalGoldenValues;349int decimalSize = goldenDecimalValues.length;350int decimalSuccessCounter = 0;351int decimalFailureCounter = 0;352for (int i = 0; i < decimalSize; i++) {353354double d = goldenDecimalValues[i];355String formatted = df.format(d);356357char[] expectedUnicodeArray =358getCharsFromUnicodeArray(359GoldenFormattedValues.DecimalGoldenFormattedValues[i]);360String expected = new String(expectedUnicodeArray);361362if (!formatted.equals(expected)) {363decimalFailureCounter++;364System.out.println(365"--- Error for golden value d = " + d +366". Exact value = " + new BigDecimal(d).toString() +367". Expected result = " + expected +368". Output result = " + formatted);369} else decimalSuccessCounter++;370}371System.out.println("Checked positively " + decimalSuccessCounter +372" golden decimal values out of " + decimalSize +373" tests. There were " + decimalFailureCounter +374" format failure");375376double[] goldenCurrencyValues = GoldenDoubleValues.CurrencyGoldenValues;377int currencySize = goldenCurrencyValues.length;378int currencySuccessCounter = 0;379int currencyFailureCounter = 0;380for (int i = 0; i < currencySize; i++) {381double d = goldenCurrencyValues[i];382String formatted = cf.format(d);383384char[] expectedUnicodeArray =385getCharsFromUnicodeArray(386GoldenFormattedValues.CurrencyGoldenFormattedValues[i]);387String expected = new String(expectedUnicodeArray);388389if (!formatted.equals(expected)) {390currencyFailureCounter++;391System.out.println(392"--- Error for golden value d = " + d +393". Exact value = " + new BigDecimal(d).toString() +394". Expected result = " + expected +395". Output result = " + formatted);396} else currencySuccessCounter++;397}398System.out.println("Checked positively " + currencySuccessCounter +399" golden currency values out of " + currencySize +400" tests. There were " + currencyFailureCounter +401" format failure");402403return (decimalFailureCounter + currencyFailureCounter);404}405406// Checks that the two passed s1 and s2 string are equal, and prints407// out message in case of error.408private static boolean resultsEqual(String propertyName,409String s1,410String s2) {411412boolean equality = s1.equals(s2);413if (!equality)414System.out.println(415"\n*** Error while reverting to default " +416propertyName + " property.\n" +417" initial output = " + s1 +418". reverted output = " + s2 + ".");419else System.out.println(" Test passed.");420421return equality;422423}424425/* This methods checks the behaviour of the management of properties426* of a DecimalFormat instance that satisfies fast-path constraints.427*428* It does this by comparing the results of the format(double) output429* obtained from initial fast-path state with the output provided by430* the same instance that has been pushed and exercised outside431* fast-path rules and finally "reverted" to its initial fast-path state.432*433* The schema of actions is this :434* - Call format(double) on a known DecimalFormat fast-path instance,435* and store this result.436* - Record the current state of a given property.437* - Change the property to invalidate the fast-path state.438* - Call again format(double) on the instance.439* - Revert state of property to validate again fast-path context.440* - Call format(double) again.441* - Check that first and last call to format(double) provide same result442* - Record failure if any.443* - Do the same for another property with the same instance.444* So all the property changes are chained one after the other on only the445* same instance.446*447* Some properties that currently do not influence the fast-path state448* are also tested. This is not useful with current fast-path source449* but is here for testing the whole set of properties. This is the case450* for prefixes and suffixes, and parseBigDecimal properties.451*/452private static int testSettersAndFastPath(DecimalFormat df,453boolean isCurrency) {454455final double d1 = GoldenDoubleValues.PROPERTY_CHECK_POSITIVE_VALUE;456final double d2 = GoldenDoubleValues.PROPERTY_CHECK_NEGATIVE_VALUE;457458int errors = 0;459boolean testSucceeded = false;460String firstFormatResult;461String secondFormatResult;462String propertyName;463464// ---- positivePrefix property test ----465testSucceeded = false;466propertyName = "positivePrefix";467System.out.print("Checking " + propertyName + " property.");468String initialPrefix = df.getPositivePrefix();469firstFormatResult = df.format(d1);470df.setPositivePrefix("positivePrefix:");471df.format(d1);472df.setPositivePrefix(initialPrefix);473secondFormatResult = df.format(d1);474testSucceeded =475resultsEqual(propertyName, firstFormatResult, secondFormatResult);476if (!testSucceeded)477errors++;478479// ---- positiveSuffix property test ----480testSucceeded = false;481propertyName = "positiveSuffix";482System.out.print("Checking " + propertyName + " property.");483String initialSuffix = df.getPositiveSuffix();484firstFormatResult = df.format(d1);485df.setPositiveSuffix("positiveSuffix:");486df.format(d1);487df.setPositiveSuffix(initialSuffix);488secondFormatResult = df.format(d1);489testSucceeded =490resultsEqual(propertyName,firstFormatResult, secondFormatResult);491if (!testSucceeded)492errors++;493494// ---- negativePrefix property test ----495testSucceeded = false;496propertyName = "negativePrefix";497System.out.print("Checking " + propertyName + " property.");498initialPrefix = df.getNegativePrefix();499firstFormatResult = df.format(d1);500df.setNegativePrefix("negativePrefix:");501df.format(d1);502df.setNegativePrefix(initialPrefix);503secondFormatResult = df.format(d1);504testSucceeded =505resultsEqual(propertyName, firstFormatResult, secondFormatResult);506if (!testSucceeded)507errors++;508509// ---- negativeSuffix property test ----510testSucceeded = false;511propertyName = "negativeSuffix";512System.out.print("Checking " + propertyName + " property.");513initialSuffix = df.getNegativeSuffix();514firstFormatResult = df.format(d1);515df.setNegativeSuffix("negativeSuffix:");516df.format(d1);517df.setNegativeSuffix(initialSuffix);518secondFormatResult = df.format(d1);519testSucceeded =520resultsEqual(propertyName, firstFormatResult, secondFormatResult);521if (!testSucceeded)522errors++;523524// ---- multiplier property test ----525testSucceeded = false;526propertyName = "multiplier";527System.out.print("Checking " + propertyName + " property.");528int initialMultiplier = df.getMultiplier();529firstFormatResult = df.format(d1);530df.setMultiplier(10);531df.format(d1);532df.setMultiplier(initialMultiplier);533secondFormatResult = df.format(d1);534testSucceeded =535resultsEqual(propertyName, firstFormatResult, secondFormatResult);536if (!testSucceeded)537errors++;538539// ---- groupingUsed property test ----540testSucceeded = false;541propertyName = "groupingUsed";542System.out.print("Checking " + propertyName + " property.");543boolean initialGroupingUsed = df.isGroupingUsed();544firstFormatResult = df.format(d1);545df.setGroupingUsed(!initialGroupingUsed);546df.format(d1);547df.setGroupingUsed(initialGroupingUsed);548secondFormatResult = df.format(d1);549testSucceeded =550resultsEqual(propertyName, firstFormatResult, secondFormatResult);551if (!testSucceeded)552errors++;553554// ---- groupingSize property test ----555testSucceeded = false;556propertyName = "groupingSize";557System.out.print("Checking " + propertyName + " property.");558int initialGroupingSize = df.getGroupingSize();559firstFormatResult = df.format(d1);560df.setGroupingSize(initialGroupingSize + 1);561df.format(d1);562df.setGroupingSize(initialGroupingSize);563secondFormatResult = df.format(d1);564testSucceeded =565resultsEqual(propertyName, firstFormatResult, secondFormatResult);566if (!testSucceeded)567errors++;568569// ---- decimalSeparatorAlwaysShown property test ----570testSucceeded = false;571propertyName = "decimalSeparatorAlwaysShown";572System.out.print("Checking " + propertyName + " property.");573boolean initialDSShown = df.isDecimalSeparatorAlwaysShown();574firstFormatResult = df.format(d1);575df.setDecimalSeparatorAlwaysShown(!initialDSShown);576df.format(d1);577df.setDecimalSeparatorAlwaysShown(initialDSShown);578secondFormatResult = df.format(d1);579testSucceeded =580resultsEqual(propertyName, firstFormatResult, secondFormatResult);581if (!testSucceeded)582errors++;583584// ---- parseBigDecimal property test ----585testSucceeded = false;586propertyName = "parseBigDecimal";587System.out.print("Checking " + propertyName + " property.");588boolean initialParseBigdecimal = df.isParseBigDecimal();589firstFormatResult = df.format(d1);590df.setParseBigDecimal(!initialParseBigdecimal);591df.format(d1);592df.setParseBigDecimal(initialParseBigdecimal);593secondFormatResult = df.format(d1);594testSucceeded =595resultsEqual(propertyName, firstFormatResult, secondFormatResult);596if (!testSucceeded)597errors++;598599// ---- maximumIntegerDigits property test ----600testSucceeded = false;601propertyName = "maximumIntegerDigits";602System.out.print("Checking " + propertyName + " property.");603int initialMaxIDs = df.getMaximumIntegerDigits();604firstFormatResult = df.format(d1);605df.setMaximumIntegerDigits(8);606df.format(d1);607df.setMaximumIntegerDigits(initialMaxIDs);608secondFormatResult = df.format(d1);609testSucceeded =610resultsEqual(propertyName, firstFormatResult, secondFormatResult);611if (!testSucceeded)612errors++;613614// ---- minimumIntegerDigits property test ----615testSucceeded = false;616propertyName = "minimumIntegerDigits";617System.out.print("Checking " + propertyName + " property.");618int initialMinIDs = df.getMinimumIntegerDigits();619firstFormatResult = df.format(d1);620df.setMinimumIntegerDigits(2);621df.format(d1);622df.setMinimumIntegerDigits(initialMinIDs);623secondFormatResult = df.format(d1);624testSucceeded =625resultsEqual(propertyName, firstFormatResult, secondFormatResult);626if (!testSucceeded)627errors++;628629// ---- maximumFractionDigits property test ----630testSucceeded = false;631propertyName = "maximumFractionDigits";632System.out.print("Checking " + propertyName + " property.");633firstFormatResult = df.format(d1);634df.setMaximumFractionDigits(8);635df.format(d1);636if (isCurrency) {637df.setMinimumFractionDigits(2);638df.setMaximumFractionDigits(2);639} else {640df.setMinimumFractionDigits(0);641df.setMaximumFractionDigits(3);642}643secondFormatResult = df.format(d1);644testSucceeded =645resultsEqual(propertyName, firstFormatResult, secondFormatResult);646if (!testSucceeded)647errors++;648649// ---- minimumFractionDigits property test ----650testSucceeded = false;651propertyName = "minimumFractionDigits";652System.out.print("Checking " + propertyName + " property.");653firstFormatResult = df.format(d1);654df.setMinimumFractionDigits(1);655df.format(d1);656if (isCurrency) {657df.setMinimumFractionDigits(2);658df.setMaximumFractionDigits(2);659} else {660df.setMinimumFractionDigits(0);661df.setMaximumFractionDigits(3);662}663secondFormatResult = df.format(d1);664testSucceeded =665resultsEqual(propertyName, firstFormatResult, secondFormatResult);666if (!testSucceeded)667errors++;668669// ---- currency property test ----670testSucceeded = false;671propertyName = "currency";672System.out.print("Checking " + propertyName + " property.");673Currency initialCurrency = df.getCurrency();674Currency japanCur = java.util.Currency.getInstance(Locale.JAPAN);675firstFormatResult = df.format(d1);676df.setCurrency(japanCur);677df.format(d1);678df.setCurrency(initialCurrency);679secondFormatResult = df.format(d1);680testSucceeded =681resultsEqual(propertyName, firstFormatResult, secondFormatResult);682if (!testSucceeded)683errors++;684685// ---- roundingMode property test ----686testSucceeded = false;687propertyName = "roundingMode";688System.out.print("Checking " + propertyName + " property.");689RoundingMode initialRMode = df.getRoundingMode();690firstFormatResult = df.format(d1);691df.setRoundingMode(RoundingMode.HALF_UP);692df.format(d1);693df.setRoundingMode(RoundingMode.HALF_EVEN);694secondFormatResult = df.format(d1);695testSucceeded =696resultsEqual(propertyName, firstFormatResult, secondFormatResult);697if (!testSucceeded)698errors++;699700// ---- decimalFormatSymbols property test ----701testSucceeded = false;702propertyName = "decimalFormatSymbols";703System.out.print("Checking " + propertyName + " property.");704DecimalFormatSymbols initialDecimalFormatSymbols = df.getDecimalFormatSymbols();705firstFormatResult = df.format(d1);706Locale bizarreLocale = new Locale("fr", "FR");707DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(bizarreLocale);708unusualSymbols.setDecimalSeparator('@');709unusualSymbols.setGroupingSeparator('|');710df.setDecimalFormatSymbols(unusualSymbols);711df.format(d1);712df.setDecimalFormatSymbols(initialDecimalFormatSymbols);713secondFormatResult = df.format(d1);714testSucceeded =715resultsEqual(propertyName, firstFormatResult, secondFormatResult);716if (!testSucceeded)717errors++;718719testSucceeded = false;720System.out.print("Checking " + propertyName + " property.");721initialDecimalFormatSymbols = df.getDecimalFormatSymbols();722firstFormatResult = df.format(d1);723Locale japanLocale = Locale.JAPAN;724unusualSymbols = new DecimalFormatSymbols(japanLocale);725unusualSymbols.setDecimalSeparator('9');726unusualSymbols.setGroupingSeparator('0');727df.setDecimalFormatSymbols(unusualSymbols);728df.format(d1);729df.setDecimalFormatSymbols(initialDecimalFormatSymbols);730secondFormatResult = df.format(d1);731testSucceeded =732resultsEqual(propertyName, firstFormatResult, secondFormatResult);733if (!testSucceeded)734errors++;735736return errors;737}738739// Main for RoundingAndPropertyTest. We test first the golden values,740// and then the property setters and getters.741public static void main(String[] args) {742743if ((args.length >= 1) &&744(args[0].equals("-gengold")))745generatesGoldenFormattedValuesClass();746else {747System.out.println("\nChecking correctness of formatting with digit localization.");748System.out.println("=============================================================");749int localizationErrors = testLocalizationValues();750if (localizationErrors != 0)751System.out.println("*** Failure in localization tests : " +752localizationErrors + " errors detected ");753else System.out.println(" Tests for full localization of digits all passed.");754755DecimalFormat df = (DecimalFormat)756NumberFormat.getInstance(GoldenDoubleValues.TestLocale);757DecimalFormat cf = (DecimalFormat)758NumberFormat.getCurrencyInstance(GoldenDoubleValues.TestLocale);759760System.out.println("\nChecking correctness of formating for golden values.");761System.out.println("=============================================================");762int goldenValuesErrors = testGoldenValues(df,cf);763if (goldenValuesErrors != 0)764System.out.println("*** Failure in goldenValues tests : " +765goldenValuesErrors + " errors detected ");766else System.out.println(" Tests for golden values all passed.");767768System.out.println("\nChecking behavior of property changes for decimal case.");769System.out.println("=============================================================");770int decimalTestsErrors = testSettersAndFastPath(df, false);771if (decimalTestsErrors != 0)772System.out.println("*** Failure in decimal property changes tests : " +773decimalTestsErrors + " errors detected ");774else System.out.println(" Tests for decimal property changes all passed.");775776System.out.println("\nChecking behavior of property changes for currency case.");777System.out.println("=============================================================");778int currencyTestsErrors = testSettersAndFastPath(cf, true);779if (currencyTestsErrors != 0)780System.out.println("*** Failure in currency property changes tests : " +781currencyTestsErrors + " errors detected ");782else System.out.println(" Tests for currency property chamges all passed.");783784if ((localizationErrors > 0) ||785(goldenValuesErrors > 0) ||786(decimalTestsErrors > 0) ||787(currencyTestsErrors > 0))788throw new RuntimeException(789"Failed with " +790(localizationErrors + goldenValuesErrors +791decimalTestsErrors + currencyTestsErrors) +792" error(s).");793}794}795}796797798