001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.sql.tests.java.sql;
019:
020: import java.sql.Date;
021: import java.util.Calendar;
022: import java.util.TimeZone;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * JUnit Testcase for the java.sql.Date class
028: *
029: */
030: public class DateTest extends TestCase {
031:
032: // A calendar object created in the GMT time zone
033: static Calendar aCal = Calendar.getInstance(TimeZone
034: .getTimeZone("GMT"));
035:
036: // Some interesting millisecond time values
037: // These millisecond times are all in GMT, effectively
038: static long TIME_AN_HOUR = 3600000; // 1000 * 60 * 60 ms
039:
040: static long TIME_EPOCH = 0;
041:
042: static long TIME_NOW = System.currentTimeMillis();
043:
044: static long TIME_NEGATIVE = -3600001;
045:
046: static long TIME_TESTDATE1 = getTime(1999, Calendar.DECEMBER, 31,
047: 23, 59, 59);
048:
049: static long TIME_TESTDATE2 = getTime(2010, Calendar.JUNE, 10, 20,
050: 3, 16);
051:
052: static long TIME_TESTDATE3 = getTime(1931, Calendar.APRIL, 21, 1,
053: 25, 1);
054:
055: static long TIME_LOWERLIMIT = Long.MIN_VALUE;
056:
057: static long TIME_UPPERLIMIT = Long.MAX_VALUE;
058:
059: // Date strings
060: static String SQL_DATESTRING1 = "1999-12-31";
061:
062: static String SQL_DATESTRING2 = "2010-06-10";
063:
064: static String SQL_DATESTRING3 = "1931-04-21";
065:
066: static String SQL_EPOCHSTRING = "1970-01-01";
067:
068: static String SQL_DATEDAY1 = "1970-01-02";
069:
070: static String SQL_NEGATIVE = "1969-12-31";
071:
072: static long[] TIME_ARRAY = new long[] { TIME_TESTDATE1,
073: TIME_TESTDATE2, TIME_TESTDATE3, TIME_NEGATIVE, TIME_EPOCH };
074:
075: // Date string array for London (GMT)
076: static String[] SQL_DATEARRAY = new String[] { SQL_DATESTRING1,
077: SQL_DATESTRING2, SQL_DATESTRING3, SQL_NEGATIVE,
078: SQL_EPOCHSTRING };
079:
080: // Date string array for New York - sometimes a day earlier than London
081: static String[] SQL_NYARRAY = new String[] { "1999-12-31",
082: "2010-06-10", "1931-04-20", "1969-12-31", "1969-12-31" };
083:
084: // Date string for Tokyo
085: static String[] SQL_JAPANARRAY = new String[] { "2000-01-01",
086: "2010-06-11", "1931-04-21", "1970-01-01", "1970-01-01" };
087:
088: static String[][] SQL_TZ_DATEARRAYS = new String[][] {
089: SQL_DATEARRAY, SQL_NYARRAY, SQL_JAPANARRAY };
090:
091: // Timezones
092: static String TZ_LONDON = "Europe/London"; // Note: != GMT
093:
094: static String TZ_PACIFIC = "America/Los_Angeles"; // GNT - 8
095:
096: static String TZ_JAPAN = "Asia/Tokyo"; // GMT + 9
097:
098: static String[] TIMEZONES = { TZ_LONDON, TZ_PACIFIC, TZ_JAPAN };
099:
100: /*
101: * Helper method to create a long milliseconds time from a supplied date and
102: * time
103: */
104: static private long getTime(int year, int month, int date,
105: int hour, int minute, int second) {
106: aCal.set(year, month, date, hour, minute, second);
107: return aCal.getTimeInMillis();
108: } // end method getTime( int, int, int, int, int, int )
109:
110: /*
111: * Test of the Date(int, int, int) constructor - now deprecated but still
112: * functioning
113: */
114: @SuppressWarnings("deprecation")
115: public void testDateintintint() {
116:
117: int init1[] = { 99, 8099, 9000, 99999, 99, 99, -1, -100 };
118: int init2[] = { 11, 0, 0, 0, 999, 0, 0, -111 };
119: int init3[] = { 31, 0, 0, 0, 0, 999, 0, -999 };
120:
121: for (int i = 0; i < init1.length; i++) {
122: Date theDate = new Date(init1[i], init2[i], init3[i]);
123: assertNotNull(theDate);
124: } // end for
125:
126: } // end method testDateintintint
127:
128: /*
129: * Test of the Date( long ) constructor
130: */
131: public void testDatelong() {
132:
133: long init1[] = { TIME_TESTDATE1, TIME_TESTDATE2,
134: TIME_TESTDATE3, TIME_NEGATIVE, TIME_LOWERLIMIT,
135: TIME_UPPERLIMIT, TIME_EPOCH, TIME_NOW };
136:
137: for (long element : init1) {
138: Date theDate = new Date(element);
139: assertNotNull(theDate);
140: } // end for
141:
142: } // end method testDatelong
143:
144: /*
145: * Test of the (deprecated) int Date.getHours() method - which always throws
146: * an IllegalArgumentException
147: */
148: @SuppressWarnings("deprecation")
149: public void testGetHours() {
150: Date theDate = new Date(TIME_TESTDATE1);
151: try {
152: theDate.getHours();
153: fail("Should throw IllegalArgumentException.");
154: } catch (IllegalArgumentException ie) {
155: // expected
156: } // end try
157: } // end method testGetHours()
158:
159: /*
160: * Test of the (deprecated) int Date.getMinutes() method - which always
161: * throws an IllegalArgumentException
162: */
163: @SuppressWarnings("deprecation")
164: public void testGetMinutes() {
165: Date theDate = new Date(TIME_TESTDATE1);
166: try {
167: theDate.getMinutes();
168: fail("Should throw IllegalArgumentException.");
169: } catch (IllegalArgumentException ie) {
170: // expected
171: } // end try
172: } // end method testGetMinutes()
173:
174: /*
175: * Test of the (deprecated) int Date.getSeconds() method - which always
176: * throws an IllegalArgumentException
177: */
178: @SuppressWarnings("deprecation")
179: public void testGetSeconds() {
180: Date theDate = new Date(TIME_TESTDATE1);
181: try {
182: theDate.getSeconds();
183: fail("Should throw IllegalArgumentException.");
184: } catch (IllegalArgumentException ie) {
185: // expected
186: } // end try
187: } // end method testGetSeconds()
188:
189: /*
190: * Test of the (deprecated) Date.setHours( int ) method - which always
191: * throws an IllegalArgumentException
192: */
193: @SuppressWarnings("deprecation")
194: public void testSetHours() {
195: Date theDate = new Date(TIME_TESTDATE1);
196: try {
197: theDate.setHours(22);
198: fail("Should throw IllegalArgumentException.");
199: } catch (IllegalArgumentException ie) {
200: // expected
201: } // end try
202: } // end method testSetHours( int )
203:
204: /*
205: * Test of the (deprecated) Date.setMinutes( int ) method - which always
206: * throws an IllegalArgumentException
207: */
208: @SuppressWarnings("deprecation")
209: public void testSetMinutes() {
210: Date theDate = new Date(TIME_TESTDATE1);
211: try {
212: theDate.setMinutes(54);
213: fail("Should throw IllegalArgumentException.");
214: } catch (IllegalArgumentException ie) {
215: // expected
216: } // end try
217:
218: } // end method testSetMinutes( int )
219:
220: /*
221: * Test of the (deprecated) Date.setSeconds( int ) method - which always
222: * throws an IllegalArgumentException
223: */
224: @SuppressWarnings("deprecation")
225: public void testSetSeconds() {
226: Date theDate = new Date(TIME_TESTDATE1);
227: try {
228: theDate.setSeconds(36);
229: fail("Should throw IllegalArgumentException.");
230: } catch (IllegalArgumentException ie) {
231: // expected
232: } // end try
233: } // end method testSetSeconds( int )
234:
235: /*
236: * Test of the String Date.toString() method This method is sensitive to the
237: * time zone setting and this test sets the time zone before calling the
238: * toString() method.
239: */
240: public void testToString() {
241: // This test is set up for GMT time zone, so need to set the time zone
242: // to GMT first
243: TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
244:
245: for (int i = 0; i < TIME_ARRAY.length; i++) {
246: Date theDate = new Date(TIME_ARRAY[i]);
247: assertEquals(SQL_DATEARRAY[i], theDate.toString());
248: } // end for
249:
250: } // end method testToString()
251:
252: /*
253: * Test of the void setTime(int) method This does depend on the Time Zone
254: * settings and sets up the time zone to one of a group of specific time
255: * zones and tests the method using each of these time zones in turn.
256: */
257: public void testSetTimelong() {
258:
259: // Loop over the array of test timezones
260: for (int i = 0; i < TIMEZONES.length; i++) {
261: testSetTimelong(TIMEZONES[i], SQL_TZ_DATEARRAYS[i]);
262: } // end for
263:
264: } // end method testSetTimelong()
265:
266: /*
267: * Internal method for testing Date.setTime with a specific time zone
268: */
269: private void testSetTimelong(String timeZoneName, String[] dateArray) {
270:
271: if (timeZoneName != null) {
272: TimeZone.setDefault(TimeZone.getTimeZone(timeZoneName));
273: } // end if
274:
275: Date theDate = new Date(TIME_TESTDATE1);
276:
277: // Loop over the array of test times & dates
278: for (int i = 0; i < dateArray.length; i++) {
279: theDate.setTime(TIME_ARRAY[i]);
280: assertEquals(dateArray[i], theDate.toString());
281: } // end for
282:
283: } // end method testSetTimelong()
284:
285: /*
286: * Test of the Date.valueOf( String ) method This test is not dependent on
287: * the default Time Zone setting
288: */
289: public void testValueOf() {
290: String SQL_NOTVALID1 = "ABCDEF"; // Invalid date string
291: String SQL_NOTVALID2 = "12321.43.56"; // Invalid date string
292: String SQL_NOTVALID3 = null; // Invalid date string
293: String[] SQL_INVALIDARRAY = { SQL_NOTVALID1, SQL_NOTVALID2,
294: SQL_NOTVALID3 };
295:
296: Date theDate;
297:
298: for (String element : SQL_DATEARRAY) {
299: theDate = Date.valueOf(element);
300: assertEquals(element, theDate.toString());
301: } // end for
302:
303: for (String element : SQL_INVALIDARRAY) {
304: try {
305: theDate = Date.valueOf(element);
306: fail("Should throw IllegalArgumentException.");
307: } catch (IllegalArgumentException e) {
308: // expected
309: } // end try
310: } // end for
311:
312: } // end method testValueOf()
313:
314: /**
315: * @tests java.sql.Date#valueOf(String )
316: */
317: public void test_valueOf_IllegalArgumentException() {
318: try {
319: Date.valueOf("1996-10-07-01");
320: fail("should throw NumberFormatException");
321: } catch (NumberFormatException e) {
322: // expected
323: }
324:
325: try {
326: Date.valueOf("-10-07-01");
327: fail("should throw IllegalArgumentException");
328: } catch (IllegalArgumentException e) {
329: // expected
330: }
331:
332: try {
333: Date.valueOf("--01");
334: fail("should throw IllegalArgumentException");
335: } catch (IllegalArgumentException e) {
336: // expected
337: }
338:
339: try {
340: Date.valueOf("1991--");
341: fail("should throw IllegalArgumentException");
342: } catch (IllegalArgumentException e) {
343: // expected
344: }
345:
346: try {
347: Date.valueOf("-01-");
348: fail("should throw IllegalArgumentException");
349: } catch (IllegalArgumentException e) {
350: // expected
351: }
352:
353: try {
354: Date.valueOf("-10-w2-01");
355: fail("should throw IllegalArgumentException");
356: } catch (IllegalArgumentException e) {
357: // expected
358: }
359:
360: try {
361: Date.valueOf("07-w2-");
362: fail("should throw IllegalArgumentException");
363: } catch (IllegalArgumentException e) {
364: // expected
365: }
366:
367: try {
368: Date.valueOf("1997-w2-w2");
369: fail("should throw NumberFormatException");
370: } catch (NumberFormatException e) {
371: // expected
372: }
373:
374: try {
375: Date.valueOf("1996--01");
376: fail("should throw NumberFormatException");
377: } catch (NumberFormatException e) {
378: // expected
379: }
380: }
381:
382: } // end class DateTest
|