Search in sources :

Example 6 with Result

use of org.springsource.loaded.test.infra.Result 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 7 with Result

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

the class ClassReflectionTests method test_getDeclaredMethodStaysAndInvoke.

/**
	 * Testing Class.getDeclaredMethod and Method.invoke
	 *
	 * Can we retrieve and invoke a method that existed before we reloaded the type?
	 */
@Test
public void test_getDeclaredMethodStaysAndInvoke() throws Exception {
    Result r = getDeclaredMethodAndInvoke("methodStays");
    Assert.assertEquals(99, r.returnValue);
    reloadType("002");
    r = getDeclaredMethodAndInvoke("methodStays");
    Assert.assertEquals(99, r.returnValue);
}
Also used : Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 8 with Result

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

the class ClassReflectionTests method test_getDeclaredMethodStays.

/**
	 * Testing Class.getDeclaredMethod() - the method exists from the start and stays
	 */
@Test
public void test_getDeclaredMethodStays() throws Exception {
    Result r = getDeclaredMethod("methodStays");
    assertMethod("public int " + TARGET_CLASS_NAME + ".methodStays()", r);
    reloadType("002");
    r = getDeclaredMethod("methodStays");
    assertMethod("public int " + TARGET_CLASS_NAME + ".methodStays()", r);
}
Also used : Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 9 with Result

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

the class ClassReflectionTests method test_callInheritedOverridenMethod2.

/**
	 * Test related to calling 'invoke' on a method declared in superclass and overridden in the subclass (via a method
	 * object gotten from the superclass).
	 * <p>
	 * Variant of the previous test, where the class containing the overridden method is reloaded. (This case is
	 * different because it will use CLV to determine an executor method).
	 */
@Test
public void test_callInheritedOverridenMethod2() throws Exception {
    reloadType(target, "002");
    Result r = getDeclaredMethod("overrideMethod");
    assertMethod("public java.lang.String " + TARGET_CLASS_NAME + ".overrideMethod()", r);
    Method m = (Method) r.returnValue;
    // Double check if we are using the right version...
    r = invoke(m);
    assertEquals("ClassTarget002.overrideMethod", 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.overrideMethod", r.returnValue);
    // Now try what happens if we reload the subclass type, changing the method
    reloadType(subTarget, "002");
    r = invokeOn(subInstance, m);
    assertEquals("SubClassTarget002.overrideMethod", r.returnValue);
    // Now try what happens if we reload the subclass type, DELETING the method
    reloadType(subTarget, "003");
    r = invokeOn(subInstance, m);
    assertEquals("ClassTarget002.overrideMethod", r.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Method(java.lang.reflect.Method) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 10 with Result

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

the class ClassReflectionTests method test_invokePublicMethodInNonReloadableDefaultClassAllowed.

/**
	 * Invoking a public method inside a default class by means of "invoke" call, from within a class in the same
	 * package as the target class should be allowed.
	 */
@Test
public void test_invokePublicMethodInNonReloadableDefaultClassAllowed() throws Exception {
    //		Class<?> defaultClass =
    nonReloadableClass(TARGET_PACKAGE + "." + "DefaultClass");
    Result r = getDeclaredMethodAndInvoke("callPublicMethodOnDefaultClass");
    Assert.assertEquals(82, r.returnValue);
}
Also used : Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Aggregations

Result (org.springsource.loaded.test.infra.Result)155 Test (org.junit.Test)139 ReloadableType (org.springsource.loaded.ReloadableType)106 TypeRegistry (org.springsource.loaded.TypeRegistry)97 ResultException (org.springsource.loaded.test.infra.ResultException)28 Method (java.lang.reflect.Method)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 PrintStream (java.io.PrintStream)5 Constructor (java.lang.reflect.Constructor)4 Ignore (org.junit.Ignore)4 Field (java.lang.reflect.Field)2 ReflectiveInterceptor.jlClassGetField (org.springsource.loaded.ri.ReflectiveInterceptor.jlClassGetField)1