001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.ui;
043:
044: import java.util.ArrayList;
045: import java.util.Arrays;
046: import java.util.List;
047: import org.netbeans.api.project.Project;
048: import org.netbeans.api.project.ProjectManager;
049: import org.netbeans.modules.apisupport.project.TestBase;
050: import org.netbeans.spi.project.ui.LogicalViewProvider;
051: import org.openide.filesystems.FileObject;
052: import org.openide.filesystems.FileUtil;
053: import org.openide.loaders.DataObject;
054: import org.openide.nodes.Children;
055: import org.openide.nodes.FilterNode;
056: import org.openide.nodes.Node;
057:
058: /**
059: * Test functionality of {@link ModuleLogicalView}.
060: * @author Jesse Glick
061: */
062: public class ModuleLogicalViewTest extends TestBase {
063:
064: public ModuleLogicalViewTest(String name) {
065: super (name);
066: }
067:
068: public void testFindPath() throws Exception {
069: Project freeform = ProjectManager.getDefault().findProject(
070: FileUtil.toFileObject(file("ant.freeform")));
071: assertNotNull("have project in ant.freeform", freeform);
072: LogicalViewProvider lvp = freeform.getLookup().lookup(
073: LogicalViewProvider.class);
074: assertNotNull("have a LogicalViewProvider", lvp);
075: assertNotNull("found arch.xml", find(lvp,
076: "ant.freeform/arch.xml"));
077: assertNotNull(
078: "found FreeformProject.java",
079: find(lvp,
080: "ant.freeform/src/org/netbeans/modules/ant/freeform/FreeformProject.java"));
081: assertNotNull(
082: "found freeform-project-general.xsd",
083: find(
084: lvp,
085: "ant.freeform/src/org/netbeans/modules/ant/freeform/resources/freeform-project-general.xsd"));
086: assertNotNull(
087: "found FreeformProjectTest.java",
088: find(
089: lvp,
090: "ant.freeform/test/unit/src/org/netbeans/modules/ant/freeform/FreeformProjectTest.java"));
091: assertNull("did not find test/cfg-unit.xml", find(lvp,
092: "ant.freeform/test/cfg-unit.xml"));
093: Node layer = find(lvp,
094: "ant.freeform/src/org/netbeans/modules/ant/freeform/resources/layer.xml");
095: assertNotNull("Found layer", layer);
096: assertEquals("Sources is parent parent of the layer",
097: "${src.dir}", layer.getParentNode().getParentNode()
098: .getName());
099: assertFalse("Has children: " + layer, layer.isLeaf());
100: DataObject obj = layer.getLookup().lookup(DataObject.class);
101: assertNotNull("There is a data object", obj);
102: Node origLayer = obj.getNodeDelegate();
103: assertFalse("Also has children", origLayer.isLeaf());
104: }
105:
106: public void testImportantFilesListening() throws Exception {
107: Project p = generateStandaloneModule("module");
108: LogicalViewProvider lvp = p.getLookup().lookup(
109: LogicalViewProvider.class);
110: assertNotNull("have a LogicalViewProvider", lvp);
111: Node root = lvp.createLogicalView();
112: Node iFiles = root.getChildren().findChild(
113: ImportantFilesNodeFactory.IMPORTANT_FILES_NAME);
114: assertNotNull("have the Important Files node", iFiles);
115: FileObject propsFO = p.getProjectDirectory().getFileObject(
116: "nbproject/project.properties");
117: propsFO = FileUtil.moveFile(propsFO, p.getProjectDirectory()
118: .getFileObject("nbproject"), "project-bck");
119: TestBase.assertAsynchronouslyUpdatedChildrenNodes(iFiles, 5);
120: FileUtil.moveFile(propsFO, p.getProjectDirectory()
121: .getFileObject("nbproject"), "project");
122: TestBase.assertAsynchronouslyUpdatedChildrenNodes(iFiles, 6);
123: }
124:
125: private Node find(LogicalViewProvider lvp, String path)
126: throws Exception {
127: FileObject f = FileUtil.toFileObject(file(path));
128: assertNotNull("found " + path, f);
129: Node root = new FilterNode(lvp.createLogicalView());
130:
131: lvp.findPath(root, f); // ping
132: waitForChildrenUpdate();
133:
134: Node n = lvp.findPath(root, f);
135: DataObject d = DataObject.find(f);
136: assertEquals("same result for DataObject as for FileObject", n,
137: lvp.findPath(root, d));
138: if (n != null) {
139: assertEquals("right DataObject", d, n.getLookup().lookup(
140: DataObject.class));
141: }
142: return n;
143: }
144:
145: private void waitForChildrenUpdate() {
146: ImportantFilesNodeFactory.RP.post(new Runnable() {
147: public void run() {
148: // flush ModuleLogicalView.RP under which is the Children's update run
149: }
150: }).waitFinished();
151: }
152:
153: public void testNewlyCreatedSourceRootsDisplayed() throws Exception { // #72476
154: Project p = generateStandaloneModule("module");
155: LogicalViewProvider lvp = p.getLookup().lookup(
156: LogicalViewProvider.class);
157: Node root = lvp.createLogicalView();
158: p.getProjectDirectory().getFileObject("test").delete();
159: Children ch = root.getChildren();
160: assertEquals(Arrays.asList(new String[] { "${src.dir}",
161: "important.files", "libraries" }), findKids(ch));
162: /* XXX does not work reliably; ChildrenArray.finalize removes listener!
163: final boolean[] added = new boolean[1];
164: root.addNodeListener(new NodeAdapter() {
165: public void childrenAdded(NodeMemberEvent ev) {
166: added[0] = true;
167: }
168: });
169: */
170: p.getProjectDirectory().createFolder("javahelp");
171: //assertTrue("got node added event", added[0]);
172: assertEquals(Arrays.asList(new String[] { "${src.dir}",
173: "javahelp", "important.files", "libraries" }),
174: findKids(ch));
175: }
176:
177: private static List<String> findKids(Children ch) {
178: List<String> l = new ArrayList<String>();
179: Node[] kids = ch.getNodes(true);
180: for (int i = 0; i < kids.length; i++) {
181: l.add(kids[i].getName());
182: }
183: return l;
184: }
185:
186: }
|