Index: proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyChildClass.java =================================================================== --- proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyChildClass.java (revision 0) +++ proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyChildClass.java (revision 0) @@ -0,0 +1,21 @@ +package org.jboss.ejb3.test.proxy.lang; + +/** + * + * MyChildClass + * + * @author Jaikiran Pai + * @version $Revision: $ + */ +public class MyChildClass extends MyClass +{ + + /** + * + * @param i + */ + public void methodWithParamAndReturningVoid(Integer i) + { + //do nothing + } +} Index: proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyClass.java =================================================================== --- proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyClass.java (revision 0) +++ proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyClass.java (revision 0) @@ -0,0 +1,123 @@ +package org.jboss.ejb3.test.proxy.lang; + +import java.lang.reflect.Method; +import java.util.List; + +import org.jboss.ejb3.proxy.lang.SerializableMethod; +import org.jboss.ejb3.test.proxy.lang.unit.SerializableMethodTestCase; + +/** + * + * MyClass - Helper class for getting various {@link Method}s for testing + * {@link SerializableMethod} + * + * @author Jaikiran Pai + * @version $Revision: $ + * @see {@link SerializableMethodTestCase} + */ +public class MyClass +{ + + /** + * + */ + public void methodWithNoParamAndReturningVoid() + { + //do nothing + } + + /** + * + * @param i + */ + public void methodWithParamAndReturningVoid(Integer i) + { + //do nothing + } + + /** + * + * @param s + */ + public void methodWithParamAndReturningVoid(String s) + { + //do nothing + } + + /** + * + * @param s + */ + public void methodWithParamAndReturningVoid(int primitive) + { + //do nothing + } + + /** + * + * @param obj + */ + public void methodWithParamAndReturningVoid(Object obj) + { + //do nothing + } + + /** + * + */ + public String toString() + { + return this.getClass().getName(); + } + + /** + * + * @param a + * @return + */ + public int methodAcceptingArrayOfPrimitives(int[] a) + { + return a[0]; + } + + /** + * + * @param a + * @return + */ + public Object methodAcceptingArrayOfObjects(Object[] objs) + { + return objs[0]; + } + + /** + * + * @param list + * @return + */ + public Class methodWithGenerics(List list, int i) + { + return null; + } + + /** + * + * @param i + * @return + */ + public Integer methodReturingInteger(Integer i) + { + return null; + } + + /** + * + * @param m + * @return + */ + public MyClass methodAcceptingMyClass(MyClass m) + { + return m; + } + +} Index: proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/SerializationUtil.java =================================================================== --- proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/SerializationUtil.java (revision 0) +++ proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/SerializationUtil.java (revision 0) @@ -0,0 +1,42 @@ +package org.jboss.ejb3.test.proxy.lang; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +/** + * + * SerializationUtil - Utility class for creating a copy of {@link Serializable} object + * by serializing and de-serializing the object. + * + * @author Jaikiran Pai + * @version $Revision: $ + */ +public class SerializationUtil +{ + + /** + * Creates a copy of the originalObject by serializing/de-serializing + * the object. + * + * @param originalObject The object whose copy will be created + * @return Returns a copy of the originalObject + * + * @throws Exception + */ + public static Serializable getCopy(Serializable originalObject) throws Exception + { + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); + objectOutputStream.writeObject(originalObject); + + ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); + ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); + Object copyOfObject = objectInputStream.readObject(); + + return (Serializable) copyOfObject; + } +} Index: proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyChildClass.java =================================================================== --- proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyChildClass.java (revision 0) +++ proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyChildClass.java (revision 0) @@ -0,0 +1,21 @@ +package org.jboss.ejb3.test.proxy.lang; + +/** + * + * MyChildClass + * + * @author Jaikiran Pai + * @version $Revision: $ + */ +public class MyChildClass extends MyClass +{ + + /** + * + * @param i + */ + public void methodWithParamAndReturningVoid(Integer i) + { + //do nothing + } +} Index: proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyClass.java =================================================================== --- proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyClass.java (revision 0) +++ proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/MyClass.java (revision 0) @@ -0,0 +1,123 @@ +package org.jboss.ejb3.test.proxy.lang; + +import java.lang.reflect.Method; +import java.util.List; + +import org.jboss.ejb3.proxy.lang.SerializableMethod; +import org.jboss.ejb3.test.proxy.lang.unit.SerializableMethodTestCase; + +/** + * + * MyClass - Helper class for getting various {@link Method}s for testing + * {@link SerializableMethod} + * + * @author Jaikiran Pai + * @version $Revision: $ + * @see {@link SerializableMethodTestCase} + */ +public class MyClass +{ + + /** + * + */ + public void methodWithNoParamAndReturningVoid() + { + //do nothing + } + + /** + * + * @param i + */ + public void methodWithParamAndReturningVoid(Integer i) + { + //do nothing + } + + /** + * + * @param s + */ + public void methodWithParamAndReturningVoid(String s) + { + //do nothing + } + + /** + * + * @param s + */ + public void methodWithParamAndReturningVoid(int primitive) + { + //do nothing + } + + /** + * + * @param obj + */ + public void methodWithParamAndReturningVoid(Object obj) + { + //do nothing + } + + /** + * + */ + public String toString() + { + return this.getClass().getName(); + } + + /** + * + * @param a + * @return + */ + public int methodAcceptingArrayOfPrimitives(int[] a) + { + return a[0]; + } + + /** + * + * @param a + * @return + */ + public Object methodAcceptingArrayOfObjects(Object[] objs) + { + return objs[0]; + } + + /** + * + * @param list + * @return + */ + public Class methodWithGenerics(List list, int i) + { + return null; + } + + /** + * + * @param i + * @return + */ + public Integer methodReturingInteger(Integer i) + { + return null; + } + + /** + * + * @param m + * @return + */ + public MyClass methodAcceptingMyClass(MyClass m) + { + return m; + } + +} Index: proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/SerializationUtil.java =================================================================== --- proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/SerializationUtil.java (revision 0) +++ proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/SerializationUtil.java (revision 0) @@ -0,0 +1,42 @@ +package org.jboss.ejb3.test.proxy.lang; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.io.Serializable; + +/** + * + * SerializationUtil - Utility class for creating a copy of {@link Serializable} object + * by serializing and de-serializing the object. + * + * @author Jaikiran Pai + * @version $Revision: $ + */ +public class SerializationUtil +{ + + /** + * Creates a copy of the originalObject by serializing/de-serializing + * the object. + * + * @param originalObject The object whose copy will be created + * @return Returns a copy of the originalObject + * + * @throws Exception + */ + public static Serializable getCopy(Serializable originalObject) throws Exception + { + + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); + objectOutputStream.writeObject(originalObject); + + ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); + ObjectInputStream objectInputStream = new ObjectInputStream(inputStream); + Object copyOfObject = objectInputStream.readObject(); + + return (Serializable) copyOfObject; + } +} Index: proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/unit/SerializableMethodTestCase.java =================================================================== --- proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/unit/SerializableMethodTestCase.java (revision 0) +++ proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/unit/SerializableMethodTestCase.java (revision 0) @@ -0,0 +1,587 @@ +package org.jboss.ejb3.test.proxy.lang.unit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Method; +import java.util.List; + +import org.jboss.ejb3.proxy.lang.SerializableMethod; +import org.jboss.ejb3.test.proxy.lang.MyChildClass; +import org.jboss.ejb3.test.proxy.lang.MyClass; +import org.jboss.ejb3.test.proxy.lang.SerializationUtil; +import org.jboss.logging.Logger; +import org.junit.Test; + +/** + * + * SerializableMethodTestCase for testing {@link SerializableMethod} + * + * @author Jaikiran Pai + * @version $Revision: $ + * + * @see {@link SerializableMethod} + */ +public class SerializableMethodTestCase +{ + + /** + * Instance of logger + */ + private static Logger logger = Logger.getLogger(SerializableMethodTestCase.class); + + /** + * + */ + private static MyClass myClass = new MyClass(); + + /** + * Test the {@link SerializableMethod#equals(Object)} and {@link SerializableMethod#hashCode()} + * with simple method which accepts no parameter and void return type + * + * @throws Throwable + */ + @Test + public void testEqualsAndHashCodeForMethodWithNoParam() throws Throwable + { + + // Test that the SerializableMethod instances, created + // for the same method, are equal and have same hashCode. + // Intention of this test is to ensure that the methods accepting no parameter are handled correctly. + + Method methodWithNoParamAndReturningVoid = myClass.getClass() + .getMethod("methodWithNoParamAndReturningVoid", null); + SerializableMethod serializableMethod = new SerializableMethod(methodWithNoParamAndReturningVoid); + SerializableMethod anotherSerializableMethod = new SerializableMethod(methodWithNoParamAndReturningVoid); + + // These 2 SerializableMethod instances should be equal, as they were created for the same Method + assertTrue("Failure - Two SerializableMethod instances created out of the same Method are not equal", + serializableMethod.equals(anotherSerializableMethod)); + // test hashCode + assertEquals( + "Failure - hashCode does not match for the two SerializableMethod instances created out of the same Method", + serializableMethod.hashCode(), anotherSerializableMethod.hashCode()); + + } + + /** + * Test the {@link SerializableMethod#equals(Object)} and {@link SerializableMethod#hashCode()} + * for methods which accept parameters + * + * @throws Throwable + */ + @Test + public void testEqualsAndHashCodeForMethodsWithParam() throws Throwable + { + + // Test that the SerializableMethod instances, created + // for the same method which accepts a array of primitives, are equal and have same hashCode. + // Intention of this test is to ensure that the methods accepting array of primitives param are handled correctly. + + Method methodAcceptingArrayOfPrimitives = myClass.getClass().getMethod("methodAcceptingArrayOfPrimitives", + new Class[] + {int[].class}); + SerializableMethod serializableMethod_arrayOfPrimitives = new SerializableMethod(methodAcceptingArrayOfPrimitives); + SerializableMethod anotherSerializableMethod_arrayOfPrimitives = new SerializableMethod( + methodAcceptingArrayOfPrimitives); + + // test equals + assertTrue( + "Failure - Two SerializableMethod instances created out of the same Method(accepting primitive array parameter) are not equal", + serializableMethod_arrayOfPrimitives.equals(anotherSerializableMethod_arrayOfPrimitives)); + // test hashCode + assertEquals( + "Failure - hashCode does not match for instances created out of the same Method(accepting primitive array parameter)", + serializableMethod_arrayOfPrimitives.hashCode(), anotherSerializableMethod_arrayOfPrimitives.hashCode()); + + // Test that the SerializableMethod instances, created + // for the same method which accepts a array of Object, are equal and have same hashCode. + // Intention of this test is to ensure that methods accepting array of Object types are handled correctly. + + Method methodAcceptingArrayOfObjects = myClass.getClass().getMethod("methodAcceptingArrayOfObjects", new Class[] + {Object[].class}); + SerializableMethod serializableMethod_arrayOfObjects = new SerializableMethod(methodAcceptingArrayOfObjects); + SerializableMethod anotherSerializableMethod_arrayOfObjects = new SerializableMethod( + methodAcceptingArrayOfObjects); + + // test equals + assertTrue( + "Failure - Two SerializableMethod instances created out of the same Method(accepting Object array parameter) are not equal", + serializableMethod_arrayOfObjects.equals(anotherSerializableMethod_arrayOfObjects)); + // test hashCode + assertEquals( + "Failure - hashCode does not match for instances created out of the same Method(accepting Object array parameter)", + serializableMethod_arrayOfObjects.hashCode(), anotherSerializableMethod_arrayOfObjects.hashCode()); + + } + + /** + * Test the {@link SerializableMethod#equals(Object)} + * for methods which are overloaded + * + * @throws Throwable + */ + @Test + public void testEqualsForOverloadedMethod() throws Throwable + { + + // Test that the SerializableMethod instances created + // for overloaded methods (one with String param and one with Integer param) are NOT equal + // Intention of this test is to ensure that two overloaded methods are NOT equal + + Method methodAcceptingStringParamAndReturingVoid = myClass.getClass().getMethod( + "methodWithParamAndReturningVoid", new Class[] + {String.class}); + Method methodAcceptingIntegerParamAndReturingVoid = myClass.getClass().getMethod( + "methodWithParamAndReturningVoid", new Class[] + {Integer.class}); + + SerializableMethod serializableMethod = new SerializableMethod(methodAcceptingStringParamAndReturingVoid); + SerializableMethod anotherSerializableMethod = new SerializableMethod(methodAcceptingIntegerParamAndReturingVoid); + + // test the equals + assertFalse("Failure - Two Serializable method instances created for 2 different overloaded methods are equal", + serializableMethod.equals(anotherSerializableMethod)); + + // Test that the SerializableMethod instances created + // for overloaded methods (one with primitive param and one with Object param) are NOT equal + // Intention of this test is to ensure that two overloaded methods are NOT equal + + Method methodAcceptingPrimitiveIntAndReturningVoid = myClass.getClass().getMethod( + "methodWithParamAndReturningVoid", new Class[] + {int.class}); + + SerializableMethod serializableMethod_PrimitiveIntParam = new SerializableMethod( + methodAcceptingPrimitiveIntAndReturningVoid); + SerializableMethod serializableMethod_IntegerParam = new SerializableMethod( + methodAcceptingIntegerParamAndReturingVoid); + + // test the equals + assertFalse( + "Failure - Two SerializableMethod instances one with primitive int param and one with Integer param are equal", + serializableMethod_PrimitiveIntParam.equals(serializableMethod_IntegerParam)); + + // Test that the SerializableMethod instances created + // for overloaded methods (one with Integer and one with base class Object param) are NOT equal + // Intention of this test is to ensure that two overloaded methods are NOT equal + + Method methodAcceptingObject = myClass.getClass().getMethod("methodWithParamAndReturningVoid", new Class[] + {Object.class}); + SerializableMethod serializableMethod_ObjectParam = new SerializableMethod(methodAcceptingObject); + + // test the equals + assertFalse( + "Failure - Two SerializableMethod instances one with Integer param and one with Object param are equal", + serializableMethod_IntegerParam.equals(serializableMethod_ObjectParam)); + + } + + /** + * Test the {@link SerializableMethod#equals(Object)} + * for method which have the same name, parameters but belong to different classes + * + * @throws Throwable + */ + @Test + public void testEqualsForSameMethodBelongingToDifferentClass() throws Throwable + { + + // Test that the SerializableMethod instances created + // for a method with same name, param and return type, but belonging to different classes are NOT equal + + Method toStringMethodOfMyClass = myClass.getClass().getDeclaredMethod("toString", null); + Method toStringMethodOfObject = Object.class.getDeclaredMethod("toString", null); + + SerializableMethod serializableMethod_toStringForMyClass = new SerializableMethod(toStringMethodOfMyClass); + SerializableMethod serializableMethod_toStringForThisTestCase = new SerializableMethod(toStringMethodOfObject); + + // test the equals + assertFalse("Failure - Two SerializableMethod instances for same methods from two different classes are equal", + serializableMethod_toStringForMyClass.equals(serializableMethod_toStringForThisTestCase)); + + } + + /** + * Test the {@link SerializableMethod#equals(Object)} and {@link SerializableMethod#hashCode()} + * on two instances of {@link SerializableMethod}, one of which is a copy created by serialization/de-serialization + * + * @throws Throwable + * @see {@link SerializationUtil} + */ + @Test + public void testEqualsAndHashCodeWithSerialization() throws Throwable + { + + // Test that the serialized version of the SerializableMethod is equal to the original + // instance and has the same hashCode + + Method methodWithParam = myClass.getClass().getMethod("methodAcceptingArrayOfObjects", new Class[] + {Object[].class}); + + SerializableMethod serializableMethod = new SerializableMethod(methodWithParam); + + SerializableMethod copyOfSerializableMethod = (SerializableMethod) SerializationUtil.getCopy(serializableMethod); + + // test equals + assertTrue("Failure - equals fails on serialized-deserialized instance and its original instance", + serializableMethod.equals(copyOfSerializableMethod)); + + // test hashCode + assertEquals("Failure - Serialized-deserialized instance has a different hashCode", + serializableMethod.hashCode(), copyOfSerializableMethod.hashCode()); + + // One more similar test with a method accepting non-serializable param + + Method method = myClass.getClass().getMethod("methodAcceptingMyClass", new Class[] + {MyClass.class}); + SerializableMethod serializableMethod_nonSerializableParam = new SerializableMethod(method); + + SerializableMethod copyOfSerializableMethod_nonSerilizableParam = (SerializableMethod) SerializationUtil + .getCopy(serializableMethod_nonSerializableParam); + + // test equals + assertTrue( + "Failure - equals fails on serialized-deserialized instance and its original instance which contained non-serializable param", + serializableMethod_nonSerializableParam.equals(copyOfSerializableMethod_nonSerilizableParam)); + + // test hashCode + assertEquals("Failure - hashCode does not match for serialized-deserialized instance and original instance", + serializableMethod_nonSerializableParam.hashCode(), copyOfSerializableMethod_nonSerilizableParam.hashCode()); + + } + + /** + * + * Test the {@link SerializableMethod#equals(Object)} and {@link SerializableMethod#hashCode()} + * with methods belonging to parent/child classes + * + * @throws Throwable + */ + @Test + public void testEqualsAndHashCodeWithInheritedClasses() throws Throwable + { + + // Test that the SerializableMethod instances created + // for a method from the same base class are equal and have the same hashCode + + // Note that this testcase should NOT override the toString method, to ensure that the getMethod() returns + // the 'Method' of Object.class + + Method toStringMethodOfThisClass = this.getClass().getMethod("toString", null); + Method toStringMethodOfObject = Object.class.getMethod("toString", null); + + SerializableMethod serializableMethod_toStringOfThisClass = new SerializableMethod(toStringMethodOfThisClass); + SerializableMethod serializableMethod_toStringOfObjectClass = new SerializableMethod(toStringMethodOfObject); + + // test equals + assertTrue("Failure - Two SerializableMethod instances of method belonging to the same base class are not equal", + serializableMethod_toStringOfObjectClass.equals(serializableMethod_toStringOfThisClass)); + + // test hashCode + assertEquals( + "Failure - Two SerializableMethod instances of method belonging to the same base class have different hashCode", + serializableMethod_toStringOfObjectClass.hashCode(), serializableMethod_toStringOfThisClass.hashCode()); + + // Test that the SerializableMethod instances created + // for overridden methods are NOT equal + + Method methodFromChildClass = MyChildClass.class.getDeclaredMethod("methodWithParamAndReturningVoid", new Class[] + {Integer.class}); + Method methodFromParentClass = MyClass.class.getDeclaredMethod("methodWithParamAndReturningVoid", new Class[] + {Integer.class}); + + SerializableMethod serializableMethodForChild = new SerializableMethod(methodFromChildClass); + SerializableMethod serializableMethodForParent = new SerializableMethod(methodFromParentClass); + + // test equals + assertFalse("Failure - The SerializableMethod instances of method from base class and child class are equal", + serializableMethodForChild.equals(serializableMethodForParent)); + + } + + /** + * Test the {@link SerializableMethod#equals(Object)} and {@link SerializableMethod#hashCode()} + * with methods involving generics + * + * @throws Throwable + */ + @Test + public void testEqualsAndHashCodeWithGenerics() throws Throwable + { + + // Just a simple test to ensure the SerializableMethod works fine with methods + // involving generics + + Method genericsMethod = myClass.getClass().getMethod("methodWithGenerics", new Class[] + {List.class, int.class}); + Method anotherGenericMethod = myClass.getClass().getMethod("methodWithGenerics", new Class[] + {List.class, int.class}); + + SerializableMethod serializableMethod = new SerializableMethod(genericsMethod); + SerializableMethod anotherSerializableMethod = new SerializableMethod(anotherGenericMethod); + + // test equals + assertTrue("Failure - Two SerializableMethod instances for a method involving generics are not equal", + serializableMethod.equals(anotherSerializableMethod)); + + // test hashCode + assertEquals( + "Failure - Two SerializableMethod instances for a method involving generics have different hashCode", + serializableMethod.hashCode(), anotherSerializableMethod.hashCode()); + + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * with simple methods + * + * @throws Throwable + */ + @Test + public void testToMethodWithoutClassloader() throws Throwable + { + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // does not involve any primitive parameters + + logger.info("Testing the toMethod(), for methods without primitive params"); + + Method methodWithoutPrimitivesParams = myClass.getClass().getMethod("methodWithParamAndReturningVoid", + new Class[] + {Integer.class}); + SerializableMethod serializableMethod = new SerializableMethod(methodWithoutPrimitivesParams); + // invoke the toMethod() + Method copyOfMethodWithoutPrimitiveParams = serializableMethod.toMethod(); + + // test equals + assertTrue( + "Failure - The method returned by toMethod() of SerializableMethod is not equal to the original method", + methodWithoutPrimitivesParams.equals(copyOfMethodWithoutPrimitiveParams)); + + // also test that the hashCode of the original method and the returned method are the same. + assertEquals("Failure - hashCode does not match for method returnd by toMethod()", methodWithoutPrimitivesParams + .hashCode(), copyOfMethodWithoutPrimitiveParams.hashCode()); + + logger.info("Completed testing the toMethod(), for methods without primitive params"); + + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * with methods involving generics + * + * @throws Throwable + */ + @Test + public void testToMethodWithoutClassloaderAndInvolvingGenerics() throws Throwable + { + + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // involves generics + + logger.info("Testing the toMethod(), for methods involving generics"); + + Method methodWithGenerics = myClass.getClass().getMethod("methodWithGenerics", new Class[] + {List.class, int.class}); + SerializableMethod serializableMethod = new SerializableMethod(methodWithGenerics); + + Method copyOfMethodWithGenerics = serializableMethod.toMethod(); + + // test equals + assertTrue( + "Failure - The method returned by toMethod() of SerializableMethod is not equal to the original method", + methodWithGenerics.equals(copyOfMethodWithGenerics)); + + // also test that the hashCode of the original method and the returned method are the same. + assertEquals("Failure - hashCode does not match for method returnd by toMethod()", methodWithGenerics.hashCode(), + copyOfMethodWithGenerics.hashCode()); + + logger.info("Completed testing the toMethod, for methods involving generics"); + + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * with methods accepting primitive parameters + * + * @throws Throwable + */ + @Test + public void testToMethodWithoutClassloaderAndInvolvingPrimitiveParams() throws Throwable + { + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // involves primitive params + + logger.info("Testing the toMethod(), for methods accepting primitives"); + + Method method = myClass.getClass().getMethod("methodWithParamAndReturningVoid", new Class[] + {int.class}); + SerializableMethod serializableMethod = new SerializableMethod(method); + // invoke the toMethod() + Method copyOfMethod = serializableMethod.toMethod(); + + // test equals + assertTrue( + "Failure - The method returned by toMethod() of SerializableMethod is not equal to the original method", + method.equals(copyOfMethod)); + // also test the hashCode + assertEquals("Failure - hashCode of the original method and the method returned by toMethod() dont match", method + .hashCode(), copyOfMethod.hashCode()); + + logger.info("Completed testing the toMethod(), for methods accepting primitives"); + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * + * @throws Throwable + */ + @Test + public void testToMethodWithoutClassloaderAndInvolvingReturnTypes() throws Throwable + { + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // returns some Object + + logger.info("Testing the toMethod(), for methods returing Object"); + + Method methodReturningInteger = myClass.getClass().getMethod("methodReturingInteger", new Class[] + {Integer.class}); + SerializableMethod serializableMethod = new SerializableMethod(methodReturningInteger); + // invoke the toMethod() + Method copyOfMethodReturingInteger = serializableMethod.toMethod(); + + // test equals + assertTrue( + "Failure - The method returned by toMethod() of SerializableMethod is not equal to the original method", + methodReturningInteger.equals(copyOfMethodReturingInteger)); + // also test the hashCode + assertEquals("Failure - hashCode of the original method and the method returned by toMethod() dont match", + methodReturningInteger.hashCode(), copyOfMethodReturingInteger.hashCode()); + + logger.info("Completed testing the toMethod(), for methods returing Object"); + + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * with methods accepting array parameters + * + * @throws Throwable + */ + @Test + public void testToMethodWithoutClassloaderAndInvolvingArrayParams() throws Throwable + { + + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // accepts array of primitives + + logger.info("Testing the toMethod(), for methods accepting array of primitives"); + + Method methodAcceptingArrayOfPrimitives = myClass.getClass().getMethod("methodAcceptingArrayOfPrimitives", + new Class[] + {int[].class}); + SerializableMethod serializableMethod = new SerializableMethod(methodAcceptingArrayOfPrimitives); + // invoke the toMethod() + Method copyOfMethodAcceptingArrayOfPrimitives = serializableMethod.toMethod(); + + //test equals + assertTrue( + "Failure - The method returned by toMethod() of SerializableMethod is not equal to the original method", + methodAcceptingArrayOfPrimitives.equals(copyOfMethodAcceptingArrayOfPrimitives)); + + // test hashCode + assertEquals("Failure - The method returned by toMethod() has a different hashCode than the original method", + methodAcceptingArrayOfPrimitives.hashCode(), copyOfMethodAcceptingArrayOfPrimitives.hashCode()); + + logger.info("Completed testing the toMethod(), for methods accepting array of primitives"); + + logger.info("Testing the toMethod(), for methods accepting array of Objects"); + + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // accepts array of Objects + + Method methodAcceptingArrayOfObjects = myClass.getClass().getMethod("methodAcceptingArrayOfObjects", new Class[] + {Object[].class}); + SerializableMethod serializableMethod_arrayOfObjParams = new SerializableMethod(methodAcceptingArrayOfObjects); + //invoke the toMethod() + Method copyOfMethodAcceptingArrayOfObjects = serializableMethod_arrayOfObjParams.toMethod(); + + // test equals + assertTrue("Failure - The method returned by toMethod() of SerializableMethod is not equal to original method", + methodAcceptingArrayOfObjects.equals(copyOfMethodAcceptingArrayOfObjects)); + + // test hashCode + assertEquals("Failure -hashCode does not match for the method returned by toMethod()", + methodAcceptingArrayOfObjects.hashCode(), copyOfMethodAcceptingArrayOfObjects.hashCode()); + + logger.info("Completed testing the toMethod(), for methods accepting array of Objects"); + + } + + /** + * Test the {@link SerializableMethod#toMethod(ClassLoader)} + * + * @throws Throwable + */ + @Test + public void testToMethodWithClassloader() throws Throwable + { + + // Test the toMethod(Classloader) works fine + + logger.info("Testing the toMethod(Classloader)"); + + Method method = myClass.getClass().getMethod("methodAcceptingMyClass", new Class[] + {MyClass.class}); + SerializableMethod serializableMethod = new SerializableMethod(method); + + // invoke toMethod + Method copyOfMethod = serializableMethod.toMethod(this.getClass().getClassLoader()); + + // test equals + assertTrue("Failure - equals fails with classsloader passed to toMethod", method.equals(copyOfMethod)); + + // test hashCode + assertEquals("Failure - hashCode does not match when classloader is passed to toMethod", method.hashCode(), + copyOfMethod.hashCode()); + + logger.info("Completed testing the toMethod(Classloader)"); + + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * on a serialized/de-serialized instance of {@link SerializableMethod} + * + * @throws Throwable + * @see {@link SerializationUtil} + */ + @Test + public void testToMethodWithSerialization() throws Throwable + { + // Test the toMethod() to ensure that the serialized/deserialized + // instance of the SerializableMethod returns the correct Method + + logger.info("Testing toMethod() with serialization"); + + Method method = myClass.getClass().getMethod("methodAcceptingMyClass", new Class[] + {MyClass.class}); + + SerializableMethod serializableMethod = new SerializableMethod(method); + + // now make a copy through serialization/de-serialization + SerializableMethod copyOfSerializableMethod = (SerializableMethod) SerializationUtil.getCopy(serializableMethod); + Method copyOfMethod = copyOfSerializableMethod.toMethod(); + + // test equals + assertTrue("Failure - equals fails for toMethod when serialized", method.equals(copyOfMethod)); + + // test hashCode + assertEquals("Failure - hashCode does not match for Method returned by toMethod, when serialized", method + .hashCode(), copyOfMethod.hashCode()); + + logger.info("Completed testing toMethod() with serialization"); + + logger.info("Completed testing toMethod() with serialization"); + } +} Index: proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/unit/SerializableMethodTestCase.java =================================================================== --- proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/unit/SerializableMethodTestCase.java (revision 0) +++ proxy/src/test/java/org/jboss/ejb3/test/proxy/lang/unit/SerializableMethodTestCase.java (revision 0) @@ -0,0 +1,587 @@ +package org.jboss.ejb3.test.proxy.lang.unit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.lang.reflect.Method; +import java.util.List; + +import org.jboss.ejb3.proxy.lang.SerializableMethod; +import org.jboss.ejb3.test.proxy.lang.MyChildClass; +import org.jboss.ejb3.test.proxy.lang.MyClass; +import org.jboss.ejb3.test.proxy.lang.SerializationUtil; +import org.jboss.logging.Logger; +import org.junit.Test; + +/** + * + * SerializableMethodTestCase for testing {@link SerializableMethod} + * + * @author Jaikiran Pai + * @version $Revision: $ + * + * @see {@link SerializableMethod} + */ +public class SerializableMethodTestCase +{ + + /** + * Instance of logger + */ + private static Logger logger = Logger.getLogger(SerializableMethodTestCase.class); + + /** + * + */ + private static MyClass myClass = new MyClass(); + + /** + * Test the {@link SerializableMethod#equals(Object)} and {@link SerializableMethod#hashCode()} + * with simple method which accepts no parameter and void return type + * + * @throws Throwable + */ + @Test + public void testEqualsAndHashCodeForMethodWithNoParam() throws Throwable + { + + // Test that the SerializableMethod instances, created + // for the same method, are equal and have same hashCode. + // Intention of this test is to ensure that the methods accepting no parameter are handled correctly. + + Method methodWithNoParamAndReturningVoid = myClass.getClass() + .getMethod("methodWithNoParamAndReturningVoid", null); + SerializableMethod serializableMethod = new SerializableMethod(methodWithNoParamAndReturningVoid); + SerializableMethod anotherSerializableMethod = new SerializableMethod(methodWithNoParamAndReturningVoid); + + // These 2 SerializableMethod instances should be equal, as they were created for the same Method + assertTrue("Failure - Two SerializableMethod instances created out of the same Method are not equal", + serializableMethod.equals(anotherSerializableMethod)); + // test hashCode + assertEquals( + "Failure - hashCode does not match for the two SerializableMethod instances created out of the same Method", + serializableMethod.hashCode(), anotherSerializableMethod.hashCode()); + + } + + /** + * Test the {@link SerializableMethod#equals(Object)} and {@link SerializableMethod#hashCode()} + * for methods which accept parameters + * + * @throws Throwable + */ + @Test + public void testEqualsAndHashCodeForMethodsWithParam() throws Throwable + { + + // Test that the SerializableMethod instances, created + // for the same method which accepts a array of primitives, are equal and have same hashCode. + // Intention of this test is to ensure that the methods accepting array of primitives param are handled correctly. + + Method methodAcceptingArrayOfPrimitives = myClass.getClass().getMethod("methodAcceptingArrayOfPrimitives", + new Class[] + {int[].class}); + SerializableMethod serializableMethod_arrayOfPrimitives = new SerializableMethod(methodAcceptingArrayOfPrimitives); + SerializableMethod anotherSerializableMethod_arrayOfPrimitives = new SerializableMethod( + methodAcceptingArrayOfPrimitives); + + // test equals + assertTrue( + "Failure - Two SerializableMethod instances created out of the same Method(accepting primitive array parameter) are not equal", + serializableMethod_arrayOfPrimitives.equals(anotherSerializableMethod_arrayOfPrimitives)); + // test hashCode + assertEquals( + "Failure - hashCode does not match for instances created out of the same Method(accepting primitive array parameter)", + serializableMethod_arrayOfPrimitives.hashCode(), anotherSerializableMethod_arrayOfPrimitives.hashCode()); + + // Test that the SerializableMethod instances, created + // for the same method which accepts a array of Object, are equal and have same hashCode. + // Intention of this test is to ensure that methods accepting array of Object types are handled correctly. + + Method methodAcceptingArrayOfObjects = myClass.getClass().getMethod("methodAcceptingArrayOfObjects", new Class[] + {Object[].class}); + SerializableMethod serializableMethod_arrayOfObjects = new SerializableMethod(methodAcceptingArrayOfObjects); + SerializableMethod anotherSerializableMethod_arrayOfObjects = new SerializableMethod( + methodAcceptingArrayOfObjects); + + // test equals + assertTrue( + "Failure - Two SerializableMethod instances created out of the same Method(accepting Object array parameter) are not equal", + serializableMethod_arrayOfObjects.equals(anotherSerializableMethod_arrayOfObjects)); + // test hashCode + assertEquals( + "Failure - hashCode does not match for instances created out of the same Method(accepting Object array parameter)", + serializableMethod_arrayOfObjects.hashCode(), anotherSerializableMethod_arrayOfObjects.hashCode()); + + } + + /** + * Test the {@link SerializableMethod#equals(Object)} + * for methods which are overloaded + * + * @throws Throwable + */ + @Test + public void testEqualsForOverloadedMethod() throws Throwable + { + + // Test that the SerializableMethod instances created + // for overloaded methods (one with String param and one with Integer param) are NOT equal + // Intention of this test is to ensure that two overloaded methods are NOT equal + + Method methodAcceptingStringParamAndReturingVoid = myClass.getClass().getMethod( + "methodWithParamAndReturningVoid", new Class[] + {String.class}); + Method methodAcceptingIntegerParamAndReturingVoid = myClass.getClass().getMethod( + "methodWithParamAndReturningVoid", new Class[] + {Integer.class}); + + SerializableMethod serializableMethod = new SerializableMethod(methodAcceptingStringParamAndReturingVoid); + SerializableMethod anotherSerializableMethod = new SerializableMethod(methodAcceptingIntegerParamAndReturingVoid); + + // test the equals + assertFalse("Failure - Two Serializable method instances created for 2 different overloaded methods are equal", + serializableMethod.equals(anotherSerializableMethod)); + + // Test that the SerializableMethod instances created + // for overloaded methods (one with primitive param and one with Object param) are NOT equal + // Intention of this test is to ensure that two overloaded methods are NOT equal + + Method methodAcceptingPrimitiveIntAndReturningVoid = myClass.getClass().getMethod( + "methodWithParamAndReturningVoid", new Class[] + {int.class}); + + SerializableMethod serializableMethod_PrimitiveIntParam = new SerializableMethod( + methodAcceptingPrimitiveIntAndReturningVoid); + SerializableMethod serializableMethod_IntegerParam = new SerializableMethod( + methodAcceptingIntegerParamAndReturingVoid); + + // test the equals + assertFalse( + "Failure - Two SerializableMethod instances one with primitive int param and one with Integer param are equal", + serializableMethod_PrimitiveIntParam.equals(serializableMethod_IntegerParam)); + + // Test that the SerializableMethod instances created + // for overloaded methods (one with Integer and one with base class Object param) are NOT equal + // Intention of this test is to ensure that two overloaded methods are NOT equal + + Method methodAcceptingObject = myClass.getClass().getMethod("methodWithParamAndReturningVoid", new Class[] + {Object.class}); + SerializableMethod serializableMethod_ObjectParam = new SerializableMethod(methodAcceptingObject); + + // test the equals + assertFalse( + "Failure - Two SerializableMethod instances one with Integer param and one with Object param are equal", + serializableMethod_IntegerParam.equals(serializableMethod_ObjectParam)); + + } + + /** + * Test the {@link SerializableMethod#equals(Object)} + * for method which have the same name, parameters but belong to different classes + * + * @throws Throwable + */ + @Test + public void testEqualsForSameMethodBelongingToDifferentClass() throws Throwable + { + + // Test that the SerializableMethod instances created + // for a method with same name, param and return type, but belonging to different classes are NOT equal + + Method toStringMethodOfMyClass = myClass.getClass().getDeclaredMethod("toString", null); + Method toStringMethodOfObject = Object.class.getDeclaredMethod("toString", null); + + SerializableMethod serializableMethod_toStringForMyClass = new SerializableMethod(toStringMethodOfMyClass); + SerializableMethod serializableMethod_toStringForThisTestCase = new SerializableMethod(toStringMethodOfObject); + + // test the equals + assertFalse("Failure - Two SerializableMethod instances for same methods from two different classes are equal", + serializableMethod_toStringForMyClass.equals(serializableMethod_toStringForThisTestCase)); + + } + + /** + * Test the {@link SerializableMethod#equals(Object)} and {@link SerializableMethod#hashCode()} + * on two instances of {@link SerializableMethod}, one of which is a copy created by serialization/de-serialization + * + * @throws Throwable + * @see {@link SerializationUtil} + */ + @Test + public void testEqualsAndHashCodeWithSerialization() throws Throwable + { + + // Test that the serialized version of the SerializableMethod is equal to the original + // instance and has the same hashCode + + Method methodWithParam = myClass.getClass().getMethod("methodAcceptingArrayOfObjects", new Class[] + {Object[].class}); + + SerializableMethod serializableMethod = new SerializableMethod(methodWithParam); + + SerializableMethod copyOfSerializableMethod = (SerializableMethod) SerializationUtil.getCopy(serializableMethod); + + // test equals + assertTrue("Failure - equals fails on serialized-deserialized instance and its original instance", + serializableMethod.equals(copyOfSerializableMethod)); + + // test hashCode + assertEquals("Failure - Serialized-deserialized instance has a different hashCode", + serializableMethod.hashCode(), copyOfSerializableMethod.hashCode()); + + // One more similar test with a method accepting non-serializable param + + Method method = myClass.getClass().getMethod("methodAcceptingMyClass", new Class[] + {MyClass.class}); + SerializableMethod serializableMethod_nonSerializableParam = new SerializableMethod(method); + + SerializableMethod copyOfSerializableMethod_nonSerilizableParam = (SerializableMethod) SerializationUtil + .getCopy(serializableMethod_nonSerializableParam); + + // test equals + assertTrue( + "Failure - equals fails on serialized-deserialized instance and its original instance which contained non-serializable param", + serializableMethod_nonSerializableParam.equals(copyOfSerializableMethod_nonSerilizableParam)); + + // test hashCode + assertEquals("Failure - hashCode does not match for serialized-deserialized instance and original instance", + serializableMethod_nonSerializableParam.hashCode(), copyOfSerializableMethod_nonSerilizableParam.hashCode()); + + } + + /** + * + * Test the {@link SerializableMethod#equals(Object)} and {@link SerializableMethod#hashCode()} + * with methods belonging to parent/child classes + * + * @throws Throwable + */ + @Test + public void testEqualsAndHashCodeWithInheritedClasses() throws Throwable + { + + // Test that the SerializableMethod instances created + // for a method from the same base class are equal and have the same hashCode + + // Note that this testcase should NOT override the toString method, to ensure that the getMethod() returns + // the 'Method' of Object.class + + Method toStringMethodOfThisClass = this.getClass().getMethod("toString", null); + Method toStringMethodOfObject = Object.class.getMethod("toString", null); + + SerializableMethod serializableMethod_toStringOfThisClass = new SerializableMethod(toStringMethodOfThisClass); + SerializableMethod serializableMethod_toStringOfObjectClass = new SerializableMethod(toStringMethodOfObject); + + // test equals + assertTrue("Failure - Two SerializableMethod instances of method belonging to the same base class are not equal", + serializableMethod_toStringOfObjectClass.equals(serializableMethod_toStringOfThisClass)); + + // test hashCode + assertEquals( + "Failure - Two SerializableMethod instances of method belonging to the same base class have different hashCode", + serializableMethod_toStringOfObjectClass.hashCode(), serializableMethod_toStringOfThisClass.hashCode()); + + // Test that the SerializableMethod instances created + // for overridden methods are NOT equal + + Method methodFromChildClass = MyChildClass.class.getDeclaredMethod("methodWithParamAndReturningVoid", new Class[] + {Integer.class}); + Method methodFromParentClass = MyClass.class.getDeclaredMethod("methodWithParamAndReturningVoid", new Class[] + {Integer.class}); + + SerializableMethod serializableMethodForChild = new SerializableMethod(methodFromChildClass); + SerializableMethod serializableMethodForParent = new SerializableMethod(methodFromParentClass); + + // test equals + assertFalse("Failure - The SerializableMethod instances of method from base class and child class are equal", + serializableMethodForChild.equals(serializableMethodForParent)); + + } + + /** + * Test the {@link SerializableMethod#equals(Object)} and {@link SerializableMethod#hashCode()} + * with methods involving generics + * + * @throws Throwable + */ + @Test + public void testEqualsAndHashCodeWithGenerics() throws Throwable + { + + // Just a simple test to ensure the SerializableMethod works fine with methods + // involving generics + + Method genericsMethod = myClass.getClass().getMethod("methodWithGenerics", new Class[] + {List.class, int.class}); + Method anotherGenericMethod = myClass.getClass().getMethod("methodWithGenerics", new Class[] + {List.class, int.class}); + + SerializableMethod serializableMethod = new SerializableMethod(genericsMethod); + SerializableMethod anotherSerializableMethod = new SerializableMethod(anotherGenericMethod); + + // test equals + assertTrue("Failure - Two SerializableMethod instances for a method involving generics are not equal", + serializableMethod.equals(anotherSerializableMethod)); + + // test hashCode + assertEquals( + "Failure - Two SerializableMethod instances for a method involving generics have different hashCode", + serializableMethod.hashCode(), anotherSerializableMethod.hashCode()); + + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * with simple methods + * + * @throws Throwable + */ + @Test + public void testToMethodWithoutClassloader() throws Throwable + { + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // does not involve any primitive parameters + + logger.info("Testing the toMethod(), for methods without primitive params"); + + Method methodWithoutPrimitivesParams = myClass.getClass().getMethod("methodWithParamAndReturningVoid", + new Class[] + {Integer.class}); + SerializableMethod serializableMethod = new SerializableMethod(methodWithoutPrimitivesParams); + // invoke the toMethod() + Method copyOfMethodWithoutPrimitiveParams = serializableMethod.toMethod(); + + // test equals + assertTrue( + "Failure - The method returned by toMethod() of SerializableMethod is not equal to the original method", + methodWithoutPrimitivesParams.equals(copyOfMethodWithoutPrimitiveParams)); + + // also test that the hashCode of the original method and the returned method are the same. + assertEquals("Failure - hashCode does not match for method returnd by toMethod()", methodWithoutPrimitivesParams + .hashCode(), copyOfMethodWithoutPrimitiveParams.hashCode()); + + logger.info("Completed testing the toMethod(), for methods without primitive params"); + + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * with methods involving generics + * + * @throws Throwable + */ + @Test + public void testToMethodWithoutClassloaderAndInvolvingGenerics() throws Throwable + { + + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // involves generics + + logger.info("Testing the toMethod(), for methods involving generics"); + + Method methodWithGenerics = myClass.getClass().getMethod("methodWithGenerics", new Class[] + {List.class, int.class}); + SerializableMethod serializableMethod = new SerializableMethod(methodWithGenerics); + + Method copyOfMethodWithGenerics = serializableMethod.toMethod(); + + // test equals + assertTrue( + "Failure - The method returned by toMethod() of SerializableMethod is not equal to the original method", + methodWithGenerics.equals(copyOfMethodWithGenerics)); + + // also test that the hashCode of the original method and the returned method are the same. + assertEquals("Failure - hashCode does not match for method returnd by toMethod()", methodWithGenerics.hashCode(), + copyOfMethodWithGenerics.hashCode()); + + logger.info("Completed testing the toMethod, for methods involving generics"); + + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * with methods accepting primitive parameters + * + * @throws Throwable + */ + @Test + public void testToMethodWithoutClassloaderAndInvolvingPrimitiveParams() throws Throwable + { + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // involves primitive params + + logger.info("Testing the toMethod(), for methods accepting primitives"); + + Method method = myClass.getClass().getMethod("methodWithParamAndReturningVoid", new Class[] + {int.class}); + SerializableMethod serializableMethod = new SerializableMethod(method); + // invoke the toMethod() + Method copyOfMethod = serializableMethod.toMethod(); + + // test equals + assertTrue( + "Failure - The method returned by toMethod() of SerializableMethod is not equal to the original method", + method.equals(copyOfMethod)); + // also test the hashCode + assertEquals("Failure - hashCode of the original method and the method returned by toMethod() dont match", method + .hashCode(), copyOfMethod.hashCode()); + + logger.info("Completed testing the toMethod(), for methods accepting primitives"); + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * + * @throws Throwable + */ + @Test + public void testToMethodWithoutClassloaderAndInvolvingReturnTypes() throws Throwable + { + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // returns some Object + + logger.info("Testing the toMethod(), for methods returing Object"); + + Method methodReturningInteger = myClass.getClass().getMethod("methodReturingInteger", new Class[] + {Integer.class}); + SerializableMethod serializableMethod = new SerializableMethod(methodReturningInteger); + // invoke the toMethod() + Method copyOfMethodReturingInteger = serializableMethod.toMethod(); + + // test equals + assertTrue( + "Failure - The method returned by toMethod() of SerializableMethod is not equal to the original method", + methodReturningInteger.equals(copyOfMethodReturingInteger)); + // also test the hashCode + assertEquals("Failure - hashCode of the original method and the method returned by toMethod() dont match", + methodReturningInteger.hashCode(), copyOfMethodReturingInteger.hashCode()); + + logger.info("Completed testing the toMethod(), for methods returing Object"); + + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * with methods accepting array parameters + * + * @throws Throwable + */ + @Test + public void testToMethodWithoutClassloaderAndInvolvingArrayParams() throws Throwable + { + + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // accepts array of primitives + + logger.info("Testing the toMethod(), for methods accepting array of primitives"); + + Method methodAcceptingArrayOfPrimitives = myClass.getClass().getMethod("methodAcceptingArrayOfPrimitives", + new Class[] + {int[].class}); + SerializableMethod serializableMethod = new SerializableMethod(methodAcceptingArrayOfPrimitives); + // invoke the toMethod() + Method copyOfMethodAcceptingArrayOfPrimitives = serializableMethod.toMethod(); + + //test equals + assertTrue( + "Failure - The method returned by toMethod() of SerializableMethod is not equal to the original method", + methodAcceptingArrayOfPrimitives.equals(copyOfMethodAcceptingArrayOfPrimitives)); + + // test hashCode + assertEquals("Failure - The method returned by toMethod() has a different hashCode than the original method", + methodAcceptingArrayOfPrimitives.hashCode(), copyOfMethodAcceptingArrayOfPrimitives.hashCode()); + + logger.info("Completed testing the toMethod(), for methods accepting array of primitives"); + + logger.info("Testing the toMethod(), for methods accepting array of Objects"); + + // Test that the toMethod() works fine when the Method, from which the SerializableMethod was created, + // accepts array of Objects + + Method methodAcceptingArrayOfObjects = myClass.getClass().getMethod("methodAcceptingArrayOfObjects", new Class[] + {Object[].class}); + SerializableMethod serializableMethod_arrayOfObjParams = new SerializableMethod(methodAcceptingArrayOfObjects); + //invoke the toMethod() + Method copyOfMethodAcceptingArrayOfObjects = serializableMethod_arrayOfObjParams.toMethod(); + + // test equals + assertTrue("Failure - The method returned by toMethod() of SerializableMethod is not equal to original method", + methodAcceptingArrayOfObjects.equals(copyOfMethodAcceptingArrayOfObjects)); + + // test hashCode + assertEquals("Failure -hashCode does not match for the method returned by toMethod()", + methodAcceptingArrayOfObjects.hashCode(), copyOfMethodAcceptingArrayOfObjects.hashCode()); + + logger.info("Completed testing the toMethod(), for methods accepting array of Objects"); + + } + + /** + * Test the {@link SerializableMethod#toMethod(ClassLoader)} + * + * @throws Throwable + */ + @Test + public void testToMethodWithClassloader() throws Throwable + { + + // Test the toMethod(Classloader) works fine + + logger.info("Testing the toMethod(Classloader)"); + + Method method = myClass.getClass().getMethod("methodAcceptingMyClass", new Class[] + {MyClass.class}); + SerializableMethod serializableMethod = new SerializableMethod(method); + + // invoke toMethod + Method copyOfMethod = serializableMethod.toMethod(this.getClass().getClassLoader()); + + // test equals + assertTrue("Failure - equals fails with classsloader passed to toMethod", method.equals(copyOfMethod)); + + // test hashCode + assertEquals("Failure - hashCode does not match when classloader is passed to toMethod", method.hashCode(), + copyOfMethod.hashCode()); + + logger.info("Completed testing the toMethod(Classloader)"); + + } + + /** + * Test the {@link SerializableMethod#toMethod()} + * on a serialized/de-serialized instance of {@link SerializableMethod} + * + * @throws Throwable + * @see {@link SerializationUtil} + */ + @Test + public void testToMethodWithSerialization() throws Throwable + { + // Test the toMethod() to ensure that the serialized/deserialized + // instance of the SerializableMethod returns the correct Method + + logger.info("Testing toMethod() with serialization"); + + Method method = myClass.getClass().getMethod("methodAcceptingMyClass", new Class[] + {MyClass.class}); + + SerializableMethod serializableMethod = new SerializableMethod(method); + + // now make a copy through serialization/de-serialization + SerializableMethod copyOfSerializableMethod = (SerializableMethod) SerializationUtil.getCopy(serializableMethod); + Method copyOfMethod = copyOfSerializableMethod.toMethod(); + + // test equals + assertTrue("Failure - equals fails for toMethod when serialized", method.equals(copyOfMethod)); + + // test hashCode + assertEquals("Failure - hashCode does not match for Method returned by toMethod, when serialized", method + .hashCode(), copyOfMethod.hashCode()); + + logger.info("Completed testing toMethod() with serialization"); + + logger.info("Completed testing toMethod() with serialization"); + } +}