ICU-9403 Calendar should return error for large negative millis out of range in strict mode

X-SVN-Rev: 32553
This commit is contained in:
Scott Russell 2012-10-08 19:13:27 +00:00
parent 33eb771977
commit 5c6dc411d8
2 changed files with 53 additions and 3 deletions

View 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;

View 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