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.Timestamp;
021: import java.util.Date;
022: import java.util.TimeZone;
023:
024: import org.apache.harmony.testframework.serialization.SerializationTest;
025: import junit.framework.TestCase;
026:
027: /**
028: * JUnit Testcase for the java.sql.Timestamp class
029: *
030: */
031:
032: public class TimestampTest extends TestCase {
033:
034: static long TIME_TEST1 = 38720231; // 10:45:20.231 GMT
035:
036: static long TIME_TEST2 = 80279000; // 22:17:59.000 GMT
037:
038: static long TIME_TEST3 = -38720691; // 13:14:39.309 GMT
039:
040: static long TIME_COMPARE = 123498845;
041:
042: static long TIME_EARLY = -2347889122L;// A time well before the Epoch
043:
044: static long TIME_LATE = 2347889122L; // A time well after the Epoch
045:
046: static String STRING_TEST1 = "1970-01-01 10:45:20.231"; // "1970-01-01
047:
048: // 10:45:20.231000000";
049:
050: static String STRING_TEST2 = "1970-01-01 22:17:59.0"; // "1970-01-01
051:
052: // 22:17:59.000000000";
053:
054: static String STRING_TEST3 = "1969-12-31 13:14:39.309"; // "1969-12-31
055:
056: // 13:14:39.309000000";
057:
058: static String STRING_INVALID1 = "ABCDEFGHI";
059:
060: static String STRING_INVALID2 = "233104";
061:
062: static String STRING_INVALID3 = "21-43-48";
063:
064: // A timepoint in the correct format but with numeric values out of range
065: // ...this is accepted despite being a crazy date specification
066: // ...it is treated as the correct format date 3000-06-08 12:40:06.875 !!
067: static String STRING_OUTRANGE = "2999-15-99 35:99:66.875";
068:
069: static long[] TIME_ARRAY = { TIME_TEST1, TIME_TEST2, TIME_TEST3 };
070:
071: static int[] YEAR_ARRAY = { 70, 70, 69 };
072:
073: static int[] MONTH_ARRAY = { 0, 0, 11 };
074:
075: static int[] DATE_ARRAY = { 1, 1, 31 };
076:
077: static int[] HOURS_ARRAY = { 10, 22, 13 };
078:
079: static int[] MINUTES_ARRAY = { 45, 17, 14 };
080:
081: static int[] SECONDS_ARRAY = { 20, 59, 39 };
082:
083: static int[] NANOS_ARRAY = { 231000000, 000000000, 309000000 };
084:
085: static int[] NANOS_ARRAY2 = { 137891990, 635665198, 109985421 };
086:
087: static String[] STRING_NANOS_ARRAY = {
088: "1970-01-01 10:45:20.13789199",
089: "1970-01-01 22:17:59.635665198",
090: "1969-12-31 13:14:39.109985421" };
091:
092: static String[] STRING_GMT_ARRAY = { STRING_TEST1, STRING_TEST2,
093: STRING_TEST3 };
094:
095: static String[] STRING_LA_ARRAY = { "02:45:20", "14:17:59",
096: "05:14:40" };
097:
098: static String[] STRING_JP_ARRAY = { "19:45:20", "07:17:59",
099: "22:14:40" };
100:
101: static String[] INVALID_STRINGS = { STRING_INVALID1,
102: STRING_INVALID2, STRING_INVALID3 };
103:
104: // Timezones
105: static String TZ_LONDON = "GMT"; // GMT (!) PS London != GMT (?!?)
106:
107: static String TZ_PACIFIC = "America/Los_Angeles"; // GMT - 8
108:
109: static String TZ_JAPAN = "Asia/Tokyo"; // GMT + 9
110:
111: static String[] TIMEZONES = { TZ_LONDON, TZ_PACIFIC, TZ_JAPAN };
112:
113: static String[][] STRING_ARRAYS = { STRING_GMT_ARRAY,
114: STRING_LA_ARRAY, STRING_JP_ARRAY };
115:
116: /*
117: * Constructor test
118: */
119: public void testTimestamplong() {
120: Timestamp theTimestamp = new Timestamp(TIME_TEST1);
121:
122: // The Timestamp should have been created
123: assertNotNull(theTimestamp);
124: } // end method testTimestamplong
125:
126: /*
127: * Constructor test
128: */
129: @SuppressWarnings("deprecation")
130: public void testTimestampintintintintintintint() {
131: int[][] valid = { { 99, 2, 14, 17, 52, 3, 213577212 }, // 0 valid
132: { 0, 0, 1, 0, 0, 0, 0 }, // 1 valid
133: { 106, 11, 31, 23, 59, 59, 999999999 }, // 2 valid
134: { 106, 11, 31, 23, 59, 61, 999999999 }, // 5 Seconds out of
135: // range
136: { 106, 11, 31, 23, 59, -1, 999999999 }, // 6 Seconds out of
137: // range
138: { 106, 11, 31, 23, 61, 59, 999999999 }, // 7 Minutes out of
139: // range
140: { 106, 11, 31, 23, -1, 59, 999999999 }, // 8 Minutes out of
141: // range
142: { 106, 11, 31, 25, 59, 59, 999999999 }, // 9 Hours out of range
143: { 106, 11, 31, -1, 59, 59, 999999999 }, // 10 Hours out of range
144: { 106, 11, 35, 23, 59, 59, 999999999 }, // 11 Days out of range
145: { 106, 11, -1, 23, 59, 59, 999999999 }, // 12 Days out of range
146: { 106, 15, 31, 23, 59, 59, 999999999 }, // 13 Months out of
147: // range
148: { 106, -1, 31, 23, 59, 59, 999999999 }, // 14 Months out of
149: // range
150: { -10, 11, 31, 23, 59, 59, 999999999 }, // 15 valid - Years
151: // negative
152: };
153:
154: for (int[] element : valid) {
155: Timestamp theTimestamp = new Timestamp(element[0],
156: element[1], element[2], element[3], element[4],
157: element[5], element[6]);
158: assertNotNull("Timestamp not generated: ", theTimestamp);
159: } // end for
160:
161: int[][] invalid = { { 106, 11, 31, 23, 59, 59, 1999999999 },
162: { 106, 11, 31, 23, 59, 59, -999999999 }, };
163: for (int[] element : invalid) {
164: try {
165: new Timestamp(element[0], element[1], element[2],
166: element[3], element[4], element[5], element[6]);
167: fail("Should throw IllegalArgumentException");
168: } catch (IllegalArgumentException e) {
169: // expected
170: }
171: }
172:
173: } // end method testTimestampintintintintintintint
174:
175: /*
176: * Method test for setTime
177: */
178: public void testSetTimelong() {
179: // First set the timezone to GMT
180: TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
181:
182: Timestamp theTimestamp = new Timestamp(TIME_TEST1);
183:
184: for (int i = 0; i < TIME_ARRAY.length; i++) {
185: theTimestamp.setTime(TIME_ARRAY[i]);
186:
187: assertEquals(TIME_ARRAY[i], theTimestamp.getTime());
188: assertEquals(NANOS_ARRAY[i], theTimestamp.getNanos());
189: } // end for
190:
191: } // end method testsetTimelong
192:
193: /*
194: * Method test for getTime
195: */
196: public void testGetTime() {
197: // First set the timezone to GMT
198: TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
199:
200: for (long element : TIME_ARRAY) {
201: Timestamp theTimestamp = new Timestamp(element);
202: assertEquals(element, theTimestamp.getTime());
203: } // end for
204:
205: } // end method testgetTime
206:
207: /*
208: * Method test for getYear
209: */
210: @SuppressWarnings("deprecation")
211: public void testGetYear() {
212: for (int i = 0; i < TIME_ARRAY.length; i++) {
213: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
214: assertEquals(YEAR_ARRAY[i], theTimestamp.getYear());
215: } // end for
216:
217: } // end method testgetYear
218:
219: /*
220: * Method test for getMonth
221: */
222: @SuppressWarnings("deprecation")
223: public void testGetMonth() {
224: for (int i = 0; i < TIME_ARRAY.length; i++) {
225: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
226: assertEquals(MONTH_ARRAY[i], theTimestamp.getMonth());
227: } // end for
228:
229: } // end method testgetMonth
230:
231: /*
232: * Method test for getDate
233: */
234: @SuppressWarnings("deprecation")
235: public void testGetDate() {
236: for (int i = 0; i < TIME_ARRAY.length; i++) {
237: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
238: assertEquals(DATE_ARRAY[i], theTimestamp.getDate());
239: } // end for
240:
241: } // end method testgetDate
242:
243: /*
244: * Method test for getHours
245: */
246: @SuppressWarnings("deprecation")
247: public void testGetHours() {
248: for (int i = 0; i < TIME_ARRAY.length; i++) {
249: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
250: assertEquals(HOURS_ARRAY[i], theTimestamp.getHours());
251: } // end for
252:
253: } // end method testgetHours
254:
255: /*
256: * Method test for getMinutes
257: */
258: @SuppressWarnings("deprecation")
259: public void testGetMinutes() {
260: for (int i = 0; i < TIME_ARRAY.length; i++) {
261: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
262: assertEquals(MINUTES_ARRAY[i], theTimestamp.getMinutes());
263: } // end for
264:
265: } // end method testgetMinutes
266:
267: /*
268: * Method test for getSeconds
269: */
270: @SuppressWarnings("deprecation")
271: public void testGetSeconds() {
272: for (int i = 0; i < TIME_ARRAY.length; i++) {
273: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
274: assertEquals(SECONDS_ARRAY[i], theTimestamp.getSeconds());
275: } // end for
276:
277: } // end method testgetSeconds
278:
279: /*
280: * Method test for valueOf
281: */
282: static String theExceptionMessage = "Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff";
283:
284: public void testValueOfString() {
285: for (int i = 0; i < TIME_ARRAY.length; i++) {
286: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
287: Timestamp theTimestamp2 = Timestamp
288: .valueOf(STRING_GMT_ARRAY[i]);
289: assertEquals(theTimestamp, theTimestamp2);
290: } // end for
291:
292: // Test for a string in correct format but with number values out of
293: // range
294: Timestamp theTimestamp = Timestamp.valueOf(STRING_OUTRANGE);
295: assertNotNull(theTimestamp);
296: /*
297: * System.out.println("testValueOfString: outrange timestamp: " +
298: * theTimestamp.toString() );
299: */
300:
301: for (String element : INVALID_STRINGS) {
302: try {
303: Timestamp.valueOf(element);
304: fail("Should throw IllegalArgumentException.");
305: } catch (IllegalArgumentException e) {
306: // expected
307: } // end try
308:
309: } // end for
310:
311: } // end method testvalueOfString
312:
313: /*
314: * Method test for valueOf
315: */
316: public void testValueOfString1() {
317:
318: Timestamp theReturn;
319: long[] theReturnTime = { 38720231, 38720231, 80279000,
320: -38720691, 38720000 };
321: int[] theReturnNanos = { 231000000, 231987654, 0, 309000000, 0, };
322:
323: String[] valid = { "1970-01-01 10:45:20.231",
324: "1970-01-01 10:45:20.231987654",
325: "1970-01-01 22:17:59.0", "1969-12-31 13:14:39.309",
326: "1970-01-01 10:45:20", };
327: String[] invalid = { null, "ABCDEFGHI", "233104",
328: "1970-01-01 22:17:59.",
329: "1970-01-01 10:45:20.231987654690645322",
330: "1970-01-01 10:45:20&231987654",
331: "1970-01-01 10:45:20.-31987654",
332: "1970-01-01 10:45:20.ABCD87654", "21-43-48", };
333:
334: for (int i = 0; i < valid.length; i++) {
335: theReturn = Timestamp.valueOf(valid[i]);
336: assertEquals(theReturnTime[i], theReturn.getTime());
337: assertEquals(theReturnNanos[i], theReturn.getNanos());
338: } // end for
339:
340: for (String element : invalid) {
341: try {
342: theReturn = Timestamp.valueOf(element);
343: fail("Should throw IllegalArgumentException.");
344: } catch (IllegalArgumentException e) {
345: // expected
346: }
347: }
348:
349: // Regression test for HARMONY-5506
350: String date = "1970-01-01 22:17:59.0 ";
351: Timestamp t = Timestamp.valueOf(date);
352: assertEquals(80279000, t.getTime());
353:
354: } // end method testValueOfString
355:
356: /*
357: * Method test for toString
358: */
359: public void testToString() {
360: for (int i = 0; i < TIME_ARRAY.length; i++) {
361: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
362: assertEquals(STRING_GMT_ARRAY[i], theTimestamp.toString());
363: } // end for
364:
365: } // end method testtoString
366:
367: /*
368: * Method test for getNanos
369: */
370: public void testGetNanos() {
371: for (int i = 0; i < TIME_ARRAY.length; i++) {
372: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
373: assertEquals(NANOS_ARRAY[i], theTimestamp.getNanos());
374: } // end for
375:
376: } // end method testgetNanos
377:
378: /*
379: * Method test for setNanos
380: */
381: public void testSetNanosint() {
382: int[] NANOS_INVALID = { -137891990, 1635665198, -1 };
383: for (int i = 0; i < TIME_ARRAY.length; i++) {
384: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
385:
386: theTimestamp.setNanos(NANOS_ARRAY2[i]);
387:
388: assertEquals(NANOS_ARRAY2[i], theTimestamp.getNanos());
389: // Also check that these Timestamps with detailed nanos values
390: // convert to
391: // strings correctly
392: assertEquals(STRING_NANOS_ARRAY[i], theTimestamp.toString());
393: } // end for
394:
395: for (int i = 0; i < NANOS_INVALID.length; i++) {
396: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
397: int originalNanos = theTimestamp.getNanos();
398: try {
399: theTimestamp.setNanos(NANOS_INVALID[i]);
400: fail("Should throw IllegalArgumentException");
401: } catch (IllegalArgumentException e) {
402: // expected
403: } // end try
404:
405: assertEquals(originalNanos, theTimestamp.getNanos());
406: } // end for
407:
408: } // end method testsetNanosint
409:
410: /*
411: * Method test for equals
412: */
413: public void testEqualsTimestamp() {
414: for (long element : TIME_ARRAY) {
415: Timestamp theTimestamp = new Timestamp(element);
416: Timestamp theTimestamp2 = new Timestamp(element);
417:
418: assertTrue(theTimestamp.equals(theTimestamp2));
419: } // end for
420:
421: Timestamp theTest = new Timestamp(TIME_COMPARE);
422:
423: for (long element : TIME_ARRAY) {
424: Timestamp theTimestamp = new Timestamp(element);
425: assertFalse(theTimestamp.equals(theTest));
426: } // end for
427:
428: // Regression for HARMONY-526
429: assertFalse(new Timestamp(0).equals((Timestamp) null));
430: } // end method testequalsTimestamp
431:
432: /*
433: * Method test for equals
434: */
435: public void testEqualsObject() {
436: for (long element : TIME_ARRAY) {
437: Timestamp theTimestamp = new Timestamp(element);
438:
439: Object theTimestamp2 = new Timestamp(element);
440:
441: assertTrue(theTimestamp.equals(theTimestamp2));
442: } // end for
443:
444: Object theTest = new Timestamp(TIME_COMPARE);
445:
446: for (long element : TIME_ARRAY) {
447: Timestamp theTimestamp = new Timestamp(element);
448:
449: assertFalse(theTimestamp.equals(theTest));
450: } // end for
451:
452: Object nastyTest = new String("Test ");
453: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[1]);
454: assertFalse(theTimestamp.equals(nastyTest));
455:
456: // Regression for HARMONY-526
457: assertFalse(new Timestamp(0).equals((Object) null));
458: } // end method testequalsObject
459:
460: /*
461: * Method test for before
462: */
463: public void testBeforeTimestamp() {
464: Timestamp theTest = new Timestamp(TIME_LATE);
465:
466: for (long element : TIME_ARRAY) {
467: Timestamp theTimestamp = new Timestamp(element);
468:
469: assertTrue(theTimestamp.before(theTest));
470: } // end for
471:
472: theTest = new Timestamp(TIME_EARLY);
473:
474: for (long element : TIME_ARRAY) {
475: Timestamp theTimestamp = new Timestamp(element);
476:
477: assertFalse(theTimestamp.before(theTest));
478: } // end for
479:
480: for (long element : TIME_ARRAY) {
481: theTest = new Timestamp(element);
482: Timestamp theTimestamp = new Timestamp(element);
483:
484: assertFalse(theTimestamp.before(theTest));
485: theTest.setNanos(theTest.getNanos() + 1);
486: assertTrue(theTimestamp.before(theTest));
487: } // end for
488:
489: } // end method testbeforeTimestamp
490:
491: /*
492: * Method test for after
493: */
494: public void testAfterTimestamp() {
495: Timestamp theTest = new Timestamp(TIME_LATE);
496:
497: for (long element : TIME_ARRAY) {
498: Timestamp theTimestamp = new Timestamp(element);
499:
500: assertFalse(theTimestamp.after(theTest));
501: } // end for
502:
503: theTest = new Timestamp(TIME_EARLY);
504:
505: for (long element : TIME_ARRAY) {
506: Timestamp theTimestamp = new Timestamp(element);
507:
508: assertTrue(theTimestamp.after(theTest));
509: } // end for
510:
511: for (long element : TIME_ARRAY) {
512: theTest = new Timestamp(element);
513: Timestamp theTimestamp = new Timestamp(element);
514:
515: assertFalse(theTimestamp.after(theTest));
516: theTimestamp.setNanos(theTimestamp.getNanos() + 1);
517: assertTrue(theTimestamp.after(theTest));
518: } // end for
519:
520: } // end method testafterTimestamp
521:
522: /*
523: * Method test for compareTo
524: */
525: @SuppressWarnings("deprecation")
526: public void testCompareToTimestamp() {
527: Timestamp theTest = new Timestamp(TIME_EARLY);
528: Timestamp theTest2 = new Timestamp(TIME_LATE);
529:
530: for (long element : TIME_ARRAY) {
531: Timestamp theTimestamp = new Timestamp(element);
532: Timestamp theTimestamp2 = new Timestamp(element);
533:
534: assertTrue(theTimestamp.compareTo(theTest) > 0);
535: assertTrue(theTimestamp.compareTo(theTest2) < 0);
536: assertEquals(0, theTimestamp.compareTo(theTimestamp2));
537: } // end for
538:
539: Timestamp t1 = new Timestamp(-1L);
540: Timestamp t2 = new Timestamp(-1L);
541:
542: t1.setTime(Long.MIN_VALUE);
543: t2.setDate(Integer.MIN_VALUE);
544: assertEquals(1, t1.compareTo(t2));
545: assertEquals(-1, t2.compareTo(t1));
546:
547: t1.setTime(Long.MAX_VALUE);
548: t2.setTime(Long.MAX_VALUE - 1);
549: assertEquals(1, t1.compareTo(t2));
550: assertEquals(-1, t2.compareTo(t1));
551:
552: t1.setTime(Integer.MAX_VALUE);
553: t2.setTime(Integer.MAX_VALUE);
554: assertEquals(0, t1.compareTo(t2));
555: assertEquals(0, t2.compareTo(t1));
556:
557: } // end method testcompareToTimestamp
558:
559: /**
560: * @tests java.sql.Timestamp#compareTo(java.util.Date)
561: */
562: public void testCompareToDate() {
563: Date theTest = new Timestamp(TIME_EARLY);
564: Date theTest2 = new Timestamp(TIME_LATE);
565:
566: for (long element : TIME_ARRAY) {
567: Timestamp theTimestamp = new Timestamp(element);
568: Date theTimestamp2 = new Timestamp(element);
569:
570: assertTrue(theTimestamp.compareTo(theTest) > 0);
571: assertTrue(theTimestamp.compareTo(theTest2) < 0);
572: assertEquals(0, theTimestamp.compareTo(theTimestamp2));
573: } // end for
574:
575: Date nastyTest = new Date();
576: Timestamp theTimestamp = new Timestamp(TIME_ARRAY[1]);
577: try {
578: theTimestamp.compareTo(nastyTest);
579: // It throws ClassCastException in JDK 1.5.0_06 but in 1.5.0_07 it
580: // does not throw the expected exception.
581: fail("testCompareToObject: Did not get expected ClassCastException");
582: } catch (ClassCastException e) {
583: // Should get here
584: /*
585: * System.out.println("testCompareToObject: ClassCastException as
586: * expected"); System.out.println("Exception message: " +
587: * e.getMessage());
588: */
589: } // end try
590:
591: } // end method testcompareToObject
592:
593: /**
594: * @tests serialization/deserialization compatibility.
595: */
596: public void testSerializationSelf() throws Exception {
597: Timestamp object = new Timestamp(100L);
598: SerializationTest.verifySelf(object);
599: }
600:
601: /**
602: * @tests serialization/deserialization compatibility with RI.
603: */
604: public void testSerializationCompatibility() throws Exception {
605: Timestamp object = new Timestamp(100L);
606: SerializationTest.verifyGolden(this , object);
607: }
608:
609: /**
610: * @tests java.sql.Timestamp#toString()
611: */
612: public void test_toString() {
613:
614: Timestamp t1 = new Timestamp(Long.MIN_VALUE);
615: assertEquals("292278994-08-17 07:12:55.192", t1.toString()); //$NON-NLS-1$
616:
617: Timestamp t2 = new Timestamp(Long.MIN_VALUE + 1);
618: assertEquals("292278994-08-17 07:12:55.193", t2.toString()); //$NON-NLS-1$
619:
620: Timestamp t3 = new Timestamp(Long.MIN_VALUE + 807);
621: assertEquals("292278994-08-17 07:12:55.999", t3.toString()); //$NON-NLS-1$
622:
623: Timestamp t4 = new Timestamp(Long.MIN_VALUE + 808);
624: assertEquals("292269055-12-02 16:47:05.0", t4.toString()); //$NON-NLS-1$
625: }
626:
627: } // end class TimestampTest
|