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 }