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
18package org.apache.harmony.sql.tests.java.sql;
19
20import java.sql.Timestamp;
21import java.util.Date;
22import java.util.TimeZone;
23
24import junit.framework.TestCase;
25
26import org.apache.harmony.testframework.serialization.SerializationTest;
27
28/**
29 * JUnit Testcase for the java.sql.Timestamp class
30 */
31
32public class TimestampTest extends TestCase {
33
34    static class MockTimestamp extends Timestamp {
35        private String holiday;
36
37        public MockTimestamp(long theTime) {
38            super(theTime);
39            holiday = "Christmas";
40        }
41
42        // Constructor should not call this public API,
43        // since it may be overrided to use variables uninitialized.
44        public void setTime(long theTime) {
45            super.setTime(theTime);
46            holiday.hashCode();
47        }
48    }
49
50    static long TIME_TEST1 = 38720231; // 10:45:20.231 GMT
51
52    static long TIME_TEST2 = 80279000; // 22:17:59.000 GMT
53
54    static long TIME_TEST3 = -38720691; // 13:14:39.309 GMT
55
56    static long TIME_COMPARE = 123498845;
57
58    static long TIME_EARLY = -2347889122L;// A time well before the Epoch
59
60    static long TIME_LATE = 2347889122L; // A time well after the Epoch
61
62    static String STRING_TEST1 = "1970-01-01 10:45:20.231"; // "1970-01-01
63
64    // 10:45:20.231000000";
65
66    static String STRING_TEST2 = "1970-01-01 22:17:59.0"; // "1970-01-01
67
68    // 22:17:59.000000000";
69
70    static String STRING_TEST3 = "1969-12-31 13:14:39.309"; // "1969-12-31
71
72    // 13:14:39.309000000";
73
74    static String STRING_INVALID1 = "ABCDEFGHI";
75
76    static String STRING_INVALID2 = "233104";
77
78    static String STRING_INVALID3 = "21-43-48";
79
80    // A timepoint in the correct format but with numeric values out of range
81    // ...this is accepted despite being a crazy date specification
82    // ...it is treated as the correct format date 3000-06-08 12:40:06.875 !!
83    static String STRING_OUTRANGE = "2999-15-99 35:99:66.875";
84
85    static long[] TIME_ARRAY = { TIME_TEST1, TIME_TEST2, TIME_TEST3 };
86
87    static int[] YEAR_ARRAY = { 70, 70, 69 };
88
89    static int[] MONTH_ARRAY = { 0, 0, 11 };
90
91    static int[] DATE_ARRAY = { 1, 1, 31 };
92
93    static int[] HOURS_ARRAY = { 10, 22, 13 };
94
95    static int[] MINUTES_ARRAY = { 45, 17, 14 };
96
97    static int[] SECONDS_ARRAY = { 20, 59, 39 };
98
99    static int[] NANOS_ARRAY = { 231000000, 000000000, 309000000 };
100
101    static int[] NANOS_ARRAY2 = { 137891990, 635665198, 109985421 };
102
103    static String[] STRING_NANOS_ARRAY = { "1970-01-01 10:45:20.13789199",
104            "1970-01-01 22:17:59.635665198", "1969-12-31 13:14:39.109985421" };
105
106    static String[] STRING_GMT_ARRAY = { STRING_TEST1, STRING_TEST2,
107            STRING_TEST3 };
108
109    static String[] STRING_LA_ARRAY = { "02:45:20", "14:17:59", "05:14:40" };
110
111    static String[] STRING_JP_ARRAY = { "19:45:20", "07:17:59", "22:14:40" };
112
113    static String[] INVALID_STRINGS = { STRING_INVALID1, STRING_INVALID2,
114            STRING_INVALID3 };
115
116    // Timezones
117    static String TZ_LONDON = "GMT"; // GMT (!) PS London != GMT (?!?)
118
119    static String TZ_PACIFIC = "America/Los_Angeles"; // GMT - 8
120
121    static String TZ_JAPAN = "Asia/Tokyo"; // GMT + 9
122
123    static String[] TIMEZONES = { TZ_LONDON, TZ_PACIFIC, TZ_JAPAN };
124
125    static String[][] STRING_ARRAYS = { STRING_GMT_ARRAY, STRING_LA_ARRAY,
126            STRING_JP_ARRAY };
127
128    // Test ToString
129    static String[][] STRING_TIMESTAMP_ARRAYS = {
130            STRING_GMT_ARRAY,
131            new String[] { "1970-01-01 02:45:20.231", "1970-01-01 14:17:59.0",
132                    "1969-12-31 05:14:39.309" },
133            new String[] { "1970-01-01 19:45:20.231", "1970-01-02 07:17:59.0", "1969-12-31 22:14:39.309" } };
134
135    /*
136    * Constructor test
137    */
138    public void testTimestamplong() {
139        Timestamp theTimestamp = new Timestamp(TIME_TEST1);
140        // The Timestamp should have been created
141        assertNotNull(theTimestamp);
142
143        Timestamp mockTimestamp = new MockTimestamp(TIME_TEST1);
144        assertNotNull(theTimestamp);
145    } // end method testTimestamplong
146
147    /*
148     * Constructor test
149     */
150    @SuppressWarnings("deprecation")
151    public void testTimestampintintintintintintint() {
152        int[][] valid = { { 99, 2, 14, 17, 52, 3, 213577212 }, // 0 valid
153                { 0, 0, 1, 0, 0, 0, 0 }, // 1 valid
154                { 106, 11, 31, 23, 59, 59, 999999999 }, // 2 valid
155                { 106, 11, 31, 23, 59, 61, 999999999 }, // 5 Seconds out of
156                // range
157                { 106, 11, 31, 23, 59, -1, 999999999 }, // 6 Seconds out of
158                // range
159                { 106, 11, 31, 23, 61, 59, 999999999 }, // 7 Minutes out of
160                // range
161                { 106, 11, 31, 23, -1, 59, 999999999 }, // 8 Minutes out of
162                // range
163                { 106, 11, 31, 25, 59, 59, 999999999 }, // 9 Hours out of range
164                { 106, 11, 31, -1, 59, 59, 999999999 }, // 10 Hours out of range
165                { 106, 11, 35, 23, 59, 59, 999999999 }, // 11 Days out of range
166                { 106, 11, -1, 23, 59, 59, 999999999 }, // 12 Days out of range
167                { 106, 15, 31, 23, 59, 59, 999999999 }, // 13 Months out of
168                // range
169                { 106, -1, 31, 23, 59, 59, 999999999 }, // 14 Months out of
170                // range
171                { -10, 11, 31, 23, 59, 59, 999999999 }, // 15 valid - Years
172                // negative
173        };
174
175        for (int[] element : valid) {
176            Timestamp theTimestamp = new Timestamp(element[0], element[1],
177                    element[2], element[3], element[4], element[5], element[6]);
178            assertNotNull("Timestamp not generated: ", theTimestamp);
179        } // end for
180
181        int[][] invalid = { { 106, 11, 31, 23, 59, 59, 1999999999 },
182                { 106, 11, 31, 23, 59, 59, -999999999 }, };
183        for (int[] element : invalid) {
184            try {
185                new Timestamp(element[0], element[1], element[2], element[3],
186                        element[4], element[5], element[6]);
187                fail("Should throw IllegalArgumentException");
188            } catch (IllegalArgumentException e) {
189                // expected
190            }
191        }
192
193    } // end method testTimestampintintintintintintint
194
195    /*
196     * Method test for setTime
197     */
198    public void testSetTimelong() {
199        // First set the timezone to GMT
200        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
201
202        Timestamp theTimestamp = new Timestamp(TIME_TEST1);
203
204        for (int i = 0; i < TIME_ARRAY.length; i++) {
205            theTimestamp.setTime(TIME_ARRAY[i]);
206
207            assertEquals(TIME_ARRAY[i], theTimestamp.getTime());
208            assertEquals(NANOS_ARRAY[i], theTimestamp.getNanos());
209        } // end for
210
211    } // end method testsetTimelong
212
213    /*
214     * Method test for getTime
215     */
216    public void testGetTime() {
217        // First set the timezone to GMT
218        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
219
220        for (long element : TIME_ARRAY) {
221            Timestamp theTimestamp = new Timestamp(element);
222            assertEquals(element, theTimestamp.getTime());
223        } // end for
224
225    } // end method testgetTime
226
227    /*
228     * Method test for getYear
229     */
230    @SuppressWarnings("deprecation")
231    public void testGetYear() {
232        for (int i = 0; i < TIME_ARRAY.length; i++) {
233            Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
234            assertEquals(YEAR_ARRAY[i], theTimestamp.getYear());
235        } // end for
236
237    } // end method testgetYear
238
239    /*
240     * Method test for getMonth
241     */
242    @SuppressWarnings("deprecation")
243    public void testGetMonth() {
244        for (int i = 0; i < TIME_ARRAY.length; i++) {
245            Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
246            assertEquals(MONTH_ARRAY[i], theTimestamp.getMonth());
247        } // end for
248
249    } // end method testgetMonth
250
251    /*
252     * Method test for getDate
253     */
254    @SuppressWarnings("deprecation")
255    public void testGetDate() {
256        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
257        for (int i = 0; i < TIME_ARRAY.length; i++) {
258            Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
259            assertEquals(DATE_ARRAY[i], theTimestamp.getDate());
260        } // end for
261
262    } // end method testgetDate
263
264    /*
265     * Method test for getHours
266     */
267    @SuppressWarnings("deprecation")
268    public void testGetHours() {
269        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
270        for (int i = 0; i < TIME_ARRAY.length; i++) {
271            Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
272            assertEquals(HOURS_ARRAY[i], theTimestamp.getHours());
273        } // end for
274
275    } // end method testgetHours
276
277    /*
278     * Method test for getMinutes
279     */
280    @SuppressWarnings("deprecation")
281    public void testGetMinutes() {
282        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
283        for (int i = 0; i < TIME_ARRAY.length; i++) {
284            Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
285            assertEquals(MINUTES_ARRAY[i], theTimestamp.getMinutes());
286        } // end for
287
288    } // end method testgetMinutes
289
290    /*
291     * Method test for getSeconds
292     */
293    @SuppressWarnings("deprecation")
294    public void testGetSeconds() {
295        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
296        for (int i = 0; i < TIME_ARRAY.length; i++) {
297            Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
298            assertEquals(SECONDS_ARRAY[i], theTimestamp.getSeconds());
299        } // end for
300
301    } // end method testgetSeconds
302
303    /*
304     * Method test for valueOf
305     */
306    static String theExceptionMessage = "Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff";
307
308    public void testValueOfString() {
309        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
310        for (int i = 0; i < TIME_ARRAY.length; i++) {
311            Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
312            Timestamp theTimestamp2 = Timestamp.valueOf(STRING_GMT_ARRAY[i]);
313            assertEquals(theTimestamp, theTimestamp2);
314        } // end for
315
316        // Test for a string in correct format but with number values out of
317        // range
318        Timestamp theTimestamp = Timestamp.valueOf(STRING_OUTRANGE);
319        assertNotNull(theTimestamp);
320        /*
321         * System.out.println("testValueOfString: outrange timestamp: " +
322         * theTimestamp.toString() );
323         */
324
325        for (String element : INVALID_STRINGS) {
326            try {
327                Timestamp.valueOf(element);
328                fail("Should throw IllegalArgumentException.");
329            } catch (IllegalArgumentException e) {
330                // expected
331            } // end try
332
333        } // end for
334
335    } // end method testvalueOfString
336
337    /*
338     * Method test for valueOf
339     */
340    public void testValueOfString1() {
341        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
342
343        Timestamp theReturn;
344        long[] theReturnTime = { 38720231, 38720231, 80279000, -38720691,
345                38720000 };
346        int[] theReturnNanos = { 231000000, 231987654, 0, 309000000, 0, };
347
348        String[] valid = { "1970-01-01 10:45:20.231",
349                "1970-01-01 10:45:20.231987654", "1970-01-01 22:17:59.0",
350                "1969-12-31 13:14:39.309", "1970-01-01 10:45:20", };
351        String[] invalid = { null, "ABCDEFGHI", "233104",
352                "1970-01-01 22:17:59.",
353                "1970-01-01 10:45:20.231987654690645322",
354                "1970-01-01 10:45:20&231987654",
355                "1970-01-01 10:45:20.-31987654",
356                "1970-01-01 10:45:20.ABCD87654", "21-43-48", };
357
358        for (int i = 0; i < valid.length; i++) {
359            theReturn = Timestamp.valueOf(valid[i]);
360            assertEquals(theReturnTime[i], theReturn.getTime());
361            assertEquals(theReturnNanos[i], theReturn.getNanos());
362        } // end for
363
364        for (String element : invalid) {
365            try {
366                theReturn = Timestamp.valueOf(element);
367                fail("Should throw IllegalArgumentException.");
368            } catch (IllegalArgumentException e) {
369                // expected
370            }
371        }
372
373        // Regression test for HARMONY-5506
374        String date = "1970-01-01 22:17:59.0                 ";
375        Timestamp t = Timestamp.valueOf(date);
376        assertEquals(80279000, t.getTime());
377
378    } // end method testValueOfString
379
380    public void testValueOf_IAE() {
381        try {
382            java.sql.Timestamp.valueOf("2008-12-22 15:00:01.");
383            fail("should throw IllegalArgumentException");
384        } catch (IllegalArgumentException e) {
385            // Expected
386        }
387
388        try {
389            // bug of RI 5, passed on RI 6
390            java.sql.Timestamp.valueOf("178548938-12-22 15:00:01.000000001");
391            fail("should throw IllegalArgumentException");
392        } catch (IllegalArgumentException e) {
393            // Expected
394        }
395
396        try {
397            java.sql.Timestamp.valueOf("2008-12-22 15:00:01.0000000011");
398            fail("should throw IllegalArgumentException");
399        } catch (IllegalArgumentException e) {
400            // Expected
401        }
402    }
403
404    /*
405     * Method test for toString
406     */
407    public void testToString() {
408        for (int i = 0; i < TIME_ARRAY.length; i++) {
409            testToString(TIMEZONES[i], TIME_ARRAY, STRING_TIMESTAMP_ARRAYS[i]);
410        } // end for
411
412        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
413
414        Timestamp t1 = new Timestamp(Long.MIN_VALUE);
415        assertEquals("292278994-08-17 07:12:55.192", t1.toString()); //$NON-NLS-1$
416
417        Timestamp t2 = new Timestamp(Long.MIN_VALUE + 1);
418        assertEquals("292278994-08-17 07:12:55.193", t2.toString()); //$NON-NLS-1$
419
420        Timestamp t3 = new Timestamp(Long.MIN_VALUE + 807);
421        assertEquals("292278994-08-17 07:12:55.999", t3.toString()); //$NON-NLS-1$
422
423        Timestamp t4 = new Timestamp(Long.MIN_VALUE + 808);
424        assertEquals("292269055-12-02 16:47:05.0", t4.toString()); //$NON-NLS-1$
425    } // end method testtoString
426
427    private void testToString(String timeZone, long[] theTimeStamps, String[] theTimeStampStrings) {
428        TimeZone.setDefault(TimeZone.getTimeZone(timeZone));
429        for (int i = 0; i < TIME_ARRAY.length; i++) {
430            Timestamp theTimestamp = new Timestamp(theTimeStamps[i]);
431            assertEquals(theTimeStampStrings[i], theTimestamp.toString());
432        } // end for
433
434    }
435
436    /*
437    * Method test for getNanos
438    */
439    public void testGetNanos() {
440        for (int i = 0; i < TIME_ARRAY.length; i++) {
441            Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
442            assertEquals(NANOS_ARRAY[i], theTimestamp.getNanos());
443        } // end for
444
445    } // end method testgetNanos
446
447    /*
448     * Method test for setNanos
449     */
450    public void testSetNanosint() {
451        TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
452
453        int[] NANOS_INVALID = { -137891990, 1635665198, -1 };
454        for (int i = 0; i < TIME_ARRAY.length; i++) {
455            Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
456
457            theTimestamp.setNanos(NANOS_ARRAY2[i]);
458
459            assertEquals(NANOS_ARRAY2[i], theTimestamp.getNanos());
460            // Also check that these Timestamps with detailed nanos values
461            // convert to
462            // strings correctly
463            assertEquals(STRING_NANOS_ARRAY[i], theTimestamp.toString());
464        } // end for
465
466        for (int i = 0; i < NANOS_INVALID.length; i++) {
467            Timestamp theTimestamp = new Timestamp(TIME_ARRAY[i]);
468            int originalNanos = theTimestamp.getNanos();
469            try {
470                theTimestamp.setNanos(NANOS_INVALID[i]);
471                fail("Should throw IllegalArgumentException");
472            } catch (IllegalArgumentException e) {
473                // expected
474            } // end try
475
476            assertEquals(originalNanos, theTimestamp.getNanos());
477        } // end for
478
479    } // end method testsetNanosint
480
481    /*
482     * Method test for equals
483     */
484    public void testEqualsTimestamp() {
485        for (long element : TIME_ARRAY) {
486            Timestamp theTimestamp = new Timestamp(element);
487            Timestamp theTimestamp2 = new Timestamp(element);
488
489            assertTrue(theTimestamp.equals(theTimestamp2));
490        } // end for
491
492        Timestamp theTest = new Timestamp(TIME_COMPARE);
493
494        for (long element : TIME_ARRAY) {
495            Timestamp theTimestamp = new Timestamp(element);
496            assertFalse(theTimestamp.equals(theTest));
497        } // end for
498
499        // Regression for HARMONY-526
500        assertFalse(new Timestamp(0).equals((Timestamp) null));
501    } // end method testequalsTimestamp
502
503    /*
504     * Method test for equals
505     */
506    public void testEqualsObject() {
507        for (long element : TIME_ARRAY) {
508            Timestamp theTimestamp = new Timestamp(element);
509
510            Object theTimestamp2 = new Timestamp(element);
511
512            assertTrue(theTimestamp.equals(theTimestamp2));
513        } // end for
514
515        Object theTest = new Timestamp(TIME_COMPARE);
516
517        for (long element : TIME_ARRAY) {
518            Timestamp theTimestamp = new Timestamp(element);
519
520            assertFalse(theTimestamp.equals(theTest));
521        } // end for
522
523        Object nastyTest = new String("Test ");
524        Timestamp theTimestamp = new Timestamp(TIME_ARRAY[1]);
525        assertFalse(theTimestamp.equals(nastyTest));
526
527        // Regression for HARMONY-526
528        assertFalse(new Timestamp(0).equals((Object) null));
529    } // end method testequalsObject
530
531    /*
532     * Method test for before
533     */
534    public void testBeforeTimestamp() {
535        Timestamp theTest = new Timestamp(TIME_LATE);
536
537        for (long element : TIME_ARRAY) {
538            Timestamp theTimestamp = new Timestamp(element);
539
540            assertTrue(theTimestamp.before(theTest));
541        } // end for
542
543        theTest = new Timestamp(TIME_EARLY);
544
545        for (long element : TIME_ARRAY) {
546            Timestamp theTimestamp = new Timestamp(element);
547
548            assertFalse(theTimestamp.before(theTest));
549        } // end for
550
551        for (long element : TIME_ARRAY) {
552            theTest = new Timestamp(element);
553            Timestamp theTimestamp = new Timestamp(element);
554
555            assertFalse(theTimestamp.before(theTest));
556            theTest.setNanos(theTest.getNanos() + 1);
557            assertTrue(theTimestamp.before(theTest));
558        } // end for
559
560    } // end method testbeforeTimestamp
561
562    /*
563     * Method test for after
564     */
565    public void testAfterTimestamp() {
566        Timestamp theTest = new Timestamp(TIME_LATE);
567
568        for (long element : TIME_ARRAY) {
569            Timestamp theTimestamp = new Timestamp(element);
570
571            assertFalse(theTimestamp.after(theTest));
572        } // end for
573
574        theTest = new Timestamp(TIME_EARLY);
575
576        for (long element : TIME_ARRAY) {
577            Timestamp theTimestamp = new Timestamp(element);
578
579            assertTrue(theTimestamp.after(theTest));
580        } // end for
581
582        for (long element : TIME_ARRAY) {
583            theTest = new Timestamp(element);
584            Timestamp theTimestamp = new Timestamp(element);
585
586            assertFalse(theTimestamp.after(theTest));
587            theTimestamp.setNanos(theTimestamp.getNanos() + 1);
588            assertTrue(theTimestamp.after(theTest));
589        } // end for
590
591    } // end method testafterTimestamp
592
593    /*
594     * Method test for compareTo
595     */
596    @SuppressWarnings("deprecation")
597    public void testCompareToTimestamp() {
598        Timestamp theTest = new Timestamp(TIME_EARLY);
599        Timestamp theTest2 = new Timestamp(TIME_LATE);
600
601        for (long element : TIME_ARRAY) {
602            Timestamp theTimestamp = new Timestamp(element);
603            Timestamp theTimestamp2 = new Timestamp(element);
604
605            assertTrue(theTimestamp.compareTo(theTest) > 0);
606            assertTrue(theTimestamp.compareTo(theTest2) < 0);
607            assertEquals(0, theTimestamp.compareTo(theTimestamp2));
608        } // end for
609
610        Timestamp t1 = new Timestamp(-1L);
611        Timestamp t2 = new Timestamp(-1L);
612
613        t1.setTime(Long.MIN_VALUE);
614        t2.setDate(Integer.MIN_VALUE);
615        assertEquals(1, t1.compareTo(t2));
616        assertEquals(-1, t2.compareTo(t1));
617
618        t1.setTime(Long.MAX_VALUE);
619        t2.setTime(Long.MAX_VALUE - 1);
620        assertEquals(1, t1.compareTo(t2));
621        assertEquals(-1, t2.compareTo(t1));
622
623        t1.setTime(Integer.MAX_VALUE);
624        t2.setTime(Integer.MAX_VALUE);
625        assertEquals(0, t1.compareTo(t2));
626        assertEquals(0, t2.compareTo(t1));
627
628    } // end method testcompareToTimestamp
629
630    /**
631     * @tests java.sql.Timestamp#compareTo(java.util.Date)
632     */
633    public void testCompareToDate() {
634        Date theTest = new Timestamp(TIME_EARLY);
635        Date theTest2 = new Timestamp(TIME_LATE);
636
637        for (long element : TIME_ARRAY) {
638            Timestamp theTimestamp = new Timestamp(element);
639            Date theTimestamp2 = new Timestamp(element);
640
641            assertTrue(theTimestamp.compareTo(theTest) > 0);
642            assertTrue(theTimestamp.compareTo(theTest2) < 0);
643            assertEquals(0, theTimestamp.compareTo(theTimestamp2));
644        } // end for
645
646        Timestamp timestamp = new Timestamp(1000000);
647        Date date = new Date(1000000);
648
649        assertEquals(0, timestamp.compareTo(date));
650        timestamp.setNanos(10);
651        assertEquals(1, timestamp.compareTo(date));
652
653        long time = System.currentTimeMillis();
654        Date date2 = new Date(time);
655        Timestamp timestamp2 = new Timestamp(date2.getTime());
656        assertEquals(0, timestamp2.compareTo(date2));
657    } // end method testcompareToObject
658
659    /**
660     * @tests serialization/deserialization compatibility.
661     */
662    public void testSerializationSelf() throws Exception {
663        Timestamp object = new Timestamp(100L);
664        SerializationTest.verifySelf(object);
665    }
666
667    /**
668     * @tests serialization/deserialization compatibility with RI.
669     */
670    public void testSerializationCompatibility() throws Exception {
671        Timestamp object = new Timestamp(100L);
672        SerializationTest.verifyGolden(this, object);
673    }
674
675    // Reset defualt timezone
676    TimeZone defaultTimeZone = TimeZone.getDefault();
677
678    protected void tearDown() {
679        TimeZone.setDefault(defaultTimeZone);
680    }
681
682    /**
683     * @tests overriding Timestamp to create an immutable class.
684     */
685    public void testOverridingTimestamp() {
686        Timestamp ts = new Timestamp(8392418) {
687            @Override
688            public void setNanos(int n) {
689                throw new RuntimeException("Overridden method shouldn't be called");
690            }
691        };
692    }
693} // end class TimestampTest
694