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 2008 Sun Microsystems, Inc.
038: */
039:
040: package org.netbeans.modules.project.ui.actions;
041:
042: import java.awt.Component;
043: import java.beans.PropertyChangeListener;
044: import java.io.IOException;
045: import java.lang.ref.WeakReference;
046: import java.util.Collection;
047: import java.util.Collections;
048: import javax.swing.Action;
049: import javax.swing.JMenuItem;
050: import junit.framework.TestCase;
051: import org.netbeans.api.project.Project;
052: import org.netbeans.api.project.ProjectManager;
053: import org.netbeans.api.project.ui.OpenProjects;
054: import org.netbeans.junit.MockServices;
055: import org.netbeans.junit.NbTestCase;
056: import org.netbeans.modules.projectapi.TimedWeakReference;
057: import org.netbeans.spi.project.ProjectConfiguration;
058: import org.netbeans.spi.project.ProjectConfigurationProvider;
059: import org.netbeans.spi.project.ProjectFactory;
060: import org.netbeans.spi.project.ProjectState;
061: import org.openide.awt.DynamicMenuContent;
062: import org.openide.filesystems.FileObject;
063: import org.openide.filesystems.FileUtil;
064: import org.openide.util.HelpCtx;
065: import org.openide.util.Lookup;
066: import org.openide.util.actions.Presenter;
067: import org.openide.util.lookup.Lookups;
068:
069: /**
070: *
071: * @author Jaroslav Tulach <jaroslav.tulach@netbeans.org>
072: */
073: public class ActiveConfigActionTest extends NbTestCase {
074:
075: static {
076: new ActiveConfigAction.DynLayer();
077: TimedWeakReference.TIMEOUT = 10;
078: }
079:
080: public ActiveConfigActionTest(String testName) {
081: super (testName);
082: }
083:
084: @Override
085: protected void setUp() throws Exception {
086: super .setUp();
087:
088: MockServices.setServices(PF.class);
089:
090: PF.toCreate = new P();
091: assertEquals(PF.toCreate, ProjectManager.getDefault()
092: .findProject(PF.toCreate.fo));
093: }
094:
095: @Override
096: protected void tearDown() throws Exception {
097: super .tearDown();
098: }
099:
100: private static Object holder;
101:
102: /**
103: * Test of createContextAwareInstance method, of class ActiveConfigAction.
104: */
105: public void testCreateContextAwareInstance() {
106: P p = PF.toCreate;
107:
108: OpenProjects.getDefault().open(new Project[] { p }, false);
109: OpenProjects.getDefault().setMainProject(p);
110: ActiveConfigAction instance = new ActiveConfigAction();
111: Action result = instance;//.createContextAwareInstance(actionContext);
112:
113: assertTrue("menu: " + result, result instanceof Presenter.Menu);
114: Presenter.Menu menu = (Presenter.Menu) result;
115:
116: JMenuItem item = menu.getMenuPresenter();
117:
118: assertNotNull(item);
119: assertTrue("Enabled", item.isEnabled());
120: DynamicMenuContent m = (DynamicMenuContent) item;
121: assertEquals("One", 1, m.getMenuPresenters().length);
122:
123: holder = item;
124:
125: OpenProjects.getDefault().close(new Project[] { p });
126: assertNull("NO project selected", OpenProjects.getDefault()
127: .getMainProject());
128:
129: WeakReference<Object> ref = new WeakReference<Object>(p);
130: p = null;
131: PF.toCreate = null;
132:
133: assertGC("Reference can go away", ref);
134: }
135:
136: public static final class PF implements ProjectFactory {
137: static P toCreate;
138:
139: public boolean isProject(FileObject projectDirectory) {
140: return projectDirectory.equals(toCreate.fo);
141: }
142:
143: public Project loadProject(FileObject projectDirectory,
144: ProjectState state) throws IOException {
145: return toCreate;
146: }
147:
148: public void saveProject(Project project) throws IOException,
149: ClassCastException {
150: }
151: }
152:
153: private static class P implements Project,
154: ProjectConfigurationProvider {
155: Lookup l = Lookups.singleton(this );
156: FileObject fo = FileUtil.createMemoryFileSystem().getRoot();
157: PC conf = new PC();
158:
159: public P() {
160: }
161:
162: public FileObject getProjectDirectory() {
163: return fo;
164: }
165:
166: public Lookup getLookup() {
167: return l;
168: }
169:
170: public Collection getConfigurations() {
171: return Collections.singleton(conf);
172: }
173:
174: public ProjectConfiguration getActiveConfiguration() {
175: return conf;
176: }
177:
178: public void setActiveConfiguration(
179: ProjectConfiguration configuration)
180: throws IllegalArgumentException, IOException {
181: throw new UnsupportedOperationException(
182: "Not supported yet.");
183: }
184:
185: public boolean hasCustomizer() {
186: return true;
187: }
188:
189: public void customize() {
190: throw new UnsupportedOperationException(
191: "Not supported yet.");
192: }
193:
194: public boolean configurationsAffectAction(String command) {
195: throw new UnsupportedOperationException(
196: "Not supported yet.");
197: }
198:
199: public void addPropertyChangeListener(PropertyChangeListener lst) {
200: }
201:
202: public void removePropertyChangeListener(
203: PropertyChangeListener lst) {
204: }
205: }
206:
207: private static final class PC implements ProjectConfiguration {
208: public String getDisplayName() {
209: return "Default";
210: }
211:
212: }
213: }
|