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