Search in sources :

Example 31 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class EnumTests method testEnumsValueOf2.

/**
	 * Test the Enum.valueOf(EnumClass,String) - needs enumConstantDirectory clearing on reload.
	 */
@Test
public void testEnumsValueOf2() throws Exception {
    String t = "enumtests.ColoursC";
    String runner = "enumtests.RunnerC";
    Class<?> clazz = binLoader.loadClass(t);
    assertNotNull(clazz);
    Class<?> runnerClazz = binLoader.loadClass(runner);
    assertNotNull(runnerClazz);
    String output = runMethodAndCollectOutput(runnerClazz, "callValueOf2");
    assertContains("[Red Green Blue Orange Yellow]", output);
    assertContains("value count = 5", output);
    // Check we loaded it as reloadable
    ReloadableType rtype = TypeRegistry.getTypeRegistryFor(binLoader).getReloadableType(toSlash(t), false);
    assertNotNull(rtype);
    rtype.loadNewVersion(retrieveRename(t, t + "2", t + "2:" + t));
    output = runMethodAndCollectOutput(runnerClazz, "callValueOf2");
    assertContains("[Red Green Blue Orange Yellow Magenta Cyan]", output);
    assertContains("value count = 7", output);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Test(org.junit.Test)

Example 32 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class EnumTests method testEnumsConstructor.

/**
	 * Old constructor deleted, new one added. This checks that the rewritten clinit that initializes the set of values
	 * is working.
	 */
@Test
public void testEnumsConstructor() throws Exception {
    String t = "enumtests.ColoursD";
    String runner = "enumtests.RunnerD";
    binLoader.loadClass(t);
    Class<?> runnerClazz = binLoader.loadClass(runner);
    assertNotNull(runnerClazz);
    String output = runMethodAndCollectOutput(runnerClazz, "run1");
    assertContains("[Red 1111 0 Green 2222 1 Blue 3333 2]", output);
    // Check we loaded it as reloadable
    ReloadableType rtype = TypeRegistry.getTypeRegistryFor(binLoader).getReloadableType(toSlash(t), false);
    assertNotNull(rtype);
    ReloadableType rtypeRunner = TypeRegistry.getTypeRegistryFor(binLoader).getReloadableType(toSlash(runner), false);
    assertNotNull(rtypeRunner);
    // Changes from ints to chars
    rtype.loadNewVersion(retrieveRename(t, t + "2"));
    rtypeRunner.loadNewVersion(retrieveRename(runner, runner + "2", "enumtests.ColoursD2:enumtests.ColoursD"));
    output = runMethodAndCollectOutput(runnerClazz, "run1");
    assertContains("[Red a 0 Green b 1 Blue c 2]", output);
    assertContains("value count = 3", output);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) Test(org.junit.Test)

Example 33 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class ExecutorBuilderTests method typeLevelAnnotations.

/**
	 * Testing that type level annotations are copied to the executor (to answer later reflection questions).
	 */
@Test
public void typeLevelAnnotations() {
    String t = "executor.A";
    TypeRegistry typeRegistry = getTypeRegistry(t);
    ReloadableType rtype = typeRegistry.addType(t, loadBytesForClass(t));
    reload(rtype, "2");
    Class<?> clazz = rtype.getLatestExecutorClass();
    Assert.assertEquals(Utils.getExecutorName(t, "2"), clazz.getName());
    Annotation[] annos = clazz.getAnnotations();
    Assert.assertNotNull(annos);
    Assert.assertEquals(1, annos.length);
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Annotation(java.lang.annotation.Annotation) Test(org.junit.Test)

Example 34 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class ExecutorBuilderTests method typeLevelAnnotations2.

/**
	 * Testing that type level annotations are copied to the executor. This loads a different form of the type with a
	 * second annotation.
	 */
@Test
public void typeLevelAnnotations2() {
    String t = "executor.A";
    TypeRegistry typeRegistry = getTypeRegistry(t);
    ReloadableType rtype = typeRegistry.addType(t, loadBytesForClass(t));
    rtype.loadNewVersion("2", retrieveRename(t, t + "2"));
    Class<?> clazz = rtype.getLatestExecutorClass();
    Assert.assertEquals(Utils.getExecutorName(t, "2"), clazz.getName());
    Annotation[] annos = clazz.getAnnotations();
    Assert.assertNotNull(annos);
    Assert.assertEquals(2, annos.length);
    Set<String> s = new HashSet<String>();
    for (Annotation anno : annos) {
        s.add(anno.toString());
    }
    Assert.assertTrue(s.remove("@common.Marker()"));
    // Allow for alternate toString() variant
    if (!s.remove("@common.Anno(someValue=37, longValue=2, id=abc)")) {
        Assert.assertTrue(s.remove("@common.Anno(longValue=2, someValue=37, id=abc)"));
    }
    Assert.assertEquals(0, s.size());
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 35 with ReloadableType

use of org.springsource.loaded.ReloadableType in project spring-loaded by spring-projects.

the class ExecutorBuilderTests method secondVersion.

@Test
public void secondVersion() throws Exception {
    String tclass = "executor.TestOne";
    TypeRegistry typeRegistry = getTypeRegistry(tclass);
    ReloadableType rtype = typeRegistry.addType(tclass, loadBytesForClass(tclass));
    rtype.loadNewVersion("2", retrieveRename(tclass, tclass + "2"));
    // testing executor is for second version and not first
    // @formatter:off
    checkType(rtype.getLatestExecutorBytes(), "CLASS: executor/TestOne$$E2 v50 0x0001(public) super java/lang/Object\n" + "SOURCE: TestOne2.java null\n" + "FIELD 0x0001(public) i I\n" + "METHOD: 0x0009(public static) ___init___(Lexecutor/TestOne;)V\n" + "    CODE\n" + " L0\n" + "    ALOAD 0\n" + "    POP\n" + "    RETURN\n" + " L1\n" + "METHOD: 0x0009(public static) foo(Lexecutor/TestOne;Ljava/lang/String;)J\n" + "    CODE\n" + " L0\n" + "    ALOAD 1\n" + "    INVOKESTATIC java/lang/Long.parseLong(Ljava/lang/String;)J\n" + "    LRETURN\n" + " L1\n" + "METHOD: 0x0009(public static) hashCode(Lexecutor/TestOne;)I\n" + "    CODE\n" + " L0\n" + "    ALOAD 0\n" + "    LDC 0\n" + "    LDC i\n" + "    INVOKESTATIC org/springsource/loaded/TypeRegistry.instanceFieldInterceptionRequired(ILjava/lang/String;)Z\n" + "    IFEQ L1\n" + "    DUP\n" + "    LDC i\n" + "    INVOKESPECIAL executor/TestOne.r$get(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;\n" + "    CHECKCAST java/lang/Integer\n" + "    INVOKEVIRTUAL java/lang/Integer.intValue()I\n" + "    GOTO L2\n" + " L1\n" + "    GETFIELD executor/TestOne.i I\n" + " L2\n" + "    ICONST_2\n" + "    IMUL\n" + "    IRETURN\n" + " L3\n" + "\n");
    Assert.assertEquals(" L0\n" + "    ALOAD 1\n" + "    INVOKESTATIC java/lang/Long.parseLong(Ljava/lang/String;)J\n" + "    LRETURN\n" + " L1\n", toStringMethod(rtype.getLatestExecutorBytes(), "foo", false));
    // @formatter:on
    //
    // @formatter:off
    Assert.assertEquals(" L0\n" + "    ALOAD 0\n" + "    LDC 0\n" + "    LDC i\n" + "    INVOKESTATIC org/springsource/loaded/TypeRegistry.instanceFieldInterceptionRequired(ILjava/lang/String;)Z\n" + "    IFEQ L1\n" + "    DUP\n" + "    LDC i\n" + "    INVOKESPECIAL executor/TestOne.r$get(Ljava/lang/Object;Ljava/lang/String;)Ljava/lang/Object;\n" + "    CHECKCAST java/lang/Integer\n" + "    INVOKEVIRTUAL java/lang/Integer.intValue()I\n" + "    GOTO L2\n" + " L1\n" + "    GETFIELD executor/TestOne.i I\n" + " L2\n" + "    ICONST_2\n" + "    IMUL\n" + "    IRETURN\n" + " L3\n", toStringMethod(rtype.getLatestExecutorBytes(), "hashCode", false));
// @formatter:on
}
Also used : ReloadableType(org.springsource.loaded.ReloadableType) TypeRegistry(org.springsource.loaded.TypeRegistry) Test(org.junit.Test)

Aggregations

ReloadableType (org.springsource.loaded.ReloadableType)375 Test (org.junit.Test)320 TypeRegistry (org.springsource.loaded.TypeRegistry)287 Result (org.springsource.loaded.test.infra.Result)106 Method (java.lang.reflect.Method)37 InvocationTargetException (java.lang.reflect.InvocationTargetException)26 TestClassloaderWithRewriting (org.springsource.loaded.test.infra.TestClassloaderWithRewriting)22 Ignore (org.junit.Ignore)17 CurrentLiveVersion (org.springsource.loaded.CurrentLiveVersion)10 AccessibleObject (java.lang.reflect.AccessibleObject)9 ResultException (org.springsource.loaded.test.infra.ResultException)9 MethodMember (org.springsource.loaded.MethodMember)8 Field (java.lang.reflect.Field)6 TypeDescriptor (org.springsource.loaded.TypeDescriptor)6 IOException (java.io.IOException)5 Annotation (java.lang.annotation.Annotation)4 ZipEntry (java.util.zip.ZipEntry)4 ZipFile (java.util.zip.ZipFile)4 LoadtimeInstrumentationPlugin (org.springsource.loaded.LoadtimeInstrumentationPlugin)4 File (java.io.File)3