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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * Portions Copyrighted 2007 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.project.ui;
041:
042: import java.awt.EventQueue;
043: import java.beans.PropertyChangeEvent;
044: import java.net.URL;
045: import java.util.ArrayList;
046: import java.util.EventObject;
047: import java.util.List;
048: import java.util.concurrent.CountDownLatch;
049: import java.util.concurrent.ExecutionException;
050: import java.util.concurrent.TimeUnit;
051: import java.util.concurrent.TimeoutException;
052: import javax.swing.Action;
053: import junit.framework.TestCase;
054: import org.netbeans.api.project.Project;
055: import org.netbeans.api.project.ProjectManager;
056: import org.netbeans.api.project.ui.OpenProjects;
057: import org.netbeans.junit.MockServices;
058: import org.netbeans.junit.NbTestCase;
059: import org.netbeans.modules.project.ui.actions.TestSupport;
060: import org.netbeans.spi.project.ui.ProjectOpenedHook;
061: import org.openide.filesystems.FileObject;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.filesystems.URLMapper;
064: import org.openide.nodes.Node;
065: import org.openide.nodes.Node.Handle;
066: import org.openide.nodes.NodeEvent;
067: import org.openide.nodes.NodeListener;
068: import org.openide.nodes.NodeMemberEvent;
069: import org.openide.nodes.NodeReorderEvent;
070: import org.openide.util.Exceptions;
071: import org.openide.util.RequestProcessor;
072: import org.openide.util.lookup.Lookups;
073:
074: /**
075: *
076: * @author Jaroslav Tulach <jtulach@netbeans.org>
077: */
078: public class ProjectsRootNodeTest extends NbTestCase {
079: CountDownLatch down;
080:
081: public ProjectsRootNodeTest(String testName) {
082: super (testName);
083: }
084:
085: @Override
086: protected void setUp() throws Exception {
087: clearWorkDir();
088:
089: MockServices.setServices(TestSupport.TestProjectFactory.class);
090:
091: FileObject workDir = FileUtil.toFileObject(getWorkDir());
092: assertNotNull(workDir);
093:
094: down = new CountDownLatch(1);
095:
096: List<URL> list = new ArrayList<URL>();
097: List<ExtIcon> icons = new ArrayList<ExtIcon>();
098: List<String> names = new ArrayList<String>();
099: for (int i = 0; i < 30; i++) {
100: FileObject prj = TestSupport.createTestProject(workDir,
101: "prj" + i);
102: URL url = URLMapper.findURL(prj, URLMapper.EXTERNAL);
103: list.add(url);
104: names.add(url.toExternalForm());
105: icons.add(new ExtIcon());
106: TestSupport.TestProject tmp = (TestSupport.TestProject) ProjectManager
107: .getDefault().findProject(prj);
108: assertNotNull("Project found", tmp);
109: tmp.setLookup(Lookups
110: .singleton(new TestProjectOpenedHookImpl(down)));
111: }
112:
113: OpenProjectListSettings.getInstance().setOpenProjectsURLs(list);
114: OpenProjectListSettings.getInstance()
115: .setOpenProjectsDisplayNames(names);
116: OpenProjectListSettings.getInstance().setOpenProjectsIcons(
117: icons);
118: }
119:
120: @Override
121: protected void tearDown() throws Exception {
122: super .tearDown();
123: }
124:
125: public void testBehaviourOfProjectsLogicNode() throws Exception {
126: Node logicalView = new ProjectsRootNode(
127: ProjectsRootNode.LOGICAL_VIEW);
128: L listener = new L();
129: logicalView.addNodeListener(listener);
130:
131: assertEquals("30 children", 30, logicalView.getChildren()
132: .getNodesCount());
133: listener.assertEvents("None", 0);
134: assertEquals("No project opened yet", 0,
135: TestProjectOpenedHookImpl.opened);
136:
137: for (Node n : logicalView.getChildren().getNodes()) {
138: TestSupport.TestProject p = n.getLookup().lookup(
139: TestSupport.TestProject.class);
140: assertNull("No project of this type, yet", p);
141: }
142:
143: // let project open code run
144: down.countDown();
145: TestProjectOpenedHookImpl.toOpen.await();
146:
147: assertEquals("All projects opened", 30,
148: TestProjectOpenedHookImpl.opened);
149:
150: OpenProjectList.waitProjectsFullyOpen();
151:
152: for (Node n : logicalView.getChildren().getNodes()) {
153: TestSupport.TestProject p = n.getLookup().lookup(
154: TestSupport.TestProject.class);
155: assertNotNull("Nodes have correct project of this type", p);
156: }
157:
158: listener.assertEvents("Goal is to receive no events at all", 0);
159: assertTrue("Finished", OpenProjects.getDefault().openProjects()
160: .isDone());
161: assertFalse("Not cancelled, Finished", OpenProjects
162: .getDefault().openProjects().isCancelled());
163: Project[] arr = OpenProjects.getDefault().openProjects().get();
164: assertEquals("30", 30, arr.length);
165: }
166:
167: private static class L implements NodeListener {
168: public List<EventObject> events = new ArrayList<EventObject>();
169:
170: public void childrenAdded(NodeMemberEvent ev) {
171: assertFalse("No event in AWT thread", EventQueue
172: .isDispatchThread());
173: events.add(ev);
174: }
175:
176: public void childrenRemoved(NodeMemberEvent ev) {
177: assertFalse("No event in AWT thread", EventQueue
178: .isDispatchThread());
179: events.add(ev);
180: }
181:
182: public void childrenReordered(NodeReorderEvent ev) {
183: assertFalse("No event in AWT thread", EventQueue
184: .isDispatchThread());
185: events.add(ev);
186: }
187:
188: public void nodeDestroyed(NodeEvent ev) {
189: assertFalse("No event in AWT thread", EventQueue
190: .isDispatchThread());
191: events.add(ev);
192: }
193:
194: public void propertyChange(PropertyChangeEvent evt) {
195: assertFalse("No event in AWT thread", EventQueue
196: .isDispatchThread());
197: events.add(evt);
198: }
199:
200: final void assertEvents(String string, int i) {
201: assertEquals(string + events, i, events.size());
202: events.clear();
203: }
204:
205: }
206:
207: private static class TestProjectOpenedHookImpl extends
208: ProjectOpenedHook implements Runnable {
209:
210: public static CountDownLatch toOpen = new CountDownLatch(30);
211: public static int opened = 0;
212: public static int closed = 0;
213:
214: private CountDownLatch toWaitOn;
215:
216: public TestProjectOpenedHookImpl(CountDownLatch toWaitOn) {
217: this .toWaitOn = toWaitOn;
218: }
219:
220: protected void projectClosed() {
221: closed++;
222: }
223:
224: Project[] arr;
225:
226: public void run() {
227: try {
228: arr = OpenProjects.getDefault().openProjects().get(50,
229: TimeUnit.MILLISECONDS);
230: } catch (InterruptedException ex) {
231: fail("Wrong exception");
232: } catch (ExecutionException ex) {
233: fail("Wrong exception");
234: } catch (TimeoutException ex) {
235: // OK
236: }
237: }
238:
239: protected void projectOpened() {
240: assertFalse("Running", OpenProjects.getDefault()
241: .openProjects().isDone());
242: // now verify that other threads do not see results from the Future
243: RequestProcessor.getDefault().post(this ).waitFinished();
244: assertNull("TimeoutException thrown", arr);
245: if (toWaitOn != null) {
246: try {
247: toWaitOn.await();
248: } catch (InterruptedException ex) {
249: throw new IllegalStateException(ex);
250: }
251: }
252: opened++;
253: toOpen.countDown();
254: }
255:
256: }
257: }
|