Search in sources :

Example 26 with ResultException

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

the class ClassReflectionTests method test_invokeStaticMethodAdded.

/**
	 * Test invoking a static method that didn't initialy exist.
	 */
@Test
public void test_invokeStaticMethodAdded() throws Exception {
    String methodName = "staticMethodAdded";
    try {
        Result r = getDeclaredMethod(methodName);
        fail("Method shouldn't exist at first!\n" + r.toString());
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + "." + methodName + "()", e);
    }
    reloadType("002");
    Result r = getDeclaredMethod(methodName);
    assertMethod("public static int " + TARGET_CLASS_NAME + "." + methodName + "()", 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(2, 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);
    assertEquals(3, r.returnValue);
    // Invoke again, using 'fresh' copy of the method
    m = (Method) getDeclaredMethod(methodName).returnValue;
    //pass in null, it shouldn't need an instance since it's static!
    r = invokeOn(null, m);
    assertEquals(3, 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 27 with ResultException

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

the class ClassReflectionTests method test_getDeclaredMethod_param_Added.

/**
	 * Testing Class.getDeclaredMethod and Method.invoke
	 *
	 * A method with a parameter - first it didn't exist - then is was added
	 */
@Test
public void test_getDeclaredMethod_param_Added() throws Exception {
    try {
        getDeclaredMethod("doubleIt", String.class);
        Assert.fail("Method shouldn't have existed");
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + ".doubleIt(java.lang.String)", e);
    }
    reloadType("002");
    Result r = getDeclaredMethod("doubleIt", String.class);
    assertMethod("public java.lang.String " + TARGET_CLASS_NAME + ".doubleIt(java.lang.String)", r);
    r = invoke((Method) r.returnValue, "hi");
    Assert.assertEquals("hihi", 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 28 with ResultException

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

the class ClassReflectionTests method test_cacheGetDeclaredMethodDeletedAndInvoke.

/**
	 * Testing Class.getDeclaredMethod and Method.invoke
	 *
	 * What happens if we invoke a cached Method object when the method it refers to was deleted?
	 */
@Test
public void test_cacheGetDeclaredMethodDeletedAndInvoke() throws Exception {
    Method m = (Method) getDeclaredMethod("methodDeleted").returnValue;
    Result r = invoke(m);
    Assert.assertEquals(37, r.returnValue);
    reloadType("002");
    try {
        invoke(m);
        fail("Invoking a deleted method should fail");
    } catch (ResultException e) {
        assertNoSuchMethodError(TARGET_CLASS_NAME + ".methodDeleted()I", 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 29 with ResultException

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

the class ClassReflectionTests method test_invokeStaticMethodAddedWithNullParams.

/**
	 * Test invoking a static method that didn't initialy exist.
	 */
@Test
public void test_invokeStaticMethodAddedWithNullParams() throws Exception {
    String methodName = "staticMethodAdded";
    try {
        Result r = getDeclaredMethod(methodName);
        fail("Method shouldn't exist at first!\n" + r.toString());
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + "." + methodName + "()", e);
    }
    reloadType("002");
    Result r = getDeclaredMethod(methodName);
    assertMethod("public static int " + TARGET_CLASS_NAME + "." + methodName + "()", r);
    Method m = (Method) r.returnValue;
    // This should be ok, since the method expects no params (should not cause an NPE)
    Object[] params = null;
    // Calling the static method
    //pass in null, it shouldn't need an instance since it's static!
    r = invokeOn(null, m, params);
    assertEquals(2, 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, params);
    assertEquals(3, r.returnValue);
    // Invoke again, using 'fresh' copy of the method
    m = (Method) getDeclaredMethod(methodName).returnValue;
    //pass in null, it shouldn't need an instance since it's static!
    r = invokeOn(null, m, params);
    assertEquals(3, 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 30 with ResultException

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

the class ClassReflectionTests method test_getDeclaredMethodDeleted.

/**
	 * Testing Class.getDeclaredMethod() - first the method exists - then it is deleted
	 */
@Test
public void test_getDeclaredMethodDeleted() throws Exception {
    Result r = getDeclaredMethod("methodDeleted");
    assertMethod("public int " + TARGET_CLASS_NAME + ".methodDeleted()", r);
    reloadType("002");
    try {
        r = getDeclaredMethod("methodDeleted");
        Assert.fail("The method shouldn't exist anymore!");
    } 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)

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