Search in sources :

Example 36 with Result

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

the class MethodInvokerRewriterTests method rewriteInvokeInterface2.

/**
	 * Variant of the above where there is a parameter.
	 */
@Test
public void rewriteInvokeInterface2() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("tgt.SimpleIClassTwo,tgt.SimpleITwo");
    ReloadableType intface = typeRegistry.addType("tgt.SimpleITwo", loadBytesForClass("tgt.SimpleITwo"));
    //		ReloadableType impl =
    typeRegistry.addType("tgt.SimpleIClassTwo", loadBytesForClass("tgt.SimpleIClassTwo"));
    byte[] callerbytes = loadBytesForClass("tgt.StaticICallerTwo");
    byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz = loadit("tgt.StaticICallerTwo", rewrittenBytes);
    // run the original
    Result result = runUnguarded(callerClazz, "run");
    assertEquals(123, result.returnValue);
    intface.loadNewVersion("2", retrieveRename("tgt.SimpleITwo", "tgt.SimpleITwo002"));
    result = runUnguarded(callerClazz, "run");
    assertEquals(123, result.returnValue);
    callerbytes = loadBytesForClass("tgt.StaticICallerTwo002");
    callerbytes = ClassRenamer.rename("tgt.StaticICallerTwo002", callerbytes, "tgt.SimpleITwo002:tgt.SimpleITwo");
    rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz002 = loadit("tgt.StaticICallerTwo002", rewrittenBytes);
    //		ClassPrinter.print(rewrittenBytes);
    //		callee.loadNewVersion("2", retrieveRename("tgt.SimpleClass", "tgt.SimpleClass002"));
    result = runUnguarded(callerClazz002, "run");
    assertEquals("27", 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 37 with Result

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

the class MethodInvokerRewriterTests method callingMethodIntroducedLaterReturningPrimitiveByte.

@Test
public void callingMethodIntroducedLaterReturningPrimitiveByte() 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, "callAppleRetByte", new Object[] { (byte) 54 });
    // Load a version of Apple that does define that method
    apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
    Result result = runUnguarded(callerClazz, "callAppleRetByte", new Object[] { (byte) 32 });
    assertEquals((byte) 64, 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 38 with Result

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

the class ReloadableTypeTests method innerTypesLosingStaticModifier.

@Test
public void innerTypesLosingStaticModifier() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("inners.Outer$Inner");
    byte[] sc = loadBytesForClass("inners.Outer$Inner");
    ReloadableType rtype = typeRegistry.addType("inners.Outer$Inner", sc);
    Class<?> simpleClass = rtype.getClazz();
    Result r = null;
    r = runUnguarded(simpleClass, "foo");
    assertEquals("foo!", r.returnValue);
    assertTrue(Modifier.isPublic((Integer) runUnguarded(simpleClass, "getModifiers").returnValue));
    assertTrue(Modifier.isStatic((Integer) runUnguarded(simpleClass, "getModifiers").returnValue));
    rtype.loadNewVersion("002", retrieveRename("inners.Outer$Inner", "inners.Outer2$Inner2"));
    r = runUnguarded(simpleClass, "foo");
    assertEquals("bar!", r.returnValue);
    assertTrue(Modifier.isPublic((Integer) runUnguarded(simpleClass, "getModifiers").returnValue));
    assertTrue(Modifier.isStatic((Integer) runUnguarded(simpleClass, "getModifiers").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 39 with Result

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

the class MethodInvokerRewriterTests method callingMethodIntroducedLaterPrimitiveParamsLongDouble.

@Test
public void callingMethodIntroducedLaterPrimitiveParamsLongDouble() 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);
    // public String callApple3(String s, int i, double d, String t, int[] is) {
    runExpectNoSuchMethodException(callerClazz, "callApple3x", new Object[] { "abc", 1, 2.0d, "def", new int[] { 42, 53 } });
    // Load a version of Apple that does define that method
    apple.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
    Result result = runUnguarded(callerClazz, "callApple3x", new Object[] { "abc", 1, 2.0d, "def", new int[] { 42, 53 } });
    assertEquals("abc12.0def42", result.returnValue);
    // Load a version of Apple that doesn't define it
    apple.loadNewVersion("003", loadBytesForClass("data.Apple"));
    runExpectNoSuchMethodException(callerClazz, "callApple3x", new Object[] { "abc", 1, 2.0d, "def", new int[] { 42, 53 } });
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 40 with Result

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

the class ProxyTests method xnonPublicInterface.

/**
	 * For non public interfaces the proxies are generated in the same package as the interface.
	 */
// To run these tests you need to have -javaagent specified
@Ignore
@Test
public void xnonPublicInterface() throws Exception {
    // Set so that the Proxy generator can see the interface class
    Thread.currentThread().setContextClassLoader(binLoader);
    Class<?> clazz = Class.forName("proxy.two.TestA1", false, binLoader);
    Result r = runUnguarded(clazz, "createProxy");
    Class<?> clazzForInterface = Class.forName("proxy.two.TestIntfaceA1", false, binLoader);
    // Call a method through the proxy
    r = runUnguarded(clazz, "runM");
    assertContains("TestInvocationHandler1.invoke() for m", r.stdout);
    TypeRegistry tr = TypeRegistry.getTypeRegistryFor(binLoader);
    assertNotNull(tr);
    ReloadableType rt = tr.getReloadableType(clazzForInterface);
    assertNotNull(rt);
    // new version adds a method called n
    byte[] newVersionOfTestInterfaceA1 = retrieveRename("proxy.two.TestIntfaceA1", "proxy.two.TestIntfaceA2");
    rt.loadNewVersion(newVersionOfTestInterfaceA1);
    // running m() should still work
    r = runUnguarded(clazz, "runM");
    assertContains("TestInvocationHandler1.invoke() for m", r.stdout);
    // Now load new version of proxy.TestA1 that will enable us to call n on the new interface
    byte[] newVersionOfTestA2 = retrieveRename("proxy.two.TestA1", "proxy.two.TestA2", "proxy.two.TestIntfaceA2:proxy.two.TestIntfaceA1");
    tr.getReloadableType(clazz).loadNewVersion(newVersionOfTestA2);
    // running m() should still work
    r = runUnguarded(clazz, "runM");
    assertContains("TestInvocationHandler1.invoke() for m", r.stdout);
    // running n() should now work! (if the proxy was auto regen/reloaded)
    r = runUnguarded(clazz, "runN");
    assertContains("TestInvocationHandler1.invoke() for n", r.stdout);
    Set<ReloadableType> proxies = tr.getJDKProxiesFor("proxy/two/TestIntfaceA1");
    assertFalse(proxies.isEmpty());
    ReloadableType proxyRT = proxies.iterator().next();
    assertStartsWith("proxy.two.", proxyRT.getName());
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Ignore(org.junit.Ignore) 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