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 Number Format API 28 * @modules jdk.localedata 29 */ 30/* 31(C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved 32(C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved 33 34 The original version of this source code and documentation is copyrighted and 35owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are 36provided under terms of a License Agreement between Taligent and Sun. This 37technology is protected by multiple US and International patents. This notice and 38attribution to Taligent may not be removed. 39 Taligent is a registered trademark of Taligent, Inc. 40*/ 41 42import java.text.*; 43import java.util.*; 44 45public class IntlTestNumberFormatAPI extends IntlTest 46{ 47 public static void main(String[] args) throws Exception { 48 new IntlTestNumberFormatAPI().run(args); 49 } 50 51 // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage. 52 public void TestAPI() 53 { 54 Locale reservedLocale = Locale.getDefault(); 55 try { 56 logln("NumberFormat API test---"); logln(""); 57 Locale.setDefault(Locale.ENGLISH); 58 59 // ======= Test constructors 60 61 logln("Testing NumberFormat constructors"); 62 63 NumberFormat def = NumberFormat.getInstance(); 64 65 NumberFormat fr = NumberFormat.getInstance(Locale.FRENCH); 66 67 NumberFormat cur = NumberFormat.getCurrencyInstance(); 68 69 NumberFormat cur_fr = 70 NumberFormat.getCurrencyInstance(Locale.FRENCH); 71 72 NumberFormat per = NumberFormat.getPercentInstance(); 73 74 NumberFormat per_fr = 75 NumberFormat.getPercentInstance(Locale.FRENCH); 76 77 // ======= Test equality 78 79 logln("Testing equality operator"); 80 81 if( per_fr.equals(cur_fr) ) { 82 errln("ERROR: == failed"); 83 } 84 85 // ======= Test various format() methods 86 87 logln("Testing various format() methods"); 88 89// final double d = -10456.0037; // this appears as 90 // -10456.003700000001 on NT 91// final double d = -1.04560037e-4; // this appears as 92 // -1.0456003700000002E-4 on NT 93 final double d = -10456.00370000000000; // this works! 94 final long l = 100000000; 95 96 String res1 = new String(); 97 String res2 = new String(); 98 StringBuffer res3 = new StringBuffer(); 99 StringBuffer res4 = new StringBuffer(); 100 StringBuffer res5 = new StringBuffer(); 101 StringBuffer res6 = new StringBuffer(); 102 FieldPosition pos1 = new FieldPosition(0); 103 FieldPosition pos2 = new FieldPosition(0); 104 FieldPosition pos3 = new FieldPosition(0); 105 FieldPosition pos4 = new FieldPosition(0); 106 107 res1 = cur_fr.format(d); 108 logln( "" + d + " formatted to " + res1); 109 110 res2 = cur_fr.format(l); 111 logln("" + l + " formatted to " + res2); 112 113 res3 = cur_fr.format(d, res3, pos1); 114 logln( "" + d + " formatted to " + res3); 115 116 res4 = cur_fr.format(l, res4, pos2); 117 logln("" + l + " formatted to " + res4); 118 119 res5 = cur_fr.format(d, res5, pos3); 120 logln("" + d + " formatted to " + res5); 121 122 res6 = cur_fr.format(l, res6, pos4); 123 logln("" + l + " formatted to " + res6); 124 125 126 // ======= Test parse() 127 128 logln("Testing parse()"); 129 130// String text = new String("-10,456.0037"); 131 String text = new String("-10456,0037"); 132 ParsePosition pos = new ParsePosition(0); 133 ParsePosition pos01 = new ParsePosition(0); 134 double d1 = ((Number)fr.parseObject(text, pos)).doubleValue(); 135 if(d1 != d) { 136 errln("ERROR: Roundtrip failed (via parse()) for " + text); 137 } 138 logln(text + " parsed into " + d1); 139 140 double d2 = fr.parse(text, pos01).doubleValue(); 141 if(d2 != d) { 142 errln("ERROR: Roundtrip failed (via parse()) for " + text); 143 } 144 logln(text + " parsed into " + d2); 145 146 double d3 = 0; 147 try { 148 d3 = fr.parse(text).doubleValue(); 149 } 150 catch (ParseException e) { 151 errln("ERROR: parse() failed"); 152 } 153 if(d3 != d) { 154 errln("ERROR: Roundtrip failed (via parse()) for " + text); 155 } 156 logln(text + " parsed into " + d3); 157 158 159 // ======= Test getters and setters 160 161 logln("Testing getters and setters"); 162 163 final Locale[] locales = NumberFormat.getAvailableLocales(); 164 long count = locales.length; 165 logln("Got " + count + " locales" ); 166 for(int i = 0; i < count; i++) { 167 String name; 168 name = locales[i].getDisplayName(); 169 logln(name); 170 } 171 172 fr.setParseIntegerOnly( def.isParseIntegerOnly() ); 173 if(fr.isParseIntegerOnly() != def.isParseIntegerOnly() ) { 174 errln("ERROR: setParseIntegerOnly() failed"); 175 } 176 177 fr.setGroupingUsed( def.isGroupingUsed() ); 178 if(fr.isGroupingUsed() != def.isGroupingUsed() ) { 179 errln("ERROR: setGroupingUsed() failed"); 180 } 181 182 fr.setMaximumIntegerDigits( def.getMaximumIntegerDigits() ); 183 if(fr.getMaximumIntegerDigits() != def.getMaximumIntegerDigits() ) { 184 errln("ERROR: setMaximumIntegerDigits() failed"); 185 } 186 187 fr.setMinimumIntegerDigits( def.getMinimumIntegerDigits() ); 188 if(fr.getMinimumIntegerDigits() != def.getMinimumIntegerDigits() ) { 189 errln("ERROR: setMinimumIntegerDigits() failed"); 190 } 191 192 fr.setMaximumFractionDigits( def.getMaximumFractionDigits() ); 193 if(fr.getMaximumFractionDigits() != def.getMaximumFractionDigits() ) { 194 errln("ERROR: setMaximumFractionDigits() failed"); 195 } 196 197 fr.setMinimumFractionDigits( def.getMinimumFractionDigits() ); 198 if(fr.getMinimumFractionDigits() != def.getMinimumFractionDigits() ) { 199 errln("ERROR: setMinimumFractionDigits() failed"); 200 } 201 202 // ======= Test getStaticClassID() 203 204// logln("Testing instanceof()"); 205 206// try { 207// NumberFormat test = new DecimalFormat(); 208 209// if (! (test instanceof DecimalFormat)) { 210// errln("ERROR: instanceof failed"); 211// } 212// } 213// catch (Exception e) { 214// errln("ERROR: Couldn't create a DecimalFormat"); 215// } 216 } finally { 217 // restore the reserved locale 218 Locale.setDefault(reservedLocale); 219 } 220 } 221} 222