KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > ModuleLogicalViewTest


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.apisupport.project.ui;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import java.util.List JavaDoc;
25 import org.netbeans.api.project.Project;
26 import org.netbeans.api.project.ProjectManager;
27 import org.netbeans.modules.apisupport.project.TestBase;
28 import org.netbeans.spi.project.ui.LogicalViewProvider;
29 import org.openide.filesystems.FileObject;
30 import org.openide.filesystems.FileUtil;
31 import org.openide.loaders.DataObject;
32 import org.openide.nodes.Children;
33 import org.openide.nodes.FilterNode;
34 import org.openide.nodes.Node;
35
36 /**
37  * Test functionality of {@link ModuleLogicalView}.
38  * @author Jesse Glick
39  */

40 public class ModuleLogicalViewTest extends TestBase {
41     
42     public ModuleLogicalViewTest(String JavaDoc name) {
43         super(name);
44     }
45     
46     public void testFindPath() throws Exception JavaDoc {
47         Project freeform = ProjectManager.getDefault().findProject(FileUtil.toFileObject(file("ant/freeform")));
48         assertNotNull("have project in ant/freeform", freeform);
49         LogicalViewProvider lvp = (LogicalViewProvider) freeform.getLookup().lookup(LogicalViewProvider.class);
50         assertNotNull("have a LogicalViewProvider", lvp);
51         assertNotNull("found arch.xml", find(lvp, "ant/freeform/arch.xml"));
52         assertNotNull("found FreeformProject.java", find(lvp, "ant/freeform/src/org/netbeans/modules/ant/freeform/FreeformProject.java"));
53         assertNotNull("found freeform-project-general.xsd", find(lvp, "ant/freeform/src/org/netbeans/modules/ant/freeform/resources/freeform-project-general.xsd"));
54         assertNotNull("found FreeformProjectTest.java", find(lvp, "ant/freeform/test/unit/src/org/netbeans/modules/ant/freeform/FreeformProjectTest.java"));
55         assertNull("did not find test/cfg-unit.xml", find(lvp, "ant/freeform/test/cfg-unit.xml"));
56         // XXX test that layer.xml is found under Original Files, not Sources
57
}
58     
59     public void testImportantFilesListening() throws Exception JavaDoc {
60         Project p = generateStandaloneModule("module");
61         LogicalViewProvider lvp = (LogicalViewProvider) p.getLookup().lookup(LogicalViewProvider.class);
62         assertNotNull("have a LogicalViewProvider", lvp);
63         Node root = lvp.createLogicalView();
64         Node iFiles = root.getChildren().findChild(ImportantFilesNodeFactory.IMPORTANT_FILES_NAME);
65         assertNotNull("have the Important Files node", iFiles);
66         iFiles.getChildren().getNodes(true); // ping
67
waitForChildrenUpdate();
68         assertEquals("five important files", 5, iFiles.getChildren().getNodes(true).length);
69         FileUtil.createData(p.getProjectDirectory(), "nbproject/project.properties");
70         iFiles.getChildren().getNodes(true); // ping
71
waitForChildrenUpdate();
72         assertEquals("nbproject/project.properties noticed", 6, iFiles.getChildren().getNodes(true).length);
73     }
74     
75     private Node find(LogicalViewProvider lvp, String JavaDoc path) throws Exception JavaDoc {
76         FileObject f = FileUtil.toFileObject(file(path));
77         assertNotNull("found " + path, f);
78         Node root = new FilterNode(lvp.createLogicalView());
79         
80         lvp.findPath(root, f); // ping
81
waitForChildrenUpdate();
82         
83         Node n = lvp.findPath(root, f);
84         DataObject d = DataObject.find(f);
85         assertEquals("same result for DataObject as for FileObject", n, lvp.findPath(root, d));
86         if (n != null) {
87             assertEquals("right DataObject", d, n.getLookup().lookup(DataObject.class));
88         }
89         return n;
90     }
91     
92     private void waitForChildrenUpdate() {
93         ImportantFilesNodeFactory.RP.post(new Runnable JavaDoc() {
94             public void run() {
95                 // flush ModuleLogicalView.RP under which is the Children's update run
96
}
97         }).waitFinished();
98     }
99     
100     public void testNewlyCreatedSourceRootsDisplayed() throws Exception JavaDoc { // #72476
101
Project p = generateStandaloneModule("module");
102         LogicalViewProvider lvp = (LogicalViewProvider) p.getLookup().lookup(LogicalViewProvider.class);
103         Node root = lvp.createLogicalView();
104         p.getProjectDirectory().getFileObject("test").delete();
105         Children ch = root.getChildren();
106         assertEquals(Arrays.asList(new String JavaDoc[] {"${src.dir}", "important.files", "libraries"}), findKids(ch));
107         /* XXX does not work reliably; ChildrenArray.finalize removes listener!
108         final boolean[] added = new boolean[1];
109         root.addNodeListener(new NodeAdapter() {
110             public void childrenAdded(NodeMemberEvent ev) {
111                 added[0] = true;
112             }
113         });
114          */

115         p.getProjectDirectory().createFolder("javahelp");
116         //assertTrue("got node added event", added[0]);
117
assertEquals(Arrays.asList(new String JavaDoc[] {"${src.dir}", "javahelp", "important.files", "libraries"}), findKids(ch));
118     }
119     
120     private static List JavaDoc/*<String>*/ findKids(Children ch) {
121         List JavaDoc l = new ArrayList JavaDoc();
122         Node[] kids = ch.getNodes(true);
123         for (int i = 0; i < kids.length; i++) {
124             l.add(kids[i].getName());
125         }
126         return l;
127     }
128     
129 }
130
Popular Tags