1 19 20 package org.netbeans.modules.j2ee.metadata; 21 22 import java.beans.PropertyChangeEvent ; 23 import java.beans.PropertyChangeListener ; 24 import java.beans.PropertyChangeSupport ; 25 import java.lang.ref.WeakReference ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.HashSet ; 30 import java.util.List ; 31 import java.util.Set ; 32 import org.netbeans.junit.NbTestCase; 33 import org.netbeans.modules.j2ee.metadata.*; 34 import org.netbeans.spi.java.classpath.ClassPathImplementation; 35 import org.netbeans.spi.java.classpath.PathResourceImplementation; 36 import org.netbeans.spi.java.classpath.support.ClassPathSupport; 37 38 42 public class WeakProxyClassPathImplementationTest extends NbTestCase { 43 44 public WeakProxyClassPathImplementationTest(String testName) { 45 super(testName); 46 } 47 48 public void testWeakProxyClassPathImplementation() throws Exception { 49 class PCL implements PropertyChangeListener { 50 51 private int changeCount; 52 53 public void propertyChange(PropertyChangeEvent event) { 54 changeCount++; 55 } 56 57 private boolean testChangeAndReset() { 58 boolean result = changeCount > 0; 59 changeCount = 0; 60 return result; 61 } 62 } 63 64 URL url11 = new URL ("file:///foo/bar11/"); 65 URL url12 = new URL ("file:///foo/bar12/"); 66 URL url2 = new URL ("file:///foo/bar2/"); 67 URL url3 = new URL ("file:///foo/bar3/"); 68 69 ClassPathImpl classPath1 = new ClassPathImpl(new URL [] { url11 }); 70 ClassPathImpl classPath2 = new ClassPathImpl(new URL [] { url2 }); 71 ClassPathImpl classPath3 = new ClassPathImpl(new URL [] { url3 }); 72 73 WeakProxyClassPathImplementation proxyClassPath = new WeakProxyClassPathImplementation(new ClassPathImplementation[] { classPath1, classPath2, classPath3 }); 74 PCL pcl = new PCL(); 75 proxyClassPath.addPropertyChangeListener(pcl); 76 77 assertResources(proxyClassPath.getResources(), new URL [] { url11, url2, url3 }); 78 79 81 classPath1.setResources(new URL [] { url11, url12 }); 82 83 assertTrue(pcl.testChangeAndReset()); 84 assertResources(proxyClassPath.getResources(), new URL [] { url11, url12, url2, url3 }); 85 86 88 WeakReference <ClassPathImpl> classPath2Ref = new WeakReference <ClassPathImpl>(classPath2); 89 classPath2 = null; 90 assertGC("Should be able to gc classpath2", classPath2Ref); 91 assertTrue(pcl.testChangeAndReset()); 92 assertResources(proxyClassPath.getResources(), new URL [] { url11, url12, url3 }); 93 94 96 classPath3.setResources(new URL [] { }); 97 98 assertTrue(pcl.testChangeAndReset()); 99 assertResources(proxyClassPath.getResources(), new URL [] { url11, url12 }); 100 101 WeakReference <ClassPathImpl> classPath1Ref = new WeakReference <ClassPathImpl>(classPath1); 102 WeakReference <ClassPathImpl> classPath3Ref = new WeakReference <ClassPathImpl>(classPath3); 103 classPath1 = null; 104 classPath3 = null; 105 assertGC("Should be able to gc classpath1", classPath1Ref); 106 assertGC("Should be able to gc classpath3", classPath3Ref); 107 assertResources(proxyClassPath.getResources(), new URL [0]); 108 } 109 110 private static void assertResources(List <PathResourceImplementation> expected, URL [] actual) { 111 Set <URL > expectedSet = new HashSet <URL >(); 112 for (PathResourceImplementation resource: expected) { 113 expectedSet.add(resource.getRoots()[0]); 116 } 117 Set <URL > actualSet = new HashSet <URL >(Arrays.asList(actual)); 118 assertEquals(expectedSet, actualSet); 119 } 120 121 private static final class ClassPathImpl implements ClassPathImplementation { 122 123 private final PropertyChangeSupport propChangeSupport = new PropertyChangeSupport (this); 124 125 private List <PathResourceImplementation> resources; 126 127 public ClassPathImpl(URL [] urls) { 128 setResources(urls); 129 } 130 131 public void addPropertyChangeListener(PropertyChangeListener listener) { 132 propChangeSupport.addPropertyChangeListener(listener); 133 } 134 135 public void removePropertyChangeListener(PropertyChangeListener listener) { 136 propChangeSupport.removePropertyChangeListener(listener); 137 } 138 139 public List <PathResourceImplementation> getResources() { 140 return resources; 141 } 142 143 public void setResources(URL [] urls) { 144 List <PathResourceImplementation> newResources = new ArrayList <PathResourceImplementation>(); 145 for (URL url : urls) { 146 newResources.add(ClassPathSupport.createResource(url)); 147 } 148 resources = newResources; 149 propChangeSupport.firePropertyChange(ClassPathImplementation.PROP_RESOURCES, null, null); 150 } 151 } 152 } 153 | Popular Tags |