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 CloseProjectsTest extends NbTestCase {
079: CountDownLatch down;
080:
081: public CloseProjectsTest(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 testClose() 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: Node[] nodeArray = logicalView.getChildren().getNodes();
138: Project[] arr = new Project[nodeArray.length];
139: int i = 0;
140: for (Node n : nodeArray) {
141: Project p = n.getLookup().lookup(
142: TestSupport.TestProject.class);
143: assertNull("No project of this type, yet", p);
144:
145: arr[i] = n.getLookup().lookup(Project.class);
146: assertNotNull("but some project is there", arr[i]);
147:
148: i++;
149: }
150: // let the project open hook run
151: down.countDown();
152:
153: OpenProjects.getDefault().close(arr);
154:
155: assertEquals("View is empty", 0, logicalView.getChildren()
156: .getNodesCount());
157: }
158:
159: private static class L implements NodeListener {
160: public List<EventObject> events = new ArrayList<EventObject>();
161:
162: public void childrenAdded(NodeMemberEvent ev) {
163: assertFalse("No event in AWT thread", EventQueue
164: .isDispatchThread());
165: events.add(ev);
166: }
167:
168: public void childrenRemoved(NodeMemberEvent ev) {
169: assertFalse("No event in AWT thread", EventQueue
170: .isDispatchThread());
171: events.add(ev);
172: }
173:
174: public void childrenReordered(NodeReorderEvent ev) {
175: assertFalse("No event in AWT thread", EventQueue
176: .isDispatchThread());
177: events.add(ev);
178: }
179:
180: public void nodeDestroyed(NodeEvent ev) {
181: assertFalse("No event in AWT thread", EventQueue
182: .isDispatchThread());
183: events.add(ev);
184: }
185:
186: public void propertyChange(PropertyChangeEvent evt) {
187: assertFalse("No event in AWT thread", EventQueue
188: .isDispatchThread());
189: events.add(evt);
190: }
191:
192: final void assertEvents(String string, int i) {
193: assertEquals(string + events, i, events.size());
194: events.clear();
195: }
196:
197: }
198:
199: private static class TestProjectOpenedHookImpl extends
200: ProjectOpenedHook implements Runnable {
201:
202: public static CountDownLatch toOpen = new CountDownLatch(30);
203: public static int opened = 0;
204: public static int closed = 0;
205:
206: private CountDownLatch toWaitOn;
207:
208: public TestProjectOpenedHookImpl(CountDownLatch toWaitOn) {
209: this .toWaitOn = toWaitOn;
210: }
211:
212: protected void projectClosed() {
213: closed++;
214: }
215:
216: Project[] arr;
217:
218: public void run() {
219: try {
220: arr = OpenProjects.getDefault().openProjects().get(50,
221: TimeUnit.MILLISECONDS);
222: } catch (InterruptedException ex) {
223: fail("Wrong exception");
224: } catch (ExecutionException ex) {
225: fail("Wrong exception");
226: } catch (TimeoutException ex) {
227: // OK
228: }
229: }
230:
231: protected void projectOpened() {
232: assertFalse("Running", OpenProjects.getDefault()
233: .openProjects().isDone());
234: // now verify that other threads do not see results from the Future
235: RequestProcessor.getDefault().post(this ).waitFinished();
236: assertNull("TimeoutException thrown", arr);
237: if (toWaitOn != null) {
238: try {
239: toWaitOn.await();
240: } catch (InterruptedException ex) {
241: throw new IllegalStateException(ex);
242: }
243: }
244: opened++;
245: toOpen.countDown();
246: }
247:
248: }
249: }
|