diff options
Diffstat (limited to 'weaver5/java5-testsrc')
-rw-r--r-- | weaver5/java5-testsrc/org/aspectj/weaver/reflect/ReflectionWorldTest.java | 70 |
1 files changed, 66 insertions, 4 deletions
diff --git a/weaver5/java5-testsrc/org/aspectj/weaver/reflect/ReflectionWorldTest.java b/weaver5/java5-testsrc/org/aspectj/weaver/reflect/ReflectionWorldTest.java index 6c65c1e64..db3e44711 100644 --- a/weaver5/java5-testsrc/org/aspectj/weaver/reflect/ReflectionWorldTest.java +++ b/weaver5/java5-testsrc/org/aspectj/weaver/reflect/ReflectionWorldTest.java @@ -8,17 +8,21 @@ * ******************************************************************/ package org.aspectj.weaver.reflect; +import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Type; - +import java.net.URL; +import java.net.URLClassLoader; import java.util.List; +import java.util.Map; -import org.aspectj.weaver.ResolvedType; -import org.aspectj.weaver.UnresolvedType; -import org.aspectj.weaver.World; import org.aspectj.bridge.IMessageHandler; import org.aspectj.weaver.ReferenceType; import org.aspectj.weaver.ResolvedMember; +import org.aspectj.weaver.ResolvedType; +import org.aspectj.weaver.UnresolvedType; +import org.aspectj.weaver.WeakClassLoaderReference; +import org.aspectj.weaver.World; import org.aspectj.weaver.bcel.BcelWorld; import junit.framework.TestCase; @@ -35,6 +39,64 @@ public class ReflectionWorldTest extends TestCase { assertNotNull(rt); assertEquals("Ljava/lang/Object;", rt.getSignature()); } + + public void testReflectionWorldFactory() throws Exception { + ClassLoader parent = getClass().getClassLoader(); + ClassLoader cl1 = new URLClassLoader(new URL[] {}, parent); + ClassLoader cl2 = new URLClassLoader(new URL[] {}, parent); + + WeakClassLoaderReference wcl1 = new WeakClassLoaderReference(cl1); + WeakClassLoaderReference wcl2 = new WeakClassLoaderReference(cl2); + ReflectionWorld a = ReflectionWorld.getReflectionWorldFor(wcl1); + + ResolvedType stringClass1 = a.resolve(String.class); + assertNotNull(stringClass1); + + ReflectionWorld b = ReflectionWorld.getReflectionWorldFor(wcl1); + + // They should be the same because the classloader has not gone away + assertTrue(a==b); + + cl1 = null; + for (int i=0;i<100;i++) { + System.gc(); // How robust is it that this should be causing the reference to be collected? + } + b = ReflectionWorld.getReflectionWorldFor(wcl1); + + assertFalse(a==b); + + cl1 = new URLClassLoader(new URL[] {}, parent); + wcl1 = new WeakClassLoaderReference(cl1); + a = ReflectionWorld.getReflectionWorldFor(wcl1); + b = ReflectionWorld.getReflectionWorldFor(wcl2); + assertFalse(a==b); + + Field declaredField = ReflectionWorld.class.getDeclaredField("rworlds"); + declaredField.setAccessible(true); + Map worlds = (Map)declaredField.get(null); + assertEquals(2, worlds.size()); + + cl2 = null; + for (int i=0;i<100;i++) { + System.gc(); // How robust is it that this should be causing the reference to be collected? + } + ReflectionWorld.getReflectionWorldFor(wcl1); // need to call this to trigger tidyup + assertEquals(1, worlds.size()); + + cl1 = null; + for (int i=0;i<100;i++) { + System.gc(); // How robust is it that this should be causing the reference to be collected? + } + ReflectionWorld.getReflectionWorldFor(wcl1); // need to call this to trigger tidyup + assertEquals(0, worlds.size()); + + cl1 = new URLClassLoader(new URL[] {}, parent); + wcl1 = new WeakClassLoaderReference(cl1); + ReflectionWorld reflectionWorldFor = ReflectionWorld.getReflectionWorldFor(wcl1); + assertEquals(1, worlds.size()); + ReflectionWorld.cleanUpWorlds(); + assertEquals(0, worlds.size()); + } public void testArrayTypes() { IReflectionWorld world = new ReflectionWorld(getClass().getClassLoader()); |