KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.EventQueue JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Locale JavaDoc;
27 import java.util.Set JavaDoc;
28 import org.netbeans.api.project.ProjectManager;
29 import org.netbeans.modules.apisupport.project.TestBase;
30 import org.netbeans.modules.apisupport.project.suite.SuiteProject;
31 import org.netbeans.modules.apisupport.project.suite.SuiteProjectTest;
32 import org.netbeans.modules.apisupport.project.ui.SuiteLogicalView.SuiteRootNode;
33 import org.netbeans.spi.project.support.ant.AntProjectHelper;
34 import org.netbeans.spi.project.support.ant.EditableProperties;
35 import org.netbeans.spi.project.ui.LogicalViewProvider;
36 import org.openide.filesystems.FileObject;
37 import org.openide.loaders.DataObject;
38 import org.openide.nodes.Node;
39 import org.openide.nodes.NodeAdapter;
40 import org.openide.util.Mutex;
41 import org.openide.util.RequestProcessor;
42
43 /**
44  * Test functionality of {@link SuiteLogicalView}.
45  *
46  * @author Martin Krauskopf
47  */

48 public class SuiteLogicalViewTest extends TestBase {
49     
50     public SuiteLogicalViewTest(String JavaDoc name) {
51         super(name);
52     }
53     
54     public void testModulesNode() throws Exception JavaDoc {
55         SuiteProject suite1 = generateSuite("suite1");
56         TestBase.generateSuiteComponent(suite1, "module1a");
57         Node modulesNode = new ModulesNodeFactory.ModulesNode(suite1);
58         modulesNode.getChildren().getNodes(true); // "expand the node" simulation
59
waitForGUIUpdate();
60         assertEquals("one children", 1, modulesNode.getChildren().getNodes(true).length);
61         
62         final ModulesNodeFactory.ModulesNode.ModuleChildren children = (ModulesNodeFactory.ModulesNode.ModuleChildren) modulesNode.getChildren();
63         TestBase.generateSuiteComponent(suite1, "module1b");
64         waitForGUIUpdate();
65         assertEquals("two children", 2, children.getNodes(true).length);
66         TestBase.generateSuiteComponent(suite1, "module1c");
67         ProjectManager.mutex().writeAccess(new Mutex.Action<Void JavaDoc>() {
68             public Void JavaDoc run() {
69                 children.stateChanged(null); // #70914
70
return null; // #70914
71
}
72         });
73         waitForGUIUpdate();
74         assertEquals("three children", 3, children.getNodes(true).length);
75     }
76     
77     public void testNameAndDisplayName() throws Exception JavaDoc {
78         SuiteProject p = generateSuite("Sweet Stuff");
79         Node n = p.getLookup().lookup(LogicalViewProvider.class).createLogicalView();
80         assertEquals("Sweet Stuff", n.getName());
81         assertEquals("Sweet Stuff", n.getDisplayName());
82         NL nl = new NL();
83         n.addNodeListener(nl);
84         EditableProperties ep = p.getHelper().getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH);
85         ep.setProperty("app.name", "sweetness");
86         ep.setProperty("app.title", "Sweetness is Now!");
87         p.getHelper().putProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH, ep);
88         assertEquals(new HashSet JavaDoc<String JavaDoc>(Arrays.asList(Node.PROP_NAME, Node.PROP_DISPLAY_NAME)), nl.changed);
89         assertEquals("Sweetness is Now!", n.getName());
90         assertEquals("Sweetness is Now!", n.getDisplayName());
91     }
92     
93     public void testProjectFiles() throws Exception JavaDoc {
94         SuiteProject suite = generateSuite("suite");
95         TestBase.generateSuiteComponent(suite, "module");
96         SuiteProjectTest.openSuite(suite);
97         SuiteRootNode rootNode = (SuiteRootNode) suite.getLookup().lookup(LogicalViewProvider.class).createLogicalView();
98         Set JavaDoc<FileObject> expected = new HashSet JavaDoc<FileObject>(Arrays.asList(
99             suite.getProjectDirectory().getFileObject("nbproject"),
100             suite.getProjectDirectory().getFileObject("build.xml")
101         ));
102         assertTrue(expected.equals(rootNode.getProjectFiles()));
103     }
104     
105     public void testImportantFiles() throws Exception JavaDoc {
106         // so getDisplayName is taken from english bundle
107
Locale.setDefault(Locale.US);
108         
109         SuiteProject suite = generateSuite("sweet");
110         FileObject master = suite.getProjectDirectory().createData("master.jnlp");
111         
112         LogicalViewProvider viewProv = suite.getLookup().lookup(LogicalViewProvider.class);
113         Node n = viewProv.createLogicalView();
114         
115         Node[] nodes = n.getChildren().getNodes(true);
116         assertEquals("Now there are two", 2, nodes.length);
117         assertEquals("Named modules", "modules", nodes[0].getName());
118         assertEquals("Named imp files", "important.files", nodes[1].getName());
119         
120         FileObject projProps = suite.getProjectDirectory().getFileObject("nbproject/project.properties");
121         assertNotNull(projProps);
122         viewProv.findPath(n, projProps); // ping
123
flushRequestProcessor();
124         Node nodeForFO = viewProv.findPath(n, projProps);
125         
126         assertNotNull("found project.properties node", nodeForFO);
127         assertEquals("Name of node is localized", "Project Properties", nodeForFO.getDisplayName());
128         
129         nodeForFO = viewProv.findPath(n, master);
130         assertNotNull("found master.jnlp node", nodeForFO);
131         assertEquals("same by DataObject", nodeForFO, viewProv.findPath(n, DataObject.find(master)));
132         assertEquals("Name of node is localized", "JNLP Descriptor", nodeForFO.getDisplayName());
133         
134         master.delete();
135         
136         nodeForFO = viewProv.findPath(n, master);
137         assertNull("For file object null", nodeForFO);
138     }
139     
140     private static final class NL extends NodeAdapter {
141         public final Set JavaDoc<String JavaDoc> changed = new HashSet JavaDoc<String JavaDoc>();
142         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
143             changed.add(evt.getPropertyName());
144         }
145     }
146     
147     private void waitForGUIUpdate() throws Exception JavaDoc {
148         EventQueue.invokeAndWait(new Runnable JavaDoc() { public void run() {} });
149     }
150     
151     private void flushRequestProcessor() {
152         RequestProcessor.getDefault().post(new Runnable JavaDoc() {
153             public void run() {
154                 // flush
155
}
156         }).waitFinished();
157     }
158     
159 }
160
Popular Tags