Home | History | Annotate | Download | only in format
      1 //  2016 and later: Unicode, Inc. and others.
      2 // License & terms of use: http://www.unicode.org/copyright.html#License
      3 /*****************************************************************************************
      4  *
      5  *   Copyright (C) 1996-2015, International Business Machines
      6  *   Corporation and others.  All Rights Reserved.
      7  **/
      8 
      9 /**
     10  * Port From:   JDK 1.4b1 : java.text.Format.IntlTestDateFormatAPI
     11  * Source File: java/text/format/IntlTestDateFormatAPI.java
     12  **/
     13 
     14 /*
     15     @test 1.4 98/03/06
     16     @summary test International Date Format API
     17 */
     18 
     19 package com.ibm.icu.dev.test.format;
     20 
     21 import java.text.FieldPosition;
     22 import java.text.ParseException;
     23 import java.text.ParsePosition;
     24 import java.util.Date;
     25 import java.util.Locale;
     26 
     27 import org.junit.Test;
     28 
     29 import com.ibm.icu.dev.test.TestUtil;
     30 import com.ibm.icu.dev.test.TestUtil.JavaVendor;
     31 import com.ibm.icu.text.DateFormat;
     32 import com.ibm.icu.text.NumberFormat;
     33 import com.ibm.icu.util.Calendar;
     34 import com.ibm.icu.util.TimeZone;
     35 
     36 public class IntlTestDateFormatAPI extends com.ibm.icu.dev.test.TestFmwk
     37 {
     38     // Test that the equals method works correctly.
     39     @Test
     40     public void TestEquals()
     41     {
     42         // Create two objects at different system times
     43         DateFormat a = DateFormat.getInstance();
     44         Date start = Calendar.getInstance().getTime();
     45         while (true) {
     46             // changed to remove compiler warnings.
     47             if (!start.equals(Calendar.getInstance().getTime())) {
     48                 break; // Wait for time to change
     49             }
     50         }
     51         DateFormat b = DateFormat.getInstance();
     52 
     53         if (!(a.equals(b)))
     54             errln("FAIL: DateFormat objects created at different times are unequal.");
     55 
     56         // Why has this test been disabled??? - aliu
     57 //        if (b instanceof SimpleDateFormat)
     58 //        {
     59 //            //double ONE_YEAR = 365*24*60*60*1000.0; //The variable is never used
     60 //            try {
     61 //                ((SimpleDateFormat)b).setTwoDigitStartDate(start.getTime() + 50*ONE_YEAR);
     62 //                if (a.equals(b))
     63 //                    errln("FAIL: DateFormat objects with different two digit start dates are equal.");
     64 //            }
     65 //            catch (Exception e) {
     66 //                errln("FAIL: setTwoDigitStartDate failed.");
     67 //            }
     68 //        }
     69     }
     70 
     71     // This test checks various generic API methods in DateFormat to achieve 100% API coverage.
     72     @Test
     73     public void TestAPI()
     74     {
     75         logln("DateFormat API test---"); logln("");
     76         Locale.setDefault(Locale.ENGLISH);
     77 
     78 
     79         // ======= Test constructors
     80 
     81         logln("Testing DateFormat constructors");
     82 
     83         DateFormat def = DateFormat.getInstance();
     84         DateFormat fr = DateFormat.getTimeInstance(DateFormat.FULL, Locale.FRENCH);
     85         DateFormat it = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ITALIAN);
     86         DateFormat de = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.GERMAN);
     87 
     88         // ======= Test equality
     89 
     90         logln("Testing equality operator");
     91 
     92         if( fr.equals(it) ) {
     93             errln("ERROR: equals failed");
     94         }
     95 
     96         // ======= Test various format() methods
     97 
     98         logln("Testing various format() methods");
     99 
    100         Date d = new Date((long)837039928046.0);
    101 
    102         StringBuffer res1 = new StringBuffer();
    103         StringBuffer res2 = new StringBuffer();
    104         String res3 = new String();
    105         FieldPosition pos1 = new FieldPosition(0);
    106         FieldPosition pos2 = new FieldPosition(0);
    107 
    108         res1 = fr.format(d, res1, pos1);
    109         logln("" + d.getTime() + " formatted to " + res1);
    110 
    111         res2 = it.format(d, res2, pos2);
    112         logln("" + d.getTime() + " formatted to " + res2);
    113 
    114         res3 = de.format(d);
    115         logln("" + d.getTime() + " formatted to " + res3);
    116 
    117         // ======= Test parse()
    118 
    119         logln("Testing parse()");
    120 
    121         String text = new String("02/03/76, 2:50 AM, CST");
    122         Object result1 = new Date();
    123         Date result2 = new Date();
    124         Date result3 = new Date();
    125         ParsePosition pos = new ParsePosition(0);
    126         ParsePosition pos01 = new ParsePosition(0);
    127 
    128         result1 = def.parseObject(text, pos);
    129         if (result1 == null) {
    130             errln("ERROR: parseObject() failed for " + text);
    131         }
    132         logln(text + " parsed into " + ((Date)result1).getTime());
    133 
    134         try {
    135             result2 = def.parse(text);
    136         }
    137         catch (ParseException e) {
    138             errln("ERROR: parse() failed");
    139         }
    140         logln(text + " parsed into " + result2.getTime());
    141 
    142         result3 = def.parse(text, pos01);
    143         if (result3 == null) {
    144             errln("ERROR: parse() failed for " + text);
    145         }
    146         logln(text + " parsed into " + result3.getTime());
    147 
    148 
    149         // ======= Test getters and setters
    150 
    151         logln("Testing getters and setters");
    152 
    153         final Locale[] locales = DateFormat.getAvailableLocales();
    154         long count = locales.length;
    155         logln("Got " + count + " locales" );
    156 
    157         // Ticket #6280, #8078 and #11674
    158         // These locales should be included in the result
    159         boolean missingLocaleNotFatal =
    160                 TestUtil.getJavaVendor() == JavaVendor.Android || TestUtil.getJavaVersion() >= 7;
    161         final Locale[] samples = {
    162                 new Locale("zh", "CN"),
    163                 new Locale("zh", "TW"),
    164                 new Locale("zh", "HK"),
    165                 new Locale("sr", "RS"),
    166         };
    167         boolean[] available = new boolean[samples.length];
    168         for(int i = 0; i < count; i++) {
    169             String name;
    170             name = locales[i].getDisplayName();
    171             logln(name);
    172             for (int j = 0; j < samples.length; j++) {
    173                 if (locales[i].equals(samples[j])) {
    174                     available[j] = true;
    175                     break;
    176                 }
    177             }
    178         }
    179         for (int i = 0; i < available.length; i++) {
    180             if (!available[i]) {
    181                 if (missingLocaleNotFatal) {
    182                     // Java 7 supports script field, so zh_Hans_CN is included
    183                     // in the available locale list.
    184                     logln("INFO: missing Locale: " + samples[i]);
    185                 } else {
    186                     errln("ERROR: missing Locale: " + samples[i]);
    187                 }
    188             }
    189         }
    190 
    191         fr.setLenient(it.isLenient());
    192         if(fr.isLenient() != it.isLenient()) {
    193             errln("ERROR: setLenient() failed");
    194         }
    195 
    196         final Calendar cal = def.getCalendar();
    197         Calendar newCal = (Calendar) cal.clone();
    198         de.setCalendar(newCal);
    199         it.setCalendar(newCal);
    200         if( ! de.getCalendar().equals(it.getCalendar())) {
    201             errln("ERROR: set Calendar() failed");
    202         }
    203 
    204         final NumberFormat nf = def.getNumberFormat();
    205         NumberFormat newNf = (NumberFormat) nf.clone();
    206         de.setNumberFormat(newNf);
    207         it.setNumberFormat(newNf);
    208         if( ! de.getNumberFormat().equals(it.getNumberFormat())) {
    209             errln("ERROR: set NumberFormat() failed");
    210         }
    211 
    212         final TimeZone tz = def.getTimeZone();
    213         TimeZone newTz = (TimeZone) tz.clone();
    214         de.setTimeZone(newTz);
    215         it.setTimeZone(newTz);
    216         if( ! de.getTimeZone().equals(it.getTimeZone())) {
    217             errln("ERROR: set TimeZone() failed");
    218         }
    219 
    220         // ======= Test getStaticClassID()
    221 
    222 //        logln("Testing instanceof()");
    223 
    224 //        try {
    225 //            DateFormat test = new SimpleDateFormat();
    226 
    227 //            if (! (test instanceof SimpleDateFormat)) {
    228 //                errln("ERROR: instanceof failed");
    229 //            }
    230 //        }
    231 //        catch (Exception e) {
    232 //            errln("ERROR: Couldn't create a DateFormat");
    233 //        }
    234     }
    235 }
    236