]> granicus.if.org Git - icu/commitdiff
ICU-9403 Calendar should return error for large negative millis out of range in stric...
authorScott Russell <DTownSMR@gmail.com>
Mon, 8 Oct 2012 19:13:27 +0000 (19:13 +0000)
committerScott Russell <DTownSMR@gmail.com>
Mon, 8 Oct 2012 19:13:27 +0000 (19:13 +0000)
X-SVN-Rev: 32553

icu4j/main/classes/core/src/com/ibm/icu/util/Calendar.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/calendar/CalendarRegression.java

index 3f8224e3e7795516ca4a6c62019cb2d799cf9094..2bfd848ac755d72bfae8624877fb0fd73b9f1ff6 100644 (file)
@@ -592,7 +592,7 @@ import com.ibm.icu.util.ULocale.Category;
  * boundaries. The new <code>Calendar</code> protocol specifies the
  * maximum range of supportable dates as those having Julian day numbers
  * of <code>-0x7F000000</code> to <code>+0x7F000000</code>. This
- * corresponds to years from ~5,000,000 BCE to ~5,000,000 CE. Programmers
+ * corresponds to years from ~5,800,000 BCE to ~5,800,000 CE. Programmers
  * should use the protected constants in <code>Calendar</code> to
  * specify an extremely early or extremely late date.</p>
  *
@@ -2091,14 +2091,25 @@ public abstract class Calendar implements Serializable, Cloneable, Comparable<Ca
 
     /**
      * Sets this Calendar's current time from the given long value.
+     * An IllegalIcuArgumentException is thrown when millis is outside the range permitted
+     * by a Calendar object when in strict mode.
+     * When in lenient mode the out of range values are pinned to their respective min/max.
      * @param millis the new time in UTC milliseconds from the epoch.
      * @stable ICU 2.0
      */
     public void setTimeInMillis( long millis ) {
         if (millis > MAX_MILLIS) {
-            millis = MAX_MILLIS;
+            if(isLenient()) {
+                millis = MAX_MILLIS;
+            } else {
+                throw new IllegalArgumentException("millis value greater than upper bounds for a Calendar : " + millis);
+            }
         } else if (millis < MIN_MILLIS) {
-            millis = MIN_MILLIS;
+            if(isLenient()) {
+                millis = MIN_MILLIS;
+            } else {
+                throw new IllegalArgumentException("millis value less than lower bounds for a Calendar : " + millis);
+            }
         }
         time = millis;
         areFieldsSet = areAllFieldsSet = false;
index a21550d8991a3ca093b6ef826d541dec3e340e71..22a1fee2cbdb0d28ce677cb536892ae7aeef0566 100644 (file)
@@ -2295,6 +2295,45 @@ public class CalendarRegression extends com.ibm.icu.dev.test.TestFmwk {
         logln("-1 day: " + dstr);
         assertEquals("Subtract 1 day", "2011-12-29T00:00:00-10:00", dstr);
     }
+    
+    /**
+     * Test case for ticket 9403
+     * semantic API change when attempting to call setTimeInMillis(long) with a value outside the bounds. 
+     * In strict mode an IllegalIcuArgumentException will be thrown
+     * In lenient mode the value will be pinned to the relative min/max 
+     */
+    public void TestT9403() {
+       Calendar myCal = Calendar.getInstance();
+       long dateBit1, dateBit2, testMillis = 0L;
+       boolean missedException = true;
+
+       testMillis = -184303902611600000L;
+        logln("Testing invalid setMillis value in lienent mode - using value: " + testMillis);
+       
+       try {
+               myCal.setTimeInMillis(testMillis);
+       } catch (IllegalArgumentException e) {
+               logln("Fail: detected as bad millis");
+               missedException = false;  
+       }
+       assertTrue("Fail: out of bound millis did not trigger exception!", missedException);
+           dateBit1 = myCal.get(Calendar.MILLISECOND);
+           assertNotEquals("Fail: millis not changed to MIN_MILLIS", testMillis, dateBit1);
+
+           
+        logln("Testing invalid setMillis value in strict mode - using value: " + testMillis);
+        myCal.setLenient(false);
+        try {
+            myCal.setTimeInMillis(testMillis);
+        } catch (IllegalArgumentException e) {
+            logln("Pass: correctly detected bad millis");
+            missedException = false;  
+        }
+        dateBit1 = myCal.get(Calendar.DAY_OF_MONTH);
+        dateBit2 = myCal.getTimeInMillis();
+        assertFalse("Fail: error in setMillis, allowed invalid value : " + testMillis + "...returned dayOfMonth : " + dateBit1 + " millis : " + dateBit2, missedException);            
+    }
+    
 }
 
 //eof