< prev index next >

test/jdk/java/text/Format/DateFormat/IntlTestSimpleDateFormatAPI.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 Simple Date Format API
 27  * @bug 8008577
 28  * @library /java/text/testlib
 29  * @run main/othervm -Djava.locale.providers=COMPAT,SPI IntlTestSimpleDateFormatAPI
 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 IntlTestSimpleDateFormatAPI extends IntlTest





 47 {
 48     public static void main(String[] args) throws Exception {
 49         Locale reservedLocale = Locale.getDefault();
 50         try {
 51             new IntlTestSimpleDateFormatAPI().run(args);
 52         } finally {
 53             // restore the reserved locale
 54             Locale.setDefault(reservedLocale);
 55         }
 56     }
 57 
 58     // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.

 59     public void TestAPI()
 60     {
 61         logln("SimpleDateFormat API test---"); logln("");
 62 
 63         Locale.setDefault(Locale.ENGLISH);
 64 
 65         // ======= Test constructors
 66 
 67         logln("Testing SimpleDateFormat constructors");
 68 
 69         SimpleDateFormat def = new SimpleDateFormat();
 70 
 71         final String pattern = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
 72         SimpleDateFormat pat = new SimpleDateFormat(pattern);
 73 
 74         SimpleDateFormat pat_fr = new SimpleDateFormat(pattern, Locale.FRENCH);
 75 
 76         DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRENCH);
 77 
 78         SimpleDateFormat cust1 = new SimpleDateFormat(pattern, symbols);
 79 
 80         // ======= Test clone() and equality
 81 
 82         logln("Testing clone(), assignment and equality operators");
 83 
 84         Format clone = (Format) def.clone();
 85         if( ! clone.equals(def) ) {
 86             errln("ERROR: Format clone or equals failed");
 87         }
 88 
 89         // ======= Test various format() methods
 90 
 91         logln("Testing various format() methods");
 92 
 93         Date d = new Date((long)837039928046.0);
 94 
 95         StringBuffer res1 = new StringBuffer();
 96         StringBuffer res2 = new StringBuffer();
 97         FieldPosition pos1 = new FieldPosition(0);
 98         FieldPosition pos2 = new FieldPosition(0);
 99 
100         res1 = def.format(d, res1, pos1);
101         logln( "" + d.getTime() + " formatted to " + res1);
102 
103         res2 = cust1.format(d, res2, pos2);
104         logln("" + d.getTime() + " formatted to " + res2);
105 
106         // ======= Test parse()
107 
108         logln("Testing parse()");
109 
110         String text = new String("02/03/76 2:50 AM, CST");
111         Date result1 = new Date();
112         Date result2 = new Date();
113         ParsePosition pos= new ParsePosition(0);
114         result1 = def.parse(text, pos);
115         logln(text + " parsed into " + result1);
116 
117         try {
118             result2 = def.parse(text);
119         }
120         catch (ParseException e) {
121             errln("ERROR: parse() failed");
122         }
123         logln(text + " parsed into " + result2);
124 
125         // ======= Test getters and setters
126 
127         logln("Testing getters and setters");
128 
129         final DateFormatSymbols syms = pat.getDateFormatSymbols();
130         def.setDateFormatSymbols(syms);
131         pat_fr.setDateFormatSymbols(syms);
132         if( ! pat.getDateFormatSymbols().equals(def.getDateFormatSymbols()) ) {
133             errln("ERROR: set DateFormatSymbols() failed");
134         }
135 
136         Date startDate = null;
137         try {
138 //            startDate = pat.getTwoDigitStartDate();
139         }
140         catch (Exception e) {
141             errln("ERROR: getTwoDigitStartDate() failed");
142         }
143 
144         try {
145 //            pat_fr.setTwoDigitStartDate(startDate);
146         }
147         catch (Exception e) {
148             errln("ERROR: setTwoDigitStartDate() failed");
149         }
150 
151         // ======= Test applyPattern()
152 
153         logln("Testing applyPattern()");
154 
155         String p1 = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
156         logln("Applying pattern " + p1);
157         pat.applyPattern(p1);
158 
159         String s2 = pat.toPattern();
160         logln("Extracted pattern is " + s2);
161         if( ! s2.equals(p1) ) {
162             errln("ERROR: toPattern() result did not match pattern applied");
163         }
164 
165         logln("Applying pattern " + p1);
166         pat.applyLocalizedPattern(p1);
167         String s3 = pat.toLocalizedPattern();
168         logln("Extracted pattern is " + s3);
169         if( ! s3.equals(p1) ) {
170             errln("ERROR: toLocalizedPattern() result did not match pattern applied");
171         }
172 
173         // ======= Test getStaticClassID()
174 
175 //        logln("Testing instanceof");
176 
177 //        try {
178 //            DateFormat test = new SimpleDateFormat();
179 
180 //            if (! (test instanceof SimpleDateFormat)) {
181 //                errln("ERROR: instanceof failed");
182 //            }
183 //        }
184 //        catch (Exception e) {
185 //            errln("ERROR: Couldn't create a SimpleDateFormat");
186 //        }
187     }
188 }

  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 Simple Date Format API
 27  * @bug 8008577
 28  * @run junit/othervm -Djava.locale.providers=COMPAT,SPI IntlTestSimpleDateFormatAPI

 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 IntlTestSimpleDateFormatAPI
 51 {









 52 
 53     // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
 54     @Test
 55     public void TestAPI()
 56     {
 57         System.out.println("SimpleDateFormat API test---"); System.out.println("");
 58 
 59         Locale.setDefault(Locale.ENGLISH);
 60 
 61         // ======= Test constructors
 62 
 63         System.out.println("Testing SimpleDateFormat constructors");
 64 
 65         SimpleDateFormat def = new SimpleDateFormat();
 66 
 67         final String pattern = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
 68         SimpleDateFormat pat = new SimpleDateFormat(pattern);
 69 
 70         SimpleDateFormat pat_fr = new SimpleDateFormat(pattern, Locale.FRENCH);
 71 
 72         DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRENCH);
 73 
 74         SimpleDateFormat cust1 = new SimpleDateFormat(pattern, symbols);
 75 
 76         // ======= Test clone() and equality
 77 
 78         System.out.println("Testing clone(), assignment and equality operators");
 79 
 80         Format clone = (Format) def.clone();
 81         if( ! clone.equals(def) ) {
 82             fail("ERROR: Format clone or equals failed");
 83         }
 84 
 85         // ======= Test various format() methods
 86 
 87         System.out.println("Testing various format() methods");
 88 
 89         Date d = new Date((long)837039928046.0);
 90 
 91         StringBuffer res1 = new StringBuffer();
 92         StringBuffer res2 = new StringBuffer();
 93         FieldPosition pos1 = new FieldPosition(0);
 94         FieldPosition pos2 = new FieldPosition(0);
 95 
 96         res1 = def.format(d, res1, pos1);
 97         System.out.println( "" + d.getTime() + " formatted to " + res1);
 98 
 99         res2 = cust1.format(d, res2, pos2);
100         System.out.println("" + d.getTime() + " formatted to " + res2);
101 
102         // ======= Test parse()
103 
104         System.out.println("Testing parse()");
105 
106         String text = new String("02/03/76 2:50 AM, CST");
107         Date result1 = new Date();
108         Date result2 = new Date();
109         ParsePosition pos= new ParsePosition(0);
110         result1 = def.parse(text, pos);
111         System.out.println(text + " parsed into " + result1);
112 
113         try {
114             result2 = def.parse(text);
115         }
116         catch (ParseException e) {
117             fail("ERROR: parse() failed");
118         }
119         System.out.println(text + " parsed into " + result2);
120 
121         // ======= Test getters and setters
122 
123         System.out.println("Testing getters and setters");
124 
125         final DateFormatSymbols syms = pat.getDateFormatSymbols();
126         def.setDateFormatSymbols(syms);
127         pat_fr.setDateFormatSymbols(syms);
128         if( ! pat.getDateFormatSymbols().equals(def.getDateFormatSymbols()) ) {
129             fail("ERROR: set DateFormatSymbols() failed");
130         }
131 
132         Date startDate = null;
133         try {
134 //            startDate = pat.getTwoDigitStartDate();
135         }
136         catch (Exception e) {
137             fail("ERROR: getTwoDigitStartDate() failed");
138         }
139 
140         try {
141 //            pat_fr.setTwoDigitStartDate(startDate);
142         }
143         catch (Exception e) {
144             fail("ERROR: setTwoDigitStartDate() failed");
145         }
146 
147         // ======= Test applyPattern()
148 
149         System.out.println("Testing applyPattern()");
150 
151         String p1 = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
152         System.out.println("Applying pattern " + p1);
153         pat.applyPattern(p1);
154 
155         String s2 = pat.toPattern();
156         System.out.println("Extracted pattern is " + s2);
157         if( ! s2.equals(p1) ) {
158             fail("ERROR: toPattern() result did not match pattern applied");
159         }
160 
161         System.out.println("Applying pattern " + p1);
162         pat.applyLocalizedPattern(p1);
163         String s3 = pat.toLocalizedPattern();
164         System.out.println("Extracted pattern is " + s3);
165         if( ! s3.equals(p1) ) {
166             fail("ERROR: toLocalizedPattern() result did not match pattern applied");
167         }
168 
169         // ======= Test getStaticClassID()
170 
171 //        logln("Testing instanceof");
172 
173 //        try {
174 //            DateFormat test = new SimpleDateFormat();
175 
176 //            if (! (test instanceof SimpleDateFormat)) {
177 //                errln("ERROR: instanceof failed");
178 //            }
179 //        }
180 //        catch (Exception e) {
181 //            errln("ERROR: Couldn't create a SimpleDateFormat");
182 //        }
183     }
184 }
< prev index next >