8068278: ArrayIndexOutOfBoundsException instead of DateTimeException in j.t.chrono.JapaneseChronology.eraOf()

Corrected era range check

Reviewed-by: mchung, lancea
This commit is contained in:
Roger Riggs 2015-02-03 14:39:57 -05:00
parent bb5e8afcf2
commit ddb472a4dc
2 changed files with 27 additions and 3 deletions
jdk
src/java.base/share/classes/java/time/chrono
test/java/time/tck/java/time/chrono

@ -195,10 +195,11 @@ public final class JapaneseEra
* @throws DateTimeException if the value is invalid
*/
public static JapaneseEra of(int japaneseEra) {
if (japaneseEra < MEIJI.eraValue || japaneseEra + ERA_OFFSET > KNOWN_ERAS.length) {
int i = ordinal(japaneseEra);
if (i < 0 || i >= KNOWN_ERAS.length) {
throw new DateTimeException("Invalid era: " + japaneseEra);
}
return KNOWN_ERAS[ordinal(japaneseEra)];
return KNOWN_ERAS[i];
}
/**

@ -59,6 +59,7 @@ package tck.java.time.chrono;
import static java.time.temporal.ChronoField.ERA;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.time.chrono.Era;
import java.time.chrono.JapaneseChronology;
@ -69,7 +70,8 @@ import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/**
* Test.
* Tests for JapaneseEra
* @bug 8068278
*/
@Test
public class TCKJapaneseEra {
@ -84,6 +86,20 @@ public class TCKJapaneseEra {
};
}
@DataProvider(name = "InvalidJapaneseEras")
Object[][] data_of_invalid_eras() {
return new Object[][] {
{-2},
{-3},
{3},
{Integer.MIN_VALUE},
{Integer.MAX_VALUE},
};
}
//-----------------------------------------------------------------------
// JapaneseEra value test
//-----------------------------------------------------------------------
@Test(dataProvider="JapaneseEras")
public void test_valueOf(JapaneseEra era , String eraName, int eraValue) {
assertEquals(era.getValue(), eraValue);
@ -118,4 +134,11 @@ public class TCKJapaneseEra {
}
}
//-----------------------------------------------------------------------
// JapaneseChronology.INSTANCE.eraOf invalid era test
//-----------------------------------------------------------------------
@Test(dataProvider="InvalidJapaneseEras", expectedExceptions=java.time.DateTimeException.class)
public void test_outofrange(int era) {
JapaneseChronology.INSTANCE.eraOf(era);
}
}