Search in sources :

Example 21 with TestClassloaderWithRewriting

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

the class GroovyTests method fields.

// Accessing a field from another type, which just turns into property
// access via a method
@Test
public void fields() throws Exception {
    binLoader = new TestClassloaderWithRewriting();
    String a = "simple.Front";
    String b = "simple.Back";
    TypeRegistry r = getTypeRegistry(a + "," + b);
    ReloadableType rtypea = r.addType(a, loadBytesForClass(a));
    ReloadableType rtypeb = r.addType(b, loadBytesForClass(b));
    result = runUnguarded(rtypea.getClazz(), "run");
    assertEquals(35, result.returnValue);
    try {
        result = runUnguarded(rtypea.getClazz(), "run2");
        fail();
    } catch (InvocationTargetException ite) {
    // success - var2 doesn't exist yet
    }
    rtypeb.loadNewVersion("2", retrieveRename(b, b + "2"));
    result = runUnguarded(rtypea.getClazz(), "run2");
    assertEquals(3355, result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TestClassloaderWithRewriting(org.springsource.loaded.test.infra.TestClassloaderWithRewriting) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 22 with TestClassloaderWithRewriting

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

the class GroovyTests method basic3.

// Similar to BasicB but now using non-static methods
@Test
public void basic3() throws Exception {
    binLoader = new TestClassloaderWithRewriting();
    String t = "simple.BasicC";
    TypeRegistry r = getTypeRegistry(t);
    ReloadableType rtype = r.addType(t, loadBytesForClass(t));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("hello", result.returnValue);
    rtype.loadNewVersion("2", retrieveRename(t, t + "2"));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("goodbye", result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TestClassloaderWithRewriting(org.springsource.loaded.test.infra.TestClassloaderWithRewriting) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 23 with TestClassloaderWithRewriting

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

the class GroovyTests method basic4.

// Now calling between two different types to check if we have
// to clear more than 'our' state on a reload. In this scenario
// the method being called is static
@Test
public void basic4() throws Exception {
    binLoader = new TestClassloaderWithRewriting();
    String t = "simple.BasicD";
    String target = "simple.BasicDTarget";
    TypeRegistry r = getTypeRegistry(t + "," + target);
    ReloadableType rtype = r.addType(t, loadBytesForClass(t));
    ReloadableType rtypeTarget = r.addType(target, loadBytesForClass(target));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("hello", result.returnValue);
    rtype.loadNewVersion("2", retrieveRename(t, t + "2"));
    rtypeTarget.loadNewVersion("2", retrieveRename(target, target + "2"));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("abc", result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TestClassloaderWithRewriting(org.springsource.loaded.test.infra.TestClassloaderWithRewriting) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 24 with TestClassloaderWithRewriting

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

the class GroovyTests method basicInstance.

@Test
public void basicInstance() throws Exception {
    binLoader = new TestClassloaderWithRewriting();
    String t = "simple.Basic";
    TypeRegistry r = getTypeRegistry(t);
    ReloadableType rtype = r.addType(t, loadBytesForClass(t));
    Object instance = null;
    instance = rtype.getClazz().newInstance();
    // First method call to 'run' should return "hello"
    result = runOnInstance(rtype.getClazz(), instance, "run");
    assertEquals("hello", result.returnValue);
    // Version 3 makes run() call another local method to get the string
    // "abc"
    rtype.loadNewVersion("3", retrieveRename(t, t + "3"));
    // Field f = rtype.getClazz().getDeclaredField("metaClass");
    // f.setAccessible(true);
    // Object mc = f.get(instance);
    // System.out.println("Metaclass is currently " + mc);
    //
    // f.set(instance, null);
    //
    // f =
    // rtype.getClazz().getDeclaredField("$class$groovy$lang$MetaClass");
    // f.setAccessible(true);
    // f.set(instance, null);
    //
    // Method m = rtype.getClazz().getDeclaredMethod("getMetaClass");
    // m.setAccessible(true);
    // m.invoke(instance);
    // f.setAccessible(true);
    // mc = f.get(instance);
    // 9: invokevirtual #23; //Method
    // $getStaticMetaClass:()Lgroovy/lang/MetaClass;
    // 12: dup
    // 13: invokestatic #27; //Method
    // $get$$class$groovy$lang$MetaClass:()Ljava/lang/Class;
    // 16: invokestatic #33; //Method
    // org/codehaus/groovy/runtime/ScriptBytecodeAdapter.castToType:(Ljava/lang/Object;Ljav
    // a/lang/Class;)Ljava/lang/Object;
    // 19: checkcast #35; //class groovy/lang/MetaClass
    // 22: aload_0
    // 23: swap
    // 24: putfield #37; //Field metaClass:Lgroovy/lang/MetaClass;
    // 27: pop
    // 28: return
    // Method m = rtype.getClazz().getDeclaredMethod("$getStaticMetaClass");
    // m.setAccessible(true);
    // Object o = m.invoke(instance);
    // m =
    // rtype.getClazz().getDeclaredMethod("$get$$class$groovy$lang$MetaClass");
    // m.setAccessible(true);
    // Object p = m.invoke(null);
    // m =
    // rtype.getClazz().getClassLoader().loadClass("org.codehaus.groovy.runtime.ScriptBytecodeAdapter")
    // .getDeclaredMethod("castToType", Object.class, Class.class);
    // m.setAccessible(true);
    //
    // Object mc = m.invoke(null, o, p);
    // Field f = rtype.getClazz().getDeclaredField("metaClass");
    // f.setAccessible(true);
    // f.set(instance, null);
    // instance = rtype.getClazz().newInstance();
    // System.out.println("Metaclass is currently " + mc);
    // Let's reinitialize the instance meta class by duplicating
    result = runOnInstance(rtype.getClazz(), instance, "run");
    // result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("abc", result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TestClassloaderWithRewriting(org.springsource.loaded.test.infra.TestClassloaderWithRewriting) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Example 25 with TestClassloaderWithRewriting

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

the class GroovyTests method basic6.

// Now call a method on the target, then load a version where it is gone,
// what happens?
// I'm looking to determine what in the caller needs clearing out based on
// what it has
// cached about the target
@Test
public void basic6() throws Exception {
    binLoader = new TestClassloaderWithRewriting();
    String t = "simple.BasicF";
    String target = "simple.BasicFTarget";
    // GlobalConfiguration.logging = true;
    // GlobalConfiguration.isRuntimeLogging = true;
    TypeRegistry r = getTypeRegistry(t + "," + target);
    ReloadableType rtype = r.addType(t, loadBytesForClass(t));
    ReloadableType rtypeTarget = r.addType(target, loadBytesForClass(target));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("123", result.returnValue);
    // rtype.loadNewVersion("2", retrieveRename(t, t + "2"));
    rtypeTarget.loadNewVersion("2", retrieveRename(target, target + "2"));
    result = null;
    // The target method has been removed, should now fail to call it
    try {
        runUnguarded(rtype.getClazz(), "run");
        fail();
    } catch (InvocationTargetException ite) {
    // success
    }
    // Load the original back in, should work again
    rtypeTarget.loadNewVersion("3", retrieveRename(target, target));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("123", result.returnValue);
    // rtype.loadNewVersion("2", rtype.bytesInitial); //reload yourself
    // Load a new version that now returns an int
    rtypeTarget.loadNewVersion("4", retrieveRename(target, target + "4"));
    result = runUnguarded(rtype.getClazz(), "run");
    assertEquals("4456", result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TestClassloaderWithRewriting(org.springsource.loaded.test.infra.TestClassloaderWithRewriting) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Aggregations

TestClassloaderWithRewriting (org.springsource.loaded.test.infra.TestClassloaderWithRewriting)26 Test (org.junit.Test)24 TypeRegistry (org.springsource.loaded.TypeRegistry)24 ReloadableType (org.springsource.loaded.ReloadableType)22 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 Ignore (org.junit.Ignore)3 ReloadEventProcessorPlugin (org.springsource.loaded.ReloadEventProcessorPlugin)3 Before (org.junit.Before)2