Search in sources :

Example 16 with ResultException

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

the class MethodGetAnnotationsTest method test.

@Override
public Result test() throws ResultException, Exception {
    try {
        Result r = runOnInstance(callerClazz, callerInstance, testedMethodCaller, method);
        Assert.assertTrue(r.returnValue instanceof List<?>);
        return r;
    } catch (ResultException e) {
        throw new Error(e);
    }
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result)

Example 17 with ResultException

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

the class ClassGetAnnotationsTest method test.

@Override
public Result test() throws ResultException, Exception {
    try {
        Result r = runOnInstance(callerClazz, callerInstance, testedMethodCaller, targetClass);
        Assert.assertTrue(r.returnValue instanceof List<?>);
        return r;
    } catch (ResultException e) {
        throw new Error(e);
    }
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result)

Example 18 with ResultException

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

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

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

the class ClassReflectionTests method test_getDeclaredConstructor.

@Test
public void test_getDeclaredConstructor() throws Exception {
    // Let's call the constructor, nothing reloaded
    Result r = getDeclaredConstructor();
    assertNotNull(r.returnValue);
    Constructor<?> ctor = (Constructor<?>) r.returnValue;
    r = invokeConstructor(ctor);
    assertNotNull(r.returnValue);
    Constructor<?> ctorFloat = (Constructor<?>) getDeclaredConstructor(Float.TYPE).returnValue;
    r = invokeConstructor(ctorFloat, 44f);
    assertNotNull(r.returnValue);
    reloadType("002");
    // now call it again, first using the one we still have,
    // then retrieving and using a new one
    r = invokeConstructor(ctor);
    assertNotNull(r.returnValue);
    ctorFloat = (Constructor<?>) getDeclaredConstructor(Float.TYPE).returnValue;
    r = invokeConstructor(ctorFloat, 22f);
    assertNotNull(r.returnValue);
    r = getDeclaredConstructor();
    assertNotNull(r.returnValue);
    ctor = (Constructor<?>) r.returnValue;
    r = invokeConstructor(ctor);
    assertNotNull(r.returnValue);
    // Access a constructor that isn't there
    try {
        r = getDeclaredConstructor(String.class);
    } catch (ResultException re) {
        assertTrue(re.getCause() instanceof InvocationTargetException);
        assertTrue(re.getCause().getCause().toString(), re.getCause().getCause() instanceof NoSuchMethodException);
    }
    // Access a new constructor (added in reload)
    r = getDeclaredConstructor(Integer.TYPE);
    assertNotNull(r.returnValue);
    ctor = (Constructor<?>) r.returnValue;
    r = invokeConstructor(ctor, 4);
    assertNotNull(r.returnValue);
    // Load new version with modified ctor
    reloadType("003");
    r = invokeConstructor(ctor, 4);
    assertContains("modified!", r.stdout);
}
Also used : Constructor(java.lang.reflect.Constructor) ResultException(org.springsource.loaded.test.infra.ResultException) InvocationTargetException(java.lang.reflect.InvocationTargetException) 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