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 }