< prev index next >

test/jdk/java/util/Date/DateTest.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.
--- 1,7 ---
  /*
!  * Copyright (c) 1997, 2023, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

*** 23,27 ***
  
  /*
   * @test
   * @bug 4143459
   * @summary test Date
!  * @library /java/text/testlib
   */
  
  import java.text.*;
  import java.util.*;
  
  @SuppressWarnings("deprecation")
! public class DateTest extends IntlTest
  {
-     public static void main(String[] args) throws Exception {
-         new DateTest().run(args);
-     }
- 
      /**
       * @bug 4143459
       * Warning: Use TestDefaultZone() for complete testing of this bug.
       */
      public void TestDefaultZoneLite() {
          // Note: This test is redundant with TestDefaultZone().  It was added by
          // request to provide a short&sweet test for this bug.  It does not test
          // all cases though, so IF THIS TEST PASSES, THE BUG MAY STILL BE
          // PRESENT.  Use TestDefaultZone() to be sure.
--- 23,28 ---
  
  /*
   * @test
   * @bug 4143459
   * @summary test Date
!  * @run junit DateTest
   */
  
  import java.text.*;
  import java.util.*;
  
+ import org.junit.jupiter.api.Test;
+ 
+ import static org.junit.jupiter.api.Assertions.fail;
+ 
  @SuppressWarnings("deprecation")
! public class DateTest
  {
      /**
       * @bug 4143459
       * Warning: Use TestDefaultZone() for complete testing of this bug.
       */
+     @Test
      public void TestDefaultZoneLite() {
          // Note: This test is redundant with TestDefaultZone().  It was added by
          // request to provide a short&sweet test for this bug.  It does not test
          // all cases though, so IF THIS TEST PASSES, THE BUG MAY STILL BE
          // PRESENT.  Use TestDefaultZone() to be sure.

*** 55,19 ***
              d.setMonth(Calendar.JANUARY);
              d.setDate(1);
              d.setHours(6);
              TimeZone.setDefault(TimeZone.getTimeZone("PST"));
              if (d.getHours() != 22) {
!                 errln("Fail: Date.setHours()/getHours() ignoring default zone");
              }
          }
          finally { TimeZone.setDefault(save); }
      }
  
      /**
       * @bug 4143459
       */
      public void TestDefaultZone() {
          // Various problems can creep up, with the current implementation of Date,
          // when the default zone is changed.
  
          TimeZone saveZone = TimeZone.getDefault();
--- 56,20 ---
              d.setMonth(Calendar.JANUARY);
              d.setDate(1);
              d.setHours(6);
              TimeZone.setDefault(TimeZone.getTimeZone("PST"));
              if (d.getHours() != 22) {
!                 fail("Fail: Date.setHours()/getHours() ignoring default zone");
              }
          }
          finally { TimeZone.setDefault(save); }
      }
  
      /**
       * @bug 4143459
       */
+     @Test
      public void TestDefaultZone() {
          // Various problems can creep up, with the current implementation of Date,
          // when the default zone is changed.
  
          TimeZone saveZone = TimeZone.getDefault();

*** 85,38 ***
  
              // There are two cases to consider: a Date object with no Calendar
              // sub-object (most Date objects), and a Date object with a Calendar
              // sub-object.  We make two passes to cover the two cases.
              for (int pass=0; pass<2; ++pass) {
!                 logln(pass == 0 ? "Normal Date object" : "Date with Calendar sub-object");
  
                  TimeZone.setDefault(GMT);
                  d = new Date(refstr);
                  if (pass == 1) {
                      // Force creation of Calendar sub-object
                      d.setYear(d.getYear());
                  }
                  if (d.getTime() != ref.getTime()) {
!                     errln("FAIL: new Date(\"" + refstr + "\") x GMT -> " + d +
                            " " + d.getTime() + " ms");
                  }
  
                  int[] fields = { d.getYear(), d.getMonth(), d.getDate(),
                                   d.getDay(), d.getHours(), d.getTimezoneOffset() };
                  for (int i=0; i<fields.length; ++i) {
                      if (fields[i] != GMT_EXP[i]) {
!                         errln("FAIL: GMT Expected " + names[i] + " of " + GMT_EXP[i] +
                                ", got " + fields[i]);
                      }
                  }
  
                  TimeZone.setDefault(PST);
                  int[] fields2 = { d.getYear(), d.getMonth(), d.getDate(),
                                    d.getDay(), d.getHours(), d.getTimezoneOffset() };
                  for (int i=0; i<fields2.length; ++i) {
                      if (fields2[i] != PST_EXP[i]) {
!                         errln("FAIL: PST Expected " + names[i] + " of " + PST_EXP[i] +
                                ", got " + fields2[i]);
                      }
                  }
              }
          }
--- 87,38 ---
  
              // There are two cases to consider: a Date object with no Calendar
              // sub-object (most Date objects), and a Date object with a Calendar
              // sub-object.  We make two passes to cover the two cases.
              for (int pass=0; pass<2; ++pass) {
!                 System.out.println(pass == 0 ? "Normal Date object" : "Date with Calendar sub-object");
  
                  TimeZone.setDefault(GMT);
                  d = new Date(refstr);
                  if (pass == 1) {
                      // Force creation of Calendar sub-object
                      d.setYear(d.getYear());
                  }
                  if (d.getTime() != ref.getTime()) {
!                     fail("FAIL: new Date(\"" + refstr + "\") x GMT -> " + d +
                            " " + d.getTime() + " ms");
                  }
  
                  int[] fields = { d.getYear(), d.getMonth(), d.getDate(),
                                   d.getDay(), d.getHours(), d.getTimezoneOffset() };
                  for (int i=0; i<fields.length; ++i) {
                      if (fields[i] != GMT_EXP[i]) {
!                         fail("FAIL: GMT Expected " + names[i] + " of " + GMT_EXP[i] +
                                ", got " + fields[i]);
                      }
                  }
  
                  TimeZone.setDefault(PST);
                  int[] fields2 = { d.getYear(), d.getMonth(), d.getDate(),
                                    d.getDay(), d.getHours(), d.getTimezoneOffset() };
                  for (int i=0; i<fields2.length; ++i) {
                      if (fields2[i] != PST_EXP[i]) {
!                         fail("FAIL: PST Expected " + names[i] + " of " + PST_EXP[i] +
                                ", got " + fields2[i]);
                      }
                  }
              }
          }

*** 124,10 ***
--- 126,11 ---
              TimeZone.setDefault(saveZone);
          }
      }
  
      // Test the performance of Date
+     @Test
      public void TestPerformance592()
      {
          int REPS = 500;
  
          // Do timing test with Date

*** 138,20 ***
              int y = d.getYear();
          }
          long ms = new Date().getTime() - start;
  
          double perLoop = ((double)ms) / REPS;
!         logln(REPS + " iterations at " + perLoop + " ms/loop");
          if (perLoop > PER_LOOP_LIMIT)
!             logln("WARNING: Date constructor/getYear slower than " +
                    PER_LOOP_LIMIT + " ms");
      }
      static double PER_LOOP_LIMIT = 3.0;
  
      /**
       * Verify that the Date(String) constructor works.
       */
      public void TestParseOfGMT()
      {
          Date OUT = null;
  
          /* Input values */
--- 141,21 ---
              int y = d.getYear();
          }
          long ms = new Date().getTime() - start;
  
          double perLoop = ((double)ms) / REPS;
!         System.out.println(REPS + " iterations at " + perLoop + " ms/loop");
          if (perLoop > PER_LOOP_LIMIT)
!             System.out.println("WARNING: Date constructor/getYear slower than " +
                    PER_LOOP_LIMIT + " ms");
      }
      static double PER_LOOP_LIMIT = 3.0;
  
      /**
       * Verify that the Date(String) constructor works.
       */
+     @Test
      public void TestParseOfGMT()
      {
          Date OUT = null;
  
          /* Input values */

*** 162,11 ***
  
          if( OUT.getTime( ) == expectedVal ) {
              // logln("PASS");
          }
          else {
!             errln( "Expected: " +
                     new Date( expectedVal ) +
                     ": " +
                     expectedVal +
                     "  Received: " +
                     OUT.toString() +
--- 166,11 ---
  
          if( OUT.getTime( ) == expectedVal ) {
              // logln("PASS");
          }
          else {
!             fail( "Expected: " +
                     new Date( expectedVal ) +
                     ": " +
                     expectedVal +
                     "  Received: " +
                     OUT.toString() +

*** 176,60 ***
      }
  
      // Check out Date's behavior with large negative year values; bug 664
      // As of the fix to bug 4056585, Date should work correctly with
      // large negative years.
      public void TestDateNegativeYears()
      {
          Date d1= new Date(80,-1,2);
!         logln(d1.toString());
          d1= new Date(-80,-1,2);
!         logln(d1.toString());
          boolean e = false;
          try {
              d1= new Date(-800000,-1,2);
!             logln(d1.toString());
          }
          catch (IllegalArgumentException ex) {
              e = true;
          }
!         if (e) errln("FAIL: Saw exception for year -800000");
!         else logln("Pass: No exception for year -800000");
      }
  
      // Verify the behavior of Date
      public void TestDate480()
      {
        TimeZone save = TimeZone.getDefault();
        try {
          TimeZone.setDefault(TimeZone.getTimeZone("PST"));
          Date d1=new java.util.Date(97,8,13,10,8,13);
!         logln("d       = "+d1);
          Date d2=new java.util.Date(97,8,13,30,8,13); // 20 hours later
!         logln("d+20h   = "+d2);
  
          double delta = (d2.getTime() - d1.getTime()) / 3600000;
  
!         logln("delta   = " + delta + "h");
  
!         if (delta != 20.0) errln("Expected delta of 20; got " + delta);
  
          Calendar cal = Calendar.getInstance();
          cal.clear();
          cal.set(1997,8,13,10,8,13);
          Date t1 = cal.getTime();
!         logln("d       = "+t1);
          cal.clear();
          cal.set(1997,8,13,30,8,13); // 20 hours later
          Date t2 = cal.getTime();
!         logln("d+20h   = "+t2);
  
          double delta2 = (t2.getTime() - t1.getTime()) / 3600000;
  
!         logln("delta   = " + delta2 + "h");
  
!         if (delta != 20.0) errln("Expected delta of 20; got " + delta2);
        }
        finally {
          TimeZone.setDefault(save);
        }
      }
--- 180,62 ---
      }
  
      // Check out Date's behavior with large negative year values; bug 664
      // As of the fix to bug 4056585, Date should work correctly with
      // large negative years.
+     @Test
      public void TestDateNegativeYears()
      {
          Date d1= new Date(80,-1,2);
!         System.out.println(d1.toString());
          d1= new Date(-80,-1,2);
!         System.out.println(d1.toString());
          boolean e = false;
          try {
              d1= new Date(-800000,-1,2);
!             System.out.println(d1.toString());
          }
          catch (IllegalArgumentException ex) {
              e = true;
          }
!         if (e) fail("FAIL: Saw exception for year -800000");
!         else System.out.println("Pass: No exception for year -800000");
      }
  
      // Verify the behavior of Date
+     @Test
      public void TestDate480()
      {
        TimeZone save = TimeZone.getDefault();
        try {
          TimeZone.setDefault(TimeZone.getTimeZone("PST"));
          Date d1=new java.util.Date(97,8,13,10,8,13);
!         System.out.println("d       = "+d1);
          Date d2=new java.util.Date(97,8,13,30,8,13); // 20 hours later
!         System.out.println("d+20h   = "+d2);
  
          double delta = (d2.getTime() - d1.getTime()) / 3600000;
  
!         System.out.println("delta   = " + delta + "h");
  
!         if (delta != 20.0) fail("Expected delta of 20; got " + delta);
  
          Calendar cal = Calendar.getInstance();
          cal.clear();
          cal.set(1997,8,13,10,8,13);
          Date t1 = cal.getTime();
!         System.out.println("d       = "+t1);
          cal.clear();
          cal.set(1997,8,13,30,8,13); // 20 hours later
          Date t2 = cal.getTime();
!         System.out.println("d+20h   = "+t2);
  
          double delta2 = (t2.getTime() - t1.getTime()) / 3600000;
  
!         System.out.println("delta   = " + delta2 + "h");
  
!         if (delta != 20.0) fail("Expected delta of 20; got " + delta2);
        }
        finally {
          TimeZone.setDefault(save);
        }
      }
< prev index next >