Search in sources :

Example 46 with Result

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

the class MethodInvokerRewriterTests method rewriteInvokeStatic6.

/**
	 * If the static method is made non-static, here is what happens in the java case:
	 * 
	 * <pre>
	 * Exception in thread "main" java.lang.IncompatibleClassChangeError: Expected static method B.foo()V
	 *         at A.main(A.java:3)
	 * </pre>
	 */
@Test
public void rewriteInvokeStatic6() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("tgt.SimpleClass");
    ReloadableType callee = typeRegistry.addType("tgt.SimpleClass", loadBytesForClass("tgt.SimpleClass"));
    byte[] callerbytes = loadBytesForClass("tgt.StaticCaller");
    byte[] rewrittenBytes = MethodInvokerRewriter.rewrite(typeRegistry, callerbytes);
    Class<?> callerClazz = loadit("tgt.StaticCaller", rewrittenBytes);
    // run the original
    Result result = runUnguarded(callerClazz, "run");
    assertEquals(123, result.returnValue);
    // new version of SimpleClass where target static method has been made non-static
    callee.loadNewVersion("6", retrieveRename("tgt.SimpleClass", "tgt.SimpleClass006"));
    try {
        result = runUnguarded(callerClazz, "run");
        Assert.fail();
    } catch (InvocationTargetException ite) {
        Throwable t = ite.getCause();
        IncompatibleClassChangeError icce = (IncompatibleClassChangeError) t;
        assertEquals("SpringLoaded: Target of static call is no longer static 'SimpleClass.toInt(Ljava/lang/String;)I'", icce.getMessage());
    }
}
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)

Example 47 with Result

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

the class MethodInvokerRewriterTests method callingMethodIntroducedLaterPrimitiveParams.

// Method is 'String run2(String a, int b, String c, int d)' which does not exist in Apple but exists in
/**
	 * Targets for the calls here are: Apple then Apple002 Caller is Orange002
	 */
@Test
public void callingMethodIntroducedLaterPrimitiveParams() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("data.Apple");
    ReloadableType target = typeRegistry.addType("data.Apple", loadBytesForClass("data.Apple"));
    byte[] callerb = ClassRenamer.rename("data.Orange", loadBytesForClass("data.Orange002"), "data.Apple002:data.Apple");
    byte[] rewrittencallerb = MethodInvokerRewriter.rewrite(typeRegistry, callerb);
    Class<?> callerClazz = loadit("data.Orange", rewrittencallerb);
    // Method does not exist yet
    runExpectNoSuchMethodException(callerClazz, "callApple2", 3);
    // Load a version of Apple that does define that method
    target.loadNewVersion("002", retrieveRename("data.Apple", "data.Apple002"));
    Result result = runUnguarded(callerClazz, "callApple2", 4);
    assertEquals("run2 4", result.returnValue);
    // Load a version of Apple that doesn't define it
    target.loadNewVersion("003", loadBytesForClass("data.Apple"));
    runExpectNoSuchMethodException(callerClazz, "callApple2", 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 48 with Result

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

the class ReloadableTypeTests method invokeStaticReloading_gh4_1.

// github issue 4
@Test
public void invokeStaticReloading_gh4_1() throws Exception {
    TypeRegistry tr = getTypeRegistry("invokestatic..*");
    tr.addType("invokestatic.issue4.A", loadBytesForClass("invokestatic.issue4.A"));
    ReloadableType B = tr.addType("invokestatic.issue4.B", loadBytesForClass("invokestatic.issue4.B"));
    Result r = runUnguarded(B.getClazz(), "getMessage");
    assertEquals("String1", r.returnValue);
    B.loadNewVersion(B.bytesInitial);
    r = runUnguarded(B.getClazz(), "getMessage");
    assertEquals("String1", r.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 49 with Result

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

the class ReloadableTypeTests method callIt.

/**
	 * Check calling it, reloading it and calling the new version.
	 */
@Test
public void callIt() throws Exception {
    TypeRegistry typeRegistry = getTypeRegistry("basic.Basic");
    byte[] sc = loadBytesForClass("basic.Basic");
    ReloadableType rtype = typeRegistry.addType("basic.Basic", sc);
    Class<?> simpleClass = rtype.getClazz();
    Result r = runUnguarded(simpleClass, "foo");
    r = runUnguarded(simpleClass, "getValue");
    assertEquals(5, r.returnValue);
    rtype.loadNewVersion("002", retrieveRename("basic.Basic", "basic.Basic002"));
    r = runUnguarded(simpleClass, "getValue");
    assertEquals(7, r.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 50 with Result

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

the class ReloadableTypeTests method invokeStaticReloading_gh4_4.

@Test
public void invokeStaticReloading_gh4_4() throws Exception {
    TypeRegistry tr = getTypeRegistry("invokestatic..*");
    ReloadableType A = tr.addType("invokestatic.issue4.A", loadBytesForClass("invokestatic.issue4.A"));
    ReloadableType B = tr.addType("invokestatic.issue4.B", loadBytesForClass("invokestatic.issue4.B"));
    Result r = runUnguarded(B.getClazz(), "getMessage");
    assertEquals("String1", r.returnValue);
    A.loadNewVersion(A.bytesInitial);
    B.loadNewVersion(B.bytesInitial);
    r = runUnguarded(B.getClazz(), "getMessage");
    assertEquals("String1", r.returnValue);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) 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