Home | History | Annotate | Download | only in format
      1 /* GENERATED SOURCE. DO NOT MODIFY. */
      2 //  2016 and later: Unicode, Inc. and others.
      3 // License & terms of use: http://www.unicode.org/copyright.html#License
      4 /*****************************************************************************************
      5  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
      6  * (C) Copyright IBM Corp. 1996-2012 - All Rights Reserved
      7  *
      8  *   The original version of this source code and documentation is copyrighted and
      9  * owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
     10  * provided under terms of a License Agreement between Taligent and Sun. This
     11  * technology is protected by multiple US and International patents. This notice and
     12  * attribution to Taligent may not be removed.
     13  *   Taligent is a registered trademark of Taligent, Inc.
     14  **/
     15 
     16 /**
     17  * Port From:   JDK 1.4b1 : java.text.Format.IntlTestSimpleDateFormatAPI
     18  * Source File: java/text/format/IntlTestSimpleDateFormatAPI.java
     19  **/
     20 
     21 package android.icu.dev.test.format;
     22 
     23 import java.text.FieldPosition;
     24 import java.text.Format;
     25 import java.text.ParseException;
     26 import java.text.ParsePosition;
     27 import java.util.Date;
     28 import java.util.Locale;
     29 
     30 import org.junit.Test;
     31 import org.junit.runner.RunWith;
     32 import org.junit.runners.JUnit4;
     33 
     34 import android.icu.dev.test.TestFmwk;
     35 import android.icu.text.DateFormatSymbols;
     36 import android.icu.text.SimpleDateFormat;
     37 import android.icu.testsharding.MainTestShard;
     38 
     39 /**
     40 * @test 1.4 98/03/06
     41 * @summary test International Simple Date Format API
     42 */
     43 @MainTestShard
     44 @RunWith(JUnit4.class)
     45 public class IntlTestSimpleDateFormatAPI extends TestFmwk
     46 {
     47     // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
     48     @Test
     49     public void TestAPI()
     50     {
     51         logln("SimpleDateFormat API test---"); logln("");
     52 
     53         Locale.setDefault(Locale.ENGLISH);
     54 
     55         // ======= Test constructors
     56 
     57         logln("Testing SimpleDateFormat constructors");
     58 
     59         SimpleDateFormat def = new SimpleDateFormat();
     60 
     61         final String pattern = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
     62         SimpleDateFormat pat = new SimpleDateFormat(pattern);
     63 
     64         SimpleDateFormat pat_fr = new SimpleDateFormat(pattern, Locale.FRENCH);
     65 
     66         DateFormatSymbols symbols = new DateFormatSymbols(Locale.FRENCH);
     67 
     68         SimpleDateFormat cust1 = new SimpleDateFormat(pattern, symbols);
     69 
     70         // ======= Test clone() and equality
     71 
     72         logln("Testing clone(), assignment and equality operators");
     73 
     74         Format clone = (Format) def.clone();
     75         if( ! clone.equals(def) ) {
     76             errln("ERROR: Format clone or equals failed");
     77         }
     78 
     79         // ======= Test various format() methods
     80 
     81         logln("Testing various format() methods");
     82 
     83         Date d = new Date((long)837039928046.0);
     84 
     85         StringBuffer res1 = new StringBuffer();
     86         StringBuffer res2 = new StringBuffer();
     87         FieldPosition pos1 = new FieldPosition(0);
     88         FieldPosition pos2 = new FieldPosition(0);
     89 
     90         res1 = def.format(d, res1, pos1);
     91         logln( "" + d.getTime() + " formatted to " + res1);
     92 
     93         res2 = cust1.format(d, res2, pos2);
     94         logln("" + d.getTime() + " formatted to " + res2);
     95 
     96         // ======= Test parse()
     97 
     98         logln("Testing parse()");
     99 
    100         String text = new String("02/03/76, 2:50 AM, CST");
    101         Date result1 = new Date();
    102         Date result2 = new Date();
    103         ParsePosition pos= new ParsePosition(0);
    104         result1 = def.parse(text, pos);
    105         logln(text + " parsed into " + result1);
    106 
    107         try {
    108             result2 = def.parse(text);
    109         }
    110         catch (ParseException e) {
    111             errln("ERROR: parse() failed");
    112         }
    113         logln(text + " parsed into " + result2);
    114 
    115         // ======= Test getters and setters
    116 
    117         logln("Testing getters and setters");
    118 
    119         final DateFormatSymbols syms = pat.getDateFormatSymbols();
    120         def.setDateFormatSymbols(syms);
    121         pat_fr.setDateFormatSymbols(syms);
    122         if( ! pat.getDateFormatSymbols().equals(def.getDateFormatSymbols()) ) {
    123             errln("ERROR: set DateFormatSymbols() failed");
    124         }
    125 
    126         /*
    127         DateFormatSymbols has not the method getTwoDigitStartDate();
    128         //Date startDate = null; //The variable is never used
    129         try {
    130 //            startDate = pat.getTwoDigitStartDate();
    131         }
    132         catch (Exception e) {
    133             errln("ERROR: getTwoDigitStartDate() failed");
    134         }
    135 
    136         try {
    137 //            pat_fr.setTwoDigitStartDate(startDate);
    138         }
    139         catch (Exception e) {
    140             errln("ERROR: setTwoDigitStartDate() failed");
    141         }*/
    142 
    143         // ======= Test applyPattern()
    144 
    145         logln("Testing applyPattern()");
    146 
    147         String p1 = new String("yyyy.MM.dd G 'at' hh:mm:ss z");
    148         logln("Applying pattern " + p1);
    149         pat.applyPattern(p1);
    150 
    151         String s2 = pat.toPattern();
    152         logln("Extracted pattern is " + s2);
    153         if( ! s2.equals(p1) ) {
    154             errln("ERROR: toPattern() result did not match pattern applied");
    155         }
    156 
    157         logln("Applying pattern " + p1);
    158         pat.applyLocalizedPattern(p1);
    159         String s3 = pat.toLocalizedPattern();
    160         logln("Extracted pattern is " + s3);
    161         if( ! s3.equals(p1) ) {
    162             errln("ERROR: toLocalizedPattern() result did not match pattern applied");
    163         }
    164 
    165         // ======= Test for Ticket 5684 (Parsing patterns with 'Y' and 'e'
    166         logln("Testing parse()");
    167 
    168         String p2 = new String("YYYY'W'wwe");
    169         logln("Applying pattern " + p2);
    170         pat.applyPattern(p2);
    171         Date dt = pat.parse("2007W014", new ParsePosition(0));
    172         if (dt == null) {
    173             errln("ERROR: Parsing failed using 'Y' and 'e'");
    174         }
    175 
    176 
    177         // ======= Test getStaticClassID()
    178 
    179 //        logln("Testing instanceof");
    180 
    181 //        try {
    182 //            DateFormat test = new SimpleDateFormat();
    183 
    184 //            if (! (test instanceof SimpleDateFormat)) {
    185 //                errln("ERROR: instanceof failed");
    186 //            }
    187 //        }
    188 //        catch (Exception e) {
    189 //            errln("ERROR: Couldn't create a SimpleDateFormat");
    190 //        }
    191     }
    192 
    193     // Jitterbug 4451, for coverage
    194     @Test
    195     public void TestCoverage(){
    196         class StubDateFormat extends SimpleDateFormat{
    197             /**
    198              * For serialization
    199              */
    200             private static final long serialVersionUID = 8460897119491427934L;
    201 
    202             public void run(){
    203                 if (!zeroPaddingNumber(12, 4, 6).equals("0012")){
    204                     errln("SimpleDateFormat(zeroPaddingNumber(long , int , int )");
    205                 }
    206             }
    207         }
    208         new StubDateFormat().run();
    209     }
    210 }
    211