< prev index next >

test/jdk/java/util/Calendar/NonLenientTest.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2003, 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) 2003, 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,36 ***
  
  /*
   * @test
   * @bug 4147269 4266783 4726030
   * @summary Make sure that validation is adequate in non-lenient mode.
!  * @library /java/text/testlib
   */
  
  import java.util.Date;
  import java.util.GregorianCalendar;
  import java.util.Locale;
  import java.util.TimeZone;
  
  import static java.util.Calendar.*;
  
! public class NonLenientTest extends IntlTest {
  
!     public static void main(String[] args) throws Exception {
!         Locale reservedLocale = Locale.getDefault();
!         TimeZone reservedTimeZone = TimeZone.getDefault();
!         try {
!             Locale.setDefault(Locale.US);
!             TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
!             new NonLenientTest().run(args);
!         } finally {
!             // restore the reserved locale and time zone
-             Locale.setDefault(reservedLocale);
-             TimeZone.setDefault(reservedTimeZone);
-         }
      }
  
      public void TestValidationInNonLenient() {
          Koyomi cal = getNonLenient();
  
          // 2003 isn't a leap year.
          cal.set(2003, FEBRUARY, 29);
--- 23,36 ---
  
  /*
   * @test
   * @bug 4147269 4266783 4726030
   * @summary Make sure that validation is adequate in non-lenient mode.
!  * @run junit/othervm NonLenientTest
   */
  
  import java.util.Date;
  import java.util.GregorianCalendar;
  import java.util.Locale;
  import java.util.TimeZone;
  
  import static java.util.Calendar.*;
  
! import org.junit.jupiter.api.Test;
+ import org.junit.jupiter.api.BeforeAll;
  
! import static org.junit.jupiter.api.Assertions.fail;
! 
! public class NonLenientTest {
! 
!     // Change JVM default Locale and TimeZone
!     @BeforeAll
!     static void initAll() {
!         Locale.setDefault(Locale.US);
!         TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles"));
      }
  
+ 
+     @Test
      public void TestValidationInNonLenient() {
          Koyomi cal = getNonLenient();
  
          // 2003 isn't a leap year.
          cal.set(2003, FEBRUARY, 29);

*** 120,10 ***
--- 120,11 ---
      }
  
      /**
       * 4266783: java.util.GregorianCalendar: incorrect validation in non-lenient
       */
+     @Test
      public void Test4266783() {
          Koyomi cal = getNonLenient();
          // 2003/1 has up to 5 weeks.
          cal.set(YEAR, 2003);
          cal.set(MONTH, JANUARY);

*** 133,10 ***
--- 134,11 ---
      }
  
      /**
       * 4726030: GregorianCalendar doesn't check invalid dates in non-lenient
       */
+     @Test
      public void Test4726030() {
          Koyomi cal = getNonLenient();
          // Default year is 1970 in GregorianCalendar which isn't a leap year.
          cal.set(MONTH, FEBRUARY);
          cal.set(DAY_OF_MONTH, 29);

*** 144,10 ***
--- 146,11 ---
      }
  
      /**
       * 4147269: java.util.GregorianCalendar.computeTime() works wrong when lenient is false
       */
+     @Test
      public void Test4147269() {
          Koyomi calendar = getNonLenient();
          Date date = (new GregorianCalendar(1996, 0, 3)).getTime();
  
          for (int field = 0; field < FIELD_COUNT; field++) {

*** 155,11 ***
              int max = calendar.getActualMaximum(field);
              int value = max + 1;
              calendar.set(field, value);
              try {
                  calendar.computeTime(); // call method under test
!                 errln("Test failed with field " + Koyomi.getFieldName(field)
                          + "\n\tdate before:  " + date
                          + "\n\tdate after:   " + calendar.getTime()
                          + "\n\tvalue: " + value + "  (max = " + max + ")");
              } catch (IllegalArgumentException e) {
              }
--- 158,11 ---
              int max = calendar.getActualMaximum(field);
              int value = max + 1;
              calendar.set(field, value);
              try {
                  calendar.computeTime(); // call method under test
!                 fail("Test failed with field " + Koyomi.getFieldName(field)
                          + "\n\tdate before:  " + date
                          + "\n\tdate after:   " + calendar.getTime()
                          + "\n\tvalue: " + value + "  (max = " + max + ")");
              } catch (IllegalArgumentException e) {
              }

*** 170,11 ***
              int min = calendar.getActualMinimum(field);
              int value = min - 1;
              calendar.set(field, value);
              try {
                  calendar.computeTime(); // call method under test
!                 errln("Test failed with field " + Koyomi.getFieldName(field)
                          + "\n\tdate before:  " + date
                          + "\n\tdate after:   " + calendar.getTime()
                          + "\n\tvalue: " + value + "  (min = " + min + ")");
              } catch (IllegalArgumentException e) {
              }
--- 173,11 ---
              int min = calendar.getActualMinimum(field);
              int value = min - 1;
              calendar.set(field, value);
              try {
                  calendar.computeTime(); // call method under test
!                 fail("Test failed with field " + Koyomi.getFieldName(field)
                          + "\n\tdate before:  " + date
                          + "\n\tdate after:   " + calendar.getTime()
                          + "\n\tvalue: " + value + "  (min = " + min + ")");
              } catch (IllegalArgumentException e) {
              }

*** 185,29 ***
          int[] originalFields = cal.getFields();
          int setFields = cal.getSetStateFields();
  
          try {
              cal.complete();
!             errln(desc + " should throw IllegalArgumentException in non-lenient.");
          } catch (IllegalArgumentException e) {
          }
  
          // The code below will be executed with the -nothrow option
  
          // In non-lenient, calendar field values that have beeb set by
          // user shouldn't be modified.
          int[] afterFields = cal.getFields();
          for (int i = 0; i < FIELD_COUNT; i++) {
              if (cal.isSet(i) && originalFields[i] != afterFields[i]) {
!                 errln("    complete() modified fields[" + Koyomi.getFieldName(i) + "] got "
                          + afterFields[i] + ", expected " + originalFields[i]);
              }
          }
          // In non-lenient, set state of fields shouldn't be modified.
          int afterSetFields = cal.getSetStateFields();
          if (setFields != afterSetFields) {
!             errln("    complate() modified set states: before 0x" + toHex(setFields)
                      + ", after 0x" + toHex(afterSetFields));
          }
      }
  
      static Koyomi getNonLenient() {
--- 188,29 ---
          int[] originalFields = cal.getFields();
          int setFields = cal.getSetStateFields();
  
          try {
              cal.complete();
!             fail(desc + " should throw IllegalArgumentException in non-lenient.");
          } catch (IllegalArgumentException e) {
          }
  
          // The code below will be executed with the -nothrow option
  
          // In non-lenient, calendar field values that have beeb set by
          // user shouldn't be modified.
          int[] afterFields = cal.getFields();
          for (int i = 0; i < FIELD_COUNT; i++) {
              if (cal.isSet(i) && originalFields[i] != afterFields[i]) {
!                 fail("    complete() modified fields[" + Koyomi.getFieldName(i) + "] got "
                          + afterFields[i] + ", expected " + originalFields[i]);
              }
          }
          // In non-lenient, set state of fields shouldn't be modified.
          int afterSetFields = cal.getSetStateFields();
          if (setFields != afterSetFields) {
!             fail("    complate() modified set states: before 0x" + toHex(setFields)
                      + ", after 0x" + toHex(afterSetFields));
          }
      }
  
      static Koyomi getNonLenient() {
< prev index next >