1/* 2 * Copyright (c) 1998, 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 * @library /java/text/testlib 27 * @summary test International Decimal Format API 28 */ 29/* 30(C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved 31(C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved 32 33 The original version of this source code and documentation is copyrighted and 34owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are 35provided under terms of a License Agreement between Taligent and Sun. This 36technology is protected by multiple US and International patents. This notice and 37attribution to Taligent may not be removed. 38 Taligent is a registered trademark of Taligent, Inc. 39*/ 40 41import java.text.*; 42import java.util.*; 43 44public class IntlTestDecimalFormatAPI extends IntlTest 45{ 46 public static void main(String[] args) throws Exception { 47 new IntlTestDecimalFormatAPI().run(args); 48 } 49 50 // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage. 51 public void TestAPI() 52 { 53 Locale reservedLocale = Locale.getDefault(); 54 try { 55 logln("DecimalFormat API test---"); logln(""); 56 Locale.setDefault(Locale.ENGLISH); 57 58 // ======= Test constructors 59 60 logln("Testing DecimalFormat constructors"); 61 62 DecimalFormat def = new DecimalFormat(); 63 64 final String pattern = new String("#,##0.# FF"); 65 DecimalFormat pat = null; 66 try { 67 pat = new DecimalFormat(pattern); 68 } 69 catch (IllegalArgumentException e) { 70 errln("ERROR: Could not create DecimalFormat (pattern)"); 71 } 72 73 DecimalFormatSymbols symbols = 74 new DecimalFormatSymbols(Locale.FRENCH); 75 76 DecimalFormat cust1 = new DecimalFormat(pattern, symbols); 77 78 // ======= Test clone(), assignment, and equality 79 80 logln("Testing clone() and equality operators"); 81 82 Format clone = (Format) def.clone(); 83 if( ! def.equals(clone)) { 84 errln("ERROR: Clone() failed"); 85 } 86 87 // ======= Test various format() methods 88 89 logln("Testing various format() methods"); 90 91// final double d = -10456.0037; // this appears as 92 // -10456.003700000001 on NT 93// final double d = -1.04560037e-4; // this appears as 94 // -1.0456003700000002E-4 on NT 95 final double d = -10456.00370000000000; // this works! 96 final long l = 100000000; 97 logln("" + d + " is the double value"); 98 99 StringBuffer res1 = new StringBuffer(); 100 StringBuffer res2 = new StringBuffer(); 101 StringBuffer res3 = new StringBuffer(); 102 StringBuffer res4 = new StringBuffer(); 103 FieldPosition pos1 = new FieldPosition(0); 104 FieldPosition pos2 = new FieldPosition(0); 105 FieldPosition pos3 = new FieldPosition(0); 106 FieldPosition pos4 = new FieldPosition(0); 107 108 res1 = def.format(d, res1, pos1); 109 logln("" + d + " formatted to " + res1); 110 111 res2 = pat.format(l, res2, pos2); 112 logln("" + l + " formatted to " + res2); 113 114 res3 = cust1.format(d, res3, pos3); 115 logln("" + d + " formatted to " + res3); 116 117 res4 = cust1.format(l, res4, pos4); 118 logln("" + l + " formatted to " + res4); 119 120 // ======= Test parse() 121 122 logln("Testing parse()"); 123 124 String text = new String("-10,456.0037"); 125 ParsePosition pos = new ParsePosition(0); 126 String patt = new String("#,##0.#"); 127 pat.applyPattern(patt); 128 double d2 = pat.parse(text, pos).doubleValue(); 129 if(d2 != d) { 130 errln("ERROR: Roundtrip failed (via parse(" + 131 d2 + " != " + d + ")) for " + text); 132 } 133 logln(text + " parsed into " + (long) d2); 134 135 // ======= Test getters and setters 136 137 logln("Testing getters and setters"); 138 139 final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols(); 140 def.setDecimalFormatSymbols(syms); 141 if(!pat.getDecimalFormatSymbols().equals( 142 def.getDecimalFormatSymbols())) { 143 errln("ERROR: set DecimalFormatSymbols() failed"); 144 } 145 146 String posPrefix; 147 pat.setPositivePrefix("+"); 148 posPrefix = pat.getPositivePrefix(); 149 logln("Positive prefix (should be +): " + posPrefix); 150 if(posPrefix != "+") { 151 errln("ERROR: setPositivePrefix() failed"); 152 } 153 154 String negPrefix; 155 pat.setNegativePrefix("-"); 156 negPrefix = pat.getNegativePrefix(); 157 logln("Negative prefix (should be -): " + negPrefix); 158 if(negPrefix != "-") { 159 errln("ERROR: setNegativePrefix() failed"); 160 } 161 162 String posSuffix; 163 pat.setPositiveSuffix("_"); 164 posSuffix = pat.getPositiveSuffix(); 165 logln("Positive suffix (should be _): " + posSuffix); 166 if(posSuffix != "_") { 167 errln("ERROR: setPositiveSuffix() failed"); 168 } 169 170 String negSuffix; 171 pat.setNegativeSuffix("~"); 172 negSuffix = pat.getNegativeSuffix(); 173 logln("Negative suffix (should be ~): " + negSuffix); 174 if(negSuffix != "~") { 175 errln("ERROR: setNegativeSuffix() failed"); 176 } 177 178 long multiplier = 0; 179 pat.setMultiplier(8); 180 multiplier = pat.getMultiplier(); 181 logln("Multiplier (should be 8): " + multiplier); 182 if(multiplier != 8) { 183 errln("ERROR: setMultiplier() failed"); 184 } 185 186 int groupingSize = 0; 187 pat.setGroupingSize(2); 188 groupingSize = pat.getGroupingSize(); 189 logln("Grouping size (should be 2): " + (long) groupingSize); 190 if(groupingSize != 2) { 191 errln("ERROR: setGroupingSize() failed"); 192 } 193 194 pat.setDecimalSeparatorAlwaysShown(true); 195 boolean tf = pat.isDecimalSeparatorAlwaysShown(); 196 logln("DecimalSeparatorIsAlwaysShown (should be true) is " + 197 (tf ? "true" : "false")); 198 if(tf != true) { 199 errln("ERROR: setDecimalSeparatorAlwaysShown() failed"); 200 } 201 202 String funkyPat; 203 funkyPat = pat.toPattern(); 204 logln("Pattern is " + funkyPat); 205 206 String locPat; 207 locPat = pat.toLocalizedPattern(); 208 logln("Localized pattern is " + locPat); 209 210 // ======= Test applyPattern() 211 212 logln("Testing applyPattern()"); 213 214 String p1 = new String("#,##0.0#;(#,##0.0#)"); 215 logln("Applying pattern " + p1); 216 pat.applyPattern(p1); 217 String s2; 218 s2 = pat.toPattern(); 219 logln("Extracted pattern is " + s2); 220 if( ! s2.equals(p1) ) { 221 errln("ERROR: toPattern() result did not match " + 222 "pattern applied"); 223 } 224 225 String p2 = new String("#,##0.0# FF;(#,##0.0# FF)"); 226 logln("Applying pattern " + p2); 227 pat.applyLocalizedPattern(p2); 228 String s3; 229 s3 = pat.toLocalizedPattern(); 230 logln("Extracted pattern is " + s3); 231 if( ! s3.equals(p2) ) { 232 errln("ERROR: toLocalizedPattern() result did not match " + 233 "pattern applied"); 234 } 235 236 // ======= Test getStaticClassID() 237 238// logln("Testing instanceof()"); 239 240// try { 241// NumberFormat test = new DecimalFormat(); 242 243// if (! (test instanceof DecimalFormat)) { 244// errln("ERROR: instanceof failed"); 245// } 246// } 247// catch (Exception e) { 248// errln("ERROR: Couldn't create a DecimalFormat"); 249// } 250 } finally { 251 // restore the reserved locale 252 Locale.setDefault(reservedLocale); 253 } 254 } 255} 256