Search in sources :

Example 71 with Result

use of org.springsource.loaded.test.infra.Result 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 72 with Result

use of org.springsource.loaded.test.infra.Result 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 73 with Result

use of org.springsource.loaded.test.infra.Result 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)

Example 74 with Result

use of org.springsource.loaded.test.infra.Result 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);
    }
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result)

Example 75 with Result

use of org.springsource.loaded.test.infra.Result 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);
    }
}
Also used : ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result)

Aggregations

Result (org.springsource.loaded.test.infra.Result)155 Test (org.junit.Test)139 ReloadableType (org.springsource.loaded.ReloadableType)106 TypeRegistry (org.springsource.loaded.TypeRegistry)97 ResultException (org.springsource.loaded.test.infra.ResultException)28 Method (java.lang.reflect.Method)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 PrintStream (java.io.PrintStream)5 Constructor (java.lang.reflect.Constructor)4 Ignore (org.junit.Ignore)4 Field (java.lang.reflect.Field)2 ReflectiveInterceptor.jlClassGetField (org.springsource.loaded.ri.ReflectiveInterceptor.jlClassGetField)1