KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > ClasspathsTest


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.java.freeform;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.beans.PropertyChangeListener JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.lang.reflect.Method JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashSet JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Set JavaDoc;
34 import java.util.SortedSet JavaDoc;
35 import java.util.TreeSet JavaDoc;
36 import java.util.concurrent.CountDownLatch JavaDoc;
37 import java.util.concurrent.TimeUnit JavaDoc;
38 import java.util.regex.Matcher JavaDoc;
39 import java.util.regex.Pattern JavaDoc;
40 import org.netbeans.api.java.classpath.ClassPath;
41 import org.netbeans.api.java.classpath.GlobalPathRegistry;
42 import org.netbeans.api.java.project.JavaProjectConstants;
43 import org.netbeans.api.project.Project;
44 import org.netbeans.api.project.ProjectManager;
45 import org.netbeans.modules.ant.freeform.FreeformProject;
46 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
47 import org.netbeans.modules.ant.freeform.TestBase;
48 import org.netbeans.modules.ant.freeform.spi.support.Util;
49 import org.netbeans.spi.project.AuxiliaryConfiguration;
50 import org.netbeans.spi.project.support.ant.AntProjectHelper;
51 import org.netbeans.spi.project.support.ant.EditableProperties;
52 import org.netbeans.spi.project.ui.ProjectOpenedHook;
53 import org.openide.filesystems.FileLock;
54 import org.openide.filesystems.FileObject;
55 import org.openide.filesystems.FileUtil;
56 import org.openide.util.Mutex;
57 import org.w3c.dom.Document JavaDoc;
58 import org.w3c.dom.Element JavaDoc;
59
60 // XXX testClasspathsOfBuildProducts
61
// - should have BOOT and EXECUTE
62
// XXX testBootClasspathChanges
63
// - check that after e.g. changing plain source level, new JDK picked
64
// XXX testSourcepathChanges
65
// - check that after adding/removing a source root to a given compilation unit, SOURCE changes to match
66
// (but can this really work generally? what if a single compilation unit is split in half??)
67
// XXX testIgnoredRoot
68
// - check that after removing a compilation unit, ClassPath.gCP returns null again, and the ClassPath object is invalidated
69
// XXX testNonexistentEntries
70
// - check that correct URL (ending in '/') picked for dirs (not JARs) in CP that do not yet exist
71
// and that for nonexistent JARs ('.' in simple name) the correct jar: protocol URL is used
72

73 /**
74  * Test functionality of classpath definitions in FreeformProject.
75  * This class just tests the basic functionality found in the "simple" project.
76  * @author Jesse Glick
77  */

78 public class ClasspathsTest extends TestBase {
79
80     public ClasspathsTest(String JavaDoc name) {
81         super(name);
82     }
83     
84     protected void setUp() throws Exception JavaDoc {
85         super.setUp();
86         assertNotNull("Must have built ant/freeform unit tests first, INCLUDING copying non-*.java resources to the classes build directory",
87             ClasspathsTest.class.getResource("/META-INF/services/org.openide.modules.InstalledFileLocator"));
88     }
89     
90     public void testSourcePath() throws Exception JavaDoc {
91         ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.SOURCE);
92         assertNotNull("have some SOURCE classpath for src/", cp);
93         FileObject[] roots = cp.getRoots();
94         assertEquals("have one entry in " + cp, 1, roots.length);
95         assertEquals("that is src/", simple.getProjectDirectory().getFileObject("src"), roots[0]);
96         cp = ClassPath.getClassPath(specialTaskJava, ClassPath.SOURCE);
97         assertNotNull("have some SOURCE classpath for antsrc/", cp);
98         roots = cp.getRoots();
99         assertEquals("have one entry", 1, roots.length);
100         assertEquals("that is antsrc/", simple.getProjectDirectory().getFileObject("antsrc"), roots[0]);
101         cp = ClassPath.getClassPath(buildProperties, ClassPath.SOURCE);
102         assertNull("have no SOURCE classpath for build.properties", cp);
103     }
104     
105     public void testCompileClasspath() throws Exception JavaDoc {
106         ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.COMPILE);
107         assertNotNull("have some COMPILE classpath for src/", cp);
108         assertEquals("have two entries in " + cp, 2, cp.entries().size());
109         assertEquals("have two roots in " + cp, 2, cp.getRoots().length);
110         assertNotNull("found WeakSet in " + cp, cp.findResource("org/openide/util/WeakSet.class"));
111         assertNotNull("found NullInputStream", cp.findResource("org/openide/util/io/NullInputStream.class"));
112         cp = ClassPath.getClassPath(specialTaskJava, ClassPath.COMPILE);
113         assertNotNull("have some COMPILE classpath for antsrc/", cp);
114         assertEquals("have one entry", 1, cp.getRoots().length);
115         assertNotNull("found Task", cp.findResource("org/apache/tools/ant/Task.class"));
116         cp = ClassPath.getClassPath(buildProperties, ClassPath.COMPILE);
117         assertNull("have no COMPILE classpath for build.properties", cp);
118     }
119     
120     public void testExecuteClasspath() throws Exception JavaDoc {
121         ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.EXECUTE);
122         assertNotNull("have some EXECUTE classpath for src/", cp);
123         Set JavaDoc<String JavaDoc> roots = new TreeSet JavaDoc<String JavaDoc>();
124         roots.add(urlForJar("lib/lib1.jar"));
125         roots.add(urlForJar("lib/lib2.jar"));
126         // #49113: includes built-to dirs as well.
127
roots.add(urlForJar("build/simple-app.jar"));
128         roots.add(urlForFolder("build/classes"));
129         assertEquals("right EXECUTE cp for src/", roots.toString(), urlsOfCp(cp).toString());
130         cp = ClassPath.getClassPath(specialTaskJava, ClassPath.EXECUTE);
131         assertNotNull("have some EXECUTE classpath for antsrc/", cp);
132         // Just check number of entries here... could instead find ${ant.home}/lib/ant.jar.
133
assertEquals("have two entries (ant.jar + build/antclasses) in " + cp, 2, cp.entries().size());
134         assertNotNull("found Task", cp.findResource("org/apache/tools/ant/Task.class"));
135         cp = ClassPath.getClassPath(buildProperties, ClassPath.EXECUTE);
136         assertNull("have no EXECUTE classpath for build.properties", cp);
137     }
138     
139     public void testBootClasspath() throws Exception JavaDoc {
140         ClassPath cp = ClassPath.getClassPath(myAppJava, ClassPath.BOOT);
141         assertNotNull("have some BOOT classpath for src/", cp);
142         assertEquals("and it is JDK 1.4", "1.4", specOfBootClasspath(cp));
143         ClassPath cp2 = ClassPath.getClassPath(specialTaskJava, ClassPath.BOOT);
144         assertNotNull("have some BOOT classpath for antsrc/", cp2);
145         assertEquals("and it is JDK 1.4", "1.4", specOfBootClasspath(cp2));
146         /* Not actually required:
147         assertEquals("same BOOT classpath for all files (since use same spec level)", cp, cp2);
148          */

149         cp = ClassPath.getClassPath(buildProperties, ClassPath.BOOT);
150         assertNull("have no BOOT classpath for build.properties", cp);
151     }
152     
153     private static String JavaDoc specOfBootClasspath(ClassPath cp) {
154         List JavaDoc<ClassPath.Entry> entries = cp.entries();
155         if (entries.size() != 1) {
156             return null;
157         }
158         ClassPath.Entry entry = entries.get(0);
159         String JavaDoc u = entry.getURL().toExternalForm();
160         // Cf. DummyJavaPlatformProvider.
161
Pattern JavaDoc p = Pattern.compile("jar:file:/c:/java/([0-9.]+)/jre/lib/rt\\.jar!/");
162         Matcher JavaDoc m = p.matcher(u);
163         if (m.matches()) {
164             return m.group(1);
165         } else {
166             return null;
167         }
168     }
169     
170     public void testGlobalPathRegistryUsage() throws Exception JavaDoc {
171         GlobalPathRegistry gpr = GlobalPathRegistry.getDefault();
172         assertEquals("no BOOT classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.BOOT));
173         assertEquals("no COMPILE classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.COMPILE));
174         assertEquals("no EXECUTE classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.EXECUTE));
175         assertEquals("no SOURCE classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.SOURCE));
176         ProjectOpenedHook hook = simple.getLookup().lookup(ProjectOpenedHook.class);
177         assertNotNull("have a ProjectOpenedHook", hook);
178         Method JavaDoc opened = ProjectOpenedHook.class.getDeclaredMethod("projectOpened");
179         opened.setAccessible(true);
180         opened.invoke(hook);
181         Set JavaDoc<ClassPath> boot = gpr.getPaths(ClassPath.BOOT);
182         Set JavaDoc<ClassPath> compile = gpr.getPaths(ClassPath.COMPILE);
183         Set JavaDoc<ClassPath> execute = gpr.getPaths(ClassPath.EXECUTE);
184         Set JavaDoc<ClassPath> source = gpr.getPaths(ClassPath.SOURCE);
185         Set JavaDoc<ClassPath> expected = new HashSet JavaDoc<ClassPath>();
186         expected.add(ClassPath.getClassPath(myAppJava, ClassPath.BOOT));
187         expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.BOOT));
188         assertEquals("correct set of BOOT classpaths", expected, boot);
189         expected = new HashSet JavaDoc<ClassPath>();
190         expected.add(ClassPath.getClassPath(myAppJava, ClassPath.COMPILE));
191         expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.COMPILE));
192         assertEquals("correct set of COMPILE classpaths", expected, compile);
193         expected = new HashSet JavaDoc<ClassPath>();
194         expected.add(ClassPath.getClassPath(myAppJava, ClassPath.EXECUTE));
195         expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.EXECUTE));
196         assertEquals("correct set of EXECUTE classpaths", expected, execute);
197         expected = new HashSet JavaDoc<ClassPath>();
198         expected.add(ClassPath.getClassPath(myAppJava, ClassPath.SOURCE));
199         expected.add(ClassPath.getClassPath(specialTaskJava, ClassPath.SOURCE));
200         assertEquals("correct set of SOURCE classpaths", expected, source);
201         Method JavaDoc closed = ProjectOpenedHook.class.getDeclaredMethod("projectClosed");
202         closed.setAccessible(true);
203         closed.invoke(hook);
204         assertEquals("again no BOOT classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.BOOT));
205         assertEquals("again no COMPILE classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.COMPILE));
206         assertEquals("again no EXECUTE classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.EXECUTE));
207         assertEquals("again no SOURCE classpaths", Collections.EMPTY_SET, gpr.getPaths(ClassPath.SOURCE));
208     }
209     
210     public void testCompileClasspathChanges() throws Exception JavaDoc {
211         clearWorkDir();
212         FreeformProject simple2 = copyProject(simple);
213         FileObject myAppJava2 = simple2.getProjectDirectory().getFileObject("src/org/foo/myapp/MyApp.java");
214         assertNotNull("found MyApp.java", myAppJava2);
215         ClassPath cp = ClassPath.getClassPath(myAppJava2, ClassPath.COMPILE);
216         assertNotNull("have some COMPILE classpath for src/", cp);
217         assertEquals("have two entries in " + cp, 2, cp.entries().size());
218         assertEquals("have two roots in " + cp, 2, cp.getRoots().length);
219         assertNotNull("found WeakSet in " + cp, cp.findResource("org/openide/util/WeakSet.class"));
220         assertNotNull("found NullInputStream", cp.findResource("org/openide/util/io/NullInputStream.class"));
221         TestPCL l = new TestPCL();
222         cp.addPropertyChangeListener(l);
223         EditableProperties props = new EditableProperties();
224         FileObject buildProperties = simple2.getProjectDirectory().getFileObject("build.properties");
225         assertNotNull("have build.properties", buildProperties);
226         InputStream JavaDoc is = buildProperties.getInputStream();
227         try {
228             props.load(is);
229         } finally {
230             is.close();
231         }
232         assertEquals("right original src.cp", "${lib.dir}/lib1.jar:${lib.dir}/lib2.jar", props.getProperty("src.cp"));
233         props.setProperty("src.cp", "${lib.dir}/lib1.jar");
234         FileLock lock = buildProperties.lock();
235         try {
236             final OutputStream JavaDoc os = buildProperties.getOutputStream(lock);
237             try {
238                 props.store(os);
239             } finally {
240                 // close file under ProjectManager.readAccess so that events are fired synchronously
241
ProjectManager.mutex().readAccess(new Mutex.ExceptionAction<Void JavaDoc>() {
242                     public Void JavaDoc run() throws Exception JavaDoc {
243                         os.close();
244                         return null;
245                     }
246                 });
247             }
248         } finally {
249             lock.releaseLock();
250         }
251         assertEquals("ROOTS fired", new HashSet JavaDoc<String JavaDoc>(Arrays.asList(ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS)), l.changed);
252         assertEquals("have one entry in " + cp, 1, cp.entries().size());
253         assertEquals("have one root in " + cp, 1, cp.getRoots().length);
254         assertNotNull("found WeakSet in " + cp, cp.findResource("org/openide/util/WeakSet.class"));
255         assertNull("did not find NullInputStream", cp.findResource("org/openide/util/io/NullInputStream.class"));
256     }
257
258     public void testCompilationUnitChanges() throws Exception JavaDoc {
259         clearWorkDir();
260         FreeformProject simple2 = copyProject(simple);
261         AntProjectHelper helper2 = simple2.helper();
262         FileObject myAppJava = simple2.getProjectDirectory().getFileObject("src/org/foo/myapp/MyApp.java");
263         assertNotNull("found MyApp.java", myAppJava);
264         ClassPath cpSrc = ClassPath.getClassPath(myAppJava, ClassPath.COMPILE);
265         assertNotNull("have some COMPILE classpath for src/", cpSrc);
266         assertEquals("have two entries in " + cpSrc, 2, cpSrc.entries().size());
267         FileObject mySpecialTask = simple2.getProjectDirectory().getFileObject("antsrc/org/foo/ant/SpecialTask.java");
268         assertNotNull("found SpecialTask.java", mySpecialTask);
269         ClassPath cpAnt = ClassPath.getClassPath(mySpecialTask, ClassPath.COMPILE);
270         assertNotNull("have some COMPILE classpath for antsrc/", cpAnt);
271         assertEquals("have one entry in " + cpAnt, 1, cpAnt.entries().size());
272         TestPCL srcL = new TestPCL();
273         cpSrc.addPropertyChangeListener(srcL);
274         TestPCL antL = new TestPCL();
275         cpAnt.addPropertyChangeListener(antL);
276         
277         ProjectOpenedHook hook = simple2.getLookup().lookup(ProjectOpenedHook.class);
278         assertNotNull("have a ProjectOpenedHook", hook);
279         GlobalPathRegistry gpr = GlobalPathRegistry.getDefault();
280         assertEquals("no COMPILE classpaths yet", Collections.EMPTY_SET, gpr.getPaths(ClassPath.COMPILE));
281         Method JavaDoc opened = ProjectOpenedHook.class.getDeclaredMethod("projectOpened");
282         opened.setAccessible(true);
283         opened.invoke(hook);
284         Set JavaDoc<ClassPath> compile = gpr.getPaths(ClassPath.COMPILE);
285         Set JavaDoc<ClassPath> expected = new HashSet JavaDoc<ClassPath>();
286         expected.add(cpSrc);
287         expected.add(cpAnt);
288         assertEquals("correct set of COMPILE classpaths", expected, compile);
289         
290         AuxiliaryConfiguration aux = Util.getAuxiliaryConfiguration(helper2);
291         List JavaDoc<JavaProjectGenerator.JavaCompilationUnit> units = JavaProjectGenerator.getJavaCompilationUnits(helper2, aux);
292         assertEquals("two compilation units", 2, units.size());
293         JavaProjectGenerator.JavaCompilationUnit cu = new JavaProjectGenerator.JavaCompilationUnit();
294         cu.packageRoots = new ArrayList JavaDoc<String JavaDoc>();
295         cu.packageRoots.add("${src.dir}");
296         cu.packageRoots.add("${ant.src.dir}");
297         cu.classpath = new ArrayList JavaDoc<JavaProjectGenerator.JavaCompilationUnit.CP>();
298         JavaProjectGenerator.JavaCompilationUnit.CP cucp = new JavaProjectGenerator.JavaCompilationUnit.CP();
299         cucp.mode = "compile";
300         cucp.classpath = "${src.cp}:${ant.src.cp}";
301         cu.classpath.add(cucp);
302         List JavaDoc<JavaProjectGenerator.JavaCompilationUnit> newUnits = new ArrayList JavaDoc<JavaProjectGenerator.JavaCompilationUnit>();
303         newUnits.add(cu);
304         JavaProjectGenerator.putJavaCompilationUnits(helper2, aux, newUnits);
305
306         ClassPath cpSrc2 = ClassPath.getClassPath(myAppJava, ClassPath.COMPILE);
307         assertNotNull("have some COMPILE classpath for src/", cpSrc2);
308         assertEquals("have three entries in " + cpSrc2, 3, cpSrc2.entries().size());
309         assertEquals("original classpath is empty now", 0, cpSrc.entries().size());
310         ClassPath cpAnt2 = ClassPath.getClassPath(mySpecialTask, ClassPath.COMPILE);
311         assertNotNull("have some COMPILE classpath for antsrc/", cpAnt2);
312         assertEquals("have three entries in " + cpAnt2, 3, cpAnt2.entries().size());
313         assertEquals("original classpath is empty now", 0, cpAnt.entries().size());
314         assertEquals("classpath for src/ and antsrc/ are the same. " + cpAnt2 + " "+cpSrc2, cpAnt2, cpSrc2);
315         
316         assertEquals("ROOTS fired", new HashSet JavaDoc<String JavaDoc>(Arrays.asList(ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS)), srcL.changed);
317         srcL.reset();
318         assertEquals("ROOTS fired", new HashSet JavaDoc<String JavaDoc>(Arrays.asList(ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS)), antL.changed);
319         antL.reset();
320         
321         compile = gpr.getPaths(ClassPath.COMPILE);
322         expected = new HashSet JavaDoc<ClassPath>();
323         expected.add(cpSrc);
324         expected.add(cpAnt);
325         expected.add(cpAnt2);
326         assertEquals("correct set of COMPILE classpaths", expected, compile);
327         
328         TestPCL cpL = new TestPCL();
329         cpAnt2.addPropertyChangeListener(cpL);
330
331         JavaProjectGenerator.putJavaCompilationUnits(helper2, aux, units);
332         ClassPath cpSrc3 = ClassPath.getClassPath(myAppJava, ClassPath.COMPILE);
333         assertNotNull("have some COMPILE classpath for src/", cpSrc3);
334         assertEquals("have two entries in " + cpSrc3, 2, cpSrc3.entries().size());
335         ClassPath cpAnt3 = ClassPath.getClassPath(mySpecialTask, ClassPath.COMPILE);
336         assertNotNull("have some COMPILE classpath for antsrc/", cpAnt3);
337         assertEquals("have one entry in " + cpAnt3, 1, cpAnt3.entries().size());
338         assertEquals("original classpath instance was reused", cpSrc, cpSrc3);
339         assertEquals("original classpath instance was reused", cpAnt, cpAnt3);
340         assertEquals("classpath of single compilation unit is empty now", 0, cpAnt2.entries().size());
341         
342         assertEquals("ROOTS fired", new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS})), cpL.changed);
343         cpL.reset();
344         assertEquals("ROOTS fired", new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS})), srcL.changed);
345         srcL.reset();
346         assertEquals("ROOTS fired", new HashSet JavaDoc(Arrays.asList(new String JavaDoc[] {ClassPath.PROP_ENTRIES, ClassPath.PROP_ROOTS})), antL.changed);
347         antL.reset();
348         
349         compile = gpr.getPaths(ClassPath.COMPILE);
350         assertEquals("all three classpaths still there", expected, compile);
351         Method JavaDoc closed = ProjectOpenedHook.class.getDeclaredMethod("projectClosed");
352         closed.setAccessible(true);
353         closed.invoke(hook);
354         compile = gpr.getPaths(ClassPath.COMPILE);
355         expected = new HashSet JavaDoc();
356         assertEquals("correct set of COMPILE classpaths", expected, compile);
357     }
358
359     public void testDeadlock77015() throws Exception JavaDoc {
360         final CountDownLatch JavaDoc l = new CountDownLatch JavaDoc(2);
361
362         Classpaths.TESTING_LATCH = l;
363
364         new Thread JavaDoc() {
365             public void run() {
366                 ClassPath.getClassPath(myAppJava, ClassPath.SOURCE);
367             }
368         }.start();
369
370         Thread JavaDoc t = new Thread JavaDoc() {
371             public void run() {
372                 ProjectManager.mutex().writeAccess(new Mutex.Action<Void JavaDoc>() {
373                     public Void JavaDoc run() {
374                         Element JavaDoc el = simple.getPrimaryConfigurationData();
375                         AuxiliaryConfiguration c = simple.getLookup().lookup(AuxiliaryConfiguration.class);
376                         
377                         l.countDown();
378                         try {
379                             l.await(2, TimeUnit.SECONDS);
380                         } catch (InterruptedException JavaDoc e) {
381                             e.printStackTrace();
382                         }
383                         
384                         c.putConfigurationFragment(el.getOwnerDocument().createElementNS("http://something/X", "A"), false);
385                         
386                         return null;
387                     }
388                 });
389             }
390         };
391
392         t.start();
393         t.join();
394     }
395     
396     // Copied from org.netbeans.modules.apisupport.project.ClassPathProviderImplTest:
397
private String JavaDoc urlForJar(String JavaDoc path) throws Exception JavaDoc {
398         return FileUtil.getArchiveRoot(simple.helper().resolveFile(path).toURI().toURL()).toExternalForm();
399     }
400     private String JavaDoc urlForFolder(String JavaDoc path) throws Exception JavaDoc {
401         String JavaDoc s = simple.helper().resolveFile(path).toURI().toURL().toExternalForm();
402         if (s.endsWith("/")) {
403             return s;
404         } else {
405             return s + "/";
406         }
407     }
408     private Set JavaDoc<String JavaDoc> urlsOfCp(ClassPath cp) {
409         Set JavaDoc<String JavaDoc> s = new TreeSet JavaDoc<String JavaDoc>();
410         for (ClassPath.Entry entry : cp.entries()) {
411             s.add(entry.getURL().toExternalForm());
412         }
413         return s;
414     }
415     
416     public void testIncludesExcludes() throws Exception JavaDoc {
417         clearWorkDir();
418         File JavaDoc d = getWorkDir();
419         AntProjectHelper helper = FreeformProjectGenerator.createProject(d, d, "prj", null);
420         Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory());
421         FileUtil.createData(new File JavaDoc(d, "s/relevant/included/file"));
422         FileUtil.createData(new File JavaDoc(d, "s/relevant/excluded/file"));
423         FileUtil.createData(new File JavaDoc(d, "s/ignored/file"));
424         Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
425         Document JavaDoc doc = data.getOwnerDocument();
426         Element JavaDoc sf = (Element JavaDoc) data.insertBefore(doc.createElementNS(Util.NAMESPACE, "folders"), Util.findElement(data, "view", Util.NAMESPACE)).
427                 appendChild(doc.createElementNS(Util.NAMESPACE, "source-folder"));
428         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "label")).appendChild(doc.createTextNode("Sources"));
429         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "type")).appendChild(doc.createTextNode(JavaProjectConstants.SOURCES_TYPE_JAVA));
430         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "location")).appendChild(doc.createTextNode("s"));
431         Util.putPrimaryConfigurationData(helper, data);
432         Element JavaDoc jd = doc.createElementNS(JavaProjectNature.NS_JAVA_2, JavaProjectNature.EL_JAVA);
433         jd.appendChild(doc.createElementNS(JavaProjectNature.NS_JAVA_2, "compilation-unit")).
434                 appendChild(doc.createElementNS(JavaProjectNature.NS_JAVA_2, "package-root")).
435                 appendChild(doc.createTextNode("s"));
436         p.getLookup().lookup(AuxiliaryConfiguration.class).putConfigurationFragment(jd, true);
437         ProjectManager.getDefault().saveProject(p);
438         FileObject s = helper.resolveFileObject("s");
439         ClassPath cp = ClassPath.getClassPath(s, ClassPath.SOURCE);
440         FileObject[] roots = cp.getRoots();
441         assertEquals(1, roots.length);
442         assertEquals(s, roots[0]);
443         assertEquals("ignored{file} relevant{excluded{file} included{file}}", expand(cp, s));
444         // Now configure includes and excludes.
445
EditableProperties ep = new EditableProperties();
446         ep.put("includes", "relevant/");
447         ep.put("excludes", "**/excluded/");
448         helper.putProperties("config.properties", ep);
449         data = Util.getPrimaryConfigurationData(helper);
450         doc = data.getOwnerDocument();
451         data.getElementsByTagName("properties").item(0).
452                 appendChild(doc.createElementNS(Util.NAMESPACE, "property-file")).
453                 appendChild(doc.createTextNode("config.properties"));
454         Util.putPrimaryConfigurationData(helper, data);
455         ProjectManager.getDefault().saveProject(p);
456         data = Util.getPrimaryConfigurationData(helper);
457         doc = data.getOwnerDocument();
458         sf = (Element JavaDoc) data.getElementsByTagName("source-folder").item(0);
459         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "includes")).
460                 appendChild(doc.createTextNode("${includes}"));
461         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "excludes")).
462                 appendChild(doc.createTextNode("${excludes}"));
463         Util.putPrimaryConfigurationData(helper, data);
464         ProjectManager.getDefault().saveProject(p);
465         assertEquals("relevant{included{file}}", expand(cp, s));
466         // Now change them.
467
TestPCL l = new TestPCL();
468         cp.addPropertyChangeListener(l);
469         ep = helper.getProperties("config.properties");
470         ep.remove("includes");
471         helper.putProperties("config.properties", ep);
472         ProjectManager.getDefault().saveProject(p);
473         assertEquals("ignored{file} relevant{included{file}}", expand(cp, s));
474         assertEquals(Collections.singleton(ClassPath.PROP_INCLUDES), l.changed);
475         // Also check floating includes.
476
ep = helper.getProperties("config.properties");
477         ep.put("includes", "relevant/included/");
478         helper.putProperties("config.properties", ep);
479         ProjectManager.getDefault().saveProject(p);
480         assertEquals("relevant{included{file}}", expand(cp, s));
481     }
482     private static String JavaDoc expand(ClassPath cp, FileObject d) {
483         SortedSet JavaDoc<String JavaDoc> subs = new TreeSet JavaDoc<String JavaDoc>();
484         for (FileObject kid : d.getChildren()) {
485             if (!cp.contains(kid)) {
486                 continue;
487             }
488             String JavaDoc sub = kid.getNameExt();
489             if (kid.isFolder()) {
490                 sub += '{' + expand(cp, kid) + '}';
491             }
492             subs.add(sub);
493         }
494         StringBuilder JavaDoc b = new StringBuilder JavaDoc();
495         for (String JavaDoc sub : subs) {
496             if (b.length() > 0) {
497                 b.append(' ');
498             }
499             b.append(sub);
500         }
501         return b.toString();
502     }
503     
504     public void testIncludesFiredJustOnce() throws Exception JavaDoc {
505         clearWorkDir();
506         File JavaDoc d = getWorkDir();
507         AntProjectHelper helper = FreeformProjectGenerator.createProject(d, d, "prj", null);
508         Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory());
509         FileObject src1 = FileUtil.createFolder(new File JavaDoc(d, "src1"));
510         FileObject src2 = FileUtil.createFolder(new File JavaDoc(d, "src2"));
511         Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
512         Document JavaDoc doc = data.getOwnerDocument();
513         data.getElementsByTagName("properties").item(0).
514                 appendChild(doc.createElementNS(Util.NAMESPACE, "property-file")).
515                 appendChild(doc.createTextNode("config.properties"));
516         Element JavaDoc folders = (Element JavaDoc) data.insertBefore(doc.createElementNS(Util.NAMESPACE, "folders"), Util.findElement(data, "view", Util.NAMESPACE));
517         Element JavaDoc sf = (Element JavaDoc) folders.appendChild(doc.createElementNS(Util.NAMESPACE, "source-folder"));
518         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "label")).appendChild(doc.createTextNode("Sources #1"));
519         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "type")).appendChild(doc.createTextNode(JavaProjectConstants.SOURCES_TYPE_JAVA));
520         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "location")).appendChild(doc.createTextNode("src1"));
521         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "includes")).
522                 appendChild(doc.createTextNode("${includes}"));
523         sf = (Element JavaDoc) folders.appendChild(doc.createElementNS(Util.NAMESPACE, "source-folder"));
524         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "label")).appendChild(doc.createTextNode("Sources #2"));
525         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "type")).appendChild(doc.createTextNode(JavaProjectConstants.SOURCES_TYPE_JAVA));
526         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "location")).appendChild(doc.createTextNode("src2"));
527         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "includes")).
528                 appendChild(doc.createTextNode("${includes}"));
529         Util.putPrimaryConfigurationData(helper, data);
530         Element JavaDoc jd = doc.createElementNS(JavaProjectNature.NS_JAVA_2, JavaProjectNature.EL_JAVA);
531         Element JavaDoc cu = (Element JavaDoc) jd.appendChild(doc.createElementNS(JavaProjectNature.NS_JAVA_2, "compilation-unit"));
532         cu.appendChild(doc.createElementNS(JavaProjectNature.NS_JAVA_2, "package-root")).
533                 appendChild(doc.createTextNode("src1"));
534         cu.appendChild(doc.createElementNS(JavaProjectNature.NS_JAVA_2, "package-root")).
535                 appendChild(doc.createTextNode("src2"));
536         p.getLookup().lookup(AuxiliaryConfiguration.class).putConfigurationFragment(jd, true);
537         ProjectManager.getDefault().saveProject(p);
538         ClassPath cp = ClassPath.getClassPath(src1, ClassPath.SOURCE);
539         FileObject[] roots = cp.getRoots();
540         assertEquals(2, roots.length);
541         assertEquals(src1, roots[0]);
542         assertEquals(src2, roots[1]);
543         ClassPath.Entry cpe2 = cp.entries().get(1);
544         assertEquals(src2.getURL(), cpe2.getURL());
545         assertTrue(cpe2.includes("stuff/"));
546         assertTrue(cpe2.includes("whatever/"));
547         class L implements PropertyChangeListener JavaDoc {
548             int cnt;
549             public void propertyChange(PropertyChangeEvent JavaDoc e) {
550                 if (ClassPath.PROP_INCLUDES.equals(e.getPropertyName())) {
551                     cnt++;
552                 }
553             }
554         }
555         L l = new L();
556         cp.addPropertyChangeListener(l);
557         EditableProperties ep = new EditableProperties();
558         ep.put("includes", "whatever/");
559         helper.putProperties("config.properties", ep);
560         ProjectManager.getDefault().saveProject(p);
561         assertEquals(1, l.cnt);
562         assertFalse(cpe2.includes("stuff/"));
563         assertTrue(cpe2.includes("whatever/"));
564         ep.setProperty("includes", "whateverelse/");
565         helper.putProperties("config.properties", ep);
566         ProjectManager.getDefault().saveProject(p);
567         assertEquals(2, l.cnt);
568         assertFalse(cpe2.includes("stuff/"));
569         assertFalse(cpe2.includes("whatever/"));
570         ep.remove("includes");
571         helper.putProperties("config.properties", ep);
572         ProjectManager.getDefault().saveProject(p);
573         assertEquals(3, l.cnt);
574         assertTrue(cpe2.includes("stuff/"));
575         assertTrue(cpe2.includes("whatever/"));
576     }
577
578 }
579
Popular Tags