• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.apache.harmony.sql.tests.java.sql;
19 
20 import java.sql.Date;
21 import java.util.Calendar;
22 import java.util.TimeZone;
23 
24 import junit.framework.TestCase;
25 
26 /**
27  * JUnit Testcase for the java.sql.Date class
28  */
29 public class DateTest extends TestCase {
30 
31     // A calendar object created in the GMT time zone
32     static Calendar aCal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
33 
34     // Some interesting millisecond time values
35     // These millisecond times are all in GMT, effectively
36     static long TIME_AN_HOUR = 3600000; // 1000 * 60 * 60 ms
37 
38     static long TIME_EPOCH = 0;
39 
40     static long TIME_NOW = System.currentTimeMillis();
41 
42     static long TIME_NEGATIVE = -3600001;
43 
44     static long TIME_TESTDATE1 = getTime(1999, Calendar.DECEMBER, 31, 23, 59,
45             59);
46 
47     static long TIME_TESTDATE2 = getTime(2010, Calendar.JUNE, 10, 20, 3, 16);
48 
49     static long TIME_TESTDATE3 = getTime(1931, Calendar.APRIL, 21, 1, 25, 1);
50 
51     static long TIME_LOWERLIMIT = Long.MIN_VALUE;
52 
53     static long TIME_UPPERLIMIT = Long.MAX_VALUE;
54 
55     // Date strings
56     static String SQL_DATESTRING1 = "1999-12-31";
57 
58     static String SQL_DATESTRING2 = "2010-06-10";
59 
60     static String SQL_DATESTRING3 = "1931-04-21";
61 
62     static String SQL_EPOCHSTRING = "1970-01-01";
63 
64     static String SQL_DATEDAY1 = "1970-01-02";
65 
66     static String SQL_NEGATIVE = "1969-12-31";
67 
68     static long[] TIME_ARRAY = new long[] { TIME_TESTDATE1, TIME_TESTDATE2,
69             TIME_TESTDATE3, TIME_NEGATIVE, TIME_EPOCH };
70 
71     // Date string array for London (GMT)
72     static String[] SQL_DATEARRAY = new String[] { SQL_DATESTRING1,
73             SQL_DATESTRING2, SQL_DATESTRING3, SQL_NEGATIVE, SQL_EPOCHSTRING };
74 
75     // Date string array for New York - sometimes a day earlier than London
76     static String[] SQL_NYARRAY = new String[] { "1999-12-31", "2010-06-10",
77             "1931-04-20", "1969-12-31", "1969-12-31" };
78 
79     // Date string for Tokyo
80     static String[] SQL_JAPANARRAY = new String[] { "2000-01-01", "2010-06-11",
81             "1931-04-21", "1970-01-01", "1970-01-01" };
82 
83     static String[][] SQL_TZ_DATEARRAYS = new String[][] { SQL_DATEARRAY,
84             SQL_NYARRAY, SQL_JAPANARRAY };
85 
86     // Timezones
87     static String TZ_LONDON = "Europe/London"; // Note: != GMT
88 
89     static String TZ_PACIFIC = "America/Los_Angeles"; // GNT - 8
90 
91     static String TZ_JAPAN = "Asia/Tokyo"; // GMT + 9
92 
93     static String[] TIMEZONES = { TZ_LONDON, TZ_PACIFIC, TZ_JAPAN };
94 
95     /*
96      * Helper method to create a long milliseconds time from a supplied date and
97      * time
98      */
getTime(int year, int month, int date, int hour, int minute, int second)99     static private long getTime(int year, int month, int date, int hour,
100             int minute, int second) {
101         aCal.set(year, month, date, hour, minute, second);
102         return aCal.getTimeInMillis();
103     } // end method getTime( int, int, int, int, int, int )
104 
105     /*
106      * Test of the Date(int, int, int) constructor - now deprecated but still
107      * functioning
108      */
109     @SuppressWarnings("deprecation")
testDateintintint()110     public void testDateintintint() {
111 
112         int init1[] = { 99, 8099, 9000, 99999, 99, 99, -1, -100 };
113         int init2[] = { 11, 0, 0, 0, 999, 0, 0, -111 };
114         int init3[] = { 31, 0, 0, 0, 0, 999, 0, -999 };
115 
116         for (int i = 0; i < init1.length; i++) {
117             Date theDate = new Date(init1[i], init2[i], init3[i]);
118             assertNotNull(theDate);
119         } // end for
120 
121     } // end method testDateintintint
122 
123     /*
124      * Test of the Date( long ) constructor
125      */
testDatelong()126     public void testDatelong() {
127 
128         long init1[] = { TIME_TESTDATE1, TIME_TESTDATE2, TIME_TESTDATE3,
129                 TIME_NEGATIVE, TIME_LOWERLIMIT, TIME_UPPERLIMIT, TIME_EPOCH,
130                 TIME_NOW };
131 
132         for (long element : init1) {
133             Date theDate = new Date(element);
134             assertNotNull(theDate);
135         } // end for
136 
137     } // end method testDatelong
138 
139     /*
140      * Test of the (deprecated) int Date.getHours() method - which always throws
141      * an IllegalArgumentException
142      */
143     @SuppressWarnings("deprecation")
testGetHours()144     public void testGetHours() {
145         Date theDate = new Date(TIME_TESTDATE1);
146         try {
147             theDate.getHours();
148             fail("Should throw IllegalArgumentException.");
149         } catch (IllegalArgumentException ie) {
150             // expected
151         } // end try
152     } // end method testGetHours()
153 
154     /*
155      * Test of the (deprecated) int Date.getMinutes() method - which always
156      * throws an IllegalArgumentException
157      */
158     @SuppressWarnings("deprecation")
testGetMinutes()159     public void testGetMinutes() {
160         Date theDate = new Date(TIME_TESTDATE1);
161         try {
162             theDate.getMinutes();
163             fail("Should throw IllegalArgumentException.");
164         } catch (IllegalArgumentException ie) {
165             // expected
166         } // end try
167     } // end method testGetMinutes()
168 
169     /*
170      * Test of the (deprecated) int Date.getSeconds() method - which always
171      * throws an IllegalArgumentException
172      */
173     @SuppressWarnings("deprecation")
testGetSeconds()174     public void testGetSeconds() {
175         Date theDate = new Date(TIME_TESTDATE1);
176         try {
177             theDate.getSeconds();
178             fail("Should throw IllegalArgumentException.");
179         } catch (IllegalArgumentException ie) {
180             // expected
181         } // end try
182     } // end method testGetSeconds()
183 
184     /*
185      * Test of the (deprecated) Date.setHours( int ) method - which always
186      * throws an IllegalArgumentException
187      */
188     @SuppressWarnings("deprecation")
testSetHours()189     public void testSetHours() {
190         Date theDate = new Date(TIME_TESTDATE1);
191         try {
192             theDate.setHours(22);
193             fail("Should throw IllegalArgumentException.");
194         } catch (IllegalArgumentException ie) {
195             // expected
196         } // end try
197     } // end method testSetHours( int )
198 
199     /*
200      * Test of the (deprecated) Date.setMinutes( int ) method - which always
201      * throws an IllegalArgumentException
202      */
203     @SuppressWarnings("deprecation")
testSetMinutes()204     public void testSetMinutes() {
205         Date theDate = new Date(TIME_TESTDATE1);
206         try {
207             theDate.setMinutes(54);
208             fail("Should throw IllegalArgumentException.");
209         } catch (IllegalArgumentException ie) {
210             // expected
211         } // end try
212 
213     } // end method testSetMinutes( int )
214 
215     /*
216      * Test of the (deprecated) Date.setSeconds( int ) method - which always
217      * throws an IllegalArgumentException
218      */
219     @SuppressWarnings("deprecation")
testSetSeconds()220     public void testSetSeconds() {
221         Date theDate = new Date(TIME_TESTDATE1);
222         try {
223             theDate.setSeconds(36);
224             fail("Should throw IllegalArgumentException.");
225         } catch (IllegalArgumentException ie) {
226             // expected
227         } // end try
228     } // end method testSetSeconds( int )
229 
230     /*
231      * Test of the String Date.toString() method This method is sensitive to the
232      * time zone setting and this test sets the time zone before calling the
233      * toString() method.
234      */
testToString()235     public void testToString() {
236         // Loop through the timezones testing the String conversion for each
237         for (int i = 0; i < TIMEZONES.length; i++) {
238             testToString(TIMEZONES[i], TIME_ARRAY, SQL_TZ_DATEARRAYS[i]);
239         } // end for
240 
241     } // end method testToString()
242 
testToString(String timeZone, long[] theDates, String[] theDateStrings)243     private void testToString(String timeZone, long[] theDates, String[] theDateStrings) {
244         // Set the timezone
245         TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
246 
247         for (int i = 0; i < theDates.length; i++) {
248             // Create the Date object
249             Date theDate = new Date(theDates[i]);
250             // Convert to a date string ... and compare
251             String JDBCString = theDate.toString();
252             assertEquals(theDateStrings[i], JDBCString);
253         } // end for
254 
255     } // end testToString( String, long[], String[] )
256 
257     /*
258      * Test of the void setTime(int) method This does depend on the Time Zone
259      * settings and sets up the time zone to one of a group of specific time
260      * zones and tests the method using each of these time zones in turn.
261      */
testSetTimelong()262     public void testSetTimelong() {
263 
264         // Loop over the array of test timezones
265         for (int i = 0; i < TIMEZONES.length; i++) {
266             testSetTimelong(TIMEZONES[i], SQL_TZ_DATEARRAYS[i]);
267         } // end for
268 
269     } // end method testSetTimelong()
270 
271     /*
272      * Internal method for testing Date.setTime with a specific time zone
273      */
testSetTimelong(String timeZoneName, String[] dateArray)274     private void testSetTimelong(String timeZoneName, String[] dateArray) {
275 
276         if (timeZoneName != null) {
277             TimeZone.setDefault(TimeZone.getTimeZone(timeZoneName));
278         } // end if
279 
280         Date theDate = new Date(TIME_TESTDATE1);
281 
282         // Loop over the array of test times & dates
283         for (int i = 0; i < dateArray.length; i++) {
284             theDate.setTime(TIME_ARRAY[i]);
285             assertEquals(dateArray[i], theDate.toString());
286         } // end for
287 
288     } // end method testSetTimelong()
289 
290     /*
291      * Test of the Date.valueOf( String ) method This test is not dependent on
292      * the default Time Zone setting
293      */
testValueOf()294     public void testValueOf() {
295         String SQL_NOTVALID1 = "ABCDEF"; // Invalid date string
296         String SQL_NOTVALID2 = "12321.43.56"; // Invalid date string
297         String SQL_NOTVALID3 = null; // Invalid date string
298         String[] SQL_INVALIDARRAY = { SQL_NOTVALID1, SQL_NOTVALID2,
299                 SQL_NOTVALID3 };
300 
301         Date theDate;
302 
303         for (String element : SQL_DATEARRAY) {
304             theDate = Date.valueOf(element);
305             assertEquals(element, theDate.toString());
306         } // end for
307 
308         for (String element : SQL_INVALIDARRAY) {
309             try {
310                 theDate = Date.valueOf(element);
311                 fail("Should throw IllegalArgumentException.");
312             } catch (IllegalArgumentException e) {
313                 // expected
314             } // end try
315         } // end for
316 
317     } // end method testValueOf()
318 
319     /**
320      * @tests java.sql.Date#valueOf(String)
321      */
test_valueOf_IllegalArgumentException()322     public void test_valueOf_IllegalArgumentException() {
323         try {
324             Date.valueOf("1996-10-07-01");
325             fail("should throw NumberFormatException");
326         } catch (NumberFormatException e) {
327             // expected
328         }
329 
330         try {
331             Date.valueOf("-10-07-01");
332             fail("should throw IllegalArgumentException");
333         } catch (IllegalArgumentException e) {
334             // expected
335         }
336 
337         try {
338             Date.valueOf("--01");
339             fail("should throw IllegalArgumentException");
340         } catch (IllegalArgumentException e) {
341             // expected
342         }
343 
344         try {
345             Date.valueOf("1991--");
346             fail("should throw IllegalArgumentException");
347         } catch (IllegalArgumentException e) {
348             // expected
349         }
350 
351         try {
352             Date.valueOf("-01-");
353             fail("should throw IllegalArgumentException");
354         } catch (IllegalArgumentException e) {
355             // expected
356         }
357 
358         try {
359             Date.valueOf("-10-w2-01");
360             fail("should throw IllegalArgumentException");
361         } catch (IllegalArgumentException e) {
362             // expected
363         }
364 
365         try {
366             Date.valueOf("07-w2-");
367             fail("should throw IllegalArgumentException");
368         } catch (IllegalArgumentException e) {
369             // expected
370         }
371 
372         try {
373             Date.valueOf("1997-w2-w2");
374             fail("should throw NumberFormatException");
375         } catch (NumberFormatException e) {
376             // expected
377         }
378 
379         try {
380             Date.valueOf("1996--01");
381             fail("should throw NumberFormatException");
382         } catch (NumberFormatException e) {
383             // expected
384         }
385     }
386 
387     // Reset defualt timezone
388     static TimeZone defaultTimeZone = TimeZone.getDefault();
389 
tearDown()390     protected void tearDown() {
391         TimeZone.setDefault(defaultTimeZone);
392     }
393 
394 } // end class DateTest
395 
396