Search in sources :

Example 11 with ResultException

use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.

the class ClassReflectionTests method test_invokeStaticMethod.

/**
	 * Test invoking a static method.
	 */
@Test
public void test_invokeStaticMethod() throws Exception {
    Result r = getDeclaredMethod("staticMethod");
    assertMethod("public static java.lang.String " + TARGET_CLASS_NAME + ".staticMethod()", r);
    Method m = (Method) r.returnValue;
    // Calling the static method
    //pass in null, it shouldn't need an instance since it's static!
    r = invokeOn(null, m);
    assertEquals("ClassTarget.staticMethod", r.returnValue);
    reloadType("002");
    // Invoke again, using 'cached' copy of the method
    //pass in null, it shouldn't need an instance since it's static!
    r = invokeOn(null, m);
    assertEquals("ClassTarget002.staticMethod", r.returnValue);
    // Invoke again, using 'fresh' copy of the method
    m = (Method) getDeclaredMethod("staticMethod").returnValue;
    //pass in null, it shouldn't need an instance since it's static!
    r = invokeOn(null, m);
    assertEquals("ClassTarget002.staticMethod", r.returnValue);
    // static method deleted now
    reloadType("003");
    // Invoke again, using 'cached' copy of the method
    try {
        //pass in null, it shouldn't need an instance since it's static!
        r = invokeOn(null, m);
        fail("The method was deleted, should not be able to call it");
    } catch (ResultException e) {
        assertNoSuchMethodError(TARGET_CLASS_NAME + ".staticMethod()Ljava/lang/String;", e);
    }
}
Also used : Method(java.lang.reflect.Method) ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 12 with ResultException

use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.

the class ClassReflectionTests method test_invokeStaticMethodAddedWithArgs.

/**
	 * Test invoking a static method that didn't initialy exist.
	 */
@Test
public void test_invokeStaticMethodAddedWithArgs() throws Exception {
    String methodName = "staticMethodAddedWithArgs";
    try {
        Result r = getDeclaredMethod(methodName, int.class, String.class);
        fail("Method should not exist initially\n" + r.toString());
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + "." + methodName + "(int, java.lang.String)", e);
    }
    reloadType("002");
    Result r = getDeclaredMethod(methodName, int.class, String.class);
    assertMethod("public static java.lang.String " + TARGET_CLASS_NAME + "." + methodName + "(int,java.lang.String)", r);
    Method m = (Method) r.returnValue;
    // Calling the static method
    //pass in null, it shouldn't need an instance since it's static!
    r = invokeOn(null, m, 123, "Hello");
    assertEquals("123Hello002", r.returnValue);
    reloadType("003");
    // Invoke again, using 'cached' copy of the method
    //pass in null, it shouldn't need an instance since it's static!
    r = invokeOn(null, m, 456, "Hi");
    assertEquals("456Hi003", r.returnValue);
    // Invoke again, using 'fresh' copy of the method
    r = getDeclaredMethod(methodName, int.class, String.class);
    //pass in null, it shouldn't need an instance since it's static!
    r = invokeOn(null, m, 456, "Hi");
    assertEquals("456Hi003", r.returnValue);
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) Method(java.lang.reflect.Method) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 13 with ResultException

use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.

the class ClassReflectionTests method test_getDeclaredMethodDeletedAndInvoke.

/**
	 * Testing Class.getDeclaredMethod and Method.invoke
	 *
	 * Can we retrieve and invoke a method that existed before we reloaded the type, and was deleted later.
	 */
@Test
public void test_getDeclaredMethodDeletedAndInvoke() throws Exception {
    Result r = getDeclaredMethodAndInvoke("methodDeleted");
    Assert.assertEquals(37, r.returnValue);
    reloadType("002");
    try {
        getDeclaredMethodAndInvoke("methodDeleted");
        fail("getting/invoking a deleted method should fail");
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + ".methodDeleted()", e);
    }
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 14 with ResultException

use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.

the class ClassReflectionTests method test_callInheritedOverridenDeletedMethod.

/**
	 * Test related to calling 'invoke' on a method declared in superclass and overriden in the subclass (via a method
	 * object gotten from the superclass).
	 * <p>
	 * What if the super method is deleted in v002?
	 */
@Test
public void test_callInheritedOverridenDeletedMethod() throws Exception {
    Result r = getDeclaredMethod("overrideMethodDeleted");
    assertMethod("public java.lang.String " + TARGET_CLASS_NAME + ".overrideMethodDeleted()", r);
    Method m = (Method) r.returnValue;
    // Setup subClass and an instance of the subclass
    String subClassName = TARGET_PACKAGE + ".SubClassTarget";
    ReloadableType subTarget = registry.addType(subClassName, loadBytesForClass(subClassName));
    Object subInstance = subTarget.getClazz().newInstance();
    // Calling the superclass method on the subinstance should execute the subclass method!
    r = invokeOn(subInstance, m);
    assertEquals("SubClassTarget.overrideMethodDeleted", r.returnValue);
    // Now try what happens if we reload the superclass type, deleting the method
    reloadType("002");
    try {
        r = invokeOn(subInstance, m);
        fail("The method was deleted, should fail!");
    } catch (ResultException e) {
        assertNoSuchMethodError("reflection.targets.ClassTarget.overrideMethodDeleted()Ljava/lang/String;", e);
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Method(java.lang.reflect.Method) ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 15 with ResultException

use of org.springsource.loaded.test.infra.ResultException in project spring-loaded by spring-projects.

the class ClassReflectionTests method test_getDeclaredMethodCachedChangedReturnType.

/**
	 * Testing Class.getDeclaredMethod and Method.invoke
	 *
	 * A method with a parameter - existed at first - then it was changed: DIFFERENT RETURN TYPE
	 *
	 * If we held on to the original method object, we should NOT be able to invoke it anymore (different return type
	 * should be treated as different method!
	 */
@Test
public void test_getDeclaredMethodCachedChangedReturnType() throws Exception {
    Result r = getDeclaredMethod("changeReturn", String.class);
    assertMethod("public java.lang.String " + TARGET_CLASS_NAME + ".changeReturn(java.lang.String)", r);
    Method m = (Method) r.returnValue;
    r = invoke(m, "hoho");
    Assert.assertEquals("hohoho!", r.returnValue);
    reloadType("002");
    try {
        r = invoke(m, "hoho");
        Assert.fail("Method return type changed, shouldn't be able to call it anymore");
    } catch (ResultException e) {
        assertNoSuchMethodError(TARGET_CLASS_NAME + ".changeReturn(Ljava/lang/String;)Ljava/lang/String;", e);
    }
}
Also used : Method(java.lang.reflect.Method) ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Aggregations

ResultException (org.springsource.loaded.test.infra.ResultException)35 Result (org.springsource.loaded.test.infra.Result)28 Test (org.junit.Test)25 Method (java.lang.reflect.Method)12 ReloadableType (org.springsource.loaded.ReloadableType)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 TypeRegistry (org.springsource.loaded.TypeRegistry)5 Field (java.lang.reflect.Field)2 IResult (org.springsource.loaded.test.infra.IResult)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 PrintStream (java.io.PrintStream)1 Constructor (java.lang.reflect.Constructor)1 ArrayList (java.util.ArrayList)1 Failure (org.junit.runner.notification.Failure)1 InitializationError (org.junit.runners.model.InitializationError)1 ReflectiveInterceptor.jlClassGetField (org.springsource.loaded.ri.ReflectiveInterceptor.jlClassGetField)1