< prev index next >

test/jdk/java/util/Calendar/CalendarLimitTest.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.

*** 24,27 ***
  /**
   * @test
   * @bug 4033662
   * @summary test for limit on Calendar
   * @library /java/text/testlib
!  * @run main CalendarLimitTest -verbose
   */
  
  import java.util.*;
  import java.text.*;
  
  /**
   * This test verifies the behavior of Calendar around the very earliest limits
   * which it can handle.  It also verifies the behavior for large values of millis.
   *
   * Note: There used to be a limit, due to a bug, for early times.  There is
   * currently no limit.
   *
   * March 17, 1998: Added code to make sure big + dates are big + AD years, and
   * big - dates are big + BC years.
   */
! public class CalendarLimitTest extends IntlTest
  {
      // This number determined empirically; this is the old limit,
      // which we test for to make sure it isn't there anymore.
      static final long EARLIEST_SUPPORTED_MILLIS = -210993120000000L;
  
--- 24,31 ---
  /**
   * @test
   * @bug 4033662
   * @summary test for limit on Calendar
   * @library /java/text/testlib
!  * @run junit CalendarLimitTest
   */
  
  import java.util.*;
  import java.text.*;
  
+ import org.junit.jupiter.api.Test;
+ 
+ import static org.junit.jupiter.api.Assertions.fail;
+ 
  /**
   * This test verifies the behavior of Calendar around the very earliest limits
   * which it can handle.  It also verifies the behavior for large values of millis.
   *
   * Note: There used to be a limit, due to a bug, for early times.  There is
   * currently no limit.
   *
   * March 17, 1998: Added code to make sure big + dates are big + AD years, and
   * big - dates are big + BC years.
   */
! public class CalendarLimitTest
  {
      // This number determined empirically; this is the old limit,
      // which we test for to make sure it isn't there anymore.
      static final long EARLIEST_SUPPORTED_MILLIS = -210993120000000L;
  

*** 59,20 ***
      static final int  ONE_WEEK   = 7*ONE_DAY;
      static final long ONE_YEAR   = (long)(365.2425 * ONE_DAY);
  
      static long ORIGIN; // This is the *approximate* point at which BC switches to AD
  
-     public static void main(String argv[]) throws Exception {
-         Locale locale = Locale.getDefault();
-         if (!TestUtils.usesGregorianCalendar(locale)) {
-             System.out.println("Skipping this test because locale is " + locale);
-             return;
-         }
- 
-         new CalendarLimitTest().run(argv);
-     }
- 
      /**
       * Converts Julian day to time as milliseconds.
       * @param julian the given Julian day number.
       * @return time as milliseconds.
       */
--- 63,10 ---

*** 106,36 ***
          cal.set(Calendar.ERA, era);
          Date rt = cal.getTime();
  
          boolean ok = true;
          if (exception != null) {
!             errln("FAIL: Exception " + s);
              ok = false;
          }
          if (((millis >= ORIGIN) && (era != GregorianCalendar.AD)) ||
                   ((millis < ORIGIN) && (era != GregorianCalendar.BC)) ||
                   (year < 1)) {
!             errln("FAIL: Bad year/era " + s);
              ok = false;
          }
          if (dom<1 || dom>31) {
!             errln("FAIL: Bad DOM " + s);
              ok = false;
          }
          if (Math.abs(millis - rt.getTime()) > ONE_DAY) {
!             errln("FAIL: RT fail " + s + " -> 0x" +
                    Long.toHexString(rt.getTime()) + " " +
                    fmt.format(rt));
              ok = false;
          }
!         if (ok) logln(s);
          if (era==GregorianCalendar.BC) year = 1-year;
          return year;
      }
  
      public void TestCalendarLimit()
      {
          ORIGIN = julianDayToMillis(JAN_1_1_JULIAN_DAY);
  
          Calendar cal = Calendar.getInstance();
          // You must set the time zone to GMT+0 or the edge cases like
          // Long.MIN_VALUE, Long.MAX_VALUE, and right around the threshold
--- 100,42 ---
          cal.set(Calendar.ERA, era);
          Date rt = cal.getTime();
  
          boolean ok = true;
          if (exception != null) {
!             fail("FAIL: Exception " + s);
              ok = false;
          }
          if (((millis >= ORIGIN) && (era != GregorianCalendar.AD)) ||
                   ((millis < ORIGIN) && (era != GregorianCalendar.BC)) ||
                   (year < 1)) {
!             fail("FAIL: Bad year/era " + s);
              ok = false;
          }
          if (dom<1 || dom>31) {
!             fail("FAIL: Bad DOM " + s);
              ok = false;
          }
          if (Math.abs(millis - rt.getTime()) > ONE_DAY) {
!             fail("FAIL: RT fail " + s + " -> 0x" +
                    Long.toHexString(rt.getTime()) + " " +
                    fmt.format(rt));
              ok = false;
          }
!         if (ok) System.out.println(s);
          if (era==GregorianCalendar.BC) year = 1-year;
          return year;
      }
  
+     @Test
      public void TestCalendarLimit()
      {
+         Locale locale = Locale.getDefault();
+         if (!TestUtils.usesGregorianCalendar(locale)) {
+             System.out.println("Skipping this test because locale is " + locale);
+             return;
+         }
          ORIGIN = julianDayToMillis(JAN_1_1_JULIAN_DAY);
  
          Calendar cal = Calendar.getInstance();
          // You must set the time zone to GMT+0 or the edge cases like
          // Long.MIN_VALUE, Long.MAX_VALUE, and right around the threshold

*** 152,22 ***
          boolean first=true;
          for (long m = Long.MAX_VALUE; m > 0; m >>= 1)
          {
              int y = test(m, cal, dateFormat);
              if (!first && y > lastYear)
!                 errln("FAIL: Years should be decreasing " + lastYear + " " + y);
              first = false;
              lastYear = y;
          }
  
          // Expect failures for negative millis below threshold
          first = true;
          for (long m = Long.MIN_VALUE; m < 0; m /= 2) // Don't use m >>= 1
          {
              int y = test(m, cal, dateFormat);
              if (!first && y < lastYear)
!                 errln("FAIL: Years should be increasing " + lastYear + " " + y);
              first = false;
              lastYear = y;
          }
  
          // Test right around the threshold
--- 152,22 ---
          boolean first=true;
          for (long m = Long.MAX_VALUE; m > 0; m >>= 1)
          {
              int y = test(m, cal, dateFormat);
              if (!first && y > lastYear)
!                 fail("FAIL: Years should be decreasing " + lastYear + " " + y);
              first = false;
              lastYear = y;
          }
  
          // Expect failures for negative millis below threshold
          first = true;
          for (long m = Long.MIN_VALUE; m < 0; m /= 2) // Don't use m >>= 1
          {
              int y = test(m, cal, dateFormat);
              if (!first && y < lastYear)
!                 fail("FAIL: Years should be increasing " + lastYear + " " + y);
              first = false;
              lastYear = y;
          }
  
          // Test right around the threshold

*** 186,20 ***
                      cal.clear();
                      cal.set(Calendar.ERA, GregorianCalendar.BC);
                      cal.set(292269055, Calendar.DECEMBER, dom, h, 0);
                      Date d = cal.getTime();
                      cal.setTime(d);
!                     logln("" + h + ":00 Dec "+dom+", 292269055 BC -> " +
                            Long.toHexString(d.getTime()) + " -> " +
                            dateFormat.format(cal.getTime()));
                  }
              }
              // Other way
              long t = 0x80000000018c5c00L; // Dec 3, 292269055 BC
              while (t<0) {
                  cal.setTime(new Date(t));
!                 logln("0x" + Long.toHexString(t) + " -> " +
                        dateFormat.format(cal.getTime()));
                  t -= ONE_HOUR;
              }
          }
      }
--- 186,20 ---
                      cal.clear();
                      cal.set(Calendar.ERA, GregorianCalendar.BC);
                      cal.set(292269055, Calendar.DECEMBER, dom, h, 0);
                      Date d = cal.getTime();
                      cal.setTime(d);
!                     System.out.println("" + h + ":00 Dec "+dom+", 292269055 BC -> " +
                            Long.toHexString(d.getTime()) + " -> " +
                            dateFormat.format(cal.getTime()));
                  }
              }
              // Other way
              long t = 0x80000000018c5c00L; // Dec 3, 292269055 BC
              while (t<0) {
                  cal.setTime(new Date(t));
!                 System.out.println("0x" + Long.toHexString(t) + " -> " +
                        dateFormat.format(cal.getTime()));
                  t -= ONE_HOUR;
              }
          }
      }
< prev index next >