1/*
2 * Copyright (c) 1997, 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 * @bug 4143459
27 * @summary test Date
28 * @library /java/text/testlib
29 */
30
31import java.text.*;
32import java.util.*;
33
34@SuppressWarnings("deprecation")
35public class DateTest extends IntlTest
36{
37    public static void main(String[] args) throws Exception {
38        new DateTest().run(args);
39    }
40
41    /**
42     * @bug 4143459
43     * Warning: Use TestDefaultZone() for complete testing of this bug.
44     */
45    public void TestDefaultZoneLite() {
46        // Note: This test is redundant with TestDefaultZone().  It was added by
47        // request to provide a short&sweet test for this bug.  It does not test
48        // all cases though, so IF THIS TEST PASSES, THE BUG MAY STILL BE
49        // PRESENT.  Use TestDefaultZone() to be sure.
50        TimeZone save = TimeZone.getDefault();
51        try {
52            TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
53            Date d = new Date();
54            d.setYear(98);
55            d.setMonth(Calendar.JANUARY);
56            d.setDate(1);
57            d.setHours(6);
58            TimeZone.setDefault(TimeZone.getTimeZone("PST"));
59            if (d.getHours() != 22) {
60                errln("Fail: Date.setHours()/getHours() ignoring default zone");
61            }
62        }
63        finally { TimeZone.setDefault(save); }
64    }
65
66    /**
67     * @bug 4143459
68     */
69    public void TestDefaultZone() {
70        // Various problems can creep up, with the current implementation of Date,
71        // when the default zone is changed.
72
73        TimeZone saveZone = TimeZone.getDefault();
74        try {
75
76            Date d = new Date(); // Trigger static init
77            Date ref = new Date(883634400000L); // This is Thu Jan 1 1998 6:00 am GMT
78            String refstr = "Jan 1 1998 6:00";
79            TimeZone GMT = TimeZone.getTimeZone("GMT");
80            TimeZone PST = TimeZone.getTimeZone("PST");
81
82            String[] names = { "year", "month", "date", "day of week", "hour", "offset" };
83            int[] GMT_EXP = { 98, Calendar.JANUARY, 1, Calendar.THURSDAY - Calendar.SUNDAY, 6, 0 };
84            int[] PST_EXP = { 97, Calendar.DECEMBER, 31, Calendar.WEDNESDAY - Calendar.SUNDAY, 22, 480 };
85
86            // There are two cases to consider: a Date object with no Calendar
87            // sub-object (most Date objects), and a Date object with a Calendar
88            // sub-object.  We make two passes to cover the two cases.
89            for (int pass=0; pass<2; ++pass) {
90                logln(pass == 0 ? "Normal Date object" : "Date with Calendar sub-object");
91
92                TimeZone.setDefault(GMT);
93                d = new Date(refstr);
94                if (pass == 1) {
95                    // Force creation of Calendar sub-object
96                    d.setYear(d.getYear());
97                }
98                if (d.getTime() != ref.getTime()) {
99                    errln("FAIL: new Date(\"" + refstr + "\") x GMT -> " + d +
100                          " " + d.getTime() + " ms");
101                }
102
103                int[] fields = { d.getYear(), d.getMonth(), d.getDate(),
104                                 d.getDay(), d.getHours(), d.getTimezoneOffset() };
105                for (int i=0; i<fields.length; ++i) {
106                    if (fields[i] != GMT_EXP[i]) {
107                        errln("FAIL: GMT Expected " + names[i] + " of " + GMT_EXP[i] +
108                              ", got " + fields[i]);
109                    }
110                }
111
112                TimeZone.setDefault(PST);
113                int[] fields2 = { d.getYear(), d.getMonth(), d.getDate(),
114                                  d.getDay(), d.getHours(), d.getTimezoneOffset() };
115                for (int i=0; i<fields2.length; ++i) {
116                    if (fields2[i] != PST_EXP[i]) {
117                        errln("FAIL: PST Expected " + names[i] + " of " + PST_EXP[i] +
118                              ", got " + fields2[i]);
119                    }
120                }
121            }
122        }
123        finally {
124            TimeZone.setDefault(saveZone);
125        }
126    }
127
128    // Test the performance of Date
129    public void TestPerformance592()
130    {
131        int REPS = 500;
132
133        // Do timing test with Date
134        long start = new Date().getTime();
135        for (int i=0; i<REPS; ++i)
136        {
137            Date d = new Date();
138            int y = d.getYear();
139        }
140        long ms = new Date().getTime() - start;
141
142        double perLoop = ((double)ms) / REPS;
143        logln(REPS + " iterations at " + perLoop + " ms/loop");
144        if (perLoop > PER_LOOP_LIMIT)
145            logln("WARNING: Date constructor/getYear slower than " +
146                  PER_LOOP_LIMIT + " ms");
147    }
148    static double PER_LOOP_LIMIT = 3.0;
149
150    /**
151     * Verify that the Date(String) constructor works.
152     */
153    public void TestParseOfGMT()
154    {
155        Date OUT = null;
156
157        /* Input values */
158        String stringVal = "Jan 01 00:00:00 GMT 1900";
159        long expectedVal = -2208988800000L;
160
161        OUT = new Date( stringVal );
162
163        if( OUT.getTime( ) == expectedVal ) {
164            // logln("PASS");
165        }
166        else {
167            errln( "Expected: " +
168                   new Date( expectedVal ) +
169                   ": " +
170                   expectedVal +
171                   "  Received: " +
172                   OUT.toString() +
173                   ": " +
174                   OUT.getTime() );
175        }
176    }
177
178    // Check out Date's behavior with large negative year values; bug 664
179    // As of the fix to bug 4056585, Date should work correctly with
180    // large negative years.
181    public void TestDateNegativeYears()
182    {
183        Date d1= new Date(80,-1,2);
184        logln(d1.toString());
185        d1= new Date(-80,-1,2);
186        logln(d1.toString());
187        boolean e = false;
188        try {
189            d1= new Date(-800000,-1,2);
190            logln(d1.toString());
191        }
192        catch (IllegalArgumentException ex) {
193            e = true;
194        }
195        if (e) errln("FAIL: Saw exception for year -800000");
196        else logln("Pass: No exception for year -800000");
197    }
198
199    // Verify the behavior of Date
200    public void TestDate480()
201    {
202      TimeZone save = TimeZone.getDefault();
203      try {
204        TimeZone.setDefault(TimeZone.getTimeZone("PST"));
205        Date d1=new java.util.Date(97,8,13,10,8,13);
206        logln("d       = "+d1);
207        Date d2=new java.util.Date(97,8,13,30,8,13); // 20 hours later
208        logln("d+20h   = "+d2);
209
210        double delta = (d2.getTime() - d1.getTime()) / 3600000;
211
212        logln("delta   = " + delta + "h");
213
214        if (delta != 20.0) errln("Expected delta of 20; got " + delta);
215
216        Calendar cal = Calendar.getInstance();
217        cal.clear();
218        cal.set(1997,8,13,10,8,13);
219        Date t1 = cal.getTime();
220        logln("d       = "+t1);
221        cal.clear();
222        cal.set(1997,8,13,30,8,13); // 20 hours later
223        Date t2 = cal.getTime();
224        logln("d+20h   = "+t2);
225
226        double delta2 = (t2.getTime() - t1.getTime()) / 3600000;
227
228        logln("delta   = " + delta2 + "h");
229
230        if (delta != 20.0) errln("Expected delta of 20; got " + delta2);
231      }
232      finally {
233        TimeZone.setDefault(save);
234      }
235    }
236}
237