KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.swing.event.ChangeListener JavaDoc;
33 import org.netbeans.api.java.classpath.ClassPath;
34 import org.netbeans.api.java.queries.SourceForBinaryQuery;
35 import org.netbeans.modules.j2ee.persistence.wizard.fromdb.ChangeSupport;
36 import org.netbeans.spi.java.classpath.ClassPathFactory;
37 import org.netbeans.spi.java.classpath.ClassPathImplementation;
38 import org.netbeans.spi.java.classpath.PathResourceImplementation;
39 import org.netbeans.spi.java.classpath.support.ClassPathSupport;
40 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
41 import org.openide.filesystems.FileObject;
42 import org.openide.filesystems.FileUtil;
43 import org.openide.filesystems.URLMapper;
44
45 /**
46  *
47  * @author Andrei Badea
48  */

49 public class ClassPathSourceCacheTest extends TestBase {
50
51     private FileObject root1FO;
52     private FileObject root2FO;
53     private URL JavaDoc root1URL;
54     private URL JavaDoc root2URL;
55
56     private URL JavaDoc binary1URL;
57     private FileObject src1;
58
59     private URL JavaDoc binary2URL;
60     private FileObject src2;
61
62     private SourceForBinaryQueryImpl s4bq1;
63     private SourceForBinaryQueryImpl s4bq2;
64
65     public ClassPathSourceCacheTest(String JavaDoc testName) {
66         super(testName);
67     }
68
69     public void setUp() throws Exception JavaDoc {
70         clearWorkDir();
71         FileObject workDir = FileUtil.toFileObject(getWorkDir());
72
73         root1FO = workDir.createFolder("root1");
74         root2FO = workDir.createFolder("root2");
75
76         root1URL = URLMapper.findURL(root1FO, URLMapper.INTERNAL);
77         root2URL = URLMapper.findURL(root2FO, URLMapper.INTERNAL);
78
79         binary1URL = FileUtil.getArchiveRoot(URLMapper.findURL(workDir.createData("binary1.jar"), URLMapper.INTERNAL));
80         src1 = workDir.createFolder("src1");
81
82         binary2URL = FileUtil.getArchiveRoot(URLMapper.findURL(workDir.createData("binary2.jar"), URLMapper.INTERNAL));
83         src2 = workDir.createFolder("src2");
84
85         s4bq1 = new SourceForBinaryQueryImpl(binary1URL, new FileObject[] { src1 });
86         s4bq2 = new SourceForBinaryQueryImpl(binary2URL, new FileObject[] { src2 });
87         setLookups(new Object JavaDoc[] { s4bq1, s4bq2 });
88     }
89
90     public void testClassPathSourceCache() throws Exception JavaDoc {
91         ClassPathImpl classPathImpl = new ClassPathImpl(new URL JavaDoc[] { root1URL, root2URL, binary1URL });
92         ClassPath classPath = ClassPathFactory.createClassPath(classPathImpl);
93         ClassPathSourceCache cache = ClassPathSourceCache.newInstance(classPath, true);
94         PCL pcl = new PCL();
95         cache.addPropertyChangeListener(pcl);
96
97         assertFalse(pcl.testChangeAndReset());
98         assertTrue(cache.contains(src1));
99         assertFalse(cache.contains(src2));
100         assertRoots(new FileObject[] { root1FO, root2FO, src1 }, cache.getRoots());
101
102         // adding a binary root with sources to the classpath
103

104         classPathImpl.changeResources(new URL JavaDoc[] { root1URL, root2URL, binary1URL, binary2URL });
105
106         assertTrue(pcl.testChangeAndReset());
107         assertTrue(cache.contains(src1));
108         assertTrue(cache.contains(src2));
109         assertRoots(new FileObject[] { root1FO, root2FO, src1, src2 }, cache.getRoots());
110
111         // removing a source root from a SFBQ result
112

113         s4bq1.getResult().changeRoots(new FileObject[0]);
114
115         assertTrue(pcl.testChangeAndReset());
116         assertFalse(cache.contains(src1));
117         assertTrue(cache.contains(src2));
118         assertRoots(new FileObject[] { root1FO, root2FO, src2 }, cache.getRoots());
119         // let's also test contains()
120
assertTrue(cache.contains(root1FO));
121         assertTrue(cache.contains(root2FO));
122         assertFalse(cache.contains(src1));
123         assertTrue(cache.contains(src2));
124
125         // deleting root2FO should remove it from the result of getRoots()
126

127         root2FO.delete();
128
129         assertTrue(pcl.testChangeAndReset());
130         assertFalse(cache.contains(src1));
131         assertTrue(cache.contains(src2));
132         assertRoots(new FileObject[] { root1FO, src2 }, cache.getRoots());
133         assertTrue(cache.contains(root1FO));
134         assertFalse(cache.contains(root2FO));
135         assertFalse(cache.contains(src1));
136         assertTrue(cache.contains(src2));
137
138         // adding some files underneath src2 to test that contains() works correctly
139

140         FileObject foo = src2.createFolder("foo");
141         FileObject bar = foo.createData("bar");
142
143         assertTrue(cache.contains(foo));
144         assertTrue(cache.contains(bar));
145
146         // should be able to GC the classpath
147

148         WeakReference JavaDoc<ClassPath> classPathRef = new WeakReference JavaDoc<ClassPath>(classPath);
149         classPathImpl = null; // as it holds classPath in its listener list
150
classPath = null;
151
152         assertGC("Should be able to GC classPath", classPathRef);
153
154         synchronized (cache) {
155             assertTrue("The cache should be deinitialized", cache.deinitialized);
156             assertEquals("The cache source roots should have deinitialized", 0, cache.sourceRoots.size());
157             assertEquals("The cache SFBQ results should have deinitialized", 0, cache.results.size());
158         }
159
160         assertEquals("Should have unregistered any SFBQ result listeners", 0, s4bq1.getResult().getListenerCount());
161         assertEquals("Should have unregistered any SFBQ result listeners", 0, s4bq2.getResult().getListenerCount());
162     }
163
164     public void testNoResolveSources() throws Exception JavaDoc {
165         ClassPathImpl classPathImpl = new ClassPathImpl(new URL JavaDoc[] { root1URL, root2URL, binary1URL });
166         ClassPath classPath = ClassPathFactory.createClassPath(classPathImpl);
167         ClassPathSourceCache cache = ClassPathSourceCache.newInstance(classPath, false);
168
169         assertRoots(new FileObject[] { root1FO, root2FO }, cache.getRoots());
170
171         // adding a binary root with sources to the classpath, but we are not resolving the sources
172
// thus the roots should not contain them
173

174         classPathImpl.changeResources(new URL JavaDoc[] { root1URL, root2URL, binary1URL, binary2URL });
175
176         assertRoots(new FileObject[] { root1FO, root2FO }, cache.getRoots());
177     }
178
179     private static void assertRoots(FileObject[] expected, FileObject[] actual) {
180         Set JavaDoc<FileObject> expectedSet = new HashSet JavaDoc<FileObject>(Arrays.asList(expected));
181         Set JavaDoc<FileObject> actualSet = new HashSet JavaDoc<FileObject>(Arrays.asList(actual));
182         assertEquals("The expected array contains duplicates", expected.length, expectedSet.size());
183         assertEquals("The actual array contains duplicates", actual.length, actualSet.size());
184         assertEquals(expectedSet, actualSet);
185     }
186
187     private static final class SourceForBinaryQueryImpl implements SourceForBinaryQueryImplementation {
188
189         private final URL JavaDoc url;
190         private final FileObject[] roots;
191
192         private ResultImpl result;
193
194         public SourceForBinaryQueryImpl(URL JavaDoc url, FileObject[] roots) {
195             this.url = url;
196             this.roots = roots;
197         }
198
199         public SourceForBinaryQuery.Result findSourceRoots(URL JavaDoc binaryRoot) {
200             if (url.equals(binaryRoot)) {
201                 synchronized (this) {
202                     if (result == null) {
203                         result = new ResultImpl(roots);
204                     }
205                     return result;
206                 }
207             }
208             return null;
209         }
210
211         public synchronized ResultImpl getResult() {
212             return result;
213         }
214     }
215
216     private static final class ResultImpl implements SourceForBinaryQuery.Result {
217
218         private final ChangeSupport JavaDoc changeSupport = new ChangeSupport JavaDoc(this);
219
220         private FileObject[] roots;
221
222         public ResultImpl(FileObject[] roots) {
223             this.roots = roots;
224         }
225
226         public synchronized void changeRoots(FileObject[] roots) {
227             this.roots = roots;
228             changeSupport.fireChange();
229         }
230
231         public synchronized FileObject[] getRoots() {
232             return roots;
233         }
234
235         public void addChangeListener(ChangeListener JavaDoc listener) {
236             changeSupport.addChangeListener(listener);
237         }
238
239         public void removeChangeListener(ChangeListener JavaDoc listener) {
240             changeSupport.removeChangeListener(listener);
241         }
242
243         public int getListenerCount() {
244             return changeSupport.getListenerCount();
245         }
246     }
247
248     private static final class ClassPathImpl implements ClassPathImplementation {
249
250         private final PropertyChangeSupport JavaDoc propChangeSupport = new PropertyChangeSupport JavaDoc(this);
251         private List JavaDoc<PathResourceImplementation> resources;
252
253         public ClassPathImpl(URL JavaDoc[] urls) {
254             computeResources(urls);
255         }
256
257         public synchronized void changeResources(URL JavaDoc[] urls) {
258             computeResources(urls);
259             propChangeSupport.firePropertyChange(ClassPathImplementation.PROP_RESOURCES, null, null);
260         }
261
262         private synchronized void computeResources(URL JavaDoc[] urls) {
263             resources = new ArrayList JavaDoc<PathResourceImplementation>();
264             for (URL JavaDoc url : urls) {
265                 resources.add(ClassPathSupport.createResource(url));
266             }
267         }
268
269         public synchronized List JavaDoc getResources() {
270             return resources;
271         }
272
273         public void addPropertyChangeListener(PropertyChangeListener JavaDoc listener) {
274             propChangeSupport.addPropertyChangeListener(listener);
275         }
276
277         public void removePropertyChangeListener(PropertyChangeListener JavaDoc listener) {
278             propChangeSupport.removePropertyChangeListener(listener);
279         }
280     }
281
282     private static final class PCL implements PropertyChangeListener JavaDoc {
283
284         private int changeCount;
285
286         public void propertyChange(PropertyChangeEvent JavaDoc event) {
287             if (ClassPathSourceCache.PROP_ROOTS.equals(event.getPropertyName())) {
288                 changeCount++;
289             }
290         }
291
292         private boolean testChangeAndReset() {
293             boolean result = changeCount > 0;
294             changeCount = 0;
295             return result;
296         }
297     }
298 }
299
Popular Tags