Search in sources :

Example 1 with ResultException

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

the class ClassReflectionTests method test_callReloadableMethodWithNonReloadableImplementation.

/**
	 * Scenario where method lookup spills over from the reloadable world into the non-reloadable world. This can only
	 * happen if are looking for a method that is declared on a reloadable type, but we need to find the implementation
	 * in a non-reloadable one.
	 */
@Test
public void test_callReloadableMethodWithNonReloadableImplementation() throws Exception {
    //Scenario requires a method that is reloadable but not implemented by a reloadable
    //class. The followig circumstances trigger this condition:
    // Situation involves three types:
    // A non reloadable superClass
    String superClassName = "reflection.NonReloadableSuperClass";
    Class<?> superClass = nonReloadableClass(superClassName);
    // A reloadable interface
    String interfaceName = TARGET_PACKAGE + ".InterfaceTarget";
    ReloadableType interfaceTarget = reloadableClass(interfaceName);
    ClassPrinter.print(interfaceTarget.getBytesLoaded());
    // A reloadable class
    String subclassName = TARGET_PACKAGE + ".SubClassImplementsInterface";
    ReloadableType subClass = reloadableClass(subclassName);
    // These types relate to one another as follows:
    //1) the method 'm' must be declared in the reloadable interface
    String interfaceMethodName = "interfaceMethod";
    Result r = getDeclaredMethod(interfaceTarget.getClazz(), interfaceMethodName);
    assertMethod("public abstract java.lang.String " + interfaceName + "." + interfaceMethodName + "()", r);
    Method m = (Method) r.returnValue;
    //2) The reloadable type implements this interface (without providing its own implementation of m)
    assertTrue(interfaceTarget.getClazz().isAssignableFrom(subClass.getClazz()));
    try {
        r = getDeclaredMethod(subClass.getClazz(), interfaceMethodName);
        fail("Assuming that interface implementation is inherited, not directly implemented");
    } catch (ResultException e) {
        assertNoSuchMethodException(subclassName + "." + interfaceMethodName + "()", e);
    }
    //3) A non-reloadable superclass provides the actual implementation of m via inheritance
    r = getDeclaredMethod(superClass, interfaceMethodName);
    assertMethod("public java.lang.String " + superClassName + "." + interfaceMethodName + "()", r);
    //4) Invoke the interface method on an instance of the subclass... should cause lookup
    //   to find implementation in non-reloadable superclass
    Object subInstance = subClass.getClazz().newInstance();
    r = invokeOn(subInstance, m);
    assertEquals("NonReloadableSuperClass.interfaceMethod", r.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Method(java.lang.reflect.Method) ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 2 with ResultException

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

the class ConstructorAdHocTest method test_accessDeletedConstructor.

//	/**
//	 * When a Constructor is "regotten" the newly gotten Field will have to be a "fresh" object with its accessibility flag NOT
//	 * set.
//	 */
//	private void doTestAccessibleFlagIsRefreshed(Constructor<?> cons) throws Exception {
//		registry = getTypeRegistry("reflection.constructors..*");
//		targetClass = reloadableClass("reflection.constructors.ClassForNewInstance");
//
//		callerClazz = nonReloadableClass(INVOKER_CLASS_NAME);
//		callerInstance = newInstance(callerClazz);
//		
//		String fieldToAccess = scope + "Field";
//		String expectMsg = "Class " + INVOKER_CLASS_NAME + " " + "can not access a member of class " + targetClass.dottedtypename + " "
//				+ "with modifiers \"" + (scope.equals("default") ? "" : scope) + "\"";
//
//		//First... we do set the Access flag, it should work!
//		Result r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, true);
//		Assert.assertEquals(r.returnValue, fieldToAccess + " value");
//
//		//Then... we do not set the Access flag, it should fail!
//		try {
//			r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, 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(targetClass, "002");
//
//		//First... we do set the Access flag, it should work!
//		r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, true);
//		Assert.assertEquals("new "+fieldToAccess + " value", r.returnValue);
//
//		//Then... we do not set the Access flag, it should not be allowed!
//		try {
//			r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, false);
//			Assert.fail("Without setting access flag shouldn't be allowed!");
//		} catch (ResultException e) {
//			assertIllegalAccess(expectMsg, e);
//		}
//	}
//	@Test
//	public void test_accessibleFlagIsRefreshedForPrivate() throws Exception {
//		doTestAccessibleFlagIsRefreshed("private");
//	}
//
//	@Test
//	public void test_accessibleFlagIsRefreshedForProtected() throws Exception {
//		doTestAccessibleFlagIsRefreshed("protected");
//	}
//
//	@Test
//	public void test_accessibleFlagIsRefreshedForDefault() throws Exception {
//		doTestAccessibleFlagIsRefreshed("default");
//	}
@Test
public void test_accessDeletedConstructor() throws Exception {
    registry = getTypeRegistry("reflection.constructors..*");
    targetClass = reloadableClass("reflection.constructors.ClassForNewInstance");
    //		Object targetInstance = 
    newInstance(targetClass.getClazz());
    Constructor<?> c = jlClassGetConstructor(targetClass.getClazz(), char.class, char.class);
    Assert.assertEquals("public reflection.constructors.ClassForNewInstance(char,char)", c.toString());
    // First try to access the Constructor before reloading
    callerClazz = nonReloadableClass(INVOKER_CLASS_NAME);
    callerInstance = newInstance(callerClazz);
    Result r = runOnInstance(callerClazz, callerInstance, "callNewInstance", c, (Object) new Object[] { 'a', 'b' });
    Assert.assertEquals(targetClass.getClazz(), r.returnValue.getClass());
    // Now reload the class... constructor is deleted
    reloadType(targetClass, "002");
    try {
        r = runOnInstance(callerClazz, callerInstance, "callNewInstance", c, (Object) new Object[] { 'a', 'b' });
        Assert.fail("Expected an error");
    } catch (ResultException re) {
        Throwable e = re.getCause();
        //			e.printStackTrace();
        Assert.assertEquals(InvocationTargetException.class, e.getClass());
        //Nested exception
        e = e.getCause();
        e.printStackTrace();
        Assert.assertEquals(NoSuchMethodError.class, e.getClass());
        //Example error message from Sun JVM:
        //			Exception in thread "main" java.lang.NoSuchMethodError: Target.<init>(C)V
        //			at Main.main(Main.java:10)
        Assert.assertEquals("reflection.constructors.ClassForNewInstance.<init>(CC)V", e.getMessage());
    }
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 3 with ResultException

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

the class FieldAdHocTest method doTestAccessibleFlagIsRefreshed.

/**
	 * When a Field is "regotten" the newly gotten Field will have to be a "fresh" object with its accessibility flag
	 * NOT set.
	 */
private void doTestAccessibleFlagIsRefreshed(String scope) throws Exception {
    registry = getTypeRegistry("reflection.fields..*");
    targetClass = reloadableClass("reflection.fields.FieldSetAccessTarget");
    callerClazz = nonReloadableClass(INVOKER_CLASS_NAME);
    callerInstance = newInstance(callerClazz);
    String fieldToAccess = scope + "Field";
    String expectMsg = "Class " + INVOKER_CLASS_NAME + " " + "can not access a member of class " + targetClass.dottedtypename + " " + "with modifiers \"" + (scope.equals("default") ? "" : scope) + "\"";
    //First... we do set the Access flag, it should work!
    Result r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, true);
    Assert.assertEquals(r.returnValue, fieldToAccess + " value");
    //Then... we do not set the Access flag, it should fail!
    try {
        r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, 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(targetClass, "002");
    //First... we do set the Access flag, it should work!
    r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, true);
    Assert.assertEquals("new " + fieldToAccess + " value", r.returnValue);
    //Then... we do not set the Access flag, it should not be allowed!
    try {
        r = runOnInstance(callerClazz, callerInstance, "getFieldWithAccess", targetClass.getClazz(), fieldToAccess, 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 4 with ResultException

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

the class FieldAdHocTest method test_accessDeletedField.

@Test
public void test_accessDeletedField() throws Exception {
    registry = getTypeRegistry("reflection.fields..*");
    targetClass = reloadableClass("reflection.fields.ClassTarget");
    Object targetInstance = newInstance(targetClass.getClazz());
    Field f = jlClassGetField(targetClass.getClazz(), "myDeletedField");
    Assert.assertEquals("myDeletedField", f.getName());
    // First try to access the field before reloading
    callerClazz = nonReloadableClass(INVOKER_CLASS_NAME);
    callerInstance = newInstance(callerClazz);
    Result r = runOnInstance(callerClazz, callerInstance, "callGet", f, targetInstance);
    Assert.assertEquals(100, r.returnValue);
    // Now reload the class... field is deleted
    reloadType(targetClass, "002");
    try {
        r = runOnInstance(callerClazz, callerInstance, "callGet", f, targetInstance);
        Assert.fail("Expected an error");
    } catch (ResultException re) {
        Throwable e = re.getCause();
        //			e.printStackTrace();
        Assert.assertEquals(InvocationTargetException.class, e.getClass());
        //Nested exception
        e = e.getCause();
        Assert.assertEquals(NoSuchFieldError.class, e.getClass());
        Assert.assertEquals("myDeletedField", e.getMessage());
    }
}
Also used : ReflectiveInterceptor.jlClassGetField(org.springsource.loaded.ri.ReflectiveInterceptor.jlClassGetField) Field(java.lang.reflect.Field) ResultException(org.springsource.loaded.test.infra.ResultException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 5 with ResultException

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

the class FieldGetAnnotationsTest method test.

@Override
public Result test() throws ResultException, Exception {
    try {
        Result r = runOnInstance(callerClazz, callerInstance, testedMethodCaller, field);
        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)

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