Search in sources :

Example 76 with Result

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

the class ClassReflectionTests method test_NoSuchMethodException.

/**
	 * Test getting a declared method with multiple params that doesn't exist.
	 */
@Test
public void test_NoSuchMethodException() throws Exception {
    try {
        //			Result r =
        getDeclaredMethod("bogus", String.class, int.class);
        fail("getting bogus method should fail");
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + ".bogus(java.lang.String, int)", e);
    }
    Result r = getDeclaredMethod("deleteThem", String.class, int.class);
    assertMethod("public java.lang.String " + TARGET_CLASS_NAME + ".deleteThem(java.lang.String,int)", r);
    reloadType("002");
    try {
        r = getDeclaredMethod("deleteThem", String.class, int.class);
        fail("getting deleted method should fail");
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + ".deleteThem(java.lang.String, int)", e);
    }
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 77 with Result

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

the class ClassReflectionTests method test_getDeclaredMethodChangedAndInvoke.

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

Example 78 with Result

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

the class ClassReflectionTests method test_getDeclaredProtectedMethod.

// Believe now failing due to quick fix to copy method/field for grails (~build 113)
/**
	 * Test to see if Method objects for 'protected' methods are correct (w.r.t. to modifier flags)
	 */
@Test
public void test_getDeclaredProtectedMethod() throws Exception {
    Result r = getDeclaredMethod("protectedMethod");
    assertMethod("protected java.lang.String " + TARGET_CLASS_NAME + ".protectedMethod()", r);
    reloadType("002");
    r = getDeclaredMethod("protectedMethod");
    assertMethod("protected java.lang.String " + TARGET_CLASS_NAME + ".protectedMethod()", r);
}
Also used : Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 79 with Result

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

the class ClassReflectionTests method test_invokeProtectedInheritedMethod.

/**
	 * Test getDeclaredMethod and invoke for protected inherited method.
	 */
@Test
public void test_invokeProtectedInheritedMethod() throws Exception {
    String subClassName = TARGET_PACKAGE + ".SubClassTarget";
    ReloadableType subTarget = registry.addType(subClassName, loadBytesForClass(subClassName));
    try {
        getDeclaredMethod(subTarget.getClazz(), "protectedMethod");
        Assert.fail("A protected inherited method should not be considered 'declared' in the subclass");
    } catch (ResultException e) {
        assertNoSuchMethodException(subClassName + ".protectedMethod()", e);
    }
    //Next check if we can call the super class method on the sub instance
    Object subInstance = subTarget.getClazz().newInstance();
    Method m = (Method) getDeclaredMethod("protectedMethod").returnValue;
    // because call is from different package!
    m.setAccessible(true);
    Result r = invokeOn(subInstance, m);
    Assert.assertEquals("protectedMethod result", r.returnValue);
    //Reload the subclass, method should now be defined on the subclass
    reloadType(subTarget, "002");
    r = getDeclaredMethod(subTarget.getClazz(), "protectedMethod");
    assertMethod("protected java.lang.String " + subClassName + ".protectedMethod()", r);
    m = (Method) r.returnValue;
    try {
        invokeOn(subInstance, m);
        Assert.fail("invoker class is in different package than target class shouldn't be allowed to invoke protected method!");
    } catch (ResultException e) {
        assertIllegalAccess("Class " + callerClazz.getName() + " can not access a member of class " + subTarget.dottedtypename + " with modifiers \"protected\"", e);
    }
    m.setAccessible(true);
    r = invokeOn(subInstance, m);
    Assert.assertEquals("SubClassTarget002.protectedMethod", r.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) ResultException(org.springsource.loaded.test.infra.ResultException) Method(java.lang.reflect.Method) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 80 with Result

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

the class ClassReflectionTests method test_invokePrivateMethodAllowed.

/**
	 * Invoking a private method by means of "invoke" call, from within the class containing the target method should be
	 * allowed.
	 */
@Test
public void test_invokePrivateMethodAllowed() throws Exception {
    Result r = getDeclaredMethodAndInvoke("callPrivateMethod");
    Assert.assertEquals("privateMethod result", r.returnValue);
    reloadType("002");
    r = getDeclaredMethodAndInvoke("callPrivateMethod");
    Assert.assertEquals("new privateMethod result", 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