8068278: ArrayIndexOutOfBoundsException instead of DateTimeException in j.t.chrono.JapaneseChronology.eraOf()
authorrriggs
Tue, 03 Feb 2015 14:39:57 -0500
changeset 28772 5259f2a59d83
parent 28771 2885fb2e1002
child 28773 0b8da4d7cda6
8068278: ArrayIndexOutOfBoundsException instead of DateTimeException in j.t.chrono.JapaneseChronology.eraOf() Summary: Corrected era range check Reviewed-by: mchung, lancea
jdk/src/java.base/share/classes/java/time/chrono/JapaneseEra.java
jdk/test/java/time/tck/java/time/chrono/TCKJapaneseEra.java
--- a/jdk/src/java.base/share/classes/java/time/chrono/JapaneseEra.java	Tue Feb 03 09:49:27 2015 -0800
+++ b/jdk/src/java.base/share/classes/java/time/chrono/JapaneseEra.java	Tue Feb 03 14:39:57 2015 -0500
@@ -195,10 +195,11 @@
      * @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];
     }
 
     /**
--- a/jdk/test/java/time/tck/java/time/chrono/TCKJapaneseEra.java	Tue Feb 03 09:49:27 2015 -0800
+++ b/jdk/test/java/time/tck/java/time/chrono/TCKJapaneseEra.java	Tue Feb 03 14:39:57 2015 -0500
@@ -59,6 +59,7 @@
 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.Test;
 
 /**
- * Test.
+ * Tests for JapaneseEra
+ * @bug 8068278
  */
 @Test
 public class TCKJapaneseEra {
@@ -84,6 +86,20 @@
         };
     }
 
+    @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 @@
         }
     }
 
+    //-----------------------------------------------------------------------
+    // JapaneseChronology.INSTANCE.eraOf invalid era test
+    //-----------------------------------------------------------------------
+    @Test(dataProvider="InvalidJapaneseEras", expectedExceptions=java.time.DateTimeException.class)
+    public void test_outofrange(int era) {
+        JapaneseChronology.INSTANCE.eraOf(era);
+    }
 }