< prev index next >

test/jdk/java/util/Date/TimestampTest.java

Print this page

  1 /*
  2  * Copyright (c) 2004, 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 5008227
 27  * @summary Make sure that changes to the Date class don't break java.sql.Timestamp.
 28  * @modules java.sql
 29  * @library /java/text/testlib
 30  */
 31 
 32 import java.util.*;
 33 import java.sql.Timestamp;
 34 
 35 public class TimestampTest extends IntlTest {
 36 
 37     public static void main(String[] args) throws Exception {
 38         new TimestampTest().run(args);
 39     }
 40 
 41     /**
 42      * 5008227: java.sql.Timestamp.after() is not returning correct result
 43      *
 44      * Test before(), after(), equals(), compareTo() and getTime().
 45      */

 46     public void Test5008227() {
 47         long t = System.currentTimeMillis();
 48         Timestamp ts1 = new Timestamp(t), ts2 = new Timestamp(t);
 49         ts1.setNanos(999999999);
 50         ts2.setNanos(  1000000);
 51         compareTimestamps(ts1, ts2, 1);
 52 
 53         ts1.setTime(t + 1000);
 54         ts2.setTime(t);
 55         ts1.setNanos(   999999);
 56         ts2.setNanos(999999999);
 57         compareTimestamps(ts1, ts2, 1);
 58 
 59         ts1.setTime(t);
 60         ts2.setTime(t);
 61         ts1.setNanos(123456789);
 62         ts2.setNanos(123456789);
 63         compareTimestamps(ts1, ts2, 0);
 64 
 65         ts1.setTime(t);

 71         ts1.setTime(t);
 72         ts2.setTime(t+1000);
 73         ts1.setNanos(999999);
 74         ts2.setNanos(     0);
 75         compareTimestamps(ts1, ts2, -1);
 76     }
 77 
 78     /**
 79      * Compares two Timestamps with the expected result.
 80      *
 81      * @param ts1 the first Timestamp
 82      * @param ts2 the second Timestamp
 83      * @param expect the expected relation between ts1 and ts2; 0 if
 84      * ts1 equals to ts2, or 1 if ts1 is after ts2, or -1 if ts1 is
 85      * before ts2.
 86      */
 87     private void compareTimestamps(Timestamp ts1, Timestamp ts2, int expected) {
 88         boolean expectedResult = expected > 0;
 89         boolean result = ts1.after(ts2);
 90         if (result != expectedResult) {
 91             errln("ts1.after(ts2) returned " + result
 92                   + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
 93         }
 94 
 95         expectedResult = expected < 0;
 96         result = ts1.before(ts2);
 97         if (result != expectedResult) {
 98             errln("ts1.before(ts2) returned " + result
 99                   + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
100         }
101 
102         expectedResult = expected == 0;
103         result = ts1.equals(ts2);
104         if (result != expectedResult) {
105             errln("ts1.equals(ts2) returned " + result
106                   + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
107         }
108 
109         int x = ts1.compareTo(ts2);
110         int y = (x > 0) ? 1 : (x < 0) ? -1 : 0;
111         if (y != expected) {
112             errln("ts1.compareTo(ts2) returned " + x + ", expected "
113                   + relation(expected, "") + "0"
114                   + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
115         }
116         long t1 = ts1.getTime();
117         long t2 = ts2.getTime();
118         int z = (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;
119         if (z == 0) {
120             int n1 = ts1.getNanos();
121             int n2 = ts2.getNanos();
122             z = (n1 > n2) ? 1 : (n1 < n2) ? -1 : 0;
123         }
124         if (z != expected) {
125             errln("ts1.getTime() " + relation(z, "==") + " ts2.getTime(), expected "
126                   + relation(expected, "==")
127                   + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
128         }
129     }
130 
131     private static String relation(int x, String whenEqual) {
132         return (x > 0) ? ">" : (x < 0) ? "<" : whenEqual;
133     }
134 }

  1 /*
  2  * Copyright (c) 2004, 2023, 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 5008227
 27  * @summary Make sure that changes to the Date class don't break java.sql.Timestamp.
 28  * @modules java.sql
 29  * @run junit TimestampTest
 30  */
 31 
 32 import java.util.*;
 33 import java.sql.Timestamp;
 34 
 35 import org.junit.jupiter.api.Test;
 36 
 37 import static org.junit.jupiter.api.Assertions.fail;
 38 
 39 public class TimestampTest {
 40 
 41     /**
 42      * 5008227: java.sql.Timestamp.after() is not returning correct result
 43      *
 44      * Test before(), after(), equals(), compareTo() and getTime().
 45      */
 46     @Test
 47     public void Test5008227() {
 48         long t = System.currentTimeMillis();
 49         Timestamp ts1 = new Timestamp(t), ts2 = new Timestamp(t);
 50         ts1.setNanos(999999999);
 51         ts2.setNanos(  1000000);
 52         compareTimestamps(ts1, ts2, 1);
 53 
 54         ts1.setTime(t + 1000);
 55         ts2.setTime(t);
 56         ts1.setNanos(   999999);
 57         ts2.setNanos(999999999);
 58         compareTimestamps(ts1, ts2, 1);
 59 
 60         ts1.setTime(t);
 61         ts2.setTime(t);
 62         ts1.setNanos(123456789);
 63         ts2.setNanos(123456789);
 64         compareTimestamps(ts1, ts2, 0);
 65 
 66         ts1.setTime(t);

 72         ts1.setTime(t);
 73         ts2.setTime(t+1000);
 74         ts1.setNanos(999999);
 75         ts2.setNanos(     0);
 76         compareTimestamps(ts1, ts2, -1);
 77     }
 78 
 79     /**
 80      * Compares two Timestamps with the expected result.
 81      *
 82      * @param ts1 the first Timestamp
 83      * @param ts2 the second Timestamp
 84      * @param expect the expected relation between ts1 and ts2; 0 if
 85      * ts1 equals to ts2, or 1 if ts1 is after ts2, or -1 if ts1 is
 86      * before ts2.
 87      */
 88     private void compareTimestamps(Timestamp ts1, Timestamp ts2, int expected) {
 89         boolean expectedResult = expected > 0;
 90         boolean result = ts1.after(ts2);
 91         if (result != expectedResult) {
 92             fail("ts1.after(ts2) returned " + result
 93                   + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
 94         }
 95 
 96         expectedResult = expected < 0;
 97         result = ts1.before(ts2);
 98         if (result != expectedResult) {
 99             fail("ts1.before(ts2) returned " + result
100                   + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
101         }
102 
103         expectedResult = expected == 0;
104         result = ts1.equals(ts2);
105         if (result != expectedResult) {
106             fail("ts1.equals(ts2) returned " + result
107                   + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
108         }
109 
110         int x = ts1.compareTo(ts2);
111         int y = (x > 0) ? 1 : (x < 0) ? -1 : 0;
112         if (y != expected) {
113             fail("ts1.compareTo(ts2) returned " + x + ", expected "
114                   + relation(expected, "") + "0"
115                   + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
116         }
117         long t1 = ts1.getTime();
118         long t2 = ts2.getTime();
119         int z = (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;
120         if (z == 0) {
121             int n1 = ts1.getNanos();
122             int n2 = ts2.getNanos();
123             z = (n1 > n2) ? 1 : (n1 < n2) ? -1 : 0;
124         }
125         if (z != expected) {
126             fail("ts1.getTime() " + relation(z, "==") + " ts2.getTime(), expected "
127                   + relation(expected, "==")
128                   + ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");
129         }
130     }
131 
132     private static String relation(int x, String whenEqual) {
133         return (x > 0) ? ">" : (x < 0) ? "<" : whenEqual;
134     }
135 }
< prev index next >