KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > ant > freeform > ui > ViewTest


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.ui;
21
22 import java.beans.PropertyChangeEvent JavaDoc;
23 import java.io.File JavaDoc;
24 import java.util.HashSet JavaDoc;
25 import java.util.Set JavaDoc;
26 import java.util.SortedSet JavaDoc;
27 import java.util.TreeSet JavaDoc;
28 import org.netbeans.api.project.Project;
29 import org.netbeans.api.project.ProjectManager;
30 import org.netbeans.junit.NbTestCase;
31 import org.netbeans.modules.ant.freeform.FreeformProjectGenerator;
32 import org.netbeans.modules.ant.freeform.FreeformProjectType;
33 import org.netbeans.modules.ant.freeform.TestBase;
34 import org.netbeans.modules.ant.freeform.spi.support.Util;
35 import org.netbeans.spi.project.support.ant.AntProjectHelper;
36 import org.netbeans.spi.project.support.ant.EditableProperties;
37 import org.netbeans.spi.project.ui.LogicalViewProvider;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.loaders.DataObject;
41 import org.openide.modules.ModuleInfo;
42 import org.openide.nodes.Children;
43 import org.openide.nodes.Node;
44 import org.openide.nodes.NodeEvent;
45 import org.openide.nodes.NodeListener;
46 import org.openide.nodes.NodeMemberEvent;
47 import org.openide.nodes.NodeReorderEvent;
48 import org.w3c.dom.Document JavaDoc;
49 import org.openide.util.Lookup;
50 import org.w3c.dom.Element JavaDoc;
51 import org.w3c.dom.NodeList JavaDoc;
52
53 // XXX testRootNodeDisplayNameChange
54

55 /**
56  * Test {@link View}: changes in children etc.
57  * @author Jesse Glick
58  */

59 public class ViewTest extends TestBase {
60     
61     public ViewTest(String JavaDoc name) {
62         super(name);
63     }
64     
65     private LogicalViewProvider lvp;
66     
67     protected void setUp() throws Exception JavaDoc {
68         super.setUp();
69         ModuleInfo info = Lookup.getDefault().lookup(ModuleInfo.class);
70         lvp = extsrcroot.getLookup().lookup(LogicalViewProvider.class);
71         assertNotNull("found a LogicalViewProvider", lvp);
72     }
73     
74     public void testViewItemBasic() throws Exception JavaDoc {
75         Node root = lvp.createLogicalView();
76         assertEquals("lookup has project", extsrcroot, root.getLookup().lookup(Project.class));
77         Children ch = root.getChildren();
78         Node[] kids = ch.getNodes(true);
79         assertEquals("two child nodes", 2, kids.length);
80         // Do not check anything about #1, since it is provided by java/freeform.
81
assertEquals("correct code name #2", "nbproject/project.xml", kids[1].getName());
82         assertEquals("correct display name #2", "project.xml", kids[1].getDisplayName());
83         assertEquals("correct cookie #2",
84                 DataObject.find(egdirFO.getFileObject("extsrcroot/proj/nbproject/project.xml")),
85                 kids[1].getLookup().lookup(DataObject.class));
86     }
87     
88     public void testViewItemChanges() throws Exception JavaDoc {
89         Node root = lvp.createLogicalView();
90         Children ch = root.getChildren();
91         Node[] kids = ch.getNodes(true);
92         assertEquals("two child nodes", 2, kids.length);
93         assertEquals("correct code name #1", "../src", kids[0].getName());
94         assertEquals("correct code name #2", "nbproject/project.xml", kids[1].getName());
95         TestNL l = new TestNL();
96         root.addNodeListener(l);
97         Element JavaDoc data = extsrcroot.getPrimaryConfigurationData();
98         Element JavaDoc view = Util.findElement(data, "view", FreeformProjectType.NS_GENERAL);
99         assertNotNull("have <view>", view);
100         Element JavaDoc items = Util.findElement(view, "items", FreeformProjectType.NS_GENERAL);
101         assertNotNull("have <items>", items);
102         Element JavaDoc sourceFolder = Util.findElement(items, "source-folder", FreeformProjectType.NS_GENERAL);
103         assertNotNull("have <source-folder>", sourceFolder);
104         Element JavaDoc location = Util.findElement(sourceFolder, "location", FreeformProjectType.NS_GENERAL);
105         assertNotNull("have <location>", location);
106         NodeList JavaDoc nl = location.getChildNodes();
107         assertEquals("one child", 1, nl.getLength());
108         location.removeChild(nl.item(0));
109         location.appendChild(location.getOwnerDocument().createTextNode("../src2"));
110         Element JavaDoc sourceFile = Util.findElement(items, "source-file", FreeformProjectType.NS_GENERAL);
111         assertNotNull("have <source-file>", sourceFile);
112         items.removeChild(sourceFile);
113         extsrcroot.putPrimaryConfigurationData(data);
114         // children keys are updated asynchronously. give them a time
115
Thread.sleep(500);
116         assertFalse("got some changes in children", l.probeChanges().isEmpty());
117         kids = ch.getNodes(true);
118         assertEquals("one child node", 1, kids.length);
119         assertEquals("correct code name #1", "../src2", kids[0].getName());
120         assertEquals("correct display name #1", "External Sources", kids[0].getDisplayName());
121         assertEquals("correct cookie #1",
122                 DataObject.find(egdirFO.getFileObject("extsrcroot/src2")),
123                 kids[0].getLookup().lookup(DataObject.class));
124     }
125     
126     public void testFindPath() throws Exception JavaDoc {
127         // Do not test packages style - provided only by java/freeform.
128
LogicalViewProvider lvp2 = simple.getLookup().lookup(LogicalViewProvider.class);
129         assertNotNull(lvp2);
130         Node root = lvp2.createLogicalView();
131         doTestFindPathPositive(lvp2, root, simple, "xdocs/foo.xml");
132         doTestFindPathPositive(lvp2, root, simple, "xdocs");
133         doTestFindPathPositive(lvp2, root, simple, "build.properties");
134         doTestFindPathPositive(lvp2, root, simple, "build.xml");
135         doTestFindPathNegative(lvp2, root, simple, "nbproject/project.xml");
136         doTestFindPathNegative(lvp2, root, simple, "nbproject");
137     }
138     
139     public static void doTestFindPathPositive(LogicalViewProvider lvp, Node root, Project project, String JavaDoc path) throws Exception JavaDoc {
140         FileObject file = project.getProjectDirectory().getFileObject(path);
141         assertNotNull("found " + path, file);
142         DataObject d = DataObject.find(file);
143         Node nDO = lvp.findPath(root, d);
144         Node nFO = lvp.findPath(root, file);
145         assertNotNull("found node for " + path, nDO);
146         assertNotNull("found node for " + path, nFO);
147         assertEquals("correct node", d, nDO.getLookup().lookup(DataObject.class));
148         //not exactly fullfilling the contract:
149
assertEquals("correct node", d, nFO.getLookup().lookup(DataObject.class));
150     }
151     
152     public static void doTestFindPathNegative(LogicalViewProvider lvp, Node root, Project project, String JavaDoc path) throws Exception JavaDoc {
153         FileObject file = project.getProjectDirectory().getFileObject(path);
154         assertNotNull("found " + path, file);
155         DataObject d = DataObject.find(file);
156         Node n = lvp.findPath(root, d);
157         assertNull("did not find node for " + path, n);
158     }
159     
160     public static void doTestIncludesExcludes(NbTestCase test, String JavaDoc style, String JavaDoc appearanceEverything, String JavaDoc appearanceIncludesExcludes, String JavaDoc appearanceExcludes, String JavaDoc appearanceFloating) throws Exception JavaDoc {
161         FolderNodeFactory.synchronous = true;
162         test.clearWorkDir();
163         File JavaDoc d = test.getWorkDir();
164         AntProjectHelper helper = FreeformProjectGenerator.createProject(d, d, "prj", null);
165         Project p = ProjectManager.getDefault().findProject(helper.getProjectDirectory());
166         FileUtil.createData(new File JavaDoc(d, "s/relevant/included/file"));
167         FileUtil.createData(new File JavaDoc(d, "s/relevant/excluded/file"));
168         FileUtil.createData(new File JavaDoc(d, "s/ignored/file"));
169         Element JavaDoc data = Util.getPrimaryConfigurationData(helper);
170         Document JavaDoc doc = data.getOwnerDocument();
171         Element JavaDoc items = (Element JavaDoc) data.getElementsByTagName("items").item(0);
172         items.removeChild(items.getElementsByTagName("source-file").item(0)); // build.xml
173
Element JavaDoc sf = (Element JavaDoc) items.appendChild(doc.createElementNS(Util.NAMESPACE, "source-folder"));
174         sf.setAttribute("style", style);
175         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "location")).appendChild(doc.createTextNode("s"));
176         Util.putPrimaryConfigurationData(helper, data);
177         ProjectManager.getDefault().saveProject(p);
178         Node r = p.getLookup().lookup(LogicalViewProvider.class).createLogicalView();
179         assertEquals(appearanceEverything, expand(r));
180         // Now configure includes and excludes.
181
EditableProperties ep = new EditableProperties();
182         ep.put("includes", "relevant/");
183         ep.put("excludes", "**/excluded/");
184         helper.putProperties("config.properties", ep);
185         data = Util.getPrimaryConfigurationData(helper);
186         doc = data.getOwnerDocument();
187         data.getElementsByTagName("properties").item(0).
188                 appendChild(doc.createElementNS(Util.NAMESPACE, "property-file")).
189                 appendChild(doc.createTextNode("config.properties"));
190         Util.putPrimaryConfigurationData(helper, data);
191         ProjectManager.getDefault().saveProject(p);
192         data = Util.getPrimaryConfigurationData(helper);
193         doc = data.getOwnerDocument();
194         sf = (Element JavaDoc) data.getElementsByTagName("source-folder").item(0);
195         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "includes")).
196                 appendChild(doc.createTextNode("${includes}"));
197         sf.appendChild(doc.createElementNS(Util.NAMESPACE, "excludes")).
198                 appendChild(doc.createTextNode("${excludes}"));
199         Util.putPrimaryConfigurationData(helper, data);
200         ProjectManager.getDefault().saveProject(p);
201         assertEquals(appearanceIncludesExcludes, expand(r));
202         // Now change them.
203
ep = helper.getProperties("config.properties");
204         ep.remove("includes");
205         helper.putProperties("config.properties", ep);
206         ProjectManager.getDefault().saveProject(p);
207         assertEquals(appearanceExcludes, expand(r));
208         // Also check floating includes.
209
ep = helper.getProperties("config.properties");
210         ep.put("includes", "relevant/included/");
211         helper.putProperties("config.properties", ep);
212         ProjectManager.getDefault().saveProject(p);
213         assertEquals(appearanceFloating, expand(r));
214     }
215     public void testIncludesExcludes() throws Exception JavaDoc {
216         doTestIncludesExcludes(this, "tree",
217                 "prj{s{ignored{file} relevant{excluded{file} included{file}}}}",
218                 "prj{s{relevant{included{file}}}}",
219                 "prj{s{ignored{file} relevant{included{file}}}}",
220                 "prj{s{relevant{included{file}}}}");
221     }
222     private static String JavaDoc expand(Node n) {
223         Node[] kids = n.getChildren().getNodes(true);
224         String JavaDoc nm = n.getDisplayName();
225         if (kids.length == 0) {
226             return nm;
227         } else {
228             SortedSet JavaDoc<String JavaDoc> under = new TreeSet JavaDoc<String JavaDoc>();
229             for (Node kid : kids) {
230                 under.add(expand(kid));
231             }
232             StringBuilder JavaDoc b = new StringBuilder JavaDoc(nm).append('{');
233             boolean first = true;
234             for (String JavaDoc s : under) {
235                 if (first) {
236                     first = false;
237                 } else {
238                     b.append(' ');
239                 }
240                 b.append(s);
241             }
242             return b.append('}').toString();
243         }
244     }
245     
246     private static final class TestNL implements NodeListener {
247         private final Set JavaDoc<String JavaDoc> changes = new HashSet JavaDoc<String JavaDoc>();
248         public TestNL() {}
249         public synchronized void childrenRemoved(NodeMemberEvent ev) {
250             changes.add("childrenRemoved");
251         }
252         public synchronized void childrenAdded(NodeMemberEvent ev) {
253             changes.add("childrenAdded");
254         }
255         public synchronized void childrenReordered(NodeReorderEvent ev) {
256             changes.add("childrenReordered");
257         }
258         public synchronized void nodeDestroyed(NodeEvent ev) {
259             changes.add("nodeDestroyed");
260         }
261         public synchronized void propertyChange(PropertyChangeEvent JavaDoc propertyChangeEvent) {
262             changes.add(propertyChangeEvent.getPropertyName());
263         }
264         /** Get a set of all change event names since the last call. Clears set too. */
265         public synchronized Set JavaDoc<String JavaDoc> probeChanges() {
266             Set JavaDoc<String JavaDoc> _changes = new HashSet JavaDoc<String JavaDoc>(changes);
267             changes.clear();
268             return _changes;
269         }
270     }
271     
272 }
273
Popular Tags