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);
}
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);
}
}
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);
}
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);
}
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", ""));
}
}
Aggregations