harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r386058 [25/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/...
Date Wed, 15 Mar 2006 11:47:39 GMT
Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/MethodTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/MethodTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/MethodTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/MethodTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,681 @@
+/* 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.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+
+public class MethodTest 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();
+
+		public void invokeCastTest1(byte param) {
+		}
+
+		public void invokeCastTest1(short param) {
+		}
+
+		public void invokeCastTest1(int param) {
+		}
+
+		public void invokeCastTest1(long param) {
+		}
+
+		public void invokeCastTest1(float param) {
+		}
+
+		public void invokeCastTest1(double param) {
+		}
+
+		public void invokeCastTest1(char param) {
+		}
+
+		public void invokeCastTest1(boolean param) {
+		}
+	}
+
+	abstract class AbstractTestMethod {
+		public abstract void puabs();
+	}
+
+	class TestMethodSub extends TestMethod {
+		public int invokeInstanceTest() {
+			return 0;
+		}
+	}
+
+	/**
+	 * @tests java.lang.reflect.Method#equals(java.lang.Object)
+	 */
+	public void test_equalsLjava_lang_Object() {
+		// Test for method boolean
+		// java.lang.reflect.Method.equals(java.lang.Object)
+
+		Method m1 = null, m2 = null;
+		try {
+			m1 = TestMethod.class.getMethod("invokeInstanceTest", new Class[0]);
+			m2 = TestMethodSub.class.getMethod("invokeInstanceTest",
+					new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during equals test : " + e.getMessage());
+		}
+		assertTrue("Overriden method returned equal", !m1.equals(m2));
+		assertTrue("Same method returned not-equal", m1.equals(m1));
+		try {
+			m1 = TestMethod.class.getMethod("invokeStaticTest", new Class[0]);
+			m2 = TestMethodSub.class
+					.getMethod("invokeStaticTest", new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during equals test : " + e.getMessage());
+		}
+		assertTrue("Inherited method returned not-equal", m1.equals(m2));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Method#getDeclaringClass()
+	 */
+	public void test_getDeclaringClass() {
+		// Test for method java.lang.Class
+		// java.lang.reflect.Method.getDeclaringClass()
+
+		Method[] mths;
+
+		try {
+			mths = TestMethod.class.getDeclaredMethods();
+			assertTrue("Returned incorrect declaring class: "
+					+ mths[0].getDeclaringClass().toString(), mths[0]
+					.getDeclaringClass().equals(TestMethod.class));
+		} catch (Exception e) {
+			fail("Exception during getDeclaringClass test: "
+					+ e.toString());
+		}
+	}
+
+	/**
+	 * @tests java.lang.reflect.Method#getExceptionTypes()
+	 */
+	public void test_getExceptionTypes() {
+		// Test for method java.lang.Class []
+		// java.lang.reflect.Method.getExceptionTypes()
+
+		try {
+			Method mth = TestMethod.class.getMethod("voidMethod", new Class[0]);
+			Class[] ex = mth.getExceptionTypes();
+			assertTrue("Returned incorrect number of exceptions",
+					ex.length == 1);
+			assertTrue("Returned incorrect exception type", ex[0]
+					.equals(IllegalArgumentException.class));
+			mth = TestMethod.class.getMethod("intMethod", new Class[0]);
+			ex = mth.getExceptionTypes();
+			assertTrue("Returned incorrect number of exceptions",
+					ex.length == 0);
+		} catch (Exception e) {
+			fail("Exception during getExceptionTypes: " + e.toString());
+		}
+
+	}
+
+	/**
+	 * @tests java.lang.reflect.Method#getModifiers()
+	 */
+	public void test_getModifiers() {
+		// Test for method int java.lang.reflect.Method.getModifiers()
+
+		Class cl = TestMethod.class;
+		int mods = 0;
+		Method mth = null;
+		int mask = 0;
+		try {
+			mth = cl.getMethod("pustatic", new Class[0]);
+			mods = mth.getModifiers();
+		} catch (Exception e) {
+			fail("Exception during getModfiers test: " + e.toString());
+		}
+		mask = Modifier.PUBLIC | Modifier.STATIC;
+		assertTrue("Incorrect modifiers returned", (mods | mask) == mask);
+		try {
+			mth = cl.getDeclaredMethod("prstatic", new Class[0]);
+			mods = mth.getModifiers();
+		} catch (Exception e) {
+			fail("Exception during getModfiers test: " + e.toString());
+		}
+		mask = Modifier.PRIVATE | Modifier.STATIC;
+		assertTrue("Incorrect modifiers returned", (mods | mask) == mask);
+		try {
+			mth = cl.getDeclaredMethod("pustatsynch", new Class[0]);
+			mods = mth.getModifiers();
+		} catch (Exception e) {
+			fail("Exception during getModfiers test: " + e.toString());
+		}
+		mask = (Modifier.PUBLIC | Modifier.STATIC) | Modifier.SYNCHRONIZED;
+		assertTrue("Incorrect modifiers returned", (mods | mask) == mask);
+		try {
+			mth = cl.getDeclaredMethod("pustatsynchnat", new Class[0]);
+			mods = mth.getModifiers();
+		} catch (Exception e) {
+			fail("Exception during getModfiers test: " + e.toString());
+		}
+		mask = ((Modifier.PUBLIC | Modifier.STATIC) | Modifier.SYNCHRONIZED)
+				| Modifier.NATIVE;
+		assertTrue("Incorrect modifiers returned", (mods | mask) == mask);
+		cl = AbstractTestMethod.class;
+		try {
+			mth = cl.getDeclaredMethod("puabs", new Class[0]);
+			mods = mth.getModifiers();
+		} catch (Exception e) {
+			fail("Exception during getModfiers test: " + e.toString());
+		}
+		mask = Modifier.PUBLIC | Modifier.ABSTRACT;
+		assertTrue("Incorrect modifiers returned", (mods | mask) == mask);
+	}
+
+	/**
+	 * @tests java.lang.reflect.Method#getName()
+	 */
+	public void test_getName() {
+		// Test for method java.lang.String java.lang.reflect.Method.getName()
+		Method mth = null;
+		try {
+			mth = TestMethod.class.getMethod("voidMethod", new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during getMethodName(): " + e.toString());
+		}
+		assertTrue("Returned incorrect method name", mth.getName().equals(
+				"voidMethod"));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Method#getParameterTypes()
+	 */
+	public void test_getParameterTypes() {
+		// Test for method java.lang.Class []
+		// java.lang.reflect.Method.getParameterTypes()
+		Class cl = TestMethod.class;
+		Method mth = null;
+		Class[] parms = null;
+		Method[] methods = null;
+		Class[] plist = { int.class, short.class, String.class, boolean.class,
+				Object.class, long.class, byte.class, char.class, double.class,
+				float.class };
+		try {
+			mth = cl.getMethod("voidMethod", new Class[0]);
+			parms = mth.getParameterTypes();
+		} catch (Exception e) {
+			fail("Exception during getParameterTypes test: "
+					+ e.toString());
+		}
+		assertTrue("Returned incorrect parameterTypes", parms.length == 0);
+		try {
+			mth = cl.getMethod("parmTest", plist);
+			parms = mth.getParameterTypes();
+		} catch (Exception e) {
+			fail("Exception during getParameterTypes test: "
+					+ e.toString());
+		}
+		assertTrue("Invalid number of parameters returned",
+				plist.length == parms.length);
+		for (int i = 0; i < plist.length; i++)
+			assertTrue("Incorrect parameter returned", plist[i]
+					.equals(parms[i]));
+
+		// Test same method. but this time pull it from the list of methods
+		// rather than asking for it explicitly
+		methods = cl.getDeclaredMethods();
+
+		int i;
+		for (i = 0; i < methods.length; i++)
+			if (methods[i].getName().equals("parmTest")) {
+				mth = methods[i];
+				i = methods.length + 1;
+			}
+		if (i < methods.length) {
+			parms = mth.getParameterTypes();
+			assertTrue("Incorrect number of parameters returned",
+					parms.length == plist.length);
+			for (i = 0; i < plist.length; i++)
+				assertTrue("Incorrect parameter returned", plist[i]
+						.equals(parms[i]));
+		}
+	}
+
+	/**
+	 * @tests java.lang.reflect.Method#getReturnType()
+	 */
+	public void test_getReturnType() {
+		// Test for method java.lang.Class
+		// java.lang.reflect.Method.getReturnType()
+		Class cl = TestMethod.class;
+		Method mth = null;
+		try {
+			mth = cl.getMethod("charMethod", new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during getReturnType test : " + e.getMessage());
+		}
+		assertTrue("Gave incorrect returne type, wanted char", mth
+				.getReturnType().equals(char.class));
+		try {
+			mth = cl.getMethod("longMethod", new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during getReturnType test : " + e.getMessage());
+		}
+		assertTrue("Gave incorrect returne type, wanted long", mth
+				.getReturnType().equals(long.class));
+		try {
+			mth = cl.getMethod("shortMethod", new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during getReturnType test : " + e.getMessage());
+		}
+		assertTrue("Gave incorrect returne type, wanted short", mth
+				.getReturnType().equals(short.class));
+		try {
+			mth = cl.getMethod("intMethod", new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during getReturnType test : " + e.getMessage());
+		}
+		assertTrue("Gave incorrect returne type, wanted int: "
+				+ mth.getReturnType(), mth.getReturnType().equals(int.class));
+		try {
+			mth = cl.getMethod("doubleMethod", new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during getReturnType test : " + e.getMessage());
+		}
+		assertTrue("Gave incorrect returne type, wanted double", mth
+				.getReturnType().equals(double.class));
+		try {
+			mth = cl.getMethod("byteMethod", new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during getReturnType test : " + e.getMessage());
+		}
+		assertTrue("Gave incorrect returne type, wanted byte", mth
+				.getReturnType().equals(byte.class));
+		try {
+			mth = cl.getMethod("byteMethod", new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during getReturnType test:" + e.toString());
+		}
+		assertTrue("Gave incorrect returne type, wanted byte", mth
+				.getReturnType().equals(byte.class));
+		try {
+			mth = cl.getMethod("objectMethod", new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during getReturnType test : " + e.getMessage());
+		}
+		assertTrue("Gave incorrect returne type, wanted Object", mth
+				.getReturnType().equals(Object.class));
+
+		try {
+			mth = cl.getMethod("voidMethod", new Class[0]);
+		} catch (Exception e) {
+			fail("Exception during getReturnType test : " + e.getMessage());
+		}
+		assertTrue("Gave incorrect returne type, wanted void", mth
+				.getReturnType().equals(void.class));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Method#invoke(java.lang.Object,
+	 *        java.lang.Object[])
+	 */
+	public void test_invokeLjava_lang_Object$Ljava_lang_Object() {
+		// Test for method java.lang.Object
+		// java.lang.reflect.Method.invoke(java.lang.Object, java.lang.Object
+		// [])
+
+		Class cl = TestMethod.class;
+		Method mth = null;
+		Object ret = null;
+		Class[] dcl = new Class[0];
+
+		// Get and invoke a static method
+		try {
+			mth = cl.getDeclaredMethod("invokeStaticTest", dcl);
+		} catch (Exception e) {
+			fail(
+					"Unable to obtain method for invoke test: invokeStaticTest");
+		}
+		try {
+			ret = mth.invoke(null, new Object[0]);
+		} catch (Exception e) {
+			fail("Exception during invoke test : " + e.getMessage());
+		}
+		assertTrue("Invoke returned incorrect value", ((Integer) ret)
+				.intValue() == 1);
+
+		// Get and invoke an instance method
+		try {
+			mth = cl.getDeclaredMethod("invokeInstanceTest", dcl);
+		} catch (Exception e) {
+			fail(
+					"Unable to obtain method for invoke test: invokeInstanceTest");
+		}
+		try {
+			ret = mth.invoke(new TestMethod(), new Object[0]);
+		} catch (Exception e) {
+			fail("Exception during invoke test : " + e.getMessage());
+		}
+		assertTrue("Invoke returned incorrect value", ((Integer) ret)
+				.intValue() == 1);
+
+		// Get and attempt to invoke a private method
+		try {
+			mth = cl.getDeclaredMethod("privateInvokeTest", dcl);
+		} catch (Exception e) {
+			fail(
+					"Unable to obtain method for invoke test: privateInvokeTest");
+		}
+		try {
+			ret = mth.invoke(new TestMethod(), new Object[0]);
+		} catch (IllegalAccessException e) {
+			// Correct behaviour
+		} catch (Exception e) {
+			fail("Exception during invoke test : " + e.getMessage());
+		}
+		// Generate an IllegalArgumentException
+		try {
+			mth = cl.getDeclaredMethod("invokeInstanceTest", dcl);
+		} catch (Exception e) {
+			fail(
+					"Unable to obtain method for invoke test: invokeInstanceTest");
+		}
+		try {
+			Object[] args = { Object.class };
+			ret = mth.invoke(new TestMethod(), args);
+		} catch (IllegalArgumentException e) {
+			// Correct behaviour
+		} catch (Exception e) {
+			fail("Exception during invoke test : " + e.getMessage());
+		}
+
+		// Generate a NullPointerException
+		try {
+			mth = cl.getDeclaredMethod("invokeInstanceTest", dcl);
+		} catch (Exception e) {
+			fail("Unable to obtain method invokeInstanceTest for invoke test : "
+					+ e.getMessage());
+		}
+		try {
+			ret = mth.invoke(null, new Object[0]);
+		} catch (NullPointerException e) {
+			// Correct behaviour
+		} catch (Exception e) {
+			fail("Exception during invoke test : " + e.getMessage());
+		}
+
+		// Generate an InvocationTargetException
+		try {
+			mth = cl.getDeclaredMethod("invokeExceptionTest", dcl);
+		} catch (Exception e) {
+			fail("Unable to obtain method invokeExceptionTest for invoke test: "
+					+ e.getMessage());
+		}
+		try {
+			ret = mth.invoke(new TestMethod(), new Object[0]);
+		} catch (InvocationTargetException e) {
+			// Correct behaviour
+		} catch (Exception e) {
+			fail("Exception during invoke test : " + e.getMessage());
+		}
+
+		TestMethod testMethod = new TestMethod();
+		Method methods[] = cl.getMethods();
+		for (int i = 0; i < methods.length; i++) {
+			if (methods[i].getName().startsWith("invokeCastTest1")) {
+				Class param = methods[i].getParameterTypes()[0];
+
+				try {
+					methods[i].invoke(testMethod, new Object[] { new Byte(
+							(byte) 1) });
+					assertTrue("invalid invoke with Byte: " + methods[i],
+							param == Byte.TYPE || param == Short.TYPE
+									|| param == Integer.TYPE
+									|| param == Long.TYPE
+									|| param == Float.TYPE
+									|| param == Double.TYPE);
+				} catch (Exception e) {
+					assertTrue("Byte invalid exception: " + e,
+							e instanceof IllegalArgumentException);
+					assertTrue("Byte invalid failure: " + methods[i],
+							param == Boolean.TYPE || param == Character.TYPE);
+				}
+
+				try {
+					methods[i].invoke(testMethod, new Object[] { new Short(
+							(short) 1) });
+					assertTrue("invalid invoke with Short: " + methods[i],
+							param == Short.TYPE || param == Integer.TYPE
+									|| param == Long.TYPE
+									|| param == Float.TYPE
+									|| param == Double.TYPE);
+				} catch (Exception e) {
+					assertTrue("Short invalid exception: " + e,
+							e instanceof IllegalArgumentException);
+					assertTrue("Short invalid failure: " + methods[i],
+							param == Byte.TYPE || param == Boolean.TYPE
+									|| param == Character.TYPE);
+				}
+
+				try {
+					methods[i].invoke(testMethod,
+							new Object[] { new Integer(1) });
+					assertTrue("invalid invoke with Integer: " + methods[i],
+							param == Integer.TYPE || param == Long.TYPE
+									|| param == Float.TYPE
+									|| param == Double.TYPE);
+				} catch (Exception e) {
+					assertTrue("Integer invalid exception: " + e,
+							e instanceof IllegalArgumentException);
+					assertTrue("Integer invalid failure: " + methods[i],
+							param == Byte.TYPE || param == Short.TYPE
+									|| param == Boolean.TYPE
+									|| param == Character.TYPE);
+				}
+
+				try {
+					methods[i].invoke(testMethod, new Object[] { new Long(1) });
+					assertTrue("invalid invoke with Long: " + methods[i],
+							param == Long.TYPE || param == Float.TYPE
+									|| param == Double.TYPE);
+				} catch (Exception e) {
+					assertTrue("Long invalid exception: " + e,
+							e instanceof IllegalArgumentException);
+					assertTrue("Long invalid failure: " + methods[i],
+							param == Byte.TYPE || param == Short.TYPE
+									|| param == Integer.TYPE
+									|| param == Boolean.TYPE
+									|| param == Character.TYPE);
+				}
+
+				try {
+					methods[i].invoke(testMethod, new Object[] { new Character(
+							'a') });
+					assertTrue("invalid invoke with Character: " + methods[i],
+							param == Character.TYPE || param == Integer.TYPE
+									|| param == Long.TYPE
+									|| param == Float.TYPE
+									|| param == Double.TYPE);
+				} catch (Exception e) {
+					assertTrue("Character invalid exception: " + e,
+							e instanceof IllegalArgumentException);
+					assertTrue("Character invalid failure: " + methods[i],
+							param == Byte.TYPE || param == Short.TYPE
+									|| param == Boolean.TYPE);
+				}
+
+				try {
+					methods[i]
+							.invoke(testMethod, new Object[] { new Float(1) });
+					assertTrue("invalid invoke with Float: " + methods[i],
+							param == Float.TYPE || param == Double.TYPE);
+				} catch (Exception e) {
+					assertTrue("Float invalid exception: " + e,
+							e instanceof IllegalArgumentException);
+					assertTrue("Float invalid failure: " + methods[i],
+							param == Byte.TYPE || param == Short.TYPE
+									|| param == Integer.TYPE
+									|| param == Long.TYPE
+									|| param == Boolean.TYPE
+									|| param == Character.TYPE);
+				}
+
+				try {
+					methods[i].invoke(testMethod,
+							new Object[] { new Double(1) });
+					assertTrue("invalid invoke with Double: " + methods[i],
+							param == Double.TYPE);
+				} catch (Exception e) {
+					assertTrue("Double invalid exception: " + e,
+							e instanceof IllegalArgumentException);
+					assertTrue("Double invalid failure: " + methods[i],
+							param == Byte.TYPE || param == Short.TYPE
+									|| param == Integer.TYPE
+									|| param == Long.TYPE
+									|| param == Boolean.TYPE
+									|| param == Character.TYPE
+									|| param == Float.TYPE);
+				}
+
+				try {
+					methods[i].invoke(testMethod, new Object[] { new Boolean(
+							true) });
+					assertTrue("invalid invoke with Boolean: " + methods[i],
+							param == Boolean.TYPE);
+				} catch (Exception e) {
+					assertTrue("Boolean invalid exception: " + e,
+							e instanceof IllegalArgumentException);
+					assertTrue("Boolean invalid failure: " + methods[i],
+							param == Byte.TYPE || param == Short.TYPE
+									|| param == Integer.TYPE
+									|| param == Long.TYPE
+									|| param == Character.TYPE
+									|| param == Float.TYPE
+									|| param == Double.TYPE);
+				}
+			}
+		}
+	}
+
+	/**
+	 * @tests java.lang.reflect.Method#toString()
+	 */
+	public void test_toString() {
+		// Test for method java.lang.String java.lang.reflect.Method.toString()
+		Method mth = null;
+		Class[] parms = { int.class, short.class, String.class, boolean.class,
+				Object.class, long.class, byte.class, char.class, double.class,
+				float.class };
+		try {
+
+			mth = TestMethod.class.getDeclaredMethod("printTest", parms);
+		} catch (Exception e) {
+			fail("Exception during toString test : " + e.getMessage());
+		}
+
+		assertTrue(
+				"Returned incorrect string for method: " + mth.toString(),
+				mth
+						.toString()
+						.equals(
+								"public static final void tests.api.java.lang.reflect.MethodTest$TestMethod.printTest(int,short,java.lang.String,boolean,java.lang.Object,long,byte,char,double,float)"));
+	}
+
+	/**
+	 * 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/ModifierTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ModifierTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ModifierTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ModifierTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,198 @@
+/* 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.Modifier;
+
+public class ModifierTest extends junit.framework.TestCase {
+
+	private static final int ALL_FLAGS = 0x7FF;
+
+	/**
+	 * @tests java.lang.reflect.Modifier#Modifier()
+	 */
+	public void test_Constructor() {
+		// Test for method java.lang.reflect.Modifier()
+		// Does nothing
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isAbstract(int)
+	 */
+	public void test_isAbstractI() {
+		// Test for method boolean java.lang.reflect.Modifier.isAbstract(int)
+		assertTrue("ABSTRACT returned false", Modifier.isAbstract(ALL_FLAGS));
+		assertTrue("ABSTRACT returned false", Modifier
+				.isAbstract(Modifier.ABSTRACT));
+		assertTrue("Non-ABSTRACT returned true", !Modifier
+				.isAbstract(Modifier.TRANSIENT));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isFinal(int)
+	 */
+	public void test_isFinalI() {
+		// Test for method boolean java.lang.reflect.Modifier.isFinal(int)
+		assertTrue("FINAL returned false", Modifier.isFinal(ALL_FLAGS));
+		assertTrue("FINAL returned false", Modifier.isFinal(Modifier.FINAL));
+		assertTrue("Non-FINAL returned true", !Modifier
+				.isFinal(Modifier.TRANSIENT));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isInterface(int)
+	 */
+	public void test_isInterfaceI() {
+		// Test for method boolean java.lang.reflect.Modifier.isInterface(int)
+		assertTrue("INTERFACE returned false", Modifier.isInterface(ALL_FLAGS));
+		assertTrue("INTERFACE returned false", Modifier
+				.isInterface(Modifier.INTERFACE));
+		assertTrue("Non-INTERFACE returned true", !Modifier
+				.isInterface(Modifier.TRANSIENT));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isNative(int)
+	 */
+	public void test_isNativeI() {
+		// Test for method boolean java.lang.reflect.Modifier.isNative(int)
+		assertTrue("NATIVE returned false", Modifier.isNative(ALL_FLAGS));
+		assertTrue("NATIVE returned false", Modifier.isNative(Modifier.NATIVE));
+		assertTrue("Non-NATIVE returned true", !Modifier
+				.isNative(Modifier.TRANSIENT));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isPrivate(int)
+	 */
+	public void test_isPrivateI() {
+		// Test for method boolean java.lang.reflect.Modifier.isPrivate(int)
+		assertTrue("PRIVATE returned false", Modifier.isPrivate(ALL_FLAGS));
+		assertTrue("PRIVATE returned false", Modifier
+				.isPrivate(Modifier.PRIVATE));
+		assertTrue("Non-PRIVATE returned true", !Modifier
+				.isPrivate(Modifier.TRANSIENT));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isProtected(int)
+	 */
+	public void test_isProtectedI() {
+		// Test for method boolean java.lang.reflect.Modifier.isProtected(int)
+		assertTrue("PROTECTED returned false", Modifier.isProtected(ALL_FLAGS));
+		assertTrue("PROTECTED returned false", Modifier
+				.isProtected(Modifier.PROTECTED));
+		assertTrue("Non-PROTECTED returned true", !Modifier
+				.isProtected(Modifier.TRANSIENT));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isPublic(int)
+	 */
+	public void test_isPublicI() {
+		// Test for method boolean java.lang.reflect.Modifier.isPublic(int)
+		assertTrue("PUBLIC returned false", Modifier.isPublic(ALL_FLAGS));
+		assertTrue("PUBLIC returned false", Modifier.isPublic(Modifier.PUBLIC));
+		assertTrue("Non-PUBLIC returned true", !Modifier
+				.isPublic(Modifier.TRANSIENT));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isStatic(int)
+	 */
+	public void test_isStaticI() {
+		// Test for method boolean java.lang.reflect.Modifier.isStatic(int)
+		assertTrue("STATIC returned false", Modifier.isStatic(ALL_FLAGS));
+		assertTrue("STATIC returned false", Modifier.isStatic(Modifier.STATIC));
+		assertTrue("Non-STATIC returned true", !Modifier
+				.isStatic(Modifier.TRANSIENT));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isStrict(int)
+	 */
+	public void test_isStrictI() {
+		// Test for method boolean java.lang.reflect.Modifier.isStrict(int)
+		assertTrue("STRICT returned false", Modifier.isStrict(Modifier.STRICT));
+		assertTrue("Non-STRICT returned true", !Modifier
+				.isStrict(Modifier.TRANSIENT));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isSynchronized(int)
+	 */
+	public void test_isSynchronizedI() {
+		// Test for method boolean
+		// java.lang.reflect.Modifier.isSynchronized(int)
+		assertTrue("Synchronized returned false", Modifier
+				.isSynchronized(ALL_FLAGS));
+		assertTrue("Non-Synchronized returned true", !Modifier
+				.isSynchronized(Modifier.VOLATILE));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isTransient(int)
+	 */
+	public void test_isTransientI() {
+		// Test for method boolean java.lang.reflect.Modifier.isTransient(int)
+		assertTrue("Transient returned false", Modifier.isTransient(ALL_FLAGS));
+		assertTrue("Transient returned false", Modifier
+				.isTransient(Modifier.TRANSIENT));
+		assertTrue("Non-Transient returned true", !Modifier
+				.isTransient(Modifier.VOLATILE));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#isVolatile(int)
+	 */
+	public void test_isVolatileI() {
+		// Test for method boolean java.lang.reflect.Modifier.isVolatile(int)
+		assertTrue("Volatile returned false", Modifier.isVolatile(ALL_FLAGS));
+		assertTrue("Volatile returned false", Modifier
+				.isVolatile(Modifier.VOLATILE));
+		assertTrue("Non-Volatile returned true", !Modifier
+				.isVolatile(Modifier.TRANSIENT));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Modifier#toString(int)
+	 */
+	public void test_toStringI() {
+		// Test for method java.lang.String
+		// java.lang.reflect.Modifier.toString(int)
+		assertTrue("Returned incorrect string value: "
+				+ Modifier.toString(java.lang.reflect.Modifier.PUBLIC
+						+ java.lang.reflect.Modifier.ABSTRACT), Modifier
+				.toString(
+						java.lang.reflect.Modifier.PUBLIC
+								+ java.lang.reflect.Modifier.ABSTRACT).equals(
+						"public abstract"));
+	}
+
+	/**
+	 * 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/ProxyTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ProxyTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ProxyTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ProxyTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,227 @@
+/* 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.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.lang.reflect.UndeclaredThrowableException;
+
+import tests.support.Support_Proxy_I1;
+import tests.support.Support_Proxy_I2;
+import tests.support.Support_Proxy_ParentException;
+import tests.support.Support_Proxy_SubException;
+
+public class ProxyTest extends junit.framework.TestCase {
+
+	/*
+	 * When multiple interfaces define the same method, the list of thrown
+	 * exceptions are those which can be mapped to another exception in the
+	 * other method:
+	 * 
+	 * String foo(String s) throws SubException, LinkageError;
+	 * 
+	 * UndeclaredThrowableException wrappers any checked exception which is not
+	 * in the merged list. So ParentException would be wrapped, BUT LinkageError
+	 * would not be since its not an Error/RuntimeException.
+	 * 
+	 * interface I1 { String foo(String s) throws ParentException, LinkageError; }
+	 * interface I2 { String foo(String s) throws SubException, Error; }
+	 */
+
+	interface Broken1 {
+		public float method(float _number0, float _number1);
+	}
+
+	class Broken1Invoke implements InvocationHandler {
+		public Object invoke(Object proxy, Method method, Object[] args)
+				throws Throwable {
+			return args[1];
+		}
+	}
+
+	/**
+	 * @tests java.lang.reflect.Proxy#getProxyClass(java.lang.ClassLoader,
+	 *        java.lang.Class[])
+	 */
+	public void test_getProxyClassLjava_lang_ClassLoader$Ljava_lang_Class() {
+		Class proxy = Proxy.getProxyClass(Support_Proxy_I1.class
+				.getClassLoader(), new Class[] { Support_Proxy_I1.class });
+
+		assertTrue("Did not create a Proxy subclass ",
+				proxy.getSuperclass() == Proxy.class);
+		assertTrue("Does not believe its a Proxy class ", Proxy
+				.isProxyClass(proxy));
+
+		assertTrue("Does not believe it's a Proxy class ", Proxy
+				.isProxyClass(Proxy.getProxyClass(null,
+						new Class[] { Comparable.class })));
+
+		boolean aborted = false;
+		try {
+			Proxy.getProxyClass(null, new Class[] { Support_Proxy_I1.class,
+					Support_Proxy_I2.class });
+		} catch (IllegalArgumentException e) {
+			aborted = true;
+		}
+		assertTrue("Default classLoader should not see app class ", aborted);
+	}
+
+	/**
+	 * @tests java.lang.reflect.Proxy#newProxyInstance(java.lang.ClassLoader,
+	 *        java.lang.Class[], java.lang.reflect.InvocationHandler)
+	 */
+	public void test_newProxyInstanceLjava_lang_ClassLoader$Ljava_lang_ClassLjava_lang_reflect_InvocationHandler() {
+		Object p = Proxy.newProxyInstance(Support_Proxy_I1.class
+				.getClassLoader(), new Class[] { Support_Proxy_I1.class,
+				Support_Proxy_I2.class }, new InvocationHandler() {
+			public Object invoke(Object proxy, Method method, Object[] args)
+					throws Throwable {
+				if (method.getName().equals("equals"))
+					return new Boolean(proxy == args[0]);
+				if (method.getName().equals("array"))
+					return new int[] { (int) ((long[]) args[0])[1], -1 };
+				if (method.getName().equals("string")) {
+					if ("".equals(args[0]))
+						throw new Support_Proxy_SubException();
+					if ("clone".equals(args[0]))
+						throw new Support_Proxy_ParentException();
+					if ("error".equals(args[0]))
+						throw new ArrayStoreException();
+					if ("any".equals(args[0]))
+						throw new IllegalAccessException();
+				}
+				return null;
+			}
+		});
+
+		Support_Proxy_I1 proxy = (Support_Proxy_I1) p;
+		assertTrue("Failed identity test ", proxy.equals(proxy));
+		assertTrue("Failed not equals test ", !proxy.equals(""));
+		int[] result = (int[]) proxy.array(new long[] { 100L, -200L });
+		assertTrue("Failed base type conversion test ", result[0] == -200);
+
+		boolean worked = false;
+		try {
+			proxy.string("");
+		} catch (Support_Proxy_SubException e) {
+			worked = true;
+		} catch (Support_Proxy_ParentException e) { // is never thrown
+		}
+		assertTrue("Problem converting exception ", worked);
+
+		worked = false;
+		try {
+			proxy.string("clone");
+		} catch (Support_Proxy_ParentException e) { // is never thrown
+		} catch (UndeclaredThrowableException e) {
+			worked = true;
+		}
+		assertTrue("Problem converting exception ", worked);
+
+		worked = false;
+		try {
+			proxy.string("error");
+		} catch (Support_Proxy_ParentException e) { // is never thrown
+		} catch (UndeclaredThrowableException e) {
+		} catch (RuntimeException e) {
+			worked = e.getClass() == ArrayStoreException.class;
+		}
+		assertTrue("Problem converting exception ", worked);
+
+		worked = false;
+		try {
+			proxy.string("any");
+		} catch (Support_Proxy_ParentException e) { // is never thrown
+		} catch (UndeclaredThrowableException e) {
+			worked = true;
+		}
+		assertTrue("Problem converting exception ", worked);
+
+		Broken1 proxyObject = null;
+		try {
+			proxyObject = (Broken1) Proxy.newProxyInstance(Broken1.class
+					.getClassLoader(), new Class[] { Broken1.class },
+					new Broken1Invoke());
+		} catch (Throwable e) {
+			fail("Failed to create proxy for class: " + Broken1.class + " - "
+					+ e);
+		}
+		float brokenResult = proxyObject.method(2.1f, 5.8f);
+		assertTrue("Invalid invoke result", brokenResult == 5.8f);
+	}
+
+	/**
+	 * @tests java.lang.reflect.Proxy#isProxyClass(java.lang.Class)
+	 */
+	public void test_isProxyClassLjava_lang_Class() {
+		Class proxy = Proxy.getProxyClass(Support_Proxy_I1.class
+				.getClassLoader(), new Class[] { Support_Proxy_I1.class });
+
+		class Fake extends Proxy {
+			Fake() {
+				super(null);
+			}
+		}
+
+		Proxy fake = new Proxy(new InvocationHandler() {
+			public Object invoke(Object proxy, Method method, Object[] args)
+					throws Throwable {
+				return null;
+			}
+		}) {
+		};
+
+		assertTrue("Does not believe its a Proxy class ", Proxy
+				.isProxyClass(proxy));
+		assertTrue("Proxy subclasses do not count ", !Proxy
+				.isProxyClass(Fake.class));
+		assertTrue("Is not a runtime generated Proxy class ", !Proxy
+				.isProxyClass(fake.getClass()));
+	}
+
+	/**
+	 * @tests java.lang.reflect.Proxy#getInvocationHandler(java.lang.Object)
+	 */
+	public void test_getInvocationHandlerLjava_lang_Object() {
+		InvocationHandler handler = new InvocationHandler() {
+			public Object invoke(Object proxy, Method method, Object[] args)
+					throws Throwable {
+				return null;
+			}
+		};
+
+		Object p = Proxy.newProxyInstance(Support_Proxy_I1.class
+				.getClassLoader(), new Class[] { Support_Proxy_I1.class },
+				handler);
+
+		assertTrue("Did not return invocation handler ", Proxy
+				.getInvocationHandler(p) == handler);
+		boolean aborted = false;
+		try {
+			Proxy.getInvocationHandler("");
+		} catch (IllegalArgumentException e) {
+			aborted = true;
+		}
+		assertTrue("Did not detect non proxy object ", aborted);
+	}
+
+	protected void setUp() {
+	}
+
+	protected void tearDown() {
+	}
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ReflectPermissionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ReflectPermissionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ReflectPermissionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/lang/reflect/ReflectPermissionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,58 @@
+/* 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.ReflectPermission;
+
+public class ReflectPermissionTest extends junit.framework.TestCase {
+
+	/**
+	 * @tests java.lang.reflect.ReflectPermission#ReflectPermission(java.lang.String)
+	 */
+	public void test_ConstructorLjava_lang_String() {
+		// Test for method java.lang.reflect.ReflectPermission(java.lang.String)
+		System.out.println(new ReflectPermission("Blah").toString());
+		assertTrue("Incorrect permission constructed", new ReflectPermission(
+				"Blah").toString().indexOf(
+				"java.lang.reflect.ReflectPermission Blah") >= 0);
+	}
+
+	/**
+	 * @tests java.lang.reflect.ReflectPermission#ReflectPermission(java.lang.String,
+	 *        java.lang.String)
+	 */
+	public void test_ConstructorLjava_lang_StringLjava_lang_String() {
+		// Test for method java.lang.reflect.ReflectPermission(java.lang.String,
+		// java.lang.String)
+		assertTrue("Incorrect permission constructed", new ReflectPermission(
+				"Blah", "suppressAccessChecks").toString().indexOf(
+				"java.lang.reflect.ReflectPermission Blah") >= 0);
+	}
+
+	/**
+	 * 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/net/AllTests.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/AllTests.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,66 @@
+/* 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.net;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+/**
+ * Test suite for java.net package.
+ */
+public class AllTests {
+
+	public static void main(String[] args) {
+		junit.textui.TestRunner.run(suite());
+	}
+
+	public static Test suite() {
+		TestSuite suite = new TestSuite("Tests for java.net");
+		// $JUnit-BEGIN$
+		suite.addTestSuite(BindExceptionTest.class);
+		suite.addTestSuite(ConnectExceptionTest.class);
+		suite.addTestSuite(DatagramPacketTest.class);
+		suite.addTestSuite(DatagramSocketTest.class);
+		suite.addTestSuite(HttpURLConnectionTest.class);
+		suite.addTestSuite(Inet4AddressTest.class);
+		suite.addTestSuite(Inet6AddressTest.class);
+		suite.addTestSuite(InetAddressTest.class);
+		suite.addTestSuite(JarURLConnectionTest.class);
+		suite.addTestSuite(MalformedURLExceptionTest.class);
+		suite.addTestSuite(MulticastSocketTest.class);
+		suite.addTestSuite(NetPermissionTest.class);
+		suite.addTestSuite(NetworkInterfaceTest.class);
+		suite.addTestSuite(NoRouteToHostExceptionTest.class);
+		suite.addTestSuite(PasswordAuthenticationTest.class);
+		suite.addTestSuite(ProtocolExceptionTest.class);
+		suite.addTestSuite(ServerSocketTest.class);
+		suite.addTestSuite(SocketTest.class);
+		suite.addTestSuite(SocketExceptionTest.class);
+		suite.addTestSuite(SocketImplTest.class);
+		suite.addTestSuite(SocketPermissionTest.class);
+		suite.addTestSuite(UnknownHostExceptionTest.class);
+		suite.addTestSuite(UnknownServiceExceptionTest.class);
+		suite.addTestSuite(URITest.class);
+		suite.addTestSuite(URISyntaxExceptionTest.class);
+		suite.addTestSuite(URLTest.class);
+		suite.addTestSuite(URLClassLoaderTest.class);
+		suite.addTestSuite(URLConnectionTest.class);
+		suite.addTestSuite(URLDecoderTest.class);
+		suite.addTestSuite(URLEncoderTest.class);
+		// $JUnit-END$
+		return suite;
+	}
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/BindExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/BindExceptionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/BindExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/BindExceptionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,65 @@
+/* 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.net;
+
+import java.net.BindException;
+
+public class BindExceptionTest extends junit.framework.TestCase {
+
+	/**
+	 * @tests java.net.BindException#BindException()
+	 */
+	public void test_Constructor() {
+		// Test for method java.net.BindException()
+		try {
+			throw new BindException();
+		} catch (BindException e) {
+			return;
+		} catch (Exception e) {
+			fail("Exception during BindException test" + e.toString());
+		}
+		fail("Failed to generate exception");
+	}
+
+	/**
+	 * @tests java.net.BindException#BindException(java.lang.String)
+	 */
+	public void test_ConstructorLjava_lang_String() {
+		// Test for method java.net.BindException(java.lang.String)
+		try {
+			throw new BindException("Some error message");
+		} catch (BindException e) {
+			return;
+		} catch (Exception e) {
+			fail("Exception during BindException test : " + e.getMessage());
+		}
+		fail("Failed to generate 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/net/ConnectExceptionTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ConnectExceptionTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ConnectExceptionTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/ConnectExceptionTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,72 @@
+/* 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.net;
+
+import java.net.ConnectException;
+import java.net.InetAddress;
+import java.net.Socket;
+
+import tests.support.Support_PortManager;
+
+public class ConnectExceptionTest extends junit.framework.TestCase {
+
+	/**
+	 * @tests java.net.ConnectException#ConnectException()
+	 */
+	public void test_Constructor() {
+		// Test for method java.net.ConnectException()
+
+		try {
+			int portNumber = Support_PortManager.getNextPort();
+			new Socket(InetAddress.getLocalHost().getHostName(), portNumber);
+		} catch (ConnectException e) {
+			return;
+		} catch (Exception e) {
+			fail("Exception during Constructor test : " + e.getMessage());
+		}
+		fail("Failed to generate exception");
+	}
+
+	/**
+	 * @tests java.net.ConnectException#ConnectException(java.lang.String)
+	 */
+	public void test_ConstructorLjava_lang_String() {
+		// Test for method java.net.ConnectException(java.lang.String)
+		try {
+			int portNumber = Support_PortManager.getNextPort();
+			new Socket(InetAddress.getLocalHost().getHostName(), portNumber);
+		} catch (ConnectException e) {
+			return;
+		} catch (Exception e) {
+			fail("Exception during Constructor test : " + e.getMessage());
+		}
+		fail("Failed to generate 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/net/DatagramPacketTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramPacketTest.java?rev=386058&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramPacketTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/test/java/tests/api/java/net/DatagramPacketTest.java Wed Mar 15 03:46:17 2006
@@ -0,0 +1,469 @@
+/* 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.net;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.SocketAddress;
+import java.net.UnknownHostException;
+
+import tests.support.Support_Configuration;
+import tests.support.Support_PortManager;
+
+public class DatagramPacketTest extends junit.framework.TestCase {
+
+	DatagramPacket dp;
+
+	volatile boolean started = false;
+
+	/**
+	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int)
+	 */
+	public void test_Constructor$BI() {
+		// Test for method java.net.DatagramPacket(byte [], int)
+		try {
+			dp = new DatagramPacket("Hello".getBytes(), 5);
+			assertTrue("Created incorrect packet", new String(dp.getData(), 0,
+					dp.getData().length).equals("Hello"));
+			assertTrue("Wrong length", dp.getLength() == 5);
+		} catch (Exception e) {
+			fail("Exception during Constructor test: " + e.toString());
+		}
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int)
+	 */
+	public void test_Constructor$BII() {
+		try {
+			dp = new DatagramPacket("Hello".getBytes(), 2, 3);
+			assertTrue("Created incorrect packet", new String(dp.getData(), 0,
+					dp.getData().length).equals("Hello"));
+			assertTrue("Wrong length", dp.getLength() == 3);
+			assertTrue("Wrong offset", dp.getOffset() == 2);
+		} catch (Exception e) {
+			fail("Exception during Constructor test: " + e.toString());
+		}
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int,
+	 *        java.net.InetAddress, int)
+	 */
+	public void test_Constructor$BIILjava_net_InetAddressI() {
+		try {
+			dp = new DatagramPacket("Hello".getBytes(), 2, 3, InetAddress
+					.getLocalHost(), 0);
+			assertTrue("Created incorrect packet", dp.getAddress().equals(
+					InetAddress.getLocalHost())
+					&& dp.getPort() == 0);
+			assertTrue("Wrong length", dp.getLength() == 3);
+			assertTrue("Wrong offset", dp.getOffset() == 2);
+		} catch (Exception e) {
+			fail("Exception during Constructor test: " + e.toString());
+		}
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int,
+	 *        java.net.InetAddress, int)
+	 */
+	public void test_Constructor$BILjava_net_InetAddressI() {
+		// Test for method java.net.DatagramPacket(byte [], int,
+		// java.net.InetAddress, int)
+		try {
+			dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress
+					.getLocalHost(), 0);
+			assertTrue("Created incorrect packet", dp.getAddress().equals(
+					InetAddress.getLocalHost())
+					&& dp.getPort() == 0);
+			assertTrue("Wrong length", dp.getLength() == 5);
+		} catch (Exception e) {
+			fail("Exception during Constructor test: " + e.toString());
+		}
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#getAddress()
+	 */
+	public void test_getAddress() {
+		// Test for method java.net.InetAddress
+		// java.net.DatagramPacket.getAddress()
+		try {
+			dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress
+					.getLocalHost(), 0);
+			assertTrue("Incorrect address returned", dp.getAddress().equals(
+					InetAddress.getLocalHost()));
+		} catch (Exception e) {
+			fail("Exception during getAddress test:" + e.toString());
+		}
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#getData()
+	 */
+	public void test_getData() {
+		// Test for method byte [] java.net.DatagramPacket.getData()
+
+		dp = new DatagramPacket("Hello".getBytes(), 5);
+		assertTrue("Incorrect length returned", new String(dp.getData(), 0, dp
+				.getData().length).equals("Hello"));
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#getLength()
+	 */
+	public void test_getLength() {
+		// Test for method int java.net.DatagramPacket.getLength()
+
+		dp = new DatagramPacket("Hello".getBytes(), 5);
+		assertTrue("Incorrect length returned", dp.getLength() == 5);
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#getOffset()
+	 */
+	public void test_getOffset() {
+		dp = new DatagramPacket("Hello".getBytes(), 3, 2);
+		assertTrue("Incorrect length returned", dp.getOffset() == 3);
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#getPort()
+	 */
+	public void test_getPort() {
+		// Test for method int java.net.DatagramPacket.getPort()
+		try {
+			dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress
+					.getLocalHost(), 1000);
+			assertTrue("Incorrect port returned", dp.getPort() == 1000);
+		} catch (Exception e) {
+			fail("Exception during getPort test : " + e.getMessage());
+		}
+
+		InetAddress localhost = null;
+		try {
+			localhost = InetAddress.getByName("localhost");
+		} catch (UnknownHostException e) {
+			fail("Unexpected UnknownHostException : " + e.getMessage());
+		}
+		final int port = Support_PortManager.getNextPort();
+		final Object lock = new Object();
+
+		Thread thread = new Thread(new Runnable() {
+			public void run() {
+				DatagramSocket socket = null;
+				try {
+					socket = new DatagramSocket(port);
+					synchronized (lock) {
+						started = true;
+						lock.notifyAll();
+					}
+					socket.setSoTimeout(3000);
+					DatagramPacket packet = new DatagramPacket(new byte[256],
+							256);
+					socket.receive(packet);
+					socket.send(packet);
+					socket.close();
+				} catch (IOException e) {
+					System.out.println("thread exception: " + e);
+					if (socket != null)
+						socket.close();
+				}
+			}
+		});
+		thread.start();
+
+		DatagramSocket socket = null;
+		try {
+			socket = new DatagramSocket(Support_PortManager.getNextPort());
+			socket.setSoTimeout(3000);
+			DatagramPacket packet = new DatagramPacket(new byte[] { 1, 2, 3, 4,
+					5, 6 }, 6, localhost, port);
+			synchronized (lock) {
+				try {
+					if (!started)
+						lock.wait();
+				} catch (InterruptedException e) {
+					fail(e.toString());
+				}
+			}
+			socket.send(packet);
+			socket.receive(packet);
+			socket.close();
+			assertTrue("datagram received wrong port: " + packet.getPort(),
+					packet.getPort() == port);
+		} catch (IOException e) {
+			if (socket != null)
+				socket.close();
+			System.err.println("port: " + port + " datagram server error: ");
+			e.printStackTrace();
+			fail("port : " + port + " datagram server error : "
+					+ e.getMessage());
+		}
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#setAddress(java.net.InetAddress)
+	 */
+	public void test_setAddressLjava_net_InetAddress() {
+		// Test for method void
+		// java.net.DatagramPacket.setAddress(java.net.InetAddress)
+		try {
+			InetAddress ia = InetAddress
+					.getByName(Support_Configuration.InetTestIP);
+			dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress
+					.getLocalHost(), 0);
+			dp.setAddress(ia);
+			assertTrue("Incorrect address returned", dp.getAddress().equals(ia));
+		} catch (Exception e) {
+			fail("Exception during getAddress test:" + e.toString());
+		}
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#setData(byte[], int, int)
+	 */
+	public void test_setData$BII() {
+		dp = new DatagramPacket("Hello".getBytes(), 5);
+		dp.setData("Wagga Wagga".getBytes(), 2, 3);
+		assertTrue("Incorrect data set", new String(dp.getData())
+				.equals("Wagga Wagga"));
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#setData(byte[])
+	 */
+	public void test_setData$B() {
+		// Test for method void java.net.DatagramPacket.setData(byte [])
+		dp = new DatagramPacket("Hello".getBytes(), 5);
+		dp.setData("Ralph".getBytes());
+		assertTrue("Incorrect data set", new String(dp.getData(), 0, dp
+				.getData().length).equals("Ralph"));
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#setLength(int)
+	 */
+	public void test_setLengthI() {
+		// Test for method void java.net.DatagramPacket.setLength(int)
+		dp = new DatagramPacket("Hello".getBytes(), 5);
+		dp.setLength(1);
+		assertTrue("Failed to set packet length", dp.getLength() == 1);
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#setPort(int)
+	 */
+	public void test_setPortI() {
+		// Test for method void java.net.DatagramPacket.setPort(int)
+		try {
+			dp = new DatagramPacket("Hello".getBytes(), 5, InetAddress
+					.getLocalHost(), 1000);
+			dp.setPort(2000);
+			assertTrue("Port not set", dp.getPort() == 2000);
+		} catch (Exception e) {
+			fail("Exception during setPort test : " + e.getMessage());
+		}
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int,
+	 *        java.net.SocketAddress)
+	 */
+	public void test_Constructor$BILjava_net_SocketAddress() {
+		class mySocketAddress extends SocketAddress {
+
+			public mySocketAddress() {
+			}
+		}
+
+		try {
+			// unsupported SocketAddress subclass
+			byte buf[] = new byte[1];
+			try {
+				DatagramPacket thePacket = new DatagramPacket(buf, 1,
+						new mySocketAddress());
+				assertFalse(
+						"No exception when constructing using unsupported SocketAddress subclass",
+						true);
+			} catch (IllegalArgumentException ex) {
+			}
+
+			// case were we try to pass in null
+			// unsupported SocketAddress subclass
+
+			try {
+				DatagramPacket thePacket = new DatagramPacket(buf, 1, null);
+				assertFalse(
+						"No exception when constructing address using null",
+						true);
+			} catch (IllegalArgumentException ex) {
+			}
+
+			// now validate we can construct
+			InetSocketAddress theAddress = new InetSocketAddress(InetAddress
+					.getLocalHost(), Support_PortManager.getNextPort());
+			DatagramPacket thePacket = new DatagramPacket(buf, 1, theAddress);
+			assertTrue("Socket address not set correctly (1)", theAddress
+					.equals(thePacket.getSocketAddress()));
+			assertTrue("Socket address not set correctly (2)", theAddress
+					.equals(new InetSocketAddress(thePacket.getAddress(),
+							thePacket.getPort())));
+		} catch (Exception e) {
+			fail("Exception during constructor test(1):" + e.toString());
+		}
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#DatagramPacket(byte[], int, int,
+	 *        java.net.SocketAddress)
+	 */
+	public void test_Constructor$BIILjava_net_SocketAddress() {
+		class mySocketAddress extends SocketAddress {
+
+			public mySocketAddress() {
+			}
+		}
+
+		try {
+			// unsupported SocketAddress subclass
+			byte buf[] = new byte[2];
+			try {
+				DatagramPacket thePacket = new DatagramPacket(buf, 1, 1,
+						new mySocketAddress());
+				assertFalse(
+						"No exception when constructing using unsupported SocketAddress subclass",
+						true);
+			} catch (IllegalArgumentException ex) {
+			}
+
+			// case were we try to pass in null
+			// unsupported SocketAddress subclass
+
+			try {
+				DatagramPacket thePacket = new DatagramPacket(buf, 1, 1, null);
+				assertFalse(
+						"No exception when constructing address using null",
+						true);
+			} catch (IllegalArgumentException ex) {
+			}
+
+			// now validate we can construct
+			InetSocketAddress theAddress = new InetSocketAddress(InetAddress
+					.getLocalHost(), Support_PortManager.getNextPort());
+			DatagramPacket thePacket = new DatagramPacket(buf, 1, 1, theAddress);
+			assertTrue("Socket address not set correctly (1)", theAddress
+					.equals(thePacket.getSocketAddress()));
+			assertTrue("Socket address not set correctly (2)", theAddress
+					.equals(new InetSocketAddress(thePacket.getAddress(),
+							thePacket.getPort())));
+			assertTrue("Offset not set correctly", thePacket.getOffset() == 1);
+		} catch (Exception e) {
+			fail("Exception during constructor test(2):" + e.toString());
+		}
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#getSocketAddress()
+	 */
+	public void test_getSocketAddress() {
+		try {
+			byte buf[] = new byte[1];
+			DatagramPacket thePacket = new DatagramPacket(buf, 1);
+
+			// validate get returns the value we set
+			InetSocketAddress theAddress = new InetSocketAddress(InetAddress
+					.getLocalHost(), Support_PortManager.getNextPort());
+			thePacket = new DatagramPacket(buf, 1);
+			thePacket.setSocketAddress(theAddress);
+			assertTrue("Socket address not set correctly (1)", theAddress
+					.equals(thePacket.getSocketAddress()));
+		} catch (Exception e) {
+			fail(
+					"Exception during getSocketAddress test:" + e.toString());
+		}
+	}
+
+	/**
+	 * @tests java.net.DatagramPacket#setSocketAddress(java.net.SocketAddress)
+	 */
+	public void test_setSocketAddressLjava_net_SocketAddress() {
+
+		class mySocketAddress extends SocketAddress {
+
+			public mySocketAddress() {
+			}
+		}
+
+		try {
+			// unsupported SocketAddress subclass
+			byte buf[] = new byte[1];
+			DatagramPacket thePacket = new DatagramPacket(buf, 1);
+			try {
+				thePacket.setSocketAddress(new mySocketAddress());
+				assertFalse(
+						"No exception when setting address using unsupported SocketAddress subclass",
+						true);
+			} catch (IllegalArgumentException ex) {
+			}
+
+			// case were we try to pass in null
+			// unsupported SocketAddress subclass
+			thePacket = new DatagramPacket(buf, 1);
+			try {
+				thePacket.setSocketAddress(null);
+				assertFalse("No exception when setting address using null",
+						true);
+			} catch (IllegalArgumentException ex) {
+			}
+
+			// now validate we can set it correctly
+			InetSocketAddress theAddress = new InetSocketAddress(InetAddress
+					.getLocalHost(), Support_PortManager.getNextPort());
+			thePacket = new DatagramPacket(buf, 1);
+			thePacket.setSocketAddress(theAddress);
+			assertTrue("Socket address not set correctly (1)", theAddress
+					.equals(thePacket.getSocketAddress()));
+			assertTrue("Socket address not set correctly (2)", theAddress
+					.equals(new InetSocketAddress(thePacket.getAddress(),
+							thePacket.getPort())));
+		} catch (Exception e) {
+			fail(
+					"Exception during setSocketAddress test:" + e.toString());
+		}
+	}
+
+	/**
+	 * 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() {
+	}
+
+	protected void doneSuite() {
+	}
+}



Mime
View raw message