KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > project > support > ant > SourcesHelperTest


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.spi.project.support.ant;
21
22 import java.util.Collections JavaDoc;
23 import org.netbeans.api.project.FileOwnerQuery;
24 import org.netbeans.api.project.Project;
25 import org.netbeans.api.project.ProjectManager;
26 import org.netbeans.api.project.ProjectUtils;
27 import org.netbeans.api.project.TestUtil;
28 import org.netbeans.junit.NbTestCase;
29 import org.netbeans.api.project.SourceGroup;
30 import org.netbeans.api.project.Sources;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.FileUtil;
33
34 /**
35  * Test functionality of SourcesHelper.
36  * @author Jesse Glick
37  */

38 public final class SourcesHelperTest extends NbTestCase {
39     
40     public SourcesHelperTest(String JavaDoc name) {
41         super(name);
42     }
43     
44     private FileObject scratch;
45     private FileObject maindir;
46     private FileObject projdir;
47     private FileObject src1dir;
48     private FileObject src2dir;
49     private FileObject src3dir;
50     private FileObject src4dir;
51     private FileObject builddir;
52     private AntProjectHelper h;
53     private Project project;
54     private SourcesHelper sh;
55     private FileObject proj2dir;
56     private FileObject proj2src1dir;
57     private FileObject proj2src2dir;
58     private AntProjectHelper h2;
59     private Project project2;
60     private SourcesHelper sh2;
61     
62     protected void setUp() throws Exception JavaDoc {
63         super.setUp();
64         TestUtil.setLookup(new Object JavaDoc[] {
65             AntBasedTestUtil.testAntBasedProjectType(),
66         });
67         scratch = TestUtil.makeScratchDir(this);
68         scratch.createData("otherfile");
69         maindir = scratch.createFolder("dir");
70         maindir.createData("readme");
71         projdir = maindir.createFolder("projdir");
72         projdir.createData("projfile");
73         src1dir = projdir.createFolder("src1");
74         src1dir.createData("src1file");
75         src2dir = scratch.createFolder("src2");
76         src2dir.createData("src2file");
77         src3dir = scratch.createFolder("src3");
78         src3dir.createData("src3file");
79         src4dir = scratch.createFolder("src4");
80         src4dir.createData("src4file");
81         builddir = scratch.createFolder("build");
82         builddir.createData("buildfile");
83         h = ProjectGenerator.createProject(projdir, "test");
84         project = ProjectManager.getDefault().findProject(projdir);
85         assertNotNull("have a project", project);
86         EditableProperties p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
87         p.setProperty("src1.dir", "src1");
88         p.setProperty("src2.dir", "../../src2");
89         p.setProperty("src2a.dir", "../../src2"); // same path as src2.dir
90
p.setProperty("src3.dir", FileUtil.toFile(src3dir).getAbsolutePath());
91         p.setProperty("src4.dir", "..");
92         p.setProperty("src5.dir", "../../nonesuch");
93         p.setProperty("build.dir", "../../build");
94         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
95         ProjectManager.getDefault().saveProject(project);
96         sh = new SourcesHelper(h, h.getStandardPropertyEvaluator());
97         sh.addPrincipalSourceRoot("${src1.dir}", "Sources #1", null, null); // inside proj dir
98
sh.addPrincipalSourceRoot("${src2.dir}", "Sources #2", null, null); // outside (rel path)
99
sh.addPrincipalSourceRoot("${src2a.dir}", "Sources #2a", null, null); // redundant
100
sh.addPrincipalSourceRoot("${src3.dir}", "Sources #3", null, null); // outside (abs path)
101
sh.addPrincipalSourceRoot("${src4.dir}", "The Whole Shebang", null, null); // above proj dir
102
sh.addPrincipalSourceRoot("${src5.dir}", "None such", null, null); // does not exist on disk
103
sh.addNonSourceRoot("${build.dir}");
104         sh.addTypedSourceRoot("${src1.dir}", "java", "Packages #1", null, null);
105         sh.addTypedSourceRoot("${src3.dir}", "java", "Packages #3", null, null);
106         sh.addTypedSourceRoot("${src5.dir}", "java", "No Packages", null, null);
107         sh.addTypedSourceRoot("${src2.dir}", "docroot", "Documents #2", null, null);
108         sh.addTypedSourceRoot("${src2a.dir}", "docroot", "Documents #2a", null, null); // redundant
109
// Separate project that has includes its project directory implicitly only.
110
// Also hardcodes paths rather than using properties.
111
proj2dir = scratch.createFolder("proj2dir");
112         proj2dir.createData("proj2file");
113         proj2src1dir = proj2dir.createFolder("src1");
114         proj2src1dir.createData("proj2src1file");
115         proj2src2dir = proj2dir.createFolder("src2");
116         proj2src2dir.createData("proj2src2file");
117         h2 = ProjectGenerator.createProject(proj2dir, "test");
118         project2 = ProjectManager.getDefault().findProject(proj2dir);
119         assertNotNull("have a project2", project2);
120         sh2 = new SourcesHelper(h2, h2.getStandardPropertyEvaluator());
121         sh2.addPrincipalSourceRoot("src1", "Sources #1", null, null);
122         sh2.addPrincipalSourceRoot("src2", "Sources #2", null, null);
123         sh2.addNonSourceRoot("build");
124         sh2.addTypedSourceRoot("src1", "java", "Packages #1", null, null);
125         sh2.addTypedSourceRoot("src2", "java", "Packages #2", null, null);
126     }
127     
128     public void testSourcesBasic() throws Exception JavaDoc {
129         Sources s = sh.createSources();
130         // XXX test that ISE is thrown if we try to add more dirs now
131
SourceGroup[] groups = s.getSourceGroups(Sources.TYPE_GENERIC);
132         assertEquals("should have maindir plus src2dir plus src3dir", 3, groups.length);
133         assertEquals("group #1 is src2dir", src2dir, groups[0].getRootFolder());
134         assertEquals("right display name for src2dir", "Sources #2", groups[0].getDisplayName());
135         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
136         assertEquals("right display name for src3dir", "Sources #3", groups[1].getDisplayName());
137         assertEquals("group #3 is maindir", maindir, groups[2].getRootFolder());
138         assertEquals("right display name for maindir", "The Whole Shebang", groups[2].getDisplayName());
139         // Now the typed source roots.
140
groups = s.getSourceGroups("java");
141         assertEquals("should have src1dir plus src3dir", 2, groups.length);
142         assertEquals("group #1 is src1dir", src1dir, groups[0].getRootFolder());
143         assertEquals("right display name for src1dir", "Packages #1", groups[0].getDisplayName());
144         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
145         assertEquals("right display name for src3dir", "Packages #3", groups[1].getDisplayName());
146         groups = s.getSourceGroups("docroot");
147         assertEquals("should have just src2dir", 1, groups.length);
148         assertEquals("group #1 is src2dir", src2dir, groups[0].getRootFolder());
149         assertEquals("right display name for src2dir", "Documents #2", groups[0].getDisplayName());
150         groups = s.getSourceGroups("unknown");
151         assertEquals("should not have any unknown dirs", 0, groups.length);
152         // Test the simpler project type.
153
s = sh2.createSources();
154         groups = s.getSourceGroups(Sources.TYPE_GENERIC);
155         assertEquals("should have just proj2dir", 1, groups.length);
156         assertEquals("group #1 is proj2dir", proj2dir, groups[0].getRootFolder());
157         assertEquals("right display name for proj2dir", ProjectUtils.getInformation(project2).getDisplayName(), groups[0].getDisplayName());
158         groups = s.getSourceGroups("java");
159         assertEquals("should have proj2src1dir plus proj2src2dir", 2, groups.length);
160         assertEquals("group #1 is proj2src1dir group", proj2src1dir, groups[0].getRootFolder());
161         assertEquals("right display name for src1dir", "Packages #1", groups[0].getDisplayName());
162         assertEquals("group #2 is proj2src2dir group", proj2src2dir, groups[1].getRootFolder());
163         assertEquals("right display name for proj2src2dir", "Packages #2", groups[1].getDisplayName());
164         // XXX test also icons
165
}
166     
167     public void testExternalRootRegistration() throws Exception JavaDoc {
168         FileObject f = maindir.getFileObject("readme");
169         assertEquals("readme not yet registered", null, FileOwnerQuery.getOwner(f));
170         f = projdir.getFileObject("projfile");
171         assertEquals("projfile initially OK", project, FileOwnerQuery.getOwner(f));
172         sh.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
173         f = maindir.getFileObject("readme");
174         assertEquals("readme now registered", project, FileOwnerQuery.getOwner(f));
175         f = projdir.getFileObject("projfile");
176         assertEquals("projfile still OK", project, FileOwnerQuery.getOwner(f));
177         f = src1dir.getFileObject("src1file");
178         assertEquals("src1file registered", project, FileOwnerQuery.getOwner(f));
179         f = src2dir.getFileObject("src2file");
180         assertEquals("src2file registered", project, FileOwnerQuery.getOwner(f));
181         f = src3dir.getFileObject("src3file");
182         assertEquals("src3file registered", project, FileOwnerQuery.getOwner(f));
183         f = builddir.getFileObject("buildfile");
184         assertEquals("buildfile registered", project, FileOwnerQuery.getOwner(f));
185         f = scratch.getFileObject("otherfile");
186         assertEquals("otherfile not registered", null, FileOwnerQuery.getOwner(f));
187         // Test the simpler project type.
188
sh2.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
189         f = proj2dir.getFileObject("proj2file");
190         assertEquals("proj2file of course OK", project2, FileOwnerQuery.getOwner(f));
191         f = proj2src1dir.getFileObject("proj2src1file");
192         assertEquals("proj2src1file registered", project2, FileOwnerQuery.getOwner(f));
193         f = proj2src2dir.getFileObject("proj2src2file");
194         assertEquals("proj2src2file registered", project2, FileOwnerQuery.getOwner(f));
195     }
196     
197     public void testSourceLocationChanges() throws Exception JavaDoc {
198         Sources s = sh.createSources();
199         SourceGroup[] groups = s.getSourceGroups(Sources.TYPE_GENERIC);
200         assertEquals("should have maindir plus src2dir plus src3dir", 3, groups.length);
201         assertEquals("group #1 is src2dir", src2dir, groups[0].getRootFolder());
202         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
203         assertEquals("group #3 is maindir", maindir, groups[2].getRootFolder());
204         groups = s.getSourceGroups("java");
205         assertEquals("should have src1dir plus src3dir", 2, groups.length);
206         assertEquals("group #1 is src1dir", src1dir, groups[0].getRootFolder());
207         assertEquals("right display name for src1dir", "Packages #1", groups[0].getDisplayName());
208         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
209         // Now change one of them.
210
EditableProperties p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
211         p.setProperty("src1.dir", "../../src4");
212         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
213         ProjectManager.getDefault().saveProject(project);
214         groups = s.getSourceGroups(Sources.TYPE_GENERIC);
215         assertEquals("should have maindir plus src4dir plus src2dir plus src3dir", 4, groups.length);
216         assertEquals("group #1 is src4dir", src4dir, groups[0].getRootFolder());
217         assertEquals("group #2 is src2dir", src2dir, groups[1].getRootFolder());
218         assertEquals("group #3 is src3dir", src3dir, groups[2].getRootFolder());
219         assertEquals("group #4 is maindir", maindir, groups[3].getRootFolder());
220         groups = s.getSourceGroups("java");
221         assertEquals("should have src4dir plus src3dir", 2, groups.length);
222         assertEquals("group #1 is src4dir", src4dir, groups[0].getRootFolder());
223         assertEquals("right display name for src4dir", "Packages #1", groups[0].getDisplayName());
224         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
225     }
226     
227     public void testSourceLocationChangesFired() throws Exception JavaDoc {
228         Sources s = sh.createSources();
229         // Listen to changes.
230
AntBasedTestUtil.TestCL l = new AntBasedTestUtil.TestCL();
231         s.addChangeListener(l);
232         // Check baseline GENERIC sources.
233
SourceGroup[] groups = s.getSourceGroups(Sources.TYPE_GENERIC);
234         assertEquals("should have maindir plus src2dir plus src3dir", 3, groups.length);
235         assertEquals("group #1 is src2dir", src2dir, groups[0].getRootFolder());
236         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
237         assertEquals("group #3 is maindir", maindir, groups[2].getRootFolder());
238         assertFalse("no initial changes", l.expect());
239         // Now change one of them to a different dir.
240
EditableProperties p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
241         p.setProperty("src2.dir", "../../src4");
242         p.setProperty("src2a.dir", "nonsense");
243         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
244         ProjectManager.getDefault().saveProject(project);
245         assertTrue("got change in GENERIC sources", l.expect());
246         // Check new values.
247
groups = s.getSourceGroups(Sources.TYPE_GENERIC);
248         assertEquals("should have maindir plus src4dir plus src3dir", 3, groups.length);
249         assertEquals("group #1 is src4dir", src4dir, groups[0].getRootFolder());
250         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
251         assertEquals("group #3 is maindir", maindir, groups[2].getRootFolder());
252         // Check 'java' type groups also.
253
groups = s.getSourceGroups("java");
254         assertEquals("should have src1dir plus src3dir", 2, groups.length);
255         assertEquals("group #1 is src1dir", src1dir, groups[0].getRootFolder());
256         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
257         assertFalse("no additional changes yet", l.expect());
258         p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
259         p.setProperty("src1.dir", "does-not-exist");
260         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
261         ProjectManager.getDefault().saveProject(project);
262         assertTrue("got change in java sources", l.expect());
263         groups = s.getSourceGroups("java");
264         assertEquals("should have just src3dir", 1, groups.length);
265         assertEquals("group #2 is src3dir", src3dir, groups[0].getRootFolder());
266         assertFalse("no further changes", l.expect());
267         // #47451: should not fire changes for unrelated properties.
268
p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
269         p.setProperty("irrelevant", "value");
270         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
271         ProjectManager.getDefault().saveProject(project);
272         assertFalse("no changes fired from an unrelated property", l.expect());
273     }
274     
275     public void testExternalRootLocationChanges() throws Exception JavaDoc {
276         FileObject readme = maindir.getFileObject("readme");
277         assertEquals("readme not yet registered", null, FileOwnerQuery.getOwner(readme));
278         sh.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
279         assertEquals("readme still registered", project, FileOwnerQuery.getOwner(readme));
280         FileObject src4file = src4dir.getFileObject("src4file");
281         assertEquals("src4file not yet owned by anyone", null, FileOwnerQuery.getOwner(src4file));
282         FileObject src2file = src2dir.getFileObject("src2file");
283         assertEquals("src2file owned by the project", project, FileOwnerQuery.getOwner(src2file));
284         // Change things around.
285
EditableProperties p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
286         p.setProperty("src1.dir", "../../src4"); // start to recognize this root
287
p.setProperty("src2.dir", "src2"); // moved from ../../src2
288
p.remove("src2a.dir"); // was also ../../src2
289
h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
290         ProjectManager.getDefault().saveProject(project);
291         assertEquals("src4file now owned by the project", project, FileOwnerQuery.getOwner(src4file));
292         assertEquals("src2file no longer owned by the project", null, FileOwnerQuery.getOwner(src2file));
293         assertEquals("readme still registered after unrelated changes", project, FileOwnerQuery.getOwner(readme));
294         FileObject otherfile = scratch.getFileObject("otherfile");
295         assertEquals("otherfile still not registered", null, FileOwnerQuery.getOwner(otherfile));
296     }
297     
298     public void testSourceRootDeletion() throws Exception JavaDoc {
299         // Cf. #40845. Need to fire a change if a root is deleted while project is open.
300
Sources s = sh.createSources();
301         SourceGroup[] groups = s.getSourceGroups("java");
302         assertEquals("should have src1dir plus src3dir", 2, groups.length);
303         assertEquals("group #1 is src1dir", src1dir, groups[0].getRootFolder());
304         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
305         AntBasedTestUtil.TestCL l = new AntBasedTestUtil.TestCL();
306         s.addChangeListener(l);
307         src3dir.delete();
308         assertTrue("got a change after src3dir deleted", l.expect());
309         groups = s.getSourceGroups("java");
310         assertEquals("should have just src1dir", 1, groups.length);
311         assertEquals("group #1 is src1dir", src1dir, groups[0].getRootFolder());
312         src1dir.delete();
313         assertTrue("got a change after src1dir deleted", l.expect());
314         groups = s.getSourceGroups("java");
315         assertEquals("should have no dirs", 0, groups.length);
316         FileObject src5dir = scratch.createFolder("nonesuch");
317         assertTrue("got a change after src5dir created", l.expect());
318         groups = s.getSourceGroups("java");
319         assertEquals("should have src15dir now", 1, groups.length);
320         assertEquals("group #1 is src5dir", src5dir, groups[0].getRootFolder());
321     }
322
323     public void testIncludesExcludes() throws Exception JavaDoc {
324         // <editor-fold desc="initial setup">
325
scratch = TestUtil.makeScratchDir(this); // have our own setup
326
maindir = scratch.createFolder("dir");
327         projdir = maindir.createFolder("proj-dir");
328         src1dir = projdir.createFolder("src1");
329         src2dir = scratch.createFolder("src2");
330         src3dir = projdir.createFolder("src3");
331         src4dir = scratch.createFolder("src4");
332         // </editor-fold>
333
// <editor-fold desc="create files in group #1">
334
FileUtil.createData(src1dir, "com/sun/tools/javac/Main.java");
335         FileUtil.createData(src1dir, "com/sun/tools/internal/ws/processor/model/java/JavaArrayType.java");
336         FileUtil.createData(src1dir, "sun/tools/javac/Main.java");
337         FileUtil.createData(src1dir, "sunw/io/Serializable.java");
338         FileUtil.createData(src1dir, "java/lang/Byte.java");
339         FileUtil.createData(src1dir, "java/text/resources/Messages.properties");
340         FileUtil.createData(src1dir, "java/text/resources/Messages_zh.properties");
341         FileUtil.createData(src1dir, "java/text/resources/Messages_zh_TW.properties");
342         FileUtil.createData(src1dir, "java/text/resources/x_y/z.properties");
343         // </editor-fold>
344
// <editor-fold desc="create files in group #2">
345
FileUtil.createData(src2dir, "java/lang/Class.java");
346         FileUtil.createData(src2dir, "javax/swing/JComponent.java");
347         FileUtil.createData(src2dir, "javax/lang/Foo.java");
348         FileUtil.createData(src2dir, "com/sun/java/swing/plaf/motif/MotifSplitPaneUI.java");
349         FileUtil.createData(src2dir, "com/sun/org/apache/xerces/internal/parsers/AbstractDOMParser.java");
350         FileUtil.createData(src2dir, "org/omg/CORBA/Any.java");
351         FileUtil.createData(src2dir, "javax/swing/doc-files/groupLayout.1.gif");
352         FileUtil.createData(src2dir, "javax/swing/plaf/resources/foo.gif");
353         FileUtil.createData(src2dir, "javax/swing/resources/bar.gif");
354         FileUtil.createData(src2dir, "docs/html/index.html");
355         // </editor-fold>
356
// <editor-fold desc="create files in group #3">
357
FileUtil.createData(src3dir, "java/lang/Class.java");
358         FileUtil.createData(src3dir, "java/util/Compat$Clazz.java");
359         FileUtil.createData(src3dir, "javax/swing/JComponent.java");
360         FileUtil.createData(src3dir, "com/sun/java/swing/plaf/motif/MotifSplitPaneUI.java");
361         FileUtil.createData(src3dir, "README");
362         FileUtil.createData(src3dir, "README.html");
363         FileUtil.createData(src3dir, "whatever.xml");
364         // </editor-fold>
365
// <editor-fold desc="create files in group #4">
366
FileUtil.createData(src4dir, "java/lang/Class.java");
367         // </editor-fold>
368
// <editor-fold desc="other setup #1">
369
h = ProjectGenerator.createProject(projdir, "test");
370         project = ProjectManager.getDefault().findProject(projdir);
371         EditableProperties p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
372         p.setProperty("src1.dir", "src1");
373         p.setProperty("src2.dir", "../../src2");
374         p.setProperty("src3.dir", "src3");
375         p.setProperty("src4.dir", "../../src4");
376         // </editor-fold>
377
// <editor-fold desc="includes & excludes">
378
p.setProperty("src1.excludes", " sun/,sunw\\, **\\internal/** ${undefined} , **/resources/*_*.properties ");
379         p.setProperty("src2.includes", "**/swing/,com/sun/org/apache/,org/omg,docs/html/index.html");
380         p.setProperty("src2.excludes", "**/doc-files/ **/swing/**/resources/");
381         p.setProperty("src3.includes", "javax/swing/,com/sun/java/swing/,README,**/*$*.java,**/*.xml");
382         // </editor-fold>
383
// <editor-fold desc="other setup #2">
384
h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
385         ProjectManager.getDefault().saveProject(project);
386         sh = new SourcesHelper(h, h.getStandardPropertyEvaluator());
387         sh.addPrincipalSourceRoot("${src1.dir}", "${src1.includes}", "${src1.excludes}", "Sources #1", null, null);
388         sh.addPrincipalSourceRoot("${src2.dir}", "${src2.includes}", "${src2.excludes}", "Sources #2", null, null);
389         sh.addPrincipalSourceRoot("${src3.dir}", "${src3.includes}", null, "Sources #3", null, null);
390         sh.addPrincipalSourceRoot("${src4.dir}", "**", "", "Sources #4", null, null);
391         sh.addTypedSourceRoot("${src1.dir}", "${src1.includes}", "${src1.excludes}", "java", "Packages #1", null, null);
392         sh.addTypedSourceRoot("${src2.dir}", "${src2.includes}", "${src2.excludes}", "java", "Packages #2", null, null);
393         sh.addTypedSourceRoot("${src3.dir}", "${src3.includes}", null, "java", "Packages #3", null, null);
394         sh.addTypedSourceRoot("${src4.dir}", "**", "", "java", "Packages #4", null, null);
395         Sources s = sh.createSources();
396         SourceGroup[] groups = s.getSourceGroups("java");
397         SourceGroup g1 = groups[0];
398         assertEquals("Packages #1", g1.getDisplayName());
399         SourceGroup g2 = groups[1];
400         assertEquals("Packages #2", g2.getDisplayName());
401         SourceGroup g3 = groups[2];
402         assertEquals("Packages #3", g3.getDisplayName());
403         SourceGroup g4 = groups[3];
404         assertEquals("Packages #4", g4.getDisplayName());
405         // </editor-fold>
406
// <editor-fold desc="testing group #1">
407
assertIncluded("not excluded despite sun/ infix", g1, "com/sun/tools/javac/Main.java");
408         assertExcluded("internal infix", g1, "com/sun/tools/internal/ws/processor/model/java/JavaArrayType.java");
409         assertExcluded("the whole folder is suppressed", g1, "com/sun/tools/internal");
410         assertExcluded("sun/ prefix", g1, "sun/tools/javac/Main.java");
411         assertExcluded("the whole folder is suppressed", g1, "sun");
412         assertExcluded("sunw/ prefix even with \\", g1, "sunw/io/Serializable.java");
413         assertExcluded("the whole folder is suppressed", g1, "sunw");
414         assertIncluded("why not?", g1, "java/lang/Byte.java");
415         assertIncluded("no _", g1, "java/text/resources/Messages.properties");
416         assertExcluded("has _", g1, "java/text/resources/Messages_zh.properties");
417         assertExcluded("has _ twice", g1, "java/text/resources/Messages_zh_TW.properties");
418         assertIncluded("* does not match /", g1, "java/text/resources/x_y/z.properties");
419         // </editor-fold>
420
// <editor-fold desc="testing group #2">
421
assertExcluded("not explicitly included", g2, "java/lang/Class.java");
422         assertExcluded("nothing in java.lang.** is", g2, "java/lang");
423         assertExcluded("nothing in java.** is", g2, "java");
424         assertIncluded("explicitly included", g2, "javax/swing/JComponent.java");
425         assertExcluded("but that does not apply to other children", g2, "javax/lang/Foo.java");
426         assertIncluded("also explicitly included", g2, "com/sun/java/swing/plaf/motif/MotifSplitPaneUI.java");
427         assertIncluded("not excluded as internal", g2, "com/sun/org/apache/xerces/internal/parsers/AbstractDOMParser.java");
428         assertExcluded("dir includes do not work without /", g2, "org/omg/CORBA/Any.java");
429         assertExcluded("dir includes do not work without / even for folder itself", g2, "org/omg");
430         assertExcluded("nothing in org included, in fact", g2, "org");
431         assertExcluded("doc-files excluded", g2, "javax/swing/doc-files/groupLayout.1.gif");
432         assertExcluded("whole doc-files excluded", g2, "javax/swing/doc-files");
433         assertExcluded("resources excluded with intermediate plaf", g2, "javax/swing/plaf/resources/foo.gif");
434         assertExcluded("whole resources excluded with intermediate plaf", g2, "javax/swing/plaf/resources");
435         assertExcluded("/**/ can match /", g2, "javax/swing/resources/bar.gif");
436         assertExcluded("/**/ can match / on whole dir", g2, "javax/swing/resources");
437         // </editor-fold>
438
// <editor-fold desc="testing group #3">
439
assertExcluded("no reason to include", g3, "java/lang/Class.java");
440         assertExcluded("java.lang not there", g3, "java/lang");
441         assertIncluded("has a $", g3, "java/util/Compat$Clazz.java");
442         assertIncluded("explicitly included", g3, "javax/swing/JComponent.java");
443         assertIncluded("explicitly included", g3, "com/sun/java/swing/plaf/motif/MotifSplitPaneUI.java");
444         assertIncluded("explicitly included", g3, "README");
445         assertExcluded("did not include file w/ ext", g3, "README.html");
446         assertIncluded("**/ can match null prefix", g3, "whatever.xml");
447         // </editor-fold>
448
// <editor-fold desc="testing group #4">
449
assertIncluded("everything included", g4, "java/lang/Class.java");
450         // </editor-fold>
451
// <editor-fold desc="testing external roots">
452
sh.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
453         FileObject projdir2 = maindir.createFolder("proj-dir2");
454         ProjectGenerator.createProject(projdir2, "test");
455         Project project2 = ProjectManager.getDefault().findProject(projdir2);
456         FileObject docfiles = src2dir.getFileObject("javax/swing/doc-files");
457         assertNotNull(docfiles);
458         FileOwnerQuery.markExternalOwner(docfiles, project2, FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
459         assertOwner(null, g2, "java/lang/Class.java");
460         assertOwner(project, g2, "javax/swing/JComponent.java");
461         assertOwner(project, g2, "javax/swing");
462         assertOwner(null, g2, "javax");
463         assertOwner(project, g2, "com/sun/java/swing/plaf/motif/MotifSplitPaneUI.java");
464         assertOwner(project, g2, "com/sun/java/swing/plaf/motif");
465         assertOwner(project, g2, "com/sun/java/swing/plaf");
466         assertOwner(project, g2, "com/sun/java/swing");
467         assertOwner(null, g2, "com/sun/java");
468         assertOwner(null, g2, "com/sun");
469         assertOwner(null, g2, "com");
470         assertOwner(null, g2, "org/omg/CORBA/Any.java");
471         assertOwner(project2, g2, "javax/swing/doc-files/groupLayout.1.gif");
472         assertOwner(project2, g2, "javax/swing/doc-files");
473         assertOwner(project, g2, "com/sun/java/swing");
474         assertOwner(project, g2, "docs/html/index.html");
475         assertOwner(project, g2, "docs/html");
476         assertOwner(null, g2, "docs");
477         p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
478         p.setProperty("src2.includes", "com/sun/org/apache/,org/omg,docs/html/index.html");
479         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
480         assertOwner(null, g2, "javax/swing/JComponent.java");
481         p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
482         p.setProperty("src2.includes", "**/swing/,com/sun/org/apache/,org/omg,docs/html/index.html");
483         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
484         assertOwner(project, g2, "javax/swing/JComponent.java");
485         assertOwner(project, g4, "java/lang/Class.java");
486         assertOwner(project, g4, "java/lang");
487         assertOwner(project, g4, "java");
488         assertOwner(project, g4, "");
489         // </editor-fold>
490
// <editor-fold desc="testing change firing">
491
AntBasedTestUtil.TestPCL l = new AntBasedTestUtil.TestPCL();
492         g2.addPropertyChangeListener(l);
493         p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
494         p.setProperty("src2.excludes", "**/doc-files/");
495         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
496         assertEquals(Collections.singleton(SourceGroup.PROP_CONTAINERSHIP), l.changed);
497         l.reset();
498         assertExcluded("doc-files still excluded", g2, "javax/swing/doc-files/groupLayout.1.gif");
499         assertIncluded("resources now included", g2, "javax/swing/plaf/resources/foo.gif");
500         p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
501         p.setProperty("src2.includes", "**");
502         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
503         assertEquals(Collections.singleton(SourceGroup.PROP_CONTAINERSHIP), l.changed);
504         l.reset();
505         assertIncluded("may as well be included", g2, "java/lang/Class.java");
506         p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
507         p.setProperty("src2.includes", "**/swing/");
508         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
509         assertEquals(Collections.singleton(SourceGroup.PROP_CONTAINERSHIP), l.changed);
510         l.reset();
511         assertExcluded("excluded again", g2, "java/lang/Class.java");
512         p = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
513         p.setProperty("irrelevant", "value");
514         h.putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, p);
515         assertEquals(Collections.emptySet(), l.changed);
516         l.reset();
517         // </editor-fold>
518
// <editor-fold desc="testing misc">
519
try {
520             FileObject f = src2dir.getFileObject("java/lang/Class.java");
521             assertNotNull(f);
522             g1.contains(f);
523             fail("wrong root");
524         } catch (IllegalArgumentException JavaDoc e) {/* good */}
525         SourceGroup[] ggroups = s.getSourceGroups(Sources.TYPE_GENERIC);
526         SourceGroup gg1 = ggroups[0];
527         assertEquals("Sources #2", gg1.getDisplayName());
528         assertIncluded("explicitly included", gg1, "javax/swing/JComponent.java");
529         assertExcluded("but that does not apply to other children", gg1, "javax/lang/Foo.java");
530         // </editor-fold>
531
}
532     private static void assertIncluded(String JavaDoc message, SourceGroup g, String JavaDoc resource) {
533         FileObject f = g.getRootFolder().getFileObject(resource);
534         assertNotNull(resource, f);
535         assertTrue(message, g.contains(f));
536         int slash = resource.lastIndexOf('/');
537         if (slash != -1) {
538             String JavaDoc parent = resource.substring(0, slash);
539             assertIncluded("parent " + parent + " of " + resource + " must also be contained by definition", g, parent);
540         } else if (resource.length() > 0) {
541             assertIncluded("root folder always contained by definition", g, "");
542         }
543     }
544     private static void assertExcluded(String JavaDoc message, SourceGroup g, String JavaDoc resource) {
545         FileObject f = g.getRootFolder().getFileObject(resource);
546         assertNotNull(resource, f);
547         assertFalse(message, g.contains(f));
548     }
549     private static void assertOwner(Project owner, SourceGroup g, String JavaDoc resource) {
550         FileObject f = g.getRootFolder().getFileObject(resource);
551         assertNotNull(resource, f);
552         assertEquals(owner, FileOwnerQuery.getOwner(f));
553     }
554     
555 }
556
Popular Tags