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 Date Format API
 27  * @bug 8008577
 28  * @run junit/othervm -Djava.locale.providers=COMPAT,SPI IntlTestDateFormatAPI
 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 import org.junit.jupiter.api.BeforeAll;
 47 
 48 import static org.junit.jupiter.api.Assertions.fail;
 49 
 50 public class IntlTestDateFormatAPI
 51 {
 52 
 53     // Test that the equals method works correctly.
 54     @Test
 55     public void TestEquals()
 56     {
 57         // Create two objects at different system times
 58         DateFormat a = DateFormat.getInstance();
 59         Date start = Calendar.getInstance().getTime();
 60         while (start.equals(Calendar.getInstance().getTime())) ; // Wait for time to change
 61         DateFormat b = DateFormat.getInstance();
 62 
 63         if (!(a.equals(b)))
 64             fail("FAIL: DateFormat objects created at different times are unequal.");
 65 
 66         if (b instanceof SimpleDateFormat)
 67         {
 68             double ONE_YEAR = 365*24*60*60*1000.0;
 69             try {
 70 //                ((SimpleDateFormat)b).setTwoDigitStartDate(start.getTime() + 50*ONE_YEAR);
 71 //                if (a.equals(b))
 72 //                    errln("FAIL: DateFormat objects with different two digit start dates are equal.");
 73             }
 74             catch (Exception e) {
 75                 fail("FAIL: setTwoDigitStartDate failed.");
 76             }
 77         }
 78     }
 79 
 80     // This test checks various generic API methods in DateFormat to achieve 100% API coverage.
 81     @Test
 82     public void TestAPI()
 83     {
 84         System.out.println("DateFormat API test---"); System.out.println("");
 85         Locale.setDefault(Locale.ENGLISH);
 86 
 87 
 88         // ======= Test constructors
 89 
 90         System.out.println("Testing DateFormat constructors");
 91 
 92         DateFormat def = DateFormat.getInstance();
 93         DateFormat fr = DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRENCH);
 94         DateFormat it = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALIAN);
 95         DateFormat de = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN);
 96 
 97         // ======= Test equality
 98 
 99         System.out.println("Testing equality operator");
100 
101         if( fr.equals(it) ) {
102             fail("ERROR: equals failed");
103         }
104 
105         // ======= Test various format() methods
106 
107         System.out.println("Testing various format() methods");
108 
109         Date d = new Date((long)837039928046.0);
110 
111         StringBuffer res1 = new StringBuffer();
112         StringBuffer res2 = new StringBuffer();
113         String res3 = new String();
114         FieldPosition pos1 = new FieldPosition(0);
115         FieldPosition pos2 = new FieldPosition(0);
116 
117         res1 = fr.format(d, res1, pos1);
118         System.out.println("" + d.getTime() + " formatted to " + res1);
119 
120         res2 = it.format(d, res2, pos2);
121         System.out.println("" + d.getTime() + " formatted to " + res2);
122 
123         res3 = de.format(d);
124         System.out.println("" + d.getTime() + " formatted to " + res3);
125 
126         // ======= Test parse()
127 
128         System.out.println("Testing parse()");
129 
130         String text = new String("02/03/76 2:50 AM, CST");
131         Object result1 = new Date();
132         Date result2 = new Date();
133         Date result3 = new Date();
134         ParsePosition pos = new ParsePosition(0);
135         ParsePosition pos01 = new ParsePosition(0);
136 
137         result1 = def.parseObject(text, pos);
138         if (result1 == null) {
139             fail("ERROR: parseObject() failed for " + text);
140         }
141         System.out.println(text + " parsed into " + ((Date)result1).getTime());
142 
143         try {
144             result2 = def.parse(text);
145         }
146         catch (ParseException e) {
147             fail("ERROR: parse() failed");
148         }
149         System.out.println(text + " parsed into " + result2.getTime());
150 
151         result3 = def.parse(text, pos01);
152         if (result3 == null) {
153             fail("ERROR: parse() failed for " + text);
154         }
155         System.out.println(text + " parsed into " + result3.getTime());
156 
157 
158         // ======= Test getters and setters
159 
160         System.out.println("Testing getters and setters");
161 
162         final Locale[] locales = DateFormat.getAvailableLocales();
163         long count = locales.length;
164         System.out.println("Got " + count + " locales" );
165         for(int i = 0; i < count; i++) {
166             String name;
167             name = locales[i].getDisplayName();
168             System.out.println(name);
169         }
170 
171         fr.setLenient(it.isLenient());
172         if(fr.isLenient() != it.isLenient()) {
173             fail("ERROR: setLenient() failed");
174         }
175 
176         final Calendar cal = def.getCalendar();
177         Calendar newCal = (Calendar) cal.clone();
178         de.setCalendar(newCal);
179         it.setCalendar(newCal);
180         if( ! de.getCalendar().equals(it.getCalendar())) {
181             fail("ERROR: set Calendar() failed");
182         }
183 
184         final NumberFormat nf = def.getNumberFormat();
185         NumberFormat newNf = (NumberFormat) nf.clone();
186         de.setNumberFormat(newNf);
187         it.setNumberFormat(newNf);
188         if( ! de.getNumberFormat().equals(it.getNumberFormat())) {
189             fail("ERROR: set NumberFormat() failed");
190         }
191 
192         final TimeZone tz = def.getTimeZone();
193         TimeZone newTz = (TimeZone) tz.clone();
194         de.setTimeZone(newTz);
195         it.setTimeZone(newTz);
196         if( ! de.getTimeZone().equals(it.getTimeZone())) {
197             fail("ERROR: set TimeZone() failed");
198         }
199 
200         // ======= Test getStaticClassID()
201 
202 //        logln("Testing instanceof()");
203 
204 //        try {
205 //            DateFormat test = new SimpleDateFormat();
206 
207 //            if (! (test instanceof SimpleDateFormat)) {
208 //                errln("ERROR: instanceof failed");
209 //            }
210 //        }
211 //        catch (Exception e) {
212 //            errln("ERROR: Couldn't create a DateFormat");
213 //        }
214     }
215 }