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.actions;
043:
044: import java.io.IOException;
045: import java.lang.ref.WeakReference;
046: import javax.swing.JMenu;
047: import javax.swing.JMenuItem;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.api.project.ProjectManager;
050: import org.netbeans.junit.MockServices;
051: import org.netbeans.junit.NbTestCase;
052: import org.netbeans.modules.project.ui.OpenProjectList;
053: import org.netbeans.modules.project.ui.actions.ProjectActionTest.ActionCreator;
054: import org.openide.filesystems.FileObject;
055: import org.openide.filesystems.FileUtil;
056: import org.openide.util.Lookup;
057:
058: public class SetMainProjectTest extends NbTestCase {
059:
060: public SetMainProjectTest(String name) {
061: super (name);
062: }
063:
064: public void setUp() throws Exception {
065: super .setUp();
066:
067: MockServices.setServices(TestSupport.TestProjectFactory.class);
068: clearWorkDir();
069: }
070:
071: public boolean runInEQ() {
072: return true;
073: }
074:
075: public void testAcceleratorsPropagated() {
076: ProjectActionTest.doTestAcceleratorsPropagated(
077: new ActionCreator() {
078: public ProjectAction create(Lookup l) {
079: return new SetMainProject(l);
080: }
081: }, false);
082: }
083:
084: public void test70368() {
085: SetMainProject a = new SetMainProject();
086: WeakReference<?> ref = new WeakReference<Object>(a);
087:
088: a = null;
089:
090: assertGC("SetMainProject action's instance can be freed:", ref);
091: }
092:
093: public void test70835() throws IOException {
094: FileObject workDir = FileUtil.toFileObject(getWorkDir());
095:
096: assertNotNull(workDir);
097:
098: FileObject f1 = TestSupport.createTestProject(workDir,
099: "project1");
100: FileObject f2 = TestSupport.createTestProject(workDir,
101: "project2");
102:
103: assertNotNull(f1);
104: assertNotNull(f2);
105:
106: Project p1 = ProjectManager.getDefault().findProject(f1);
107: Project p2 = ProjectManager.getDefault().findProject(f2);
108:
109: assertNotNull(p1);
110: assertNotNull(p2);
111:
112: OpenProjectList.getDefault().open(new Project[] { p1, p2 },
113: false);
114:
115: SetMainProject a = new SetMainProject();
116:
117: JMenuItem item = a.getMenuPresenter();
118:
119: assertTrue(item instanceof JMenu);
120:
121: JMenu menu = (JMenu) item;
122:
123: item = null;
124:
125: assertEquals(2, menu.getItemCount());
126: assertTrue(menu.isEnabled());
127:
128: WeakReference<?> menuRef = new WeakReference<Object>(menu);
129: WeakReference<?> actionRef = new WeakReference<Object>(a);
130:
131: a = null;
132:
133: try {
134: assertGC("", actionRef);
135: } catch (Error e) {
136: //ignore....
137: }
138:
139: OpenProjectList.getDefault().close(new Project[] { p1 }, false);
140:
141: assertEquals(1, menu.getItemCount());
142: assertTrue(menu.isEnabled());
143:
144: OpenProjectList.getDefault().close(new Project[] { p2 }, false);
145:
146: assertEquals(0, menu.getItemCount());
147: assertFalse(menu.isEnabled());
148:
149: OpenProjectList.getDefault().open(new Project[] { p1 }, false);
150:
151: assertEquals(1, menu.getItemCount());
152: assertTrue(menu.isEnabled());
153:
154: menu = null;
155:
156: assertGC("", menuRef);
157: assertGC("", actionRef);
158: }
159: }
|