< prev index next >

test/jdk/java/text/Format/DateFormat/IntlTestDateFormatAPI.java

Print this page

  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  * @summary test International Date Format API
 27  * @bug 8008577
 28  * @library /java/text/testlib
 29  * @run main/othervm -Djava.locale.providers=COMPAT,SPI IntlTestDateFormatAPI
 30  */
 31 /*
 32 (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
 33 (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
 34 
 35   The original version of this source code and documentation is copyrighted and
 36 owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
 37 provided under terms of a License Agreement between Taligent and Sun. This
 38 technology is protected by multiple US and International patents. This notice and
 39 attribution to Taligent may not be removed.
 40   Taligent is a registered trademark of Taligent, Inc.
 41 */
 42 
 43 import java.text.*;
 44 import java.util.*;
 45 
 46 public class IntlTestDateFormatAPI extends IntlTest





 47 {
 48     public static void main(String[] args) throws Exception {
 49         Locale reservedLocale = Locale.getDefault();
 50         try {
 51             new IntlTestDateFormatAPI().run(args);
 52         } finally {
 53             // restore the reserved locale
 54             Locale.setDefault(reservedLocale);
 55         }
 56     }
 57 
 58     // Test that the equals method works correctly.

 59     public void TestEquals()
 60     {
 61         // Create two objects at different system times
 62         DateFormat a = DateFormat.getInstance();
 63         Date start = Calendar.getInstance().getTime();
 64         while (start.equals(Calendar.getInstance().getTime())) ; // Wait for time to change
 65         DateFormat b = DateFormat.getInstance();
 66 
 67         if (!(a.equals(b)))
 68             errln("FAIL: DateFormat objects created at different times are unequal.");
 69 
 70         if (b instanceof SimpleDateFormat)
 71         {
 72             double ONE_YEAR = 365*24*60*60*1000.0;
 73             try {
 74 //                ((SimpleDateFormat)b).setTwoDigitStartDate(start.getTime() + 50*ONE_YEAR);
 75 //                if (a.equals(b))
 76 //                    errln("FAIL: DateFormat objects with different two digit start dates are equal.");
 77             }
 78             catch (Exception e) {
 79                 errln("FAIL: setTwoDigitStartDate failed.");
 80             }
 81         }
 82     }
 83 
 84     // This test checks various generic API methods in DateFormat to achieve 100% API coverage.

 85     public void TestAPI()
 86     {
 87         logln("DateFormat API test---"); logln("");
 88         Locale.setDefault(Locale.ENGLISH);
 89 
 90 
 91         // ======= Test constructors
 92 
 93         logln("Testing DateFormat constructors");
 94 
 95         DateFormat def = DateFormat.getInstance();
 96         DateFormat fr = DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRENCH);
 97         DateFormat it = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALIAN);
 98         DateFormat de = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN);
 99 
100         // ======= Test equality
101 
102         logln("Testing equality operator");
103 
104         if( fr.equals(it) ) {
105             errln("ERROR: equals failed");
106         }
107 
108         // ======= Test various format() methods
109 
110         logln("Testing various format() methods");
111 
112         Date d = new Date((long)837039928046.0);
113 
114         StringBuffer res1 = new StringBuffer();
115         StringBuffer res2 = new StringBuffer();
116         String res3 = new String();
117         FieldPosition pos1 = new FieldPosition(0);
118         FieldPosition pos2 = new FieldPosition(0);
119 
120         res1 = fr.format(d, res1, pos1);
121         logln("" + d.getTime() + " formatted to " + res1);
122 
123         res2 = it.format(d, res2, pos2);
124         logln("" + d.getTime() + " formatted to " + res2);
125 
126         res3 = de.format(d);
127         logln("" + d.getTime() + " formatted to " + res3);
128 
129         // ======= Test parse()
130 
131         logln("Testing parse()");
132 
133         String text = new String("02/03/76 2:50 AM, CST");
134         Object result1 = new Date();
135         Date result2 = new Date();
136         Date result3 = new Date();
137         ParsePosition pos = new ParsePosition(0);
138         ParsePosition pos01 = new ParsePosition(0);
139 
140         result1 = def.parseObject(text, pos);
141         if (result1 == null) {
142             errln("ERROR: parseObject() failed for " + text);
143         }
144         logln(text + " parsed into " + ((Date)result1).getTime());
145 
146         try {
147             result2 = def.parse(text);
148         }
149         catch (ParseException e) {
150             errln("ERROR: parse() failed");
151         }
152         logln(text + " parsed into " + result2.getTime());
153 
154         result3 = def.parse(text, pos01);
155         if (result3 == null) {
156             errln("ERROR: parse() failed for " + text);
157         }
158         logln(text + " parsed into " + result3.getTime());
159 
160 
161         // ======= Test getters and setters
162 
163         logln("Testing getters and setters");
164 
165         final Locale[] locales = DateFormat.getAvailableLocales();
166         long count = locales.length;
167         logln("Got " + count + " locales" );
168         for(int i = 0; i < count; i++) {
169             String name;
170             name = locales[i].getDisplayName();
171             logln(name);
172         }
173 
174         fr.setLenient(it.isLenient());
175         if(fr.isLenient() != it.isLenient()) {
176             errln("ERROR: setLenient() failed");
177         }
178 
179         final Calendar cal = def.getCalendar();
180         Calendar newCal = (Calendar) cal.clone();
181         de.setCalendar(newCal);
182         it.setCalendar(newCal);
183         if( ! de.getCalendar().equals(it.getCalendar())) {
184             errln("ERROR: set Calendar() failed");
185         }
186 
187         final NumberFormat nf = def.getNumberFormat();
188         NumberFormat newNf = (NumberFormat) nf.clone();
189         de.setNumberFormat(newNf);
190         it.setNumberFormat(newNf);
191         if( ! de.getNumberFormat().equals(it.getNumberFormat())) {
192             errln("ERROR: set NumberFormat() failed");
193         }
194 
195         final TimeZone tz = def.getTimeZone();
196         TimeZone newTz = (TimeZone) tz.clone();
197         de.setTimeZone(newTz);
198         it.setTimeZone(newTz);
199         if( ! de.getTimeZone().equals(it.getTimeZone())) {
200             errln("ERROR: set TimeZone() failed");
201         }
202 
203         // ======= Test getStaticClassID()
204 
205 //        logln("Testing instanceof()");
206 
207 //        try {
208 //            DateFormat test = new SimpleDateFormat();
209 
210 //            if (! (test instanceof SimpleDateFormat)) {
211 //                errln("ERROR: instanceof failed");
212 //            }
213 //        }
214 //        catch (Exception e) {
215 //            errln("ERROR: Couldn't create a DateFormat");
216 //        }
217     }
218 }

  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 }
< prev index next >