Search in sources :

Example 21 with ResultException

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

the class FieldReloadingTests method changingFieldFromStaticToNonstaticWithSubtypes.

/**
	 * A static field is accessed from a subtype, then the supertype is reloaded where the field has been made
	 * non-static. Should be an error when the subtype attempts to access it again.
	 */
@Test
public void changingFieldFromStaticToNonstaticWithSubtypes() throws Exception {
    String y = "fields.Yb";
    String z = "fields.Zb";
    TypeRegistry tr = getTypeRegistry(y + "," + z);
    ReloadableType rtype = tr.addType(y, loadBytesForClass(y));
    ReloadableType ztype = tr.addType(z, loadBytesForClass(z));
    Class<?> clazz = ztype.getClazz();
    Object instance = clazz.newInstance();
    assertEquals(5, runOnInstance(clazz, instance, "getJ").returnValue);
    runOnInstance(clazz, instance, "setJ", 4);
    assertEquals(4, runOnInstance(clazz, instance, "getJ").returnValue);
    rtype.loadNewVersion("2", retrieveRename(y, y + "002"));
    // Now should be an IncompatibleClassChangeError
    try {
        runOnInstance(clazz, instance, "setJ", 4);
        fail("Should not have worked, field has changed from static to non-static");
    } catch (ResultException re) {
        assertTrue(re.getCause() instanceof InvocationTargetException);
        assertTrue(re.getCause().getCause() instanceof IncompatibleClassChangeError);
        // When compiled with AspectJ vs Eclipse JDT the GETSTATIC actually varies.
        // With AspectJ it is: PUTSTATIC fields/Yb.j : I
        // With JDT (4.3) it is: PUTSTATIC fields/Zb.j : I
        // hence the error is different
        assertEquals("Expected static field fields.Zb.j", re.getCause().getCause().getMessage());
    }
    // Now should be an IncompatibleClassChangeError
    try {
        runOnInstance(clazz, instance, "getJ");
        fail("Should not have worked, field has changed from static to non-static");
    } catch (ResultException re) {
        assertTrue(re.getCause() instanceof InvocationTargetException);
        assertTrue(re.getCause().getCause() instanceof IncompatibleClassChangeError);
        // When compiled with AspectJ vs Eclipse JDT the GETSTATIC actually varies.
        // With AspectJ it is: GETSTATIC fields/Yb.j : I
        // With JDT (4.3) it is: GETSTATIC fields/Zb.j : I
        // hence the error is different
        assertEquals("Expected static field fields.Zb.j", re.getCause().getCause().getMessage());
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) ResultException(org.springsource.loaded.test.infra.ResultException) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 22 with ResultException

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

the class FieldReloadingTests method changingFieldFromNonstaticToStaticWithSubtypes.

/**
	 * An instance field is accessed from a subtype, then the supertype is reloaded where the field has been made
	 * static. Should be an error when the subtype attempts to access it again.
	 */
@Test
public void changingFieldFromNonstaticToStaticWithSubtypes() throws Exception {
    String y = "fields.Ya";
    String z = "fields.Za";
    TypeRegistry tr = getTypeRegistry(y + "," + z);
    ReloadableType rtype = tr.addType(y, loadBytesForClass(y));
    ReloadableType ztype = tr.addType(z, loadBytesForClass(z));
    Class<?> clazz = ztype.getClazz();
    Object instance = clazz.newInstance();
    assertEquals(5, runOnInstance(clazz, instance, "getJ").returnValue);
    runOnInstance(clazz, instance, "setJ", 4);
    assertEquals(4, runOnInstance(clazz, instance, "getJ").returnValue);
    rtype.loadNewVersion("2", retrieveRename(y, y + "002"));
    try {
        runOnInstance(clazz, instance, "setJ", 4);
        fail("should not have worked, field has changed from non-static to static");
    } catch (ResultException re) {
        assertTrue(re.getCause() instanceof InvocationTargetException);
        assertTrue(re.getCause().getCause() instanceof IncompatibleClassChangeError);
        assertEquals("Expected non-static field fields.Za.j", re.getCause().getCause().getMessage());
    }
    // Now should be an IncompatibleClassChangeError
    try {
        runOnInstance(clazz, instance, "getJ");
        fail("should not have worked, field has changed from non-static to static");
    } catch (ResultException re) {
        assertTrue(re.getCause() instanceof InvocationTargetException);
        assertTrue(re.getCause().getCause() instanceof IncompatibleClassChangeError);
        assertEquals("Expected non-static field fields.Za.j", re.getCause().getCause().getMessage());
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) ResultException(org.springsource.loaded.test.infra.ResultException) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 23 with ResultException

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

the class TypeRewriterTests method reflectiveFieldGet.

/**
	 * Testing reflective field access when the field flips from static to non-static.
	 */
@Test
public void reflectiveFieldGet() throws Exception {
    String a = "reflect.FieldAccessing";
    TypeRegistry r = getTypeRegistry(a);
    ReloadableType type = r.addType(a, loadBytesForClass(a));
    Object o = type.getClazz().newInstance();
    // Access the fields
    result = runOnInstance(type.getClazz(), o, "geti");
    assertEquals(4, ((Integer) result.returnValue).intValue());
    result = runOnInstance(type.getClazz(), o, "getj");
    assertEquals(5, ((Integer) result.returnValue).intValue());
    // Load a new version that switches them from static<>non-static
    type.loadNewVersion("2", retrieveRename(a, a + "2"));
    try {
        result = runOnInstance(type.getClazz(), o, "geti");
        fail();
    } catch (ResultException re) {
        Throwable cause = re.getCause();
        assertTrue(cause instanceof InvocationTargetException);
        cause = ((InvocationTargetException) cause).getCause();
        assertTrue(cause instanceof IncompatibleClassChangeError);
        assertEquals("Expected non-static field reflect/FieldAccessing.i", cause.getMessage());
    }
    try {
        result = runOnInstance(type.getClazz(), o, "getj");
        fail();
    } catch (ResultException re) {
        Throwable cause = re.getCause();
        assertTrue(cause instanceof InvocationTargetException);
        cause = ((InvocationTargetException) cause).getCause();
        assertTrue(cause instanceof IncompatibleClassChangeError);
        assertEquals("Expected static field reflect/FieldAccessing.j", cause.getMessage());
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) ResultException(org.springsource.loaded.test.infra.ResultException) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 24 with ResultException

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

the class ClassGetFieldsTest method test.

@SuppressWarnings("unchecked")
@Override
public Result test() throws ResultException, Exception {
    try {
        Result r = runOnInstance(callerClazz, callerInstance, targetMethodName, targetClass);
        collectFieldNames((List<Field>) r.returnValue);
        return r;
    } catch (ResultException e) {
        throw new Error(e);
    }
}
Also used : Field(java.lang.reflect.Field) ResultException(org.springsource.loaded.test.infra.ResultException) Result(org.springsource.loaded.test.infra.Result)

Example 25 with ResultException

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

the class MethodGetParamAnnotationsTest 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)

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