KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > FreeformSourcesTest


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.ant.freeform;
21
22 import java.io.File JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.SortedSet JavaDoc;
26 import java.util.TreeSet JavaDoc;
27 import org.netbeans.api.project.FileOwnerQuery;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectManager;
30 import org.netbeans.api.project.ProjectUtils;
31 import org.netbeans.api.project.SourceGroup;
32 import org.netbeans.api.project.Sources;
33 import org.netbeans.modules.ant.freeform.spi.support.Util;
34 import org.netbeans.spi.project.support.ant.AntProjectHelper;
35 import org.netbeans.spi.project.support.ant.EditableProperties;
36 import org.openide.filesystems.FileObject;
37 import org.openide.filesystems.FileUtil;
38 import org.w3c.dom.Document JavaDoc;
39 import org.w3c.dom.Element JavaDoc;
40 import org.w3c.dom.NodeList JavaDoc;
41
42 // XXX testExternalSourceRootChanges
43
// - should check that FOQ changes as well
44

45 /**
46  * Test {@link FreeformSources}.
47  * @author Jesse Glick
48  */

49 public class FreeformSourcesTest extends TestBase {
50     
51     public FreeformSourcesTest(String JavaDoc name) {
52         super(name);
53     }
54     
55     public void testSources() throws Exception JavaDoc {
56         Sources s = ProjectUtils.getSources(simple);
57         SourceGroup[] groups = s.getSourceGroups(Sources.TYPE_GENERIC);
58         assertEquals("one generic group", 1, groups.length);
59         assertEquals("right root folder", simple.getProjectDirectory(), groups[0].getRootFolder());
60         assertEquals("right display name", "Simple Freeform Project", groups[0].getDisplayName());
61         groups = s.getSourceGroups("java");
62         assertEquals("two Java groups", 2, groups.length);
63         assertEquals("right root folder #1", simple.getProjectDirectory().getFileObject("src"), groups[0].getRootFolder());
64         assertEquals("right display name #1", "Main Sources", groups[0].getDisplayName());
65         assertEquals("right root folder #2", simple.getProjectDirectory().getFileObject("antsrc"), groups[1].getRootFolder());
66         assertEquals("right display name #2", "Ant Task Sources", groups[1].getDisplayName());
67     }
68     
69     public void testExternalSourceRoot() throws Exception JavaDoc {
70         Sources s = ProjectUtils.getSources(extsrcroot);
71         SourceGroup[] groups = s.getSourceGroups(Sources.TYPE_GENERIC);
72         assertEquals("one generic group", 1, groups.length);
73         assertEquals("right root folder", egdirFO.getFileObject("extsrcroot"), groups[0].getRootFolder());
74         assertEquals("right display name", "Top-Level Dir", groups[0].getDisplayName());
75         groups = s.getSourceGroups("java");
76         assertEquals("one Java group", 1, groups.length);
77         assertEquals("right root folder", egdirFO.getFileObject("extsrcroot/src"), groups[0].getRootFolder());
78         assertEquals("right display name", "External Sources", groups[0].getDisplayName());
79         assertEquals("correct file owner", extsrcroot, FileOwnerQuery.getOwner(egdirFO.getFileObject("extsrcroot/src/org/foo/Foo.java")));
80     }
81     
82     public void testSourceRootChanges() throws Exception JavaDoc {
83         Sources s = ProjectUtils.getSources(extsrcroot);
84         SourceGroup[] groups = s.getSourceGroups("java");
85         assertEquals("one Java group", 1, groups.length);
86         assertEquals("right root folder", egdirFO.getFileObject("extsrcroot/src"), groups[0].getRootFolder());
87         TestCL l = new TestCL();
88         s.addChangeListener(l);
89         Element JavaDoc data = extsrcroot.getPrimaryConfigurationData();
90         Element JavaDoc folders = Util.findElement(data, "folders", FreeformProjectType.NS_GENERAL);
91         assertNotNull("have <folders>", folders);
92         List JavaDoc/*<Element>*/ sourceFolders = Util.findSubElements(folders);
93         assertEquals("have 2 <source-folder>s", 2, sourceFolders.size());
94         Element JavaDoc sourceFolder = (Element JavaDoc) sourceFolders.get(1);
95         Element JavaDoc location = Util.findElement(sourceFolder, "location", FreeformProjectType.NS_GENERAL);
96         assertNotNull("have <location>", location);
97         NodeList JavaDoc nl = location.getChildNodes();
98         assertEquals("one child (text)", 1, nl.getLength());
99         location.removeChild(nl.item(0));
100         location.appendChild(location.getOwnerDocument().createTextNode("../src2"));
101         extsrcroot.putPrimaryConfigurationData(data);
102         assertEquals("got a change in Sources", 1, l.changeCount());
103         groups = s.getSourceGroups("java");
104         assertEquals("one Java group", 1, groups.length);
105         assertEquals("right root folder", egdirFO.getFileObject("extsrcroot/src2"), groups[0].getRootFolder());
106     }
107     
108     public void testExternalBuildRoot() throws Exception JavaDoc {
109         // Check that <build-folder> works.
110
FileObject builtFile = egdirFO.getFileObject("extbuildroot/build/built.file");
111         assertNotNull("have built.file", builtFile);
112         assertEquals("owned by extbuildroot project", extbuildroot, FileOwnerQuery.getOwner(builtFile));
113     }
114
115     public void testIncludesExcludes() throws Exception JavaDoc {
116         clearWorkDir();
117         File JavaDoc d = getWorkDir();
118         AntProjectHelper helper = FreeformProjectGenerator.createProject(d, d, "prj", null);
119         Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory());
120         FileUtil.createData(new File JavaDoc(d, "s/relevant/included/file"));
121         FileUtil.createData(new File JavaDoc(d, "s/relevant/excluded/file"));
122         FileUtil.createData(new File JavaDoc(d, "s/ignored/file"));
123         Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
124         Document JavaDoc doc = data.getOwnerDocument();
125         Element JavaDoc sf = (Element JavaDoc) data.insertBefore(doc.createElementNS(Util.NAMESPACE, "folders"), Util.findElement(data, "view", Util.NAMESPACE)).
126                 appendChild(doc.createElementNS(Util.NAMESPACE, "source-folder"));
127         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "label")).appendChild(doc.createTextNode("Sources"));
128         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "type")).appendChild(doc.createTextNode("stuff"));
129         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "location")).appendChild(doc.createTextNode("s"));
130         Util.putPrimaryConfigurationData(helper, data);
131         ProjectManager.getDefault().saveProject(p);
132         Sources s = ProjectUtils.getSources(p);
133         SourceGroup[] gs = s.getSourceGroups("stuff");
134         assertEquals(1, gs.length);
135         assertEquals(FileUtil.toFileObject(new File JavaDoc(d, "s")), gs[0].getRootFolder());
136         assertEquals("Sources", gs[0].getDisplayName());
137         assertEquals("ignored{file} relevant{excluded{file} included{file}}", expand(gs[0]));
138         // Now configure includes and excludes.
139
EditableProperties ep = new EditableProperties();
140         ep.put("includes", "relevant/");
141         ep.put("excludes", "**/excluded/");
142         helper.putProperties("config.properties", ep);
143         data = Util.getPrimaryConfigurationData(helper);
144         doc = data.getOwnerDocument();
145         data.getElementsByTagName("properties").item(0).
146                 appendChild(doc.createElementNS(Util.NAMESPACE, "property-file")).
147                 appendChild(doc.createTextNode("config.properties"));
148         Util.putPrimaryConfigurationData(helper, data);
149         ProjectManager.getDefault().saveProject(p);
150         data = Util.getPrimaryConfigurationData(helper);
151         doc = data.getOwnerDocument();
152         sf = (Element JavaDoc) data.getElementsByTagName("source-folder").item(0);
153         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "includes")).
154                 appendChild(doc.createTextNode("${includes}"));
155         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "excludes")).
156                 appendChild(doc.createTextNode("${excludes}"));
157         Util.putPrimaryConfigurationData(helper, data);
158         ProjectManager.getDefault().saveProject(p);
159         gs = s.getSourceGroups("stuff");
160         assertEquals("relevant{included{file}}", expand(gs[0]));
161         // Now change them.
162
TestPCL l = new TestPCL();
163         gs[0].addPropertyChangeListener(l);
164         ep = helper.getProperties("config.properties");
165         ep.remove("includes");
166         helper.putProperties("config.properties", ep);
167         ProjectManager.getDefault().saveProject(p);
168         assertEquals("ignored{file} relevant{included{file}}", expand(gs[0]));
169         assertEquals(Collections.singleton(SourceGroup.PROP_CONTAINERSHIP), l.changed);
170     }
171     private static String JavaDoc expand(SourceGroup g) {
172         return expand(g, g.getRootFolder());
173     }
174     private static String JavaDoc expand(SourceGroup g, FileObject d) {
175         SortedSet JavaDoc<String JavaDoc> subs = new TreeSet JavaDoc<String JavaDoc>();
176         for (FileObject kid : d.getChildren()) {
177             if (!g.contains(kid)) {
178                 continue;
179             }
180             String JavaDoc sub = kid.getNameExt();
181             if (kid.isFolder()) {
182                 sub += '{' + expand(g, kid) + '}';
183             }
184             subs.add(sub);
185         }
186         StringBuilder JavaDoc b = new StringBuilder JavaDoc();
187         for (String JavaDoc sub : subs) {
188             if (b.length() > 0) {
189                 b.append(' ');
190             }
191             b.append(sub);
192         }
193         return b.toString();
194     }
195     
196 }
197
Popular Tags