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