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.project.ui;
043:
044: import java.beans.PropertyChangeEvent;
045: import java.beans.PropertyChangeListener;
046: import java.io.IOException;
047: import java.util.ArrayList;
048: import java.util.List;
049: import org.netbeans.api.project.Project;
050: import org.netbeans.api.project.ProjectManager;
051: import org.netbeans.api.project.TestUtil;
052: import org.netbeans.junit.NbTestCase;
053: import org.openide.filesystems.FileObject;
054: import org.openide.util.Lookup;
055: import org.openide.util.lookup.Lookups;
056:
057: public class OpenProjectsTrampolineImplTest extends NbTestCase {
058:
059: public OpenProjectsTrampolineImplTest(String name) {
060: super (name);
061: }
062:
063: private FileObject scratch;
064: private FileObject goodproject;
065: private FileObject goodproject2;
066: // private FileObject badproject;
067: // private FileObject mysteryproject;
068: private ProjectManager pm;
069:
070: protected void setUp() throws Exception {
071: super .setUp();
072: scratch = TestUtil.makeScratchDir(this );
073: goodproject = scratch.createFolder("good");
074: goodproject.createFolder("testproject");
075: goodproject2 = scratch.createFolder("good2");
076: goodproject2.createFolder("testproject");
077: // badproject = scratch.createFolder("bad");
078: // badproject.createFolder("testproject").createData("broken");
079: // mysteryproject = scratch.createFolder("mystery");
080: TestUtil.setLookup(Lookups.singleton(TestUtil
081: .testProjectFactory()));
082: pm = ProjectManager.getDefault();
083: OpenProjectList.waitProjectsFullyOpen();
084: }
085:
086: protected void tearDown() throws Exception {
087: scratch = null;
088: goodproject = null;
089: // badproject = null;
090: // mysteryproject = null;
091: pm = null;
092: TestUtil.setLookup(Lookup.EMPTY);
093: super .tearDown();
094: }
095:
096: public void testOpenProjects() throws Exception {
097:
098: OpenProjectsTrampolineImpl trampoline = new OpenProjectsTrampolineImpl();
099: TestPropertyChangeListener tpchl = new TestPropertyChangeListener();
100: trampoline.addPropertyChangeListenerAPI(tpchl, this );
101:
102: Project[] projects = trampoline.getOpenProjectsAPI();
103:
104: assertEquals("No project should be open.", 0, projects.length);
105: assertEquals("No events.", 0, tpchl.getEvents().size());
106:
107: Project p1 = null;
108:
109: try {
110: p1 = pm.findProject(goodproject);
111: } catch (IOException e) {
112: fail("Should not fail to load goodproject: " + e);
113: }
114:
115: assertNotNull("Project should not be null", p1);
116:
117: OpenProjectList.getDefault().open(p1);
118: projects = trampoline.getOpenProjectsAPI();
119:
120: assertEquals("One project should be open.", 1, projects.length);
121: assertEquals("Obe event.", 1, tpchl.getEvents().size());
122:
123: Project p2 = null;
124:
125: try {
126: p2 = pm.findProject(goodproject2);
127: } catch (IOException e) {
128: fail("Should not fail to load goodproject: " + e);
129: }
130:
131: assertNotNull("Project should not be null", p2);
132:
133: OpenProjectList.getDefault().open(p2);
134: projects = trampoline.getOpenProjectsAPI();
135: assertEquals("Two projects should be open.", 2, projects.length);
136: assertEquals("Two events.", 2, tpchl.getEvents().size());
137:
138: OpenProjectList.getDefault().close(new Project[] { p1 }, false);
139: projects = trampoline.getOpenProjectsAPI();
140: assertEquals("Two projects should be open.", 1, projects.length);
141: assertEquals("Two events.", 3, tpchl.getEvents().size());
142:
143: OpenProjectList.getDefault().close(new Project[] { p2 }, false);
144: projects = trampoline.getOpenProjectsAPI();
145: assertEquals("Two projects should be open.", 0, projects.length);
146: assertEquals("Two events.", 4, tpchl.getEvents().size());
147:
148: }
149:
150: private static class TestPropertyChangeListener implements
151: PropertyChangeListener {
152:
153: List<PropertyChangeEvent> events = new ArrayList<PropertyChangeEvent>();
154:
155: public void propertyChange(PropertyChangeEvent e) {
156: events.add(e);
157: }
158:
159: void clear() {
160: events.clear();
161: }
162:
163: List<PropertyChangeEvent> getEvents() {
164: return events;
165: }
166:
167: }
168:
169: }
|