KickJava   Java API By Example, From Geeks To Geeks.

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

37 public final class SourcesHelperTest extends NbTestCase {
38     
39     public SourcesHelperTest(String JavaDoc name) {
40         super(name);
41     }
42     
43     private FileObject scratch;
44     private FileObject maindir;
45     private FileObject projdir;
46     private FileObject src1dir;
47     private FileObject src2dir;
48     private FileObject src3dir;
49     private FileObject src4dir;
50     private FileObject builddir;
51     private RakeProjectHelper h;
52     private Project project;
53     private SourcesHelper sh;
54     private FileObject proj2dir;
55     private FileObject proj2src1dir;
56     private FileObject proj2src2dir;
57     private RakeProjectHelper h2;
58     private Project project2;
59     private SourcesHelper sh2;
60     
61     protected void setUp() throws Exception JavaDoc {
62         super.setUp();
63         TestUtil.setLookup(new Object JavaDoc[] {
64             RakeBasedTestUtil.testRakeBasedProjectType(),
65         });
66         scratch = TestUtil.makeScratchDir(this);
67         scratch.createData("otherfile");
68         maindir = scratch.createFolder("dir");
69         maindir.createData("readme");
70         projdir = maindir.createFolder("projdir");
71         projdir.createData("projfile");
72         src1dir = projdir.createFolder("src1");
73         src1dir.createData("src1file");
74         src2dir = scratch.createFolder("src2");
75         src2dir.createData("src2file");
76         src3dir = scratch.createFolder("src3");
77         src3dir.createData("src3file");
78         src4dir = scratch.createFolder("src4");
79         src4dir.createData("src4file");
80         builddir = scratch.createFolder("build");
81         builddir.createData("buildfile");
82         h = ProjectGenerator.createProject(projdir, "test");
83         project = ProjectManager.getDefault().findProject(projdir);
84         assertNotNull("have a project", project);
85         EditableProperties p = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
86         p.setProperty("src1.dir", "src1");
87         p.setProperty("src2.dir", "../../src2");
88         p.setProperty("src2a.dir", "../../src2"); // same path as src2.dir
89
p.setProperty("src3.dir", FileUtil.toFile(src3dir).getAbsolutePath());
90         p.setProperty("src4.dir", "..");
91         p.setProperty("src5.dir", "../../nonesuch");
92         p.setProperty("build.dir", "../../build");
93         h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, p);
94         ProjectManager.getDefault().saveProject(project);
95         sh = new SourcesHelper(h, h.getStandardPropertyEvaluator());
96         sh.addPrincipalSourceRoot("${src1.dir}", "Sources #1", null, null); // inside proj dir
97
sh.addPrincipalSourceRoot("${src2.dir}", "Sources #2", null, null); // outside (rel path)
98
sh.addPrincipalSourceRoot("${src2a.dir}", "Sources #2a", null, null); // redundant
99
sh.addPrincipalSourceRoot("${src3.dir}", "Sources #3", null, null); // outside (abs path)
100
sh.addPrincipalSourceRoot("${src4.dir}", "The Whole Shebang", null, null); // above proj dir
101
sh.addPrincipalSourceRoot("${src5.dir}", "None such", null, null); // does not exist on disk
102
sh.addNonSourceRoot("${build.dir}");
103         sh.addTypedSourceRoot("${src1.dir}", "java", "Packages #1", null, null);
104         sh.addTypedSourceRoot("${src3.dir}", "java", "Packages #3", null, null);
105         sh.addTypedSourceRoot("${src5.dir}", "java", "No Packages", null, null);
106         sh.addTypedSourceRoot("${src2.dir}", "docroot", "Documents #2", null, null);
107         sh.addTypedSourceRoot("${src2a.dir}", "docroot", "Documents #2a", null, null); // redundant
108
// Separate project that has includes its project directory implicitly only.
109
// Also hardcodes paths rather than using properties.
110
proj2dir = scratch.createFolder("proj2dir");
111         proj2dir.createData("proj2file");
112         proj2src1dir = proj2dir.createFolder("src1");
113         proj2src1dir.createData("proj2src1file");
114         proj2src2dir = proj2dir.createFolder("src2");
115         proj2src2dir.createData("proj2src2file");
116         h2 = ProjectGenerator.createProject(proj2dir, "test");
117         project2 = ProjectManager.getDefault().findProject(proj2dir);
118         assertNotNull("have a project2", project2);
119         sh2 = new SourcesHelper(h2, h2.getStandardPropertyEvaluator());
120         sh2.addPrincipalSourceRoot("src1", "Sources #1", null, null);
121         sh2.addPrincipalSourceRoot("src2", "Sources #2", null, null);
122         sh2.addNonSourceRoot("build");
123         sh2.addTypedSourceRoot("src1", "java", "Packages #1", null, null);
124         sh2.addTypedSourceRoot("src2", "java", "Packages #2", null, null);
125     }
126     
127     public void testSourcesBasic() throws Exception JavaDoc {
128         Sources s = sh.createSources();
129         // XXX test that ISE is thrown if we try to add more dirs now
130
SourceGroup[] groups = s.getSourceGroups(Sources.TYPE_GENERIC);
131         assertEquals("should have maindir plus src2dir plus src3dir", 3, groups.length);
132         assertEquals("group #1 is src2dir", src2dir, groups[0].getRootFolder());
133         assertEquals("right display name for src2dir", "Sources #2", groups[0].getDisplayName());
134         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
135         assertEquals("right display name for src3dir", "Sources #3", groups[1].getDisplayName());
136         assertEquals("group #3 is maindir", maindir, groups[2].getRootFolder());
137         assertEquals("right display name for maindir", "The Whole Shebang", groups[2].getDisplayName());
138         // Now the typed source roots.
139
groups = s.getSourceGroups("java");
140         assertEquals("should have src1dir plus src3dir", 2, groups.length);
141         assertEquals("group #1 is src1dir", src1dir, groups[0].getRootFolder());
142         assertEquals("right display name for src1dir", "Packages #1", groups[0].getDisplayName());
143         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
144         assertEquals("right display name for src3dir", "Packages #3", groups[1].getDisplayName());
145         groups = s.getSourceGroups("docroot");
146         assertEquals("should have just src2dir", 1, groups.length);
147         assertEquals("group #1 is src2dir", src2dir, groups[0].getRootFolder());
148         assertEquals("right display name for src2dir", "Documents #2", groups[0].getDisplayName());
149         groups = s.getSourceGroups("unknown");
150         assertEquals("should not have any unknown dirs", 0, groups.length);
151         // Test the simpler project type.
152
s = sh2.createSources();
153         groups = s.getSourceGroups(Sources.TYPE_GENERIC);
154         assertEquals("should have just proj2dir", 1, groups.length);
155         assertEquals("group #1 is proj2dir", proj2dir, groups[0].getRootFolder());
156         assertEquals("right display name for proj2dir", ProjectUtils.getInformation(project2).getDisplayName(), groups[0].getDisplayName());
157         groups = s.getSourceGroups("java");
158         assertEquals("should have proj2src1dir plus proj2src2dir", 2, groups.length);
159         assertEquals("group #1 is proj2src1dir group", proj2src1dir, groups[0].getRootFolder());
160         assertEquals("right display name for src1dir", "Packages #1", groups[0].getDisplayName());
161         assertEquals("group #2 is proj2src2dir group", proj2src2dir, groups[1].getRootFolder());
162         assertEquals("right display name for proj2src2dir", "Packages #2", groups[1].getDisplayName());
163         // XXX test also icons
164
}
165     
166     public void testExternalRootRegistration() throws Exception JavaDoc {
167         FileObject f = maindir.getFileObject("readme");
168         assertEquals("readme not yet registered", null, FileOwnerQuery.getOwner(f));
169         f = projdir.getFileObject("projfile");
170         assertEquals("projfile initially OK", project, FileOwnerQuery.getOwner(f));
171         sh.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
172         f = maindir.getFileObject("readme");
173         assertEquals("readme now registered", project, FileOwnerQuery.getOwner(f));
174         f = projdir.getFileObject("projfile");
175         assertEquals("projfile still OK", project, FileOwnerQuery.getOwner(f));
176         f = src1dir.getFileObject("src1file");
177         assertEquals("src1file registered", project, FileOwnerQuery.getOwner(f));
178         f = src2dir.getFileObject("src2file");
179         assertEquals("src2file registered", project, FileOwnerQuery.getOwner(f));
180         f = src3dir.getFileObject("src3file");
181         assertEquals("src3file registered", project, FileOwnerQuery.getOwner(f));
182         f = builddir.getFileObject("buildfile");
183         assertEquals("buildfile registered", project, FileOwnerQuery.getOwner(f));
184         f = scratch.getFileObject("otherfile");
185         assertEquals("otherfile not registered", null, FileOwnerQuery.getOwner(f));
186         // Test the simpler project type.
187
sh2.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
188         f = proj2dir.getFileObject("proj2file");
189         assertEquals("proj2file of course OK", project2, FileOwnerQuery.getOwner(f));
190         f = proj2src1dir.getFileObject("proj2src1file");
191         assertEquals("proj2src1file registered", project2, FileOwnerQuery.getOwner(f));
192         f = proj2src2dir.getFileObject("proj2src2file");
193         assertEquals("proj2src2file registered", project2, FileOwnerQuery.getOwner(f));
194     }
195     
196     public void testSourceLocationChanges() throws Exception JavaDoc {
197         Sources s = sh.createSources();
198         SourceGroup[] groups = s.getSourceGroups(Sources.TYPE_GENERIC);
199         assertEquals("should have maindir plus src2dir plus src3dir", 3, groups.length);
200         assertEquals("group #1 is src2dir", src2dir, groups[0].getRootFolder());
201         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
202         assertEquals("group #3 is maindir", maindir, groups[2].getRootFolder());
203         groups = s.getSourceGroups("java");
204         assertEquals("should have src1dir plus src3dir", 2, groups.length);
205         assertEquals("group #1 is src1dir", src1dir, groups[0].getRootFolder());
206         assertEquals("right display name for src1dir", "Packages #1", groups[0].getDisplayName());
207         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
208         // Now change one of them.
209
EditableProperties p = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
210         p.setProperty("src1.dir", "../../src4");
211         h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, p);
212         ProjectManager.getDefault().saveProject(project);
213         groups = s.getSourceGroups(Sources.TYPE_GENERIC);
214         assertEquals("should have maindir plus src4dir plus src2dir plus src3dir", 4, groups.length);
215         assertEquals("group #1 is src4dir", src4dir, groups[0].getRootFolder());
216         assertEquals("group #2 is src2dir", src2dir, groups[1].getRootFolder());
217         assertEquals("group #3 is src3dir", src3dir, groups[2].getRootFolder());
218         assertEquals("group #4 is maindir", maindir, groups[3].getRootFolder());
219         groups = s.getSourceGroups("java");
220         assertEquals("should have src4dir plus src3dir", 2, groups.length);
221         assertEquals("group #1 is src4dir", src4dir, groups[0].getRootFolder());
222         assertEquals("right display name for src4dir", "Packages #1", groups[0].getDisplayName());
223         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
224     }
225     
226     public void testSourceLocationChangesFired() throws Exception JavaDoc {
227         Sources s = sh.createSources();
228         // Listen to changes.
229
RakeBasedTestUtil.TestCL l = new RakeBasedTestUtil.TestCL();
230         s.addChangeListener(l);
231         // Check baseline GENERIC sources.
232
SourceGroup[] groups = s.getSourceGroups(Sources.TYPE_GENERIC);
233         assertEquals("should have maindir plus src2dir plus src3dir", 3, groups.length);
234         assertEquals("group #1 is src2dir", src2dir, groups[0].getRootFolder());
235         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
236         assertEquals("group #3 is maindir", maindir, groups[2].getRootFolder());
237         assertFalse("no initial changes", l.expect());
238         // Now change one of them to a different dir.
239
EditableProperties p = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
240         p.setProperty("src2.dir", "../../src4");
241         p.setProperty("src2a.dir", "nonsense");
242         h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, p);
243         ProjectManager.getDefault().saveProject(project);
244         assertTrue("got change in GENERIC sources", l.expect());
245         // Check new values.
246
groups = s.getSourceGroups(Sources.TYPE_GENERIC);
247         assertEquals("should have maindir plus src4dir plus src3dir", 3, groups.length);
248         assertEquals("group #1 is src4dir", src4dir, groups[0].getRootFolder());
249         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
250         assertEquals("group #3 is maindir", maindir, groups[2].getRootFolder());
251         // Check 'java' type groups also.
252
groups = s.getSourceGroups("java");
253         assertEquals("should have src1dir plus src3dir", 2, groups.length);
254         assertEquals("group #1 is src1dir", src1dir, groups[0].getRootFolder());
255         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
256         assertFalse("no additional changes yet", l.expect());
257         p = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
258         p.setProperty("src1.dir", "does-not-exist");
259         h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, p);
260         ProjectManager.getDefault().saveProject(project);
261         assertTrue("got change in java sources", l.expect());
262         groups = s.getSourceGroups("java");
263         assertEquals("should have just src3dir", 1, groups.length);
264         assertEquals("group #2 is src3dir", src3dir, groups[0].getRootFolder());
265         assertFalse("no further changes", l.expect());
266         // #47451: should not fire changes for unrelated properties.
267
p = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
268         p.setProperty("irrelevant", "value");
269         h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, p);
270         ProjectManager.getDefault().saveProject(project);
271         assertFalse("no changes fired from an unrelated property", l.expect());
272     }
273     
274     public void testExternalRootLocationChanges() throws Exception JavaDoc {
275         FileObject readme = maindir.getFileObject("readme");
276         assertEquals("readme not yet registered", null, FileOwnerQuery.getOwner(readme));
277         sh.registerExternalRoots(FileOwnerQuery.EXTERNAL_ALGORITHM_TRANSIENT);
278         assertEquals("readme still registered", project, FileOwnerQuery.getOwner(readme));
279         FileObject src4file = src4dir.getFileObject("src4file");
280         assertEquals("src4file not yet owned by anyone", null, FileOwnerQuery.getOwner(src4file));
281         FileObject src2file = src2dir.getFileObject("src2file");
282         assertEquals("src2file owned by the project", project, FileOwnerQuery.getOwner(src2file));
283         // Change things around.
284
EditableProperties p = h.getProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH);
285         p.setProperty("src1.dir", "../../src4"); // start to recognize this root
286
p.setProperty("src2.dir", "src2"); // moved from ../../src2
287
p.remove("src2a.dir"); // was also ../../src2
288
h.putProperties(RakeProjectHelper.PROJECT_PROPERTIES_PATH, p);
289         ProjectManager.getDefault().saveProject(project);
290         assertEquals("src4file now owned by the project", project, FileOwnerQuery.getOwner(src4file));
291         assertEquals("src2file no longer owned by the project", null, FileOwnerQuery.getOwner(src2file));
292         assertEquals("readme still registered after unrelated changes", project, FileOwnerQuery.getOwner(readme));
293         FileObject otherfile = scratch.getFileObject("otherfile");
294         assertEquals("otherfile still not registered", null, FileOwnerQuery.getOwner(otherfile));
295     }
296     
297     public void testSourceRootDeletion() throws Exception JavaDoc {
298         // Cf. #40845. Need to fire a change if a root is deleted while project is open.
299
Sources s = sh.createSources();
300         SourceGroup[] groups = s.getSourceGroups("java");
301         assertEquals("should have src1dir plus src3dir", 2, groups.length);
302         assertEquals("group #1 is src1dir", src1dir, groups[0].getRootFolder());
303         assertEquals("group #2 is src3dir", src3dir, groups[1].getRootFolder());
304         RakeBasedTestUtil.TestCL l = new RakeBasedTestUtil.TestCL();
305         s.addChangeListener(l);
306         src3dir.delete();
307         assertTrue("got a change after src3dir deleted", l.expect());
308         groups = s.getSourceGroups("java");
309         assertEquals("should have just src1dir", 1, groups.length);
310         assertEquals("group #1 is src1dir", src1dir, groups[0].getRootFolder());
311         src1dir.delete();
312         assertTrue("got a change after src1dir deleted", l.expect());
313         groups = s.getSourceGroups("java");
314         assertEquals("should have no dirs", 0, groups.length);
315         FileObject src5dir = scratch.createFolder("nonesuch");
316         assertTrue("got a change after src5dir created", l.expect());
317         groups = s.getSourceGroups("java");
318         assertEquals("should have src15dir now", 1, groups.length);
319         assertEquals("group #1 is src5dir", src5dir, groups[0].getRootFolder());
320     }
321     
322 }
323
Popular Tags