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); 66 ts2.setTime(t); 67 ts1.setNanos(1); 68 ts2.setNanos(2); 69 compareTimestamps(ts1, ts2, -1); 70 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 }