Search in sources :

Example 31 with ResultException

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

the class ClassReflectionTests method test_getDeclaredLateMethodAndInvoke.

/**
	 * Testing Class.getDeclaredMethod and Method.invoke
	 *
	 * Can we retrieve and invoke a method that was added in a reloaded type?
	 */
@Test
public void test_getDeclaredLateMethodAndInvoke() throws Exception {
    try {
        getDeclaredMethodAndInvoke("lateMethod");
        fail("getting/invoking a method that hs not yet been defined should fail");
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + ".lateMethod()", e);
    }
    reloadType("002");
    Result r = getDeclaredMethodAndInvoke("lateMethod");
    Assert.assertEquals(42, r.returnValue);
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 32 with ResultException

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

the class ClassReflectionTests method doTestAccessibleFlagIsRefreshed.

/**
	 * When a Method is "regotten" the newly gotten Method will have to be a "fresh" method object with its
	 * accessibility flag NOT set. (This is the behaviour on Sun JVM, I'm not sure if this specified or accidental
	 * though, but some comments in the Method class source code suggest that it is required... see code of the 'copy'
	 * method and its comments)
	 */
private void doTestAccessibleFlagIsRefreshed(String scope) throws ResultException {
    String methodToCall = scope + "Method";
    String expectMsg = "Class " + INVOKER_CLASS_NAME + " " + "can not access a member of class " + TARGET_CLASS_NAME + " " + "with modifiers \"" + (scope.equals("default") ? "" : scope) + "\"";
    //First... we do set the Access flag, it should work!
    Result r = runOnInstance(callerClazz, callerInstance, "callMethodWithAccess", methodToCall, true);
    Assert.assertEquals(r.returnValue, methodToCall + " result");
    //Then... we do not set the Access flag, it should fail!
    try {
        r = runOnInstance(callerClazz, callerInstance, "callMethodWithAccess", methodToCall, false);
        Assert.fail("Without setting access flag shouldn't be allowed!");
    } catch (ResultException e) {
        assertIllegalAccess(expectMsg, e);
    }
    //Finally, this should also still work... after we reload the type!
    reloadType("002");
    //First... we do set the Access flag, it should work!
    r = runOnInstance(callerClazz, callerInstance, "callMethodWithAccess", methodToCall, true);
    Assert.assertEquals(r.returnValue, "new " + methodToCall + " result");
    //Then... we do not set the Access flag, it should not be allowed!
    try {
        r = runOnInstance(callerClazz, callerInstance, "callMethodWithAccess", methodToCall, false);
        Assert.fail("Without setting access flag shouldn't be allowed!");
    } catch (ResultException e) {
        assertIllegalAccess(expectMsg, e);
    }
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result)

Example 33 with ResultException

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

the class ClassReflectionTests method test_getDeclaredMethodWithParams.

/**
	 * Testing Class.getDeclaredMethod() - method with parameters should fail before reloading, work after
	 */
@Test
public void test_getDeclaredMethodWithParams() throws Exception {
    try {
        getDeclaredMethod("doubleIt", String.class);
        Assert.fail("Method shouldn't exist yet");
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + ".doubleIt(java.lang.String)", e);
    }
    try {
        getDeclaredMethod("custard", String.class, int[].class, int.class);
        Assert.fail("Method shouldn't exist");
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + ".custard(java.lang.String, [I, int)", e);
    }
    reloadType("002");
    Result r = getDeclaredMethod("doubleIt", String.class);
    assertMethod("public java.lang.String " + TARGET_CLASS_NAME + ".doubleIt(java.lang.String)", r);
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 34 with ResultException

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

the class ClassReflectionTests method test_getDeclaredLateMethod.

/**
	 * Testing Class.getDeclaredMethod() - - first the method doesn't exist - then it does
	 */
@Test
public void test_getDeclaredLateMethod() throws Exception {
    try {
        getDeclaredMethod("lateMethod");
        Assert.fail("lateMethod shouldn't exist yet");
    } catch (ResultException e) {
        assertNoSuchMethodException(TARGET_CLASS_NAME + ".lateMethod()", e);
    }
    reloadType("002");
    Result r = getDeclaredMethod("lateMethod");
    assertMethod("public int " + TARGET_CLASS_NAME + ".lateMethod()", r);
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 35 with ResultException

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

the class SpringLoadedTests method runOnInstance.

/**
	 * Proposed alternate version of runOnInstance that produces a wrapper Exception object similar to the Result
	 * object, but where the "result" is an exception. This is done so as not to lose the grabbed output when exception
	 * is raised by the test case.
	 */
public static Result runOnInstance(Class<?> clazz, Object instance, String methodname, Object... params) throws ResultException {
    PrintStream oldo = System.out;
    PrintStream olde = System.err;
    Object result = null;
    Throwable exception = null;
    ByteArrayOutputStream oso = new ByteArrayOutputStream();
    ByteArrayOutputStream ose = new ByteArrayOutputStream();
    try {
        if (capture) {
            System.setOut(new PrintStream(oso));
            System.setErr(new PrintStream(ose));
        }
        Method m = null;
        Method[] ms = clazz.getMethods();
        for (Method mm : ms) {
            if (mm.getName().equals(methodname)) {
                m = mm;
                break;
            }
        }
        if (m == null) {
            throw new IllegalStateException("No method called " + methodname + " to call on type " + (instance == null ? "null" : instance.getClass().getName()));
        }
        try {
            result = m.invoke(instance, params);
        } catch (Throwable e) {
            exception = e;
        }
    } finally {
        System.setOut(oldo);
        System.setErr(olde);
    }
    if (printOutput) {
        System.out.println("Collected output running: " + methodname);
        System.out.println(oso.toString());
        System.out.println(ose.toString());
    }
    if (exception != null) {
        throw new ResultException(exception, oso.toString().replace("\r", ""), ose.toString().replace("\r", ""));
    } else {
        return new Result(result, oso.toString().replace("\r", ""), ose.toString().replace("\r", ""));
    }
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result)

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