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