Path: blob/master/test/jdk/java/util/Date/TimestampTest.java
41149 views
/*1* Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 500822726* @summary Make sure that changes to the Date class don't break java.sql.Timestamp.27* @modules java.sql28* @library /java/text/testlib29*/3031import java.util.*;32import java.sql.Timestamp;3334public class TimestampTest extends IntlTest {3536public static void main(String[] args) throws Exception {37new TimestampTest().run(args);38}3940/**41* 5008227: java.sql.Timestamp.after() is not returning correct result42*43* Test before(), after(), equals(), compareTo() and getTime().44*/45public void Test5008227() {46long t = System.currentTimeMillis();47Timestamp ts1 = new Timestamp(t), ts2 = new Timestamp(t);48ts1.setNanos(999999999);49ts2.setNanos( 1000000);50compareTimestamps(ts1, ts2, 1);5152ts1.setTime(t + 1000);53ts2.setTime(t);54ts1.setNanos( 999999);55ts2.setNanos(999999999);56compareTimestamps(ts1, ts2, 1);5758ts1.setTime(t);59ts2.setTime(t);60ts1.setNanos(123456789);61ts2.setNanos(123456789);62compareTimestamps(ts1, ts2, 0);6364ts1.setTime(t);65ts2.setTime(t);66ts1.setNanos(1);67ts2.setNanos(2);68compareTimestamps(ts1, ts2, -1);6970ts1.setTime(t);71ts2.setTime(t+1000);72ts1.setNanos(999999);73ts2.setNanos( 0);74compareTimestamps(ts1, ts2, -1);75}7677/**78* Compares two Timestamps with the expected result.79*80* @param ts1 the first Timestamp81* @param ts2 the second Timestamp82* @param expect the expected relation between ts1 and ts2; 0 if83* ts1 equals to ts2, or 1 if ts1 is after ts2, or -1 if ts1 is84* before ts2.85*/86private void compareTimestamps(Timestamp ts1, Timestamp ts2, int expected) {87boolean expectedResult = expected > 0;88boolean result = ts1.after(ts2);89if (result != expectedResult) {90errln("ts1.after(ts2) returned " + result91+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");92}9394expectedResult = expected < 0;95result = ts1.before(ts2);96if (result != expectedResult) {97errln("ts1.before(ts2) returned " + result98+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");99}100101expectedResult = expected == 0;102result = ts1.equals(ts2);103if (result != expectedResult) {104errln("ts1.equals(ts2) returned " + result105+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");106}107108int x = ts1.compareTo(ts2);109int y = (x > 0) ? 1 : (x < 0) ? -1 : 0;110if (y != expected) {111errln("ts1.compareTo(ts2) returned " + x + ", expected "112+ relation(expected, "") + "0"113+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");114}115long t1 = ts1.getTime();116long t2 = ts2.getTime();117int z = (t1 > t2) ? 1 : (t1 < t2) ? -1 : 0;118if (z == 0) {119int n1 = ts1.getNanos();120int n2 = ts2.getNanos();121z = (n1 > n2) ? 1 : (n1 < n2) ? -1 : 0;122}123if (z != expected) {124errln("ts1.getTime() " + relation(z, "==") + " ts2.getTime(), expected "125+ relation(expected, "==")126+ ". (ts1=" + ts1 + ", ts2=" + ts2 + ")");127}128}129130private static String relation(int x, String whenEqual) {131return (x > 0) ? ">" : (x < 0) ? "<" : whenEqual;132}133}134135136