Move all testing of runtime-thrown exceptions' detail messages into the runtime tests.
Change-Id: I6427b09c40bc9a48e39c560f01d2be14ba115ccc
diff --git a/test/201-built-in-exception-detail-messages/expected.txt b/test/201-built-in-exception-detail-messages/expected.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/201-built-in-exception-detail-messages/expected.txt
diff --git a/test/201-built-in-exception-detail-messages/info.txt b/test/201-built-in-exception-detail-messages/info.txt
new file mode 100644
index 0000000..a01b727
--- /dev/null
+++ b/test/201-built-in-exception-detail-messages/info.txt
@@ -0,0 +1 @@
+Tests of the built-in exceptions' detail messages.
diff --git a/test/201-built-in-exception-detail-messages/src/Main.java b/test/201-built-in-exception-detail-messages/src/Main.java
new file mode 100644
index 0000000..638fb47
--- /dev/null
+++ b/test/201-built-in-exception-detail-messages/src/Main.java
@@ -0,0 +1,356 @@
+/*
+ * Copyright (C) 2012 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.lang.reflect.Array;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+
+public class Main {
+ public static void main(String[] args) throws Exception {
+ arrayAccess();
+ arrayStore();
+ classCast();
+ classNotFound();
+ reflection();
+ stringIndex();
+ }
+
+ private static void assertEquals(String expected, String actual) {
+ if (expected == null && actual == null) {
+ return;
+ }
+ if (expected != null && expected.equals(actual)) {
+ return;
+ }
+ throw new AssertionError("not equal\n" +
+ "expected: " + expected + "\n" +
+ "actual: " + actual);
+ }
+
+ private static void fail() {
+ throw new AssertionError();
+ }
+
+ private static void arrayAccess() throws Exception {
+ byte[] bs = new byte[1];
+ double[] ds = new double[1];
+ Object[] os = new Object[1];
+
+ // aput
+ try {
+ bs[2] = 0;
+ fail();
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ assertEquals("length=1; index=2", ex.getMessage());
+ }
+
+ // aget
+ try {
+ byte b = bs[2];
+ fail();
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ assertEquals("length=1; index=2", ex.getMessage());
+ }
+
+ // aput-wide
+ try {
+ ds[2] = 0.0;
+ fail();
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ assertEquals("length=1; index=2", ex.getMessage());
+ }
+
+ // aget-wide
+ try {
+ double d = ds[2];
+ fail();
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ assertEquals("length=1; index=2", ex.getMessage());
+ }
+
+ // aput-object
+ try {
+ os[2] = null;
+ fail();
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ assertEquals("length=1; index=2", ex.getMessage());
+ }
+
+ // aget-object
+ try {
+ Object o = os[2];
+ fail();
+ } catch (ArrayIndexOutOfBoundsException ex) {
+ assertEquals("length=1; index=2", ex.getMessage());
+ }
+ }
+
+ private static void arrayStore() throws Exception {
+ try {
+ Object[] array = new String[10];
+ Object o = new Exception();
+ array[0] = o;
+ fail();
+ } catch (ArrayStoreException ex) {
+ assertEquals("java.lang.Exception cannot be stored in an array of type java.lang.String[]",
+ ex.getMessage());
+ }
+
+ try {
+ Object[] array = new C[10][];
+ Object o = new Integer(5);
+ array[0] = o;
+ fail();
+ } catch (ArrayStoreException ex) {
+ assertEquals("java.lang.Integer cannot be stored in an array of type Main$C[][]",
+ ex.getMessage());
+ }
+
+ try {
+ Object[] array = new Float[10][];
+ Object o = new C[4];
+ array[0] = o;
+ fail();
+ } catch (ArrayStoreException ex) {
+ assertEquals("Main$C[] cannot be stored in an array of type java.lang.Float[][]",
+ ex.getMessage());
+ }
+
+ try {
+ String[] src = new String[] { null, null, null, null, "hello", "goodbye" };
+ Integer[] dst = new Integer[10];
+ System.arraycopy(src, 1, dst, 0, 5);
+ } catch (ArrayStoreException ex) {
+ assertEquals("source[4] of type java.lang.String cannot be stored in destination array of type java.lang.Integer[]",
+ ex.getMessage());
+ }
+
+ try {
+ String[] src = new String[1];
+ int[] dst = new int[1];
+ System.arraycopy(src, 0, dst, 0, 1);
+ } catch (ArrayStoreException ex) {
+ assertEquals("Incompatible types: src=java.lang.String[], dst=int[]", ex.getMessage());
+ }
+
+ try {
+ float[] src = new float[1];
+ Runnable[] dst = new Runnable[1];
+ System.arraycopy(src, 0, dst, 0, 1);
+ } catch (ArrayStoreException ex) {
+ assertEquals("Incompatible types: src=float[], dst=java.lang.Runnable[]", ex.getMessage());
+ }
+
+ try {
+ boolean[] src = new boolean[1];
+ double[][] dst = new double[1][];
+ System.arraycopy(src, 0, dst, 0, 1);
+ } catch (ArrayStoreException ex) {
+ assertEquals("Incompatible types: src=boolean[], dst=double[][]", ex.getMessage());
+ }
+
+ try {
+ String src = "hello";
+ Object[] dst = new Object[1];
+ System.arraycopy(src, 0, dst, 0, 1);
+ } catch (ArrayStoreException ex) {
+ assertEquals("source of type java.lang.String is not an array", ex.getMessage());
+ }
+
+ try {
+ Object[] src = new Object[1];
+ Integer dst = new Integer(5);
+ System.arraycopy(src, 0, dst, 0, 1);
+ } catch (ArrayStoreException ex) {
+ assertEquals("destination of type java.lang.Integer is not an array", ex.getMessage());
+ }
+
+ // This test demonstrates that the exception message complains
+ // about the source in cases where neither source nor
+ // destination is an array.
+ try {
+ System.arraycopy(new C(), 0, "hello", 0, 1);
+ } catch (ArrayStoreException ex) {
+ assertEquals("source of type Main$C is not an array", ex.getMessage());
+ }
+ }
+
+ private static void classCast() throws Exception {
+ // Reference types.
+ try {
+ Object o = new Exception();
+ String s = (String) o;
+ fail();
+ } catch (ClassCastException ex) {
+ assertEquals("java.lang.Exception cannot be cast to java.lang.String", ex.getMessage());
+ }
+
+ // Arrays of reference types.
+ try {
+ Object o = (C) makeArray(String.class);
+ fail();
+ } catch (ClassCastException ex) {
+ assertEquals("java.lang.String[] cannot be cast to Main$C", ex.getMessage());
+ }
+
+ // Arrays of primitives.
+ try {
+ Object o = (C) makeArray(float.class);
+ fail();
+ } catch (ClassCastException ex) {
+ assertEquals("float[] cannot be cast to Main$C", ex.getMessage());
+ }
+
+ // Multi-dimensional arrays of primitives.
+ try {
+ Object o = (C) makeArray(char[].class);
+ fail();
+ } catch (ClassCastException ex) {
+ assertEquals("char[][] cannot be cast to Main$C", ex.getMessage());
+ }
+
+ // Multi-dimensional arrays of references.
+ try {
+ Object o = (Object[][][]) makeInteger();
+ fail();
+ } catch (ClassCastException ex) {
+ assertEquals("java.lang.Integer cannot be cast to java.lang.Object[][][]", ex.getMessage());
+ }
+ }
+
+ static class C { }
+
+ /**
+ * Helper for testCastOperator and testCastOperatorWithArrays. It's important that the
+ * return type is Object, since otherwise the compiler will just reject the code.
+ */
+ private static Object makeInteger() {
+ return new Integer(5);
+ }
+
+ /**
+ * Helper for testCastOperatorWithArrays. It's important that
+ * the return type is Object.
+ */
+ private static Object makeArray(Class c) {
+ return Array.newInstance(c, 1);
+ }
+
+ private static void classNotFound() throws Exception {
+ try {
+ // There is no such thing as an array of void.
+ Class.forName("[V");
+ fail();
+ } catch (ClassNotFoundException ex) {
+ assertEquals("Invalid name: [V", ex.getMessage());
+ }
+
+ try {
+ // This class name is valid, but doesn't exist.
+ Class.forName("package.Class");
+ fail();
+ } catch (ClassNotFoundException ex) {
+ assertEquals("package.Class", ex.getMessage());
+ }
+
+ try {
+ // This array class name is valid, but the type doesn't exist.
+ Class.forName("[[Lpackage.Class;");
+ fail();
+ } catch (ClassNotFoundException ex) {
+ assertEquals("[[Lpackage.Class;", ex.getMessage());
+ }
+ }
+
+ private static void reflection() throws Exception {
+ // Can't assign Integer to a String field.
+ try {
+ Field field = A.class.getField("b");
+ field.set(new A(), 5);
+ fail();
+ } catch (IllegalArgumentException expected) {
+ assertEquals("field A.b has type java.lang.String, got java.lang.Integer", expected.getMessage());
+ }
+
+ // Can't unbox null to a primitive.
+ try {
+ Field field = A.class.getField("i");
+ field.set(new A(), null);
+ fail();
+ } catch (IllegalArgumentException expected) {
+ assertEquals("field A.i has type int, got null", expected.getMessage());
+ }
+
+ // Can't unbox String to a primitive.
+ try {
+ Field field = A.class.getField("i");
+ field.set(new A(), "hello, world!");
+ fail();
+ } catch (IllegalArgumentException expected) {
+ assertEquals("field A.i has type int, got java.lang.String", expected.getMessage());
+ }
+
+ // Can't pass an Integer as a String.
+ try {
+ Method m = A.class.getMethod("m", int.class, String.class);
+ m.invoke(new A(), 2, 2);
+ fail();
+ } catch (IllegalArgumentException expected) {
+ assertEquals("method A.m argument 2 has type java.lang.String, got java.lang.Integer", expected.getMessage());
+ }
+
+ // Can't pass null as an int.
+ try {
+ Method m = A.class.getMethod("m", int.class, String.class);
+ m.invoke(new A(), null, "");
+ fail();
+ } catch (IllegalArgumentException expected) {
+ assertEquals("method A.m argument 1 has type int, got null", expected.getMessage());
+ }
+ }
+
+ private static void stringIndex() throws Exception {
+ // charAt too small.
+ try {
+ "hello".charAt(-1);
+ fail();
+ } catch (StringIndexOutOfBoundsException ex) {
+ assertEquals("length=5; index=-1", ex.getMessage());
+ }
+
+ // charAt too big.
+ try {
+ "hello".charAt(7);
+ fail();
+ } catch (StringIndexOutOfBoundsException ex) {
+ assertEquals("length=5; index=7", ex.getMessage());
+ }
+
+ // substring too big.
+ try {
+ "hello there".substring(9,14);
+ fail();
+ } catch (StringIndexOutOfBoundsException ex) {
+ assertEquals("length=11; regionStart=9; regionLength=5", ex.getMessage());
+ }
+ }
+}
+
+class A {
+ public String b;
+ public int i;
+ public void m(int i, String s) {}
+}