KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > metadata > WeakProxyClassPathImplementationTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.metadata;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.beans.PropertyChangeSupport JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Set JavaDoc;
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 /**
39  *
40  * @author Andrei Badea
41  */

42 public class WeakProxyClassPathImplementationTest extends NbTestCase {
43
44     public WeakProxyClassPathImplementationTest(String JavaDoc testName) {
45         super(testName);
46     }
47
48     public void testWeakProxyClassPathImplementation() throws Exception JavaDoc {
49         class PCL implements PropertyChangeListener JavaDoc {
50
51             private int changeCount;
52
53             public void propertyChange(PropertyChangeEvent JavaDoc 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 JavaDoc url11 = new URL JavaDoc("file:///foo/bar11/");
65         URL JavaDoc url12 = new URL JavaDoc("file:///foo/bar12/");
66         URL JavaDoc url2 = new URL JavaDoc("file:///foo/bar2/");
67         URL JavaDoc url3 = new URL JavaDoc("file:///foo/bar3/");
68
69         ClassPathImpl classPath1 = new ClassPathImpl(new URL JavaDoc[] { url11 });
70         ClassPathImpl classPath2 = new ClassPathImpl(new URL JavaDoc[] { url2 });
71         ClassPathImpl classPath3 = new ClassPathImpl(new URL JavaDoc[] { 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 JavaDoc[] { url11, url2, url3 });
78
79         // changing a delegate classpath by adding an entry
80

81         classPath1.setResources(new URL JavaDoc[] { url11, url12 });
82
83         assertTrue(pcl.testChangeAndReset());
84         assertResources(proxyClassPath.getResources(), new URL JavaDoc[] { url11, url12, url2, url3 });
85
86         // allowing a delegate classpath to be gc'd
87

88         WeakReference JavaDoc<ClassPathImpl> classPath2Ref = new WeakReference JavaDoc<ClassPathImpl>(classPath2);
89         classPath2 = null;
90         assertGC("Should be able to gc classpath2", classPath2Ref);
91         assertTrue(pcl.testChangeAndReset());
92         assertResources(proxyClassPath.getResources(), new URL JavaDoc[] { url11, url12, url3 });
93
94         // changing another delegate classpath by removing an entry
95

96         classPath3.setResources(new URL JavaDoc[] { });
97
98         assertTrue(pcl.testChangeAndReset());
99         assertResources(proxyClassPath.getResources(), new URL JavaDoc[] { url11, url12 });
100
101         WeakReference JavaDoc<ClassPathImpl> classPath1Ref = new WeakReference JavaDoc<ClassPathImpl>(classPath1);
102         WeakReference JavaDoc<ClassPathImpl> classPath3Ref = new WeakReference JavaDoc<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 JavaDoc[0]);
108     }
109
110     private static void assertResources(List JavaDoc<PathResourceImplementation> expected, URL JavaDoc[] actual) {
111         Set JavaDoc<URL JavaDoc> expectedSet = new HashSet JavaDoc<URL JavaDoc>();
112         for (PathResourceImplementation resource: expected) {
113             // taking the first root because our PathResourceImplementation's
114
// all should only have one root
115
expectedSet.add(resource.getRoots()[0]);
116         }
117         Set JavaDoc<URL JavaDoc> actualSet = new HashSet JavaDoc<URL JavaDoc>(Arrays.asList(actual));
118         assertEquals(expectedSet, actualSet);
119     }
120
121     private static final class ClassPathImpl implements ClassPathImplementation {
122
123         private final PropertyChangeSupport JavaDoc propChangeSupport = new PropertyChangeSupport JavaDoc(this);
124
125         private List JavaDoc<PathResourceImplementation> resources;
126
127         public ClassPathImpl(URL JavaDoc[] urls) {
128             setResources(urls);
129         }
130
131         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
132             propChangeSupport.addPropertyChangeListener(listener);
133         }
134
135         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
136             propChangeSupport.removePropertyChangeListener(listener);
137         }
138
139         public List JavaDoc<PathResourceImplementation> getResources() {
140             return resources;
141         }
142
143         public void setResources(URL JavaDoc[] urls) {
144             List JavaDoc<PathResourceImplementation> newResources = new ArrayList JavaDoc<PathResourceImplementation>();
145             for (URL JavaDoc 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