You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by te...@apache.org on 2006/03/15 12:47:39 UTC
svn commit: r386058 [24/49] - in /incubator/harmony/enhanced/classlib/trunk:
make/ modules/archive/make/common/ modules/archive/src/test/java/tests/
modules/archive/src/test/java/tests/api/
modules/archive/src/test/java/tests/api/java/ modules/archive/...
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ArrayTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ArrayTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ArrayTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ArrayTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,735 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * 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.
+ */
+
+package tests.api.java.lang.reflect;
+
+import java.lang.reflect.Array;
+
+public class ArrayTest extends junit.framework.TestCase {
+
+ /**
+ * @tests java.lang.reflect.Array#get(java.lang.Object, int)
+ */
+ public void test_getLjava_lang_ObjectI() {
+ // Test for method java.lang.Object
+ // java.lang.reflect.Array.get(java.lang.Object, int)
+
+ int[] x = { 1 };
+ Object ret = null;
+ boolean thrown = false;
+ try {
+ ret = Array.get(x, 0);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value",
+ ((Integer) ret).intValue() == 1);
+ try {
+ ret = Array.get(new Object(), 0);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ ret = Array.get(x, 4);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#getBoolean(java.lang.Object, int)
+ */
+ public void test_getBooleanLjava_lang_ObjectI() {
+ // Test for method boolean
+ // java.lang.reflect.Array.getBoolean(java.lang.Object, int)
+ boolean[] x = { true };
+ boolean ret = false;
+ boolean thrown = false;
+ try {
+ ret = Array.getBoolean(x, 0);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", ret);
+ try {
+ ret = Array.getBoolean(new Object(), 0);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ ret = Array.getBoolean(x, 4);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#getByte(java.lang.Object, int)
+ */
+ public void test_getByteLjava_lang_ObjectI() {
+ // Test for method byte
+ // java.lang.reflect.Array.getByte(java.lang.Object, int)
+ byte[] x = { 1 };
+ byte ret = 0;
+ boolean thrown = false;
+ try {
+ ret = Array.getByte(x, 0);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", ret == 1);
+ try {
+ ret = Array.getByte(new Object(), 0);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ ret = Array.getByte(x, 4);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#getChar(java.lang.Object, int)
+ */
+ public void test_getCharLjava_lang_ObjectI() {
+ // Test for method char
+ // java.lang.reflect.Array.getChar(java.lang.Object, int)
+ char[] x = { 1 };
+ char ret = 0;
+ boolean thrown = false;
+ try {
+ ret = Array.getChar(x, 0);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", ret == 1);
+ try {
+ ret = Array.getChar(new Object(), 0);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ ret = Array.getChar(x, 4);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#getDouble(java.lang.Object, int)
+ */
+ public void test_getDoubleLjava_lang_ObjectI() {
+ // Test for method double
+ // java.lang.reflect.Array.getDouble(java.lang.Object, int)
+ double[] x = { 1 };
+ double ret = 0;
+ boolean thrown = false;
+ try {
+ ret = Array.getDouble(x, 0);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", ret == 1);
+ try {
+ ret = Array.getDouble(new Object(), 0);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ ret = Array.getDouble(x, 4);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#getFloat(java.lang.Object, int)
+ */
+ public void test_getFloatLjava_lang_ObjectI() {
+ // Test for method float
+ // java.lang.reflect.Array.getFloat(java.lang.Object, int)
+ float[] x = { 1 };
+ float ret = 0;
+ boolean thrown = false;
+ try {
+ ret = Array.getFloat(x, 0);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", ret == 1);
+ try {
+ ret = Array.getFloat(new Object(), 0);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ ret = Array.getFloat(x, 4);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#getInt(java.lang.Object, int)
+ */
+ public void test_getIntLjava_lang_ObjectI() {
+ // Test for method int java.lang.reflect.Array.getInt(java.lang.Object,
+ // int)
+ int[] x = { 1 };
+ int ret = 0;
+ boolean thrown = false;
+ try {
+ ret = Array.getInt(x, 0);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", ret == 1);
+ try {
+ ret = Array.getInt(new Object(), 0);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ ret = Array.getInt(x, 4);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#getLength(java.lang.Object)
+ */
+ public void test_getLengthLjava_lang_Object() {
+ // Test for method int
+ // java.lang.reflect.Array.getLength(java.lang.Object)
+ long[] x = { 1 };
+
+ assertTrue("Returned incorrect length", Array.getLength(x) == 1);
+ assertTrue("Returned incorrect length", Array
+ .getLength(new Object[10000]) == 10000);
+ try {
+ Array.getLength(new Object());
+ } catch (IllegalArgumentException e) {
+ // Correct
+ return;
+ }
+ fail("Failed to throw exception when passed non-array");
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#getLong(java.lang.Object, int)
+ */
+ public void test_getLongLjava_lang_ObjectI() {
+ // Test for method long
+ // java.lang.reflect.Array.getLong(java.lang.Object, int)
+ long[] x = { 1 };
+ long ret = 0;
+ boolean thrown = false;
+ try {
+ ret = Array.getLong(x, 0);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", ret == 1);
+ try {
+ ret = Array.getLong(new Object(), 0);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ ret = Array.getLong(x, 4);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#getShort(java.lang.Object, int)
+ */
+ public void test_getShortLjava_lang_ObjectI() {
+ // Test for method short
+ // java.lang.reflect.Array.getShort(java.lang.Object, int)
+ short[] x = { 1 };
+ short ret = 0;
+ boolean thrown = false;
+ try {
+ ret = Array.getShort(x, 0);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", ret == 1);
+ try {
+ ret = Array.getShort(new Object(), 0);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ ret = Array.getShort(x, 4);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#newInstance(java.lang.Class, int[])
+ */
+ public void test_newInstanceLjava_lang_Class$I() {
+ // Test for method java.lang.Object
+ // java.lang.reflect.Array.newInstance(java.lang.Class, int [])
+ int[][] x;
+ int[] y = { 2 };
+
+ x = (int[][]) Array.newInstance(int[].class, y);
+ assertTrue("Failed to instantiate array properly", x.length == 2);
+
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#newInstance(java.lang.Class, int)
+ */
+ public void test_newInstanceLjava_lang_ClassI() {
+ // Test for method java.lang.Object
+ // java.lang.reflect.Array.newInstance(java.lang.Class, int)
+ int[] x;
+
+ x = (int[]) Array.newInstance(int.class, 100);
+ assertTrue("Failed to instantiate array properly", x.length == 100);
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#set(java.lang.Object, int,
+ * java.lang.Object)
+ */
+ public void test_setLjava_lang_ObjectILjava_lang_Object() {
+ // Test for method void java.lang.reflect.Array.set(java.lang.Object,
+ // int, java.lang.Object)
+ int[] x = { 0 };
+ boolean thrown = false;
+ try {
+ Array.set(x, 0, new Integer(1));
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", ((Integer) Array.get(x, 0))
+ .intValue() == 1);
+ try {
+ Array.set(new Object(), 0, new Object());
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ Array.set(x, 4, new Integer(1));
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+
+ // trying to put null in a primitive array causes
+ // a NullPointerException in 1.4
+ boolean exception = false;
+ try {
+ Array.set(new int[1], 0, null);
+ } catch (NullPointerException e) {
+ exception = true;
+ }
+ assertTrue("expected exception not thrown", exception);
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#setBoolean(java.lang.Object, int, boolean)
+ */
+ public void test_setBooleanLjava_lang_ObjectIZ() {
+ // Test for method void
+ // java.lang.reflect.Array.setBoolean(java.lang.Object, int, boolean)
+ boolean[] x = { false };
+ boolean thrown = false;
+ try {
+ Array.setBoolean(x, 0, true);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Failed to set correct value", Array.getBoolean(x, 0));
+ try {
+ Array.setBoolean(new Object(), 0, false);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown){
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ Array.setBoolean(x, 4, false);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#setByte(java.lang.Object, int, byte)
+ */
+ public void test_setByteLjava_lang_ObjectIB() {
+ // Test for method void
+ // java.lang.reflect.Array.setByte(java.lang.Object, int, byte)
+ byte[] x = { 0 };
+ boolean thrown = false;
+ try {
+ Array.setByte(x, 0, (byte) 1);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", Array.getByte(x, 0) == 1);
+ try {
+ Array.setByte(new Object(), 0, (byte) 9);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ Array.setByte(x, 4, (byte) 9);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#setChar(java.lang.Object, int, char)
+ */
+ public void test_setCharLjava_lang_ObjectIC() {
+ // Test for method void
+ // java.lang.reflect.Array.setChar(java.lang.Object, int, char)
+ char[] x = { 0 };
+ boolean thrown = false;
+ try {
+ Array.setChar(x, 0, (char) 1);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", Array.getChar(x, 0) == 1);
+ try {
+ Array.setChar(new Object(), 0, (char) 9);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ Array.setChar(x, 4, (char) 9);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#setDouble(java.lang.Object, int, double)
+ */
+ public void test_setDoubleLjava_lang_ObjectID() {
+ // Test for method void
+ // java.lang.reflect.Array.setDouble(java.lang.Object, int, double)
+ double[] x = { 0 };
+ boolean thrown = false;
+ try {
+ Array.setDouble(x, 0, (double) 1);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", Array.getDouble(x, 0) == 1);
+ try {
+ Array.setDouble(new Object(), 0, (double) 9);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ Array.setDouble(x, 4, (double) 9);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#setFloat(java.lang.Object, int, float)
+ */
+ public void test_setFloatLjava_lang_ObjectIF() {
+ // Test for method void
+ // java.lang.reflect.Array.setFloat(java.lang.Object, int, float)
+ float[] x = { 0.0f };
+ boolean thrown = false;
+ try {
+ Array.setFloat(x, 0, (float) 1);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", Array.getFloat(x, 0) == 1);
+ try {
+ Array.setFloat(new Object(), 0, (float) 9);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ Array.setFloat(x, 4, (float) 9);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#setInt(java.lang.Object, int, int)
+ */
+ public void test_setIntLjava_lang_ObjectII() {
+ // Test for method void java.lang.reflect.Array.setInt(java.lang.Object,
+ // int, int)
+ int[] x = { 0 };
+ boolean thrown = false;
+ try {
+ Array.setInt(x, 0, (int) 1);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", Array.getInt(x, 0) == 1);
+ try {
+ Array.setInt(new Object(), 0, (int) 9);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ Array.setInt(x, 4, (int) 9);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#setLong(java.lang.Object, int, long)
+ */
+ public void test_setLongLjava_lang_ObjectIJ() {
+ // Test for method void
+ // java.lang.reflect.Array.setLong(java.lang.Object, int, long)
+ long[] x = { 0 };
+ boolean thrown = false;
+ try {
+ Array.setLong(x, 0, (long) 1);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", Array.getLong(x, 0) == 1);
+ try {
+ Array.setLong(new Object(), 0, (long) 9);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ Array.setLong(x, 4, (long) 9);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Array#setShort(java.lang.Object, int, short)
+ */
+ public void test_setShortLjava_lang_ObjectIS() {
+ // Test for method void
+ // java.lang.reflect.Array.setShort(java.lang.Object, int, short)
+ short[] x = { 0 };
+ boolean thrown = false;
+ try {
+ Array.setShort(x, 0, (short) 1);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Get returned incorrect value", Array.getShort(x, 0) == 1);
+ try {
+ Array.setShort(new Object(), 0, (short) 9);
+ } catch (IllegalArgumentException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Passing non-array failed to throw exception");
+ }
+ thrown = false;
+ try {
+ Array.setShort(x, 4, (short) 9);
+ } catch (ArrayIndexOutOfBoundsException e) {
+ // Correct behaviour
+ thrown = true;
+ }
+ if (!thrown) {
+ fail("Invalid index failed to throw exception");
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ConstructorTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ConstructorTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ConstructorTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ConstructorTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,251 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * 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.
+ */
+
+package tests.api.java.lang.reflect;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Modifier;
+
+public class ConstructorTest extends junit.framework.TestCase {
+
+ static class ConstructorTestHelper extends Object {
+ int cval;
+
+ public ConstructorTestHelper() throws IndexOutOfBoundsException {
+ cval = 99;
+ }
+
+ public ConstructorTestHelper(Object x) {
+ }
+
+ private ConstructorTestHelper(int a) {
+ }
+
+ protected ConstructorTestHelper(long a) {
+ }
+
+ public int check() {
+ return cval;
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Constructor#equals(java.lang.Object)
+ */
+ public void test_equalsLjava_lang_Object() {
+ // Test for method boolean
+ // java.lang.reflect.Constructor.equals(java.lang.Object)
+ Class[] types = null;
+ Constructor ctor1 = null, ctor2 = null;
+ try {
+ ctor1 = new ConstructorTestHelper().getClass().getConstructor(
+ new Class[0]);
+
+ Class[] parms = null;
+ parms = new Class[1];
+ parms[0] = new Object().getClass();
+ ctor2 = new ConstructorTestHelper().getClass()
+ .getConstructor(parms);
+ } catch (Exception e) {
+ fail("Exception during equals test : " + e.getMessage());
+ }
+ assertTrue("Different Contructors returned equal", !ctor1.equals(ctor2));
+ }
+
+ /**
+ * @tests java.lang.reflect.Constructor#getDeclaringClass()
+ */
+ public void test_getDeclaringClass() {
+ // Test for method java.lang.Class
+ // java.lang.reflect.Constructor.getDeclaringClass()
+ boolean val = false;
+ try {
+ Class pclass = new ConstructorTestHelper().getClass();
+ Constructor ctor = pclass.getConstructor(new Class[0]);
+ val = ctor.getDeclaringClass().equals(pclass);
+ } catch (Exception e) {
+ fail("Exception during test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect declaring class", val);
+ }
+
+ /**
+ * @tests java.lang.reflect.Constructor#getExceptionTypes()
+ */
+ public void test_getExceptionTypes() {
+ // Test for method java.lang.Class []
+ // java.lang.reflect.Constructor.getExceptionTypes()
+ Class[] exceptions = null;
+ Class ex = null;
+ try {
+ Constructor ctor = new ConstructorTestHelper().getClass()
+ .getConstructor(new Class[0]);
+ exceptions = ctor.getExceptionTypes();
+ ex = new IndexOutOfBoundsException().getClass();
+ } catch (Exception e) {
+ fail("Exception during test : " + e.getMessage());
+ }
+ assertTrue("Returned exception list of incorrect length",
+ exceptions.length == 1);
+ assertTrue("Returned incorrect exception", exceptions[0].equals(ex));
+ }
+
+ /**
+ * @tests java.lang.reflect.Constructor#getModifiers()
+ */
+ public void test_getModifiers() {
+ // Test for method int java.lang.reflect.Constructor.getModifiers()
+ int mod = 0;
+ try {
+ Constructor ctor = new ConstructorTestHelper().getClass()
+ .getConstructor(new Class[0]);
+ mod = ctor.getModifiers();
+ assertTrue("Returned incorrect modifers for public ctor",
+ ((mod & Modifier.PUBLIC) == Modifier.PUBLIC)
+ && ((mod & Modifier.PRIVATE) == 0));
+ } catch (NoSuchMethodException e) {
+ fail("Exception during test : " + e.getMessage());
+ }
+ try {
+ Class[] cl = { int.class };
+ Constructor ctor = new ConstructorTestHelper().getClass()
+ .getDeclaredConstructor(cl);
+ mod = ctor.getModifiers();
+ assertTrue("Returned incorrect modifers for private ctor",
+ ((mod & Modifier.PRIVATE) == Modifier.PRIVATE)
+ && ((mod & Modifier.PUBLIC) == 0));
+ } catch (NoSuchMethodException e) {
+ fail("Exception during test : " + e.getMessage());
+ }
+ try {
+ Class[] cl = { long.class };
+ Constructor ctor = new ConstructorTestHelper().getClass()
+ .getDeclaredConstructor(cl);
+ mod = ctor.getModifiers();
+ assertTrue("Returned incorrect modifers for private ctor",
+ ((mod & Modifier.PROTECTED) == Modifier.PROTECTED)
+ && ((mod & Modifier.PUBLIC) == 0));
+ } catch (NoSuchMethodException e) {
+ fail("NoSuchMethodException during test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Constructor#getName()
+ */
+ public void test_getName() {
+ // Test for method java.lang.String
+ // java.lang.reflect.Constructor.getName()
+ try {
+ Constructor ctor = new ConstructorTestHelper().getClass()
+ .getConstructor(new Class[0]);
+ assertTrue(
+ "Returned incorrect name: " + ctor.getName(),
+ ctor
+ .getName()
+ .equals(
+ "tests.api.java.lang.reflect.ConstructorTest$ConstructorTestHelper"));
+ } catch (Exception e) {
+ fail("Exception obtaining contructor : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Constructor#getParameterTypes()
+ */
+ public void test_getParameterTypes() {
+ // Test for method java.lang.Class []
+ // java.lang.reflect.Constructor.getParameterTypes()
+ Class[] types = null;
+ try {
+ Constructor ctor = new ConstructorTestHelper().getClass()
+ .getConstructor(new Class[0]);
+ types = ctor.getParameterTypes();
+ } catch (Exception e) {
+ fail("Exception during getParameterTypes test:"
+ + e.toString());
+ }
+ assertTrue("Incorrect paramter returned", types.length == 0);
+
+ Class[] parms = null;
+ try {
+ parms = new Class[1];
+ parms[0] = new Object().getClass();
+ Constructor ctor = new ConstructorTestHelper().getClass()
+ .getConstructor(parms);
+ types = ctor.getParameterTypes();
+ } catch (Exception e) {
+ fail("Exception during getParameterTypes test:"
+ + e.toString());
+ }
+ assertTrue("Incorrect paramter returned", types[0].equals(parms[0]));
+ }
+
+ /**
+ * @tests java.lang.reflect.Constructor#newInstance(java.lang.Object[])
+ */
+ public void test_newInstance$Ljava_lang_Object() {
+ // Test for method java.lang.Object
+ // java.lang.reflect.Constructor.newInstance(java.lang.Object [])
+
+ ConstructorTestHelper test = null;
+ try {
+ Constructor ctor = new ConstructorTestHelper().getClass()
+ .getConstructor(new Class[0]);
+ test = (ConstructorTestHelper) ctor.newInstance(null);
+ } catch (Exception e) {
+ fail("Failed to create instance : " + e.getMessage());
+ }
+ assertTrue("improper instance created", test.check() == 99);
+ }
+
+ /**
+ * @tests java.lang.reflect.Constructor#toString()
+ */
+ public void test_toString() {
+ // Test for method java.lang.String
+ // java.lang.reflect.Constructor.toString()
+ Class[] parms = null;
+ Constructor ctor = null;
+ try {
+ parms = new Class[1];
+ parms[0] = new Object().getClass();
+ ctor = new ConstructorTestHelper().getClass().getConstructor(parms);
+ } catch (Exception e) {
+ fail("Exception during getParameterTypes test:"
+ + e.toString());
+ }
+ assertTrue(
+ "Returned incorrect string representation: " + ctor.toString(),
+ ctor
+ .toString()
+ .equals(
+ "public tests.api.java.lang.reflect.ConstructorTest$ConstructorTestHelper(java.lang.Object)"));
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/FieldTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/FieldTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/FieldTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/FieldTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,1207 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * 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.
+ */
+
+package tests.api.java.lang.reflect;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.Modifier;
+
+import tests.support.Support_Field;
+
+public class FieldTest extends junit.framework.TestCase {
+
+ static class TestField {
+ public static int pubfield1;
+
+ protected static double doubleSField = Double.MAX_VALUE;
+
+ private static int privfield1;
+
+ protected int intField = Integer.MAX_VALUE;
+
+ protected short shortField = Short.MAX_VALUE;
+
+ protected boolean booleanField = true;
+
+ protected byte byteField = Byte.MAX_VALUE;
+
+ protected long longField = Long.MAX_VALUE;
+
+ protected double doubleField = Double.MAX_VALUE;
+
+ protected float floatField = Float.MAX_VALUE;
+
+ protected char charField = 'T';
+
+ protected final int intFField = Integer.MAX_VALUE;
+
+ protected final short shortFField = Short.MAX_VALUE;
+
+ protected final boolean booleanFField = true;
+
+ protected final byte byteFField = Byte.MAX_VALUE;
+
+ protected final long longFField = Long.MAX_VALUE;
+
+ protected final double doubleFField = Double.MAX_VALUE;
+
+ protected final float floatFField = Float.MAX_VALUE;
+
+ protected final char charFField = 'T';
+
+ private static final int x = 1;
+
+ public volatile transient int y = 0;
+
+ protected static transient volatile int prsttrvol = 99;
+ }
+
+ public class TestFieldSub1 extends TestField {
+ }
+
+ public class TestFieldSub2 extends TestField {
+ }
+
+ static class A {
+ protected short shortField = Short.MAX_VALUE;
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#equals(java.lang.Object)
+ */
+ public void test_equalsLjava_lang_Object() {
+ // Test for method boolean
+ // java.lang.reflect.Field.equals(java.lang.Object)
+ TestField x = new TestField();
+ Field f = null;
+ try {
+ f = x.getClass().getDeclaredField("shortField");
+ } catch (Exception e) {
+ fail("Exception during getType test : " + e.getMessage());
+ }
+ try {
+ assertTrue("Same Field returned false", f.equals(f));
+ assertTrue("Inherited Field returned false", f.equals(x.getClass()
+ .getDeclaredField("shortField")));
+ assertTrue("Identical Field from different class returned true", !f
+ .equals(A.class.getDeclaredField("shortField")));
+ } catch (Exception e) {
+ fail("Exception during getType test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#get(java.lang.Object)
+ */
+ public void test_getLjava_lang_Object() {
+ // Test for method java.lang.Object
+ // java.lang.reflect.Field.get(java.lang.Object)
+ TestField x = new TestField();
+ Field f = null;
+ Double val = new Double(0.0);
+ boolean exFlag = true;
+ try {
+ f = x.getClass().getDeclaredField("doubleField");
+ val = (Double) f.get(x);
+ } catch (Exception e) {
+ fail("Exception during get test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect double field value",
+ val.doubleValue() == Double.MAX_VALUE);
+ try {
+ // Test getting a static field;
+ f = x.getClass().getDeclaredField("doubleSField");
+ f.set(x, new Double(1.0));
+ val = (Double) f.get(x);
+ assertTrue("Returned incorrect double field value", val
+ .doubleValue() == 1.0);
+
+ // Try a get on a private field
+ try {
+ f = x.getClass().getDeclaredField("privfield1");
+ f.get(x);
+ } catch (IllegalAccessException ex) {
+ // Correct - An exception should be thrown here.
+ exFlag = false;
+ }
+
+ if (exFlag) {
+ fail("No expected IllegalAccessException during get tests");
+ } else {
+ exFlag = true;
+ }
+
+ // Try a get using an invalid class.
+ try {
+ f = x.getClass().getDeclaredField("doubleField");
+ f.get(new String());
+ } catch (IllegalArgumentException exc) {
+ // Correct - Passed an Object that does not declare or inherit f
+ exFlag = false;
+ }
+ } catch (Exception e) {
+ fail("Exception during get tests : " + e.getMessage());
+ }
+
+ if (exFlag) {
+ fail("No expected IllegalArgumentException during get tests");
+ }
+ }
+
+ class SupportSubClass extends Support_Field {
+
+ Object getField(char primitiveType, Object o, Field f,
+ Class expectedException) {
+ Object res = null;
+ try {
+ primitiveType = Character.toUpperCase(primitiveType);
+ switch (primitiveType) {
+ case 'I': // int
+ res = new Integer(f.getInt(o));
+ break;
+ case 'J': // long
+ res = new Long(f.getLong(o));
+ break;
+ case 'Z': // boolean
+ res = new Boolean(f.getBoolean(o));
+ break;
+ case 'S': // short
+ res = new Short(f.getShort(o));
+ break;
+ case 'B': // byte
+ res = new Byte(f.getByte(o));
+ break;
+ case 'C': // char
+ res = new Character(f.getChar(o));
+ break;
+ case 'D': // double
+ res = new Double(f.getDouble(o));
+ break;
+ case 'F': // float
+ res = new Float(f.getFloat(o));
+ break;
+ default:
+ res = f.get(o);
+ }
+ if (expectedException != null) {
+ fail("expected exception " + expectedException.getName());
+ }
+ } catch (Exception e) {
+ if (expectedException == null) {
+ fail("unexpected exception " + e);
+ } else {
+ assertTrue("expected exception "
+ + expectedException.getName() + " and got " + e, e
+ .getClass().equals(expectedException));
+ }
+ }
+ return res;
+ }
+
+ void setField(char primitiveType, Object o, Field f,
+ Class expectedException, Object value) {
+ try {
+ primitiveType = Character.toUpperCase(primitiveType);
+ switch (primitiveType) {
+ case 'I': // int
+ f.setInt(o, ((Integer) value).intValue());
+ break;
+ case 'J': // long
+ f.setLong(o, ((Long) value).longValue());
+ break;
+ case 'Z': // boolean
+ f.setBoolean(o, ((Boolean) value).booleanValue());
+ break;
+ case 'S': // short
+ f.setShort(o, ((Short) value).shortValue());
+ break;
+ case 'B': // byte
+ f.setByte(o, ((Byte) value).byteValue());
+ break;
+ case 'C': // char
+ f.setChar(o, ((Character) value).charValue());
+ break;
+ case 'D': // double
+ f.setDouble(o, ((Double) value).doubleValue());
+ break;
+ case 'F': // float
+ f.setFloat(o, ((Float) value).floatValue());
+ break;
+ default:
+ f.set(o, value);
+ }
+ if (expectedException != null) {
+ fail("expected exception " + expectedException.getName());
+ }
+ } catch (Exception e) {
+ if (expectedException == null) {
+ fail("unexpected exception " + e);
+ } else {
+ assertTrue("expected exception "
+ + expectedException.getName() + " and got " + e, e
+ .getClass().equals(expectedException));
+ }
+ }
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#get(java.lang.Object)
+ * @tests java.lang.reflect.Field#getByte(java.lang.Object)
+ * @tests java.lang.reflect.Field#getBoolean(java.lang.Object)
+ * @tests java.lang.reflect.Field#getShort(java.lang.Object)
+ * @tests java.lang.reflect.Field#getInt(java.lang.Object)
+ * @tests java.lang.reflect.Field#getLong(java.lang.Object)
+ * @tests java.lang.reflect.Field#getFloat(java.lang.Object)
+ * @tests java.lang.reflect.Field#getDouble(java.lang.Object)
+ * @tests java.lang.reflect.Field#getChar(java.lang.Object)
+ * @tests java.lang.reflect.Field#set(java.lang.Object, java.lang.Object)
+ * @tests java.lang.reflect.Field#setByte(java.lang.Object, byte)
+ * @tests java.lang.reflect.Field#setBoolean(java.lang.Object, boolean)
+ * @tests java.lang.reflect.Field#setShort(java.lang.Object, short)
+ * @tests java.lang.reflect.Field#setInt(java.lang.Object, int)
+ * @tests java.lang.reflect.Field#setLong(java.lang.Object, long)
+ * @tests java.lang.reflect.Field#setFloat(java.lang.Object, float)
+ * @tests java.lang.reflect.Field#setDouble(java.lang.Object, double)
+ * @tests java.lang.reflect.Field#setChar(java.lang.Object, char)
+ */
+ public void testProtectedFieldAccess() {
+ Class fieldClass = new Support_Field().getClass();
+ String fieldName = null;
+ Field objectField = null;
+ Field booleanField = null;
+ Field byteField = null;
+ Field charField = null;
+ Field shortField = null;
+ Field intField = null;
+ Field longField = null;
+ Field floatField = null;
+ Field doubleField = null;
+ try {
+ fieldName = "objectField";
+ objectField = fieldClass.getDeclaredField(fieldName);
+
+ fieldName = "booleanField";
+ booleanField = fieldClass.getDeclaredField(fieldName);
+
+ fieldName = "byteField";
+ byteField = fieldClass.getDeclaredField(fieldName);
+
+ fieldName = "charField";
+ charField = fieldClass.getDeclaredField(fieldName);
+
+ fieldName = "shortField";
+ shortField = fieldClass.getDeclaredField(fieldName);
+
+ fieldName = "intField";
+ intField = fieldClass.getDeclaredField(fieldName);
+
+ fieldName = "longField";
+ longField = fieldClass.getDeclaredField(fieldName);
+
+ fieldName = "floatField";
+ floatField = fieldClass.getDeclaredField(fieldName);
+
+ fieldName = "doubleField";
+ doubleField = fieldClass.getDeclaredField(fieldName);
+ } catch (NoSuchFieldException e) {
+ fail("missing field " + fieldName + " in test support class "
+ + fieldClass.getName());
+ }
+
+ // create the various objects that might or might not have an instance
+ // of the field
+ Support_Field parentClass = new Support_Field();
+ SupportSubClass subclass = new SupportSubClass();
+ SupportSubClass otherSubclass = new SupportSubClass();
+ Object plainObject = new Object();
+
+ Class illegalAccessExceptionClass = new IllegalAccessException()
+ .getClass();
+ Class illegalArgumentExceptionClass = new IllegalArgumentException()
+ .getClass();
+
+ // The test will attempt to use pass an object to set for object, byte,
+ // short, ..., float and double fields
+ // and pass a byte to to setByte for byte, short, ..., float and double
+ // fields and so on.
+ // It will also test if IllegalArgumentException is thrown when the
+ // field does not exist in the given object and that
+ // IllegalAccessException is thrown when trying to access an
+ // inaccessible protected field.
+ // The test will also check that IllegalArgumentException is thrown for
+ // all other attempts.
+
+ // Ordered by widening conversion, except for 'L' at the beg (which
+ // stands for Object).
+ // If the object provided to set can be unwrapped to a primitive, then
+ // the set method can set
+ // primitive fields.
+ char types[] = { 'L', 'B', 'S', 'C', 'I', 'J', 'F', 'D' };
+ Field fields[] = { objectField, byteField, shortField, charField,
+ intField, longField, floatField, doubleField };
+ Object values[] = { new Byte((byte) 1), new Byte((byte) 1),
+ new Short((short) 1), new Character((char) 1), new Integer(1),
+ new Long(1), new Float(1), new Double(1) };
+
+ // test set methods
+ for (int i = 0; i < types.length; i++) {
+ char type = types[i];
+ Object value = values[i];
+ for (int j = i; j < fields.length; j++) {
+ Field field = fields[j];
+ fieldName = field.getName();
+ if (field == charField && type != 'C') {
+ // the exception is that bytes and shorts CANNOT be
+ // converted into chars even though chars CAN be
+ // converted into ints, longs, floats and doubles
+ subclass.setField(type, subclass, field,
+ illegalArgumentExceptionClass, value);
+ } else {
+ // setting type into field);
+ subclass.setField(type, subclass, field, null, value);
+ subclass.setField(type, otherSubclass, field, null, value);
+ subclass.setField(type, parentClass, field,
+ illegalAccessExceptionClass, value);
+ subclass.setField(type, plainObject, field,
+ illegalArgumentExceptionClass, value);
+ }
+ }
+ for (int j = 0; j < i; j++) {
+ Field field = fields[j];
+ fieldName = field.getName();
+ // not setting type into field);
+ subclass.setField(type, subclass, field,
+ illegalArgumentExceptionClass, value);
+ }
+ }
+
+ // test setBoolean
+ Boolean booleanValue = Boolean.TRUE;
+ subclass.setField('Z', subclass, booleanField, null, booleanValue);
+ subclass.setField('Z', otherSubclass, booleanField, null, booleanValue);
+ subclass.setField('Z', parentClass, booleanField,
+ illegalAccessExceptionClass, booleanValue);
+ subclass.setField('Z', plainObject, booleanField,
+ illegalArgumentExceptionClass, booleanValue);
+ for (int j = 0; j < fields.length; j++) {
+ Field listedField = fields[j];
+ fieldName = listedField.getName();
+ // not setting boolean into listedField
+ subclass.setField('Z', subclass, listedField,
+ illegalArgumentExceptionClass, booleanValue);
+ }
+ for (int i = 0; i < types.length; i++) {
+ char type = types[i];
+ Object value = values[i];
+ subclass.setField(type, subclass, booleanField,
+ illegalArgumentExceptionClass, value);
+ }
+
+ // We perform the analagous test on the get methods.
+
+ // ordered by widening conversion, except for 'L' at the end (which
+ // stands for Object), to which all primitives can be converted by
+ // wrapping
+ char newTypes[] = new char[] { 'B', 'S', 'C', 'I', 'J', 'F', 'D', 'L' };
+ Field newFields[] = { byteField, shortField, charField, intField,
+ longField, floatField, doubleField, objectField };
+ fields = newFields;
+ types = newTypes;
+ // test get methods
+ for (int i = 0; i < types.length; i++) {
+ char type = types[i];
+ for (int j = 0; j <= i; j++) {
+ Field field = fields[j];
+ fieldName = field.getName();
+ if (type == 'C' && field != charField) {
+ // the exception is that bytes and shorts CANNOT be
+ // converted into chars even though chars CAN be
+ // converted into ints, longs, floats and doubles
+ subclass.getField(type, subclass, field,
+ illegalArgumentExceptionClass);
+ } else {
+ // getting type from field
+ subclass.getField(type, subclass, field, null);
+ subclass.getField(type, otherSubclass, field, null);
+ subclass.getField(type, parentClass, field,
+ illegalAccessExceptionClass);
+ subclass.getField(type, plainObject, field,
+ illegalArgumentExceptionClass);
+ }
+ }
+ for (int j = i + 1; j < fields.length; j++) {
+ Field field = fields[j];
+ fieldName = field.getName();
+ subclass.getField(type, subclass, field,
+ illegalArgumentExceptionClass);
+ }
+ }
+
+ // test getBoolean
+ subclass.getField('Z', subclass, booleanField, null);
+ subclass.getField('Z', otherSubclass, booleanField, null);
+ subclass.getField('Z', parentClass, booleanField,
+ illegalAccessExceptionClass);
+ subclass.getField('Z', plainObject, booleanField,
+ illegalArgumentExceptionClass);
+ for (int j = 0; j < fields.length; j++) {
+ Field listedField = fields[j];
+ fieldName = listedField.getName();
+ // not getting boolean from listedField
+ subclass.getField('Z', subclass, listedField,
+ illegalArgumentExceptionClass);
+ }
+ for (int i = 0; i < types.length - 1; i++) {
+ char type = types[i];
+ subclass.getField(type, subclass, booleanField,
+ illegalArgumentExceptionClass);
+ }
+ Object res = subclass.getField('L', subclass, booleanField, null);
+ assertTrue("unexpected object " + res, res instanceof Boolean);
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getBoolean(java.lang.Object)
+ */
+ public void test_getBooleanLjava_lang_Object() {
+ // Test for method boolean
+ // java.lang.reflect.Field.getBoolean(java.lang.Object)
+
+ TestField x = new TestField();
+ Field f = null;
+ boolean val = false;
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ val = f.getBoolean(x);
+
+ } catch (Exception e) {
+ fail("Exception during getBoolean test: " + e.toString());
+ }
+ assertTrue("Returned incorrect boolean field value", val);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("doubleField");
+ f.getBoolean(x);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since doubleField is not a
+ // boolean type
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during getBoolean test: " + e.toString());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getByte(java.lang.Object)
+ */
+ public void test_getByteLjava_lang_Object() {
+ // Test for method byte
+ // java.lang.reflect.Field.getByte(java.lang.Object)
+ TestField x = new TestField();
+ Field f = null;
+ byte val = 0;
+ try {
+ f = x.getClass().getDeclaredField("byteField");
+ val = f.getByte(x);
+ } catch (Exception e) {
+ fail("Exception during getbyte test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect byte field value", val == Byte.MAX_VALUE);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.getByte(x);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since byteField is not a
+ // boolean type
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during getbyte test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getChar(java.lang.Object)
+ */
+ public void test_getCharLjava_lang_Object() {
+ // Test for method char
+ // java.lang.reflect.Field.getChar(java.lang.Object)
+ TestField x = new TestField();
+ Field f = null;
+ char val = 0;
+ try {
+ f = x.getClass().getDeclaredField("charField");
+ val = f.getChar(x);
+ } catch (Exception e) {
+ fail("Exception during getCharacter test: " + e.toString());
+ }
+ assertTrue("Returned incorrect char field value", val == 'T');
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.getChar(x);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since charField is not a
+ // boolean type
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during getchar test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getDeclaringClass()
+ */
+ public void test_getDeclaringClass() {
+ // Test for method java.lang.Class
+ // java.lang.reflect.Field.getDeclaringClass()
+ Field[] fields;
+
+ try {
+ fields = new TestField().getClass().getFields();
+ assertTrue("Returned incorrect declaring class", fields[0]
+ .getDeclaringClass().equals(new TestField().getClass()));
+
+ // Check the case where the field is inherited to be sure the parent
+ // is returned as the declarator
+ fields = new TestFieldSub1().getClass().getFields();
+ assertTrue("Returned incorrect declaring class", fields[0]
+ .getDeclaringClass().equals(new TestField().getClass()));
+ } catch (Exception e) {
+ fail("Exception : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getDouble(java.lang.Object)
+ */
+ public void test_getDoubleLjava_lang_Object() {
+ // Test for method double
+ // java.lang.reflect.Field.getDouble(java.lang.Object)
+ TestField x = new TestField();
+ Field f = null;
+ double val = 0.0;
+ try {
+ f = x.getClass().getDeclaredField("doubleField");
+ val = f.getDouble(x);
+ } catch (Exception e) {
+ fail("Exception during getDouble test: " + e.toString());
+ }
+ assertTrue("Returned incorrect double field value",
+ val == Double.MAX_VALUE);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.getDouble(x);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since doubleField is not a
+ // boolean type
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during getDouble test: " + e.toString());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getFloat(java.lang.Object)
+ */
+ public void test_getFloatLjava_lang_Object() {
+ // Test for method float
+ // java.lang.reflect.Field.getFloat(java.lang.Object)
+ TestField x = new TestField();
+ Field f = null;
+ float val = 0;
+ try {
+ f = x.getClass().getDeclaredField("floatField");
+ val = f.getFloat(x);
+ } catch (Exception e) {
+ fail("Exception during getFloat test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect float field value",
+ val == Float.MAX_VALUE);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.getFloat(x);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since floatField is not a
+ // boolean type
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during getfloat test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getInt(java.lang.Object)
+ */
+ public void test_getIntLjava_lang_Object() {
+ // Test for method int java.lang.reflect.Field.getInt(java.lang.Object)
+ TestField x = new TestField();
+ Field f = null;
+ int val = 0;
+ try {
+ f = x.getClass().getDeclaredField("intField");
+ val = f.getInt(x);
+ } catch (Exception e) {
+ fail("Exception during getInt test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect Int field value",
+ val == Integer.MAX_VALUE);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.getInt(x);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since IntField is not a
+ // boolean type
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during getInt test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getLong(java.lang.Object)
+ */
+ public void test_getLongLjava_lang_Object() {
+ // Test for method long
+ // java.lang.reflect.Field.getLong(java.lang.Object)
+ TestField x = new TestField();
+ Field f = null;
+ long val = 0;
+ try {
+ f = x.getClass().getDeclaredField("longField");
+ val = f.getLong(x);
+ } catch (Exception e) {
+ fail("Exception during getLong test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect long field value", val == Long.MAX_VALUE);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.getLong(x);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since booleanField is not a
+ // long type
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during getlong test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getModifiers()
+ */
+ public void test_getModifiers() {
+ // Test for method int java.lang.reflect.Field.getModifiers()
+ TestField x = new TestField();
+ Field f = null;
+ try {
+ f = x.getClass().getDeclaredField("prsttrvol");
+ } catch (Exception e) {
+ fail("Exception during getModifiers test: " + e.toString());
+ }
+ int mod = f.getModifiers();
+ int mask = (Modifier.PROTECTED | Modifier.STATIC)
+ | (Modifier.TRANSIENT | Modifier.VOLATILE);
+ int nmask = (Modifier.PUBLIC | Modifier.NATIVE);
+ assertTrue("Returned incorrect field modifiers: ",
+ ((mod & mask) == mask) && ((mod & nmask) == 0));
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getName()
+ */
+ public void test_getName() {
+ // Test for method java.lang.String java.lang.reflect.Field.getName()
+ TestField x = new TestField();
+ Field f = null;
+ try {
+ f = x.getClass().getDeclaredField("shortField");
+ } catch (Exception e) {
+ fail("Exception during getType test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect field name", f.getName().equals(
+ "shortField"));
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getShort(java.lang.Object)
+ */
+ public void test_getShortLjava_lang_Object() {
+ // Test for method short
+ // java.lang.reflect.Field.getShort(java.lang.Object)
+ TestField x = new TestField();
+ Field f = null;
+ short val = 0;
+ ;
+ try {
+ f = x.getClass().getDeclaredField("shortField");
+ val = f.getShort(x);
+ } catch (Exception e) {
+ fail("Exception during getShort test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect short field value",
+ val == Short.MAX_VALUE);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.getShort(x);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since booleanField is not a
+ // short type
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during getshort test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#getType()
+ */
+ public void test_getType() {
+ // Test for method java.lang.Class java.lang.reflect.Field.getType()
+ TestField x = new TestField();
+ Field f = null;
+ try {
+ f = x.getClass().getDeclaredField("shortField");
+ } catch (Exception e) {
+ fail("Exception during getType test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect field type: " + f.getType().toString(),
+ f.getType().equals(short.class));
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#set(java.lang.Object, java.lang.Object)
+ */
+ public void test_setLjava_lang_ObjectLjava_lang_Object() {
+ // Test for method void java.lang.reflect.Field.set(java.lang.Object,
+ // java.lang.Object)
+ TestField x = new TestField();
+ Field f = null;
+ double val = 0.0;
+ try {
+ f = x.getClass().getDeclaredField("doubleField");
+ f.set(x, new Double(1.0));
+ val = f.getDouble(x);
+ } catch (Exception e) {
+ fail("Exception during set test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect double field value", val == 1.0);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.set(x, new Double(1.0));
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since booleanField is not a
+ // double type
+ return;
+ }
+ try {
+ f = x.getClass().getDeclaredField("doubleFField");
+ f.set(x, new Double(1.0));
+ } catch (IllegalAccessException ex) {
+ // Good, Exception should be thrown since doubleFField is
+ // declared as final
+ return;
+ }
+ // Test setting a static field;
+ f = x.getClass().getDeclaredField("doubleSField");
+ f.set(x, new Double(1.0));
+ val = f.getDouble(x);
+ assertTrue("Returned incorrect double field value", val == 1.0);
+ } catch (Exception e) {
+ fail("Exception during setDouble test: " + e.toString());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#setBoolean(java.lang.Object, boolean)
+ */
+ public void test_setBooleanLjava_lang_ObjectZ() {
+ // Test for method void
+ // java.lang.reflect.Field.setBoolean(java.lang.Object, boolean)
+ TestField x = new TestField();
+ Field f = null;
+ boolean val = false;
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.setBoolean(x, false);
+ val = f.getBoolean(x);
+ } catch (Exception e) {
+ fail("Exception during setboolean test: " + e.toString());
+ }
+ assertTrue("Returned incorrect float field value", !val);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.setBoolean(x, true);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since booleanField is not a
+ // boolean type
+ return;
+ }
+
+ try {
+ f = x.getClass().getDeclaredField("booleanFField");
+ f.setBoolean(x, true);
+ } catch (IllegalAccessException ex) {
+ // Good, Exception should be thrown since booleanField is
+ // declared as final
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during setboolean test: " + e.toString());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#setByte(java.lang.Object, byte)
+ */
+ public void test_setByteLjava_lang_ObjectB() {
+ // Test for method void
+ // java.lang.reflect.Field.setByte(java.lang.Object, byte)
+ TestField x = new TestField();
+ Field f = null;
+ byte val = 0;
+ try {
+ f = x.getClass().getDeclaredField("byteField");
+ f.setByte(x, (byte) 1);
+ val = f.getByte(x);
+ } catch (Exception e) {
+ fail("Exception during setByte test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect float field value", val == 1);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.setByte(x, (byte) 1);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since booleanField is not a
+ // byte type
+ return;
+ }
+
+ try {
+ f = x.getClass().getDeclaredField("byteFField");
+ f.setByte(x, (byte) 1);
+ } catch (IllegalAccessException ex) {
+ // Good, Exception should be thrown since byteFField is declared
+ // as final
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during setByte test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#setChar(java.lang.Object, char)
+ */
+ public void test_setCharLjava_lang_ObjectC() {
+ // Test for method void
+ // java.lang.reflect.Field.setChar(java.lang.Object, char)
+ TestField x = new TestField();
+ Field f = null;
+ char val = 0;
+ try {
+ f = x.getClass().getDeclaredField("charField");
+ f.setChar(x, (char) 1);
+ val = f.getChar(x);
+ } catch (Exception e) {
+ fail("Exception during setChar test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect float field value", val == 1);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.setChar(x, (char) 1);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since booleanField is not a
+ // char type
+ return;
+ }
+
+ try {
+ f = x.getClass().getDeclaredField("charFField");
+ f.setChar(x, (char) 1);
+ } catch (IllegalAccessException ex) {
+ // Good, Exception should be thrown since charFField is declared
+ // as final
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during setChar test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#setDouble(java.lang.Object, double)
+ */
+ public void test_setDoubleLjava_lang_ObjectD() {
+ // Test for method void
+ // java.lang.reflect.Field.setDouble(java.lang.Object, double)
+ TestField x = new TestField();
+ Field f = null;
+ double val = 0.0;
+ try {
+ f = x.getClass().getDeclaredField("doubleField");
+ f.setDouble(x, 1.0);
+ val = f.getDouble(x);
+ } catch (Exception e) {
+ fail("Exception during setDouble test: " + e.toString());
+ }
+ assertTrue("Returned incorrect double field value", val == 1.0);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.setDouble(x, 1.0);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since booleanField is not a
+ // double type
+ return;
+ }
+
+ try {
+ f = x.getClass().getDeclaredField("doubleFField");
+ f.setDouble(x, 1.0);
+ } catch (IllegalAccessException ex) {
+ // Good, Exception should be thrown since doubleFField is
+ // declared as final
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during setDouble test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#setFloat(java.lang.Object, float)
+ */
+ public void test_setFloatLjava_lang_ObjectF() {
+ // Test for method void
+ // java.lang.reflect.Field.setFloat(java.lang.Object, float)
+ TestField x = new TestField();
+ Field f = null;
+ float val = 0.0F;
+ try {
+ f = x.getClass().getDeclaredField("floatField");
+ f.setFloat(x, (float) 1);
+ val = f.getFloat(x);
+ } catch (Exception e) {
+ fail("Exception during setFloat test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect float field value", val == 1);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.setFloat(x, (float) 1);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since booleanField is not a
+ // float type
+ return;
+ }
+ try {
+ f = x.getClass().getDeclaredField("floatFField");
+ f.setFloat(x, (float) 1);
+ } catch (IllegalAccessException ex) {
+ // Good, Exception should be thrown since floatFField is
+ // declared as final
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during setFloat test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#setInt(java.lang.Object, int)
+ */
+ public void test_setIntLjava_lang_ObjectI() {
+ // Test for method void java.lang.reflect.Field.setInt(java.lang.Object,
+ // int)
+ TestField x = new TestField();
+ Field f = null;
+ int val = 0;
+ try {
+ f = x.getClass().getDeclaredField("intField");
+ f.setInt(x, (int) 1);
+ val = f.getInt(x);
+ } catch (Exception e) {
+ fail("Exception during setInteger test: " + e.toString());
+ }
+ assertTrue("Returned incorrect int field value", val == 1);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.setInt(x, (int) 1);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since booleanField is not a
+ // int type
+ return;
+ }
+ try {
+ f = x.getClass().getDeclaredField("intFField");
+ f.setInt(x, (int) 1);
+ } catch (IllegalAccessException ex) {
+ // Good, Exception should be thrown since intFField is declared
+ // as final
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during setInteger test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#setLong(java.lang.Object, long)
+ */
+ public void test_setLongLjava_lang_ObjectJ() {
+ // Test for method void
+ // java.lang.reflect.Field.setLong(java.lang.Object, long)
+ TestField x = new TestField();
+ Field f = null;
+ long val = 0L;
+ try {
+ f = x.getClass().getDeclaredField("longField");
+ f.setLong(x, (long) 1);
+ val = f.getLong(x);
+ } catch (Exception e) {
+ fail("Exception during setLong test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect long field value", val == 1);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.setLong(x, (long) 1);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since booleanField is not a
+ // long type
+ return;
+ }
+ try {
+ f = x.getClass().getDeclaredField("longFField");
+ f.setLong(x, (long) 1);
+ } catch (IllegalAccessException ex) {
+ // Good, Exception should be thrown since longFField is declared
+ // as final
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during setLong test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#setShort(java.lang.Object, short)
+ */
+ public void test_setShortLjava_lang_ObjectS() {
+ // Test for method void
+ // java.lang.reflect.Field.setShort(java.lang.Object, short)
+ TestField x = new TestField();
+ Field f = null;
+ short val = 0;
+ try {
+ f = x.getClass().getDeclaredField("shortField");
+ f.setShort(x, (short) 1);
+ val = f.getShort(x);
+ } catch (Exception e) {
+ fail("Exception during setShort test : " + e.getMessage());
+ }
+ assertTrue("Returned incorrect short field value", val == 1);
+ try {
+ try {
+ f = x.getClass().getDeclaredField("booleanField");
+ f.setShort(x, (short) 1);
+ } catch (IllegalArgumentException ex) {
+ // Good, Exception should be thrown since booleanField is not a
+ // short type
+ return;
+ }
+ try {
+ f = x.getClass().getDeclaredField("shortFField");
+ f.setShort(x, (short) 1);
+ } catch (IllegalAccessException ex) {
+ // Good, Exception should be thrown since shortFField is
+ // declared as final
+ return;
+ }
+ } catch (Exception e) {
+ fail("Exception during setShort test : " + e.getMessage());
+ }
+ fail("Accessed field of invalid type");
+ }
+
+ /**
+ * @tests java.lang.reflect.Field#toString()
+ */
+ public void test_toString() {
+ // Test for method java.lang.String java.lang.reflect.Field.toString()
+ Field f = null;
+
+ try {
+ f = TestField.class.getDeclaredField("x");
+ } catch (Exception e) {
+ fail("Exception getting field : " + e.getMessage());
+ }
+ assertTrue(
+ "Field returned incorrect string",
+ f
+ .toString()
+ .equals(
+ "private static final int tests.api.java.lang.reflect.FieldTest$TestField.x"));
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/InvocationTargetExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/InvocationTargetExceptionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/InvocationTargetExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/InvocationTargetExceptionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,264 @@
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * 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.
+ */
+
+package tests.api.java.lang.reflect;
+
+import java.io.ByteArrayOutputStream;
+import java.io.CharArrayWriter;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class InvocationTargetExceptionTest extends junit.framework.TestCase {
+
+ static class TestMethod {
+ public TestMethod() {
+ }
+
+ public void voidMethod() throws IllegalArgumentException {
+ }
+
+ public void parmTest(int x, short y, String s, boolean bool, Object o,
+ long l, byte b, char c, double d, float f) {
+ }
+
+ public int intMethod() {
+ return 1;
+ }
+
+ public static final void printTest(int x, short y, String s,
+ boolean bool, Object o, long l, byte b, char c, double d,
+ float f) {
+ }
+
+ public double doubleMethod() {
+ return 1.0;
+ }
+
+ public short shortMethod() {
+ return (short) 1;
+ }
+
+ public byte byteMethod() {
+ return (byte) 1;
+ }
+
+ public float floatMethod() {
+ return 1.0f;
+ }
+
+ public long longMethod() {
+ return 1l;
+ }
+
+ public char charMethod() {
+ return 'T';
+ }
+
+ public Object objectMethod() {
+ return new Object();
+ }
+
+ private static void prstatic() {
+ }
+
+ public static void pustatic() {
+ }
+
+ public static synchronized void pustatsynch() {
+ }
+
+ public static int invokeStaticTest() {
+ return 1;
+ }
+
+ public int invokeInstanceTest() {
+ return 1;
+ }
+
+ private int privateInvokeTest() {
+ return 1;
+ }
+
+ public int invokeExceptionTest() throws NullPointerException {
+ throw new NullPointerException();
+ }
+
+ public static synchronized native void pustatsynchnat();
+
+ }
+
+ abstract class AbstractTestMethod {
+ public abstract void puabs();
+ }
+
+ /**
+ * @tests java.lang.reflect.InvocationTargetException#InvocationTargetException(java.lang.Throwable)
+ */
+ public void test_ConstructorLjava_lang_Throwable() {
+ // Test for method
+ // java.lang.reflect.InvocationTargetException(java.lang.Throwable)
+ try {
+ Method mth = TestMethod.class.getDeclaredMethod(
+ "invokeExceptionTest", new Class[0]);
+ Object[] args = { Object.class };
+ Object ret = mth.invoke(new TestMethod(), new Object[0]);
+ } catch (InvocationTargetException e) {
+ // Correct behaviour
+ return;
+ } catch (NoSuchMethodException e) {
+ } catch (IllegalAccessException e) {
+ }
+ fail("Failed to throw exception");
+ }
+
+ /**
+ * @tests java.lang.reflect.InvocationTargetException#InvocationTargetException(java.lang.Throwable,
+ * java.lang.String)
+ */
+ public void test_ConstructorLjava_lang_ThrowableLjava_lang_String() {
+ // Test for method
+ // java.lang.reflect.InvocationTargetException(java.lang.Throwable,
+ // java.lang.String)
+ try {
+ Method mth = TestMethod.class.getDeclaredMethod(
+ "invokeExceptionTest", new Class[0]);
+ Object[] args = { Object.class };
+ Object ret = mth.invoke(new TestMethod(), new Object[0]);
+ } catch (InvocationTargetException e) {
+ // Correct behaviour
+ return;
+ } catch (NoSuchMethodException e) {
+ ;
+ } catch (IllegalAccessException e) {
+ }
+ fail("Failed to throw exception");
+ }
+
+ /**
+ * @tests java.lang.reflect.InvocationTargetException#getTargetException()
+ */
+ public void test_getTargetException() {
+ // Test for method java.lang.Throwable
+ // java.lang.reflect.InvocationTargetException.getTargetException()
+ try {
+ Method mth = TestMethod.class.getDeclaredMethod(
+ "invokeExceptionTest", new Class[0]);
+ Object[] args = { Object.class };
+ Object ret = mth.invoke(new TestMethod(), new Object[0]);
+ } catch (InvocationTargetException e) {
+ // Correct behaviour
+ assertTrue("Returned incorrect target exception", e
+ .getTargetException() instanceof NullPointerException);
+ return;
+ } catch (Exception e) {
+ fail("Exception during constructor test : " + e.getMessage());
+ }
+ fail("Failed to throw exception");
+ }
+
+ /**
+ * @tests java.lang.reflect.InvocationTargetException#printStackTrace()
+ */
+ public void test_printStackTrace() {
+ // Test for method void
+ // java.lang.reflect.InvocationTargetException.printStackTrace()
+ try {
+ ByteArrayOutputStream bao = new ByteArrayOutputStream();
+ PrintStream ps = new PrintStream(bao);
+ PrintStream oldErr = System.err;
+ System.setErr(ps);
+ InvocationTargetException ite = new InvocationTargetException(null);
+ ite.printStackTrace();
+ System.setErr(oldErr);
+
+ String s = new String(bao.toByteArray());
+
+ assertTrue("Incorrect Stack trace: " + s, s != null
+ && s.length() > 300);
+ } catch (Exception e) {
+ fail("printStackTrace() caused exception : " + e.getMessage());
+ }
+ }
+
+ /**
+ * @tests java.lang.reflect.InvocationTargetException#printStackTrace(java.io.PrintStream)
+ */
+ public void test_printStackTraceLjava_io_PrintStream() {
+ // Test for method void
+ // java.lang.reflect.InvocationTargetException.printStackTrace(java.io.PrintStream)
+ assertTrue("Tested via test_printStackTrace().", true);
+ ByteArrayOutputStream bao = new ByteArrayOutputStream();
+ PrintStream ps = new PrintStream(bao);
+ InvocationTargetException ite = new InvocationTargetException(
+ new InvocationTargetException(null));
+ ite.printStackTrace(ps);
+ String s = bao.toString();
+ assertTrue("printStackTrace failed." + s.length(), s != null
+ && s.length() > 400);
+ }
+
+ /**
+ * @tests java.lang.reflect.InvocationTargetException#printStackTrace(java.io.PrintWriter)
+ */
+ public void test_printStackTraceLjava_io_PrintWriter() {
+ // Test for method void
+ // java.lang.reflect.InvocationTargetException.printStackTrace(java.io.PrintWriter)
+ try {
+ PrintWriter pw;
+ InvocationTargetException ite;
+ String s;
+ CharArrayWriter caw = new CharArrayWriter();
+ pw = new PrintWriter(caw);
+ ite = new InvocationTargetException(new InvocationTargetException(
+ null));
+ ite.printStackTrace(pw);
+
+ s = caw.toString();
+ assertTrue("printStackTrace failed." + s.length(), s != null
+ && s.length() > 400);
+ pw.close();
+
+ ByteArrayOutputStream bao = new ByteArrayOutputStream();
+ pw = new PrintWriter(bao);
+ ite = new InvocationTargetException(new InvocationTargetException(
+ null));
+ ite.printStackTrace(pw);
+
+ pw.flush(); // Test will fail if this line removed.
+ s = bao.toString();
+ assertTrue("printStackTrace failed." + s.length(), s != null
+ && s.length() > 400);
+
+ } catch (Exception e) {
+ fail("Exception during test : " + e.getMessage());
+ }
+ }
+
+ /**
+ * Sets up the fixture, for example, open a network connection. This method
+ * is called before a test is executed.
+ */
+ protected void setUp() {
+ }
+
+ /**
+ * Tears down the fixture, for example, close a network connection. This
+ * method is called after a test is executed.
+ */
+ protected void tearDown() {
+ }
+}