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