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.beans.PropertyChangeListener;
045: import java.net.URL;
046: import java.util.ArrayList;
047: import java.util.EventObject;
048: import java.util.List;
049: import java.util.concurrent.CountDownLatch;
050: import javax.swing.Action;
051: import junit.framework.TestCase;
052: import org.netbeans.api.project.Project;
053: import org.netbeans.api.project.ProjectManager;
054: import org.netbeans.api.project.ui.OpenProjects;
055: import org.netbeans.junit.MockServices;
056: import org.netbeans.junit.NbTestCase;
057: import org.netbeans.modules.project.ui.actions.TestSupport;
058: import org.netbeans.spi.project.ui.ProjectOpenedHook;
059: import org.openide.filesystems.FileObject;
060: import org.openide.filesystems.FileUtil;
061: import org.openide.filesystems.URLMapper;
062: import org.openide.nodes.Node;
063: import org.openide.nodes.Node.Handle;
064: import org.openide.nodes.NodeEvent;
065: import org.openide.nodes.NodeListener;
066: import org.openide.nodes.NodeMemberEvent;
067: import org.openide.nodes.NodeReorderEvent;
068: import org.openide.util.Exceptions;
069: import org.openide.util.Lookup;
070: import org.openide.util.lookup.Lookups;
071: import org.openidex.search.SearchInfo;
072:
073: /**
074: *
075: * @author Jaroslav Tulach <jtulach@netbeans.org>
076: */
077: public class ProjectsRootNodePreferredOpenTest extends NbTestCase
078: implements PropertyChangeListener {
079: CountDownLatch first;
080: CountDownLatch middle;
081: CountDownLatch rest;
082: private int events;
083:
084: public ProjectsRootNodePreferredOpenTest(String testName) {
085: super (testName);
086: }
087:
088: Lookup createLookup(TestSupport.TestProject project, Object instance) {
089: return Lookups.singleton(instance);
090: }
091:
092: @Override
093: protected void setUp() throws Exception {
094: clearWorkDir();
095:
096: MockServices.setServices(TestSupport.TestProjectFactory.class);
097:
098: FileObject workDir = FileUtil.toFileObject(getWorkDir());
099: assertNotNull(workDir);
100:
101: first = new CountDownLatch(1);
102: middle = new CountDownLatch(1);
103: rest = new CountDownLatch(2);
104:
105: List<URL> list = new ArrayList<URL>();
106: List<ExtIcon> icons = new ArrayList<ExtIcon>();
107: List<String> names = new ArrayList<String>();
108: for (int i = 0; i < 10; i++) {
109: FileObject prj = TestSupport.createTestProject(workDir,
110: "prj" + i);
111: URL url = URLMapper.findURL(prj, URLMapper.EXTERNAL);
112: list.add(url);
113: names.add(url.toExternalForm());
114: icons.add(new ExtIcon());
115: TestSupport.TestProject tmp = (TestSupport.TestProject) ProjectManager
116: .getDefault().findProject(prj);
117: assertNotNull("Project found", tmp);
118: CountDownLatch down = i == 0 ? first : (i == 5 ? middle
119: : rest);
120: tmp.setLookup(createLookup(tmp,
121: new TestProjectOpenedHookImpl(down)));
122: }
123:
124: OpenProjectListSettings.getInstance().setOpenProjectsURLs(list);
125: OpenProjectListSettings.getInstance()
126: .setOpenProjectsDisplayNames(names);
127: OpenProjectListSettings.getInstance().setOpenProjectsIcons(
128: icons);
129:
130: OpenProjects.getDefault().addPropertyChangeListener(this );
131: }
132:
133: @Override
134: protected void tearDown() throws Exception {
135: super .tearDown();
136: }
137:
138: public void testPreferencesInOpenCanBeChanged()
139: throws InterruptedException {
140: assertEquals("No events in API", 0, events);
141:
142: Node logicalView = new ProjectsRootNode(
143: ProjectsRootNode.LOGICAL_VIEW);
144: L listener = new L();
145: logicalView.addNodeListener(listener);
146:
147: assertEquals("No events in API", 0, events);
148: assertEquals("10 children", 10, logicalView.getChildren()
149: .getNodesCount());
150: listener.assertEvents("None", 0);
151: assertEquals("No project opened yet", 0,
152: TestProjectOpenedHookImpl.opened);
153: assertEquals("No events in API", 0, events);
154:
155: for (Node n : logicalView.getChildren().getNodes()) {
156: TestSupport.TestProject p = n.getLookup().lookup(
157: TestSupport.TestProject.class);
158: assertNull("No project of this type, yet", p);
159: }
160: assertEquals("No events in API", 0, events);
161:
162: Node midNode = logicalView.getChildren().getNodes()[5];
163: {
164: TestSupport.TestProject p = midNode.getLookup().lookup(
165: TestSupport.TestProject.class);
166: assertNull("No project of this type, yet", p);
167: }
168: Project lazyP = midNode.getLookup().lookup(Project.class);
169: assertNotNull("Some project is found", lazyP);
170: assertEquals("It is lazy project", LazyProject.class, lazyP
171: .getClass());
172: assertEquals("No events in API", 0, events);
173:
174: middle.countDown();
175: // not necessary, but to ensure middle really does not run
176: Thread.sleep(300);
177: assertEquals("Still no processing", 0,
178: TestProjectOpenedHookImpl.opened);
179: // trigger initialization of the node, shall trigger OpenProjectList.preferredProject(lazyP);
180: midNode.getChildren().getNodes();
181: first.countDown();
182:
183: TestProjectOpenedHookImpl.toOpen.await();
184:
185: {
186: TestSupport.TestProject p = null;
187: for (int i = 0; i < 10; i++) {
188: Node midNode2 = logicalView.getChildren().getNodes()[5];
189: p = midNode.getLookup().lookup(
190: TestSupport.TestProject.class);
191: if (p != null) {
192: break;
193: }
194: Thread.sleep(100);
195: }
196: assertNotNull("The right project opened", p);
197: }
198: assertEquals("No events in API", 0, events);
199:
200: {
201: int cnt = 0;
202: for (int i = 0; i < 10; i++) {
203: Node n = logicalView.getChildren().getNodes()[i];
204: TestSupport.TestProject p = null;
205: p = n.getLookup().lookup(TestSupport.TestProject.class);
206: if (p != null) {
207: cnt++;
208: }
209: }
210: assertEquals(
211: "First and fifth projects are open, nobody else is",
212: 2, cnt);
213: }
214:
215: assertEquals("No events in API", 0, events);
216: rest.countDown();
217: rest.countDown();
218:
219: OpenProjectList.waitProjectsFullyOpen();
220: assertEquals("Finally notified in API", 1, events);
221:
222: assertEquals("All projects opened", 10,
223: TestProjectOpenedHookImpl.opened);
224:
225: for (Node n : logicalView.getChildren().getNodes()) {
226: TestSupport.TestProject p = n.getLookup().lookup(
227: TestSupport.TestProject.class);
228: assertNotNull("Nodes have correct project of this type", p);
229: SearchInfo s = n.getLookup().lookup(SearchInfo.class);
230: assertNotNull("Nodes have correct project of this type", s);
231: }
232: }
233:
234: public void propertyChange(PropertyChangeEvent evt) {
235: if (OpenProjects.PROPERTY_OPEN_PROJECTS.equals(evt
236: .getPropertyName())) {
237: events++;
238: }
239: }
240:
241: private static class L implements NodeListener,
242: PropertyChangeListener {
243: public List<EventObject> events = new ArrayList<EventObject>();
244:
245: public void childrenAdded(NodeMemberEvent ev) {
246: assertFalse("No event in AWT thread", EventQueue
247: .isDispatchThread());
248: events.add(ev);
249: }
250:
251: public void childrenRemoved(NodeMemberEvent ev) {
252: assertFalse("No event in AWT thread", EventQueue
253: .isDispatchThread());
254: events.add(ev);
255: }
256:
257: public void childrenReordered(NodeReorderEvent ev) {
258: assertFalse("No event in AWT thread", EventQueue
259: .isDispatchThread());
260: events.add(ev);
261: }
262:
263: public void nodeDestroyed(NodeEvent ev) {
264: assertFalse("No event in AWT thread", EventQueue
265: .isDispatchThread());
266: events.add(ev);
267: }
268:
269: public void propertyChange(PropertyChangeEvent evt) {
270: assertFalse("No event in AWT thread", EventQueue
271: .isDispatchThread());
272: events.add(evt);
273: }
274:
275: final void assertEvents(String string, int i) {
276: assertEquals(string + events, i, events.size());
277: events.clear();
278: }
279:
280: }
281:
282: private static class TestProjectOpenedHookImpl extends
283: ProjectOpenedHook {
284:
285: public static CountDownLatch toOpen = new CountDownLatch(2);
286: public static int opened = 0;
287: public static int closed = 0;
288:
289: private CountDownLatch toWaitOn;
290:
291: public TestProjectOpenedHookImpl(CountDownLatch toWaitOn) {
292: this .toWaitOn = toWaitOn;
293: }
294:
295: protected void projectClosed() {
296: closed++;
297: }
298:
299: protected void projectOpened() {
300: if (toWaitOn != null) {
301: try {
302: toWaitOn.await();
303: } catch (InterruptedException ex) {
304: throw new IllegalStateException(ex);
305: }
306: }
307: opened++;
308: toOpen.countDown();
309: }
310:
311: }
312: }
|