Search in sources :

Example 51 with Result

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

the class ReloadableTypeTests method invokeStaticReloading_gh4_6.

// extra class in the middle: A in jar, subtype AB reloadable, subtype BBBBB reloadable
@Test
public void invokeStaticReloading_gh4_6() throws Exception {
    TypeRegistry tr = getTypeRegistry("invokestatic.issue4..*");
    tr.addType("invokestatic.issue4.AB", loadBytesForClass("invokestatic.issue4.AB"));
    ReloadableType B = tr.addType("invokestatic.issue4.BBBBB", loadBytesForClass("invokestatic.issue4.BBBBB"));
    Result r = runUnguarded(B.getClazz(), "getMessage");
    assertEquals("Hello", r.returnValue);
    ReloadableType thesuper = B.getSuperRtype();
    thesuper = tr.getReloadableType("invokestatic/issue4/subpkg/AAAA");
    assertNull(thesuper);
    B.loadNewVersion(B.bytesInitial);
    r = runUnguarded(B.getClazz(), "getMessage");
    assertEquals("Hello", 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 52 with Result

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

the class ReloadableTypeTests method serialization3.

// Variant of the second test but using serialVersionUID and adding methods to the class on reload
@Test
public void serialization3() throws Exception {
    TypeRegistry tr = getTypeRegistry("remote..*");
    ReloadableType person = tr.addType("remote.PersonB", loadBytesForClass("remote.PersonB"));
    ReloadableType runner = tr.addType("remote.SerializeB", loadBytesForClass("remote.SerializeB"));
    Class<?> clazz = runner.getClazz();
    Object instance = clazz.newInstance();
    Result r = null;
    // Basic: write and read the same Person
    r = runOnInstance(runner.getClazz(), instance, "writePerson");
    assertStdoutContains("Person stored ok", r);
    r = runOnInstance(runner.getClazz(), instance, "readPerson");
    assertContains("Person read ok", r.stdout);
    // Advanced: write it, reload, then read back from the written form
    r = runOnInstance(runner.getClazz(), instance, "writePerson");
    assertStdoutContains("Person stored ok", r);
    person.loadNewVersion("2", retrieveRename("remote.PersonB", "remote.PersonB2"));
    r = runOnInstance(runner.getClazz(), instance, "readPerson");
    assertContains("Person read ok", r.stdout);
    r = runOnInstance(clazz, instance, "printInitials");
    assertContains("Person read ok\nWS", r.stdout);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result) Test(org.junit.Test)

Example 53 with Result

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

the class ReloadableTypeTests method serialization4.

// Deserialize something we serialized earlier
// This test cannot work without the agent. The agent must intercept java.lang.ObjectStream and its use of reflection
// There is a test that will work in the SpringLoadedTestsInSeparateJVM
public void serialization4() throws Exception {
    TypeRegistry tr = getTypeRegistry("remote..*");
    //		ReloadableType person =
    tr.addType("remote.Person", loadBytesForClass("remote.Person"));
    // When the Serialize class is run directly, we see: byteinfo:len=98:crc=c1047cf6
    // When run via this test, we see: byteinfo:len=98:crc=7e07276a
    ReloadableType runner = tr.addType("remote.Serialize", loadBytesForClass("remote.Serialize"));
    Class<?> clazz = runner.getClazz();
    Object instance = clazz.newInstance();
    Result r = runOnInstance(clazz, instance, "checkPredeserializedData");
    assertStdoutContains("Person stored ok", r);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Result(org.springsource.loaded.test.infra.Result)

Example 54 with Result

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

the class SpringLoadedTests method runUnguarded.

public Result runUnguarded(Class<?> clazz, String methodname, Object... params) throws InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
    PrintStream oldo = System.out;
    PrintStream olde = System.err;
    Object result = null;
    ByteArrayOutputStream oso = new ByteArrayOutputStream();
    ByteArrayOutputStream ose = new ByteArrayOutputStream();
    try {
        if (capture) {
            System.setOut(new PrintStream(oso));
            System.setErr(new PrintStream(ose));
        }
        Object o = clazz.newInstance();
        Method m = null;
        Method[] ms = clazz.getMethods();
        for (Method mm : ms) {
            if (mm.getName().equals(methodname)) {
                m = mm;
                break;
            }
        }
        if (m == null) {
            Assert.fail("Invocation failure: could not find method '" + methodname + "' on type '" + clazz.getName());
        }
        m.setAccessible(true);
        result = m.invoke(o, params);
    } finally {
        if (capture) {
            System.setOut(oldo);
            System.setErr(olde);
        }
    }
    return new Result(result, oso.toString().replace("\r", ""), ose.toString().replace("\r", ""));
}
Also used : PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) Result(org.springsource.loaded.test.infra.Result)

Example 55 with Result

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

the class ExecutionContextsTest method test.

@Override
public Result test() throws ResultException, RejectedChoice, Exception {
    //Check that each context is loading the correct version of the class
    if (version.equals("")) {
        try {
            getDeclaredMethod();
            Assert.fail("lateMethod should not exist in first version of the test");
        } catch (NoSuchMethodException e) {
        //OK
        }
    } else {
        Method m = getDeclaredMethod();
        Assert.assertEquals("lateMethod", m.getName());
    }
    //Check that tests are generated/executed in the order they are presumed to:
    checkTestHistory();
    return new Result(targetTypeName, "", "");
}
Also used : Method(java.lang.reflect.Method) Result(org.springsource.loaded.test.infra.Result)

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