Search in sources :

Example 41 with Result

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

the class MethodInvokerRewriterTests method invokevirtual.

@Test
public void invokevirtual() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("virtual.CalleeOne");
    // The first target does not define toString()
    ReloadableType target = typeRegistry.addType("virtual.CalleeOne", loadBytesForClass("virtual.CalleeOne"));
    Class<?> callerClazz = loadit("virtual.CallerOne", loadBytesForClass("virtual.CallerOne"));
    // Run the initial version which does not define toString()
    Result result = runUnguarded(callerClazz, "run");
    // something like virtual.CalleeOne@4cee32
    assertTrue(((String) result.returnValue).startsWith("virtual.CalleeOne@"));
    // Load a version that does define toString()
    target.loadNewVersion("002", retrieveRename("virtual.CalleeOne", "virtual.CalleeOne002"));
    result = runUnguarded(callerClazz, "run");
    assertEquals("abcd", result.returnValue);
    // Load a version that does not define toString()
    target.loadNewVersion("003", retrieveRename("virtual.CalleeOne", "virtual.CalleeOne003"));
    result = runUnguarded(callerClazz, "run");
    // something like virtual.CalleeOne@4cee32
    assertTrue(((String) result.returnValue).startsWith("virtual.CalleeOne@"));
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 42 with Result

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

the class MethodInvokerRewriterTests method rewriteInvokeInterface1.

/**
	 * Here an interface is changed (reloaded) to include a new method, the implementing class already provides an
	 * implementation.
	 */
@Test
public void rewriteInvokeInterface1() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("tgt.SimpleIClass,tgt.SimpleI");
    ReloadableType intface = typeRegistry.addType("tgt.SimpleI", loadBytesForClass("tgt.SimpleI"));
    //		ReloadableType impl =
    typeRegistry.addType("tgt.SimpleIClass", loadBytesForClass("tgt.SimpleIClass"));
    byte[] callerbytes = loadBytesForClass("tgt.StaticICaller");
    byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz = loadit("tgt.StaticICaller", rewrittenBytes);
    // run the original
    Result result = runUnguarded(callerClazz, "run");
    assertEquals(123, result.returnValue);
    intface.loadNewVersion("2", retrieveRename("tgt.SimpleI", "tgt.SimpleI002"));
    // run the original working thing post-reload - check it is still ok
    result = runUnguarded(callerClazz, "run");
    assertEquals(123, result.returnValue);
    callerbytes = loadBytesForClass("tgt.StaticICaller002");
    callerbytes = ClassRenamer.rename("tgt.StaticICaller002", callerbytes, "tgt.SimpleI002:tgt.SimpleI");
    rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz002 = loadit("tgt.StaticICaller002", rewrittenBytes);
    result = runUnguarded(callerClazz002, "run");
    assertEquals("42", result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 43 with Result

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

the class MethodInvokerRewriterTests method callingMethodIntroducedLaterReturningPrimitiveDouble.

@Test
public void callingMethodIntroducedLaterReturningPrimitiveDouble() throws Exception {
    TypeRegistry typeRegistry = TypeRegistry.getTypeRegistryFor(binLoader);
    // Configure it directly such that data.Apple is considered reloadable
    configureForTesting(typeRegistry, "data.Apple");
    ReloadableType apple = typeRegistry.addType("data.Apple", loadBytesForClass("data.Apple"));
    byte[] callerbytes = loadBytesForClass("data.Orange002");
    callerbytes = ClassRenamer.rename("data.Orange", callerbytes, "data.Apple002:data.Apple");
    byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz = loadit("data.Orange", rewrittenBytes);
    runExpectNoSuchMethodException(callerClazz, "callAppleRetDouble", new Object[] { 5.0d });
    // Load a version of Apple that does define that method
    apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
    Result result = runUnguarded(callerClazz, "callAppleRetDouble", new Object[] { 3.0d });
    assertEquals(6.0d, result.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 44 with Result

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

the class MethodInvokerRewriterTests method callingMethodChangedFromNonStaticToStatic.

@Test
public void callingMethodChangedFromNonStaticToStatic() throws Exception {
    TypeRegistry typeRegistry = TypeRegistry.getTypeRegistryFor(binLoader);
    // Configure it directly such that data.Apple is considered reloadable
    configureForTesting(typeRegistry, "data.Apple");
    ReloadableType apple = typeRegistry.addType("data.Apple", loadBytesForClass("data.Apple"));
    byte[] callerbytes = loadBytesForClass("data.Orange002");
    callerbytes = ClassRenamer.rename("data.Orange", callerbytes, "data.Apple002:data.Apple");
    byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz = loadit("data.Orange", rewrittenBytes);
    runExpectNoSuchMethodException(callerClazz, "callApple4", new Object[] { 3 });
    // Load a version of Apple that does define that method
    apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
    Result result = runUnguarded(callerClazz, "callApple4", new Object[] { 4 });
    assertEquals(8, result.returnValue);
    // Load a version of Apple that doesn't define it
    apple.loadNewVersion("003", loadBytesForClass("data.Apple"));
    runExpectNoSuchMethodException(callerClazz, "callApple4", new Object[] { 5 });
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 45 with Result

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

the class MethodInvokerRewriterTests method rewriteCallArguments.

/**
	 * Target method here takes (string,integer,string,integer) and return a string
	 */
@Test
public void rewriteCallArguments() throws Exception {
    TypeRegistry typeRegistry = TypeRegistry.getTypeRegistryFor(binLoader);
    // Configure it directly such that data.Apple is considered reloadable
    configureForTesting(typeRegistry, "data.Apple");
    ReloadableType apple = typeRegistry.addType("data.Apple", loadBytesForClass("data.Apple"));
    byte[] callerbytes = loadBytesForClass("data.Orange002");
    callerbytes = ClassRenamer.rename("data.Orange", callerbytes, "data.Apple002:data.Apple");
    byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz = loadit("data.Orange", rewrittenBytes);
    try {
        runUnguarded(callerClazz, "callApple1", new Object[] { "a", 1, "b", 2 });
        Assert.fail("should not work, Apple doesn't have that method in it!");
    } catch (InvocationTargetException ite) {
        String cause = ite.getCause().toString();
        if (!cause.startsWith("java.lang.NoSuchMethodError")) {
            ite.printStackTrace();
            Assert.fail("Should be a NoSuchMethodError, but got " + ite.getCause());
        }
    }
    // Load a version of Apple that does define that method
    apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
    Result result = runUnguarded(callerClazz, "callApple1", new Object[] { "a", 1, "b", 2 });
    assertEquals("a 1 b 2", result.returnValue);
    // Load a version of Apple that doesn't define it
    apple.loadNewVersion("003", loadBytesForClass("data.Apple"));
    try {
        result = runUnguarded(callerClazz, "callApple1", new Object[] { "a", 1, "b", 2 });
        Assert.fail("should not work, Apple doesn't have that method in it!");
    } catch (InvocationTargetException ite) {
        String cause = ite.getCause().toString();
        if (!cause.startsWith("java.lang.NoSuchMethodError")) {
            ite.printStackTrace();
            Assert.fail("Should be a NoSuchMethodError, but got " + ite);
        }
    }
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) InvocationTargetException(java.lang.reflect.InvocationTargetException) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

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