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.apisupport.project.ui;
043:
044: import java.awt.EventQueue;
045: import java.util.Arrays;
046: import javax.swing.JDialog;
047: import org.apache.tools.ant.module.api.support.ActionUtils;
048: import org.netbeans.api.project.Project;
049: import org.netbeans.modules.apisupport.project.DialogDisplayerImpl;
050: import org.netbeans.modules.apisupport.project.InstalledFileLocatorImpl;
051: import org.netbeans.modules.apisupport.project.NbModuleProject;
052: import org.netbeans.modules.apisupport.project.TestBase;
053: import org.netbeans.modules.apisupport.project.layers.LayerTestBase;
054: import org.netbeans.spi.project.ActionProvider;
055: import org.netbeans.spi.project.support.ProjectOperations;
056: import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
057: import org.netbeans.spi.project.ui.support.CommonProjectActions;
058: import org.openide.DialogDescriptor;
059: import org.openide.DialogDisplayer;
060: import org.openide.filesystems.FileObject;
061: import org.openide.filesystems.FileUtil;
062: import org.openide.util.ContextGlobalProvider;
063: import org.openide.util.Lookup;
064: import org.openide.util.NbBundle;
065: import org.openide.util.lookup.Lookups;
066:
067: /**
068: * Test ModuleOperations.
069: *
070: * @author Martin Krauskopf
071: */
072: public class ModuleOperationsTest extends TestBase {
073:
074: private static ContextGlobalProviderImpl cgpi = new ContextGlobalProviderImpl();
075:
076: static {
077: System.setProperty(
078: "org.netbeans.core.startup.ModuleSystem.CULPRIT",
079: "true");
080: LayerTestBase.Lkp.setLookup(new Object[] { cgpi, });
081: DialogDisplayerImpl
082: .returnFromNotify(DialogDescriptor.NO_OPTION);
083: }
084:
085: public ModuleOperationsTest(String name) {
086: super (name);
087: }
088:
089: protected @Override
090: void setUp() throws Exception {
091: super .setUp();
092: InstalledFileLocatorImpl.registerDestDir(destDirF);
093: }
094:
095: public void testDelete() throws Exception {
096: NbModuleProject project = generateStandaloneModule("module");
097: project.open();
098: ActionProvider ap = project.getLookup().lookup(
099: ActionProvider.class);
100: assertNotNull("have an action provider", ap);
101: assertTrue("delete action is enabled", ap.isActionEnabled(
102: ActionProvider.COMMAND_DELETE, null));
103:
104: FileObject prjDir = project.getProjectDirectory();
105:
106: FileObject buildXML = prjDir
107: .getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
108:
109: // build project
110: ActionUtils.runTarget(buildXML, new String[] { "compile" },
111: null).waitFinished();
112: assertNotNull("project was build", prjDir
113: .getFileObject("build"));
114:
115: FileObject[] expectedMetadataFiles = { buildXML,
116: prjDir.getFileObject("manifest.mf"),
117: prjDir.getFileObject("nbproject"), };
118: assertEquals("correct metadata files", Arrays
119: .asList(expectedMetadataFiles), ProjectOperations
120: .getMetadataFiles(project));
121:
122: FileObject[] expectedDataFiles = { prjDir.getFileObject("src"),
123: prjDir.getFileObject("test"), };
124: assertEquals("correct data files", Arrays
125: .asList(expectedDataFiles), ProjectOperations
126: .getDataFiles(project));
127:
128: // It is hard to simulate exact scenario invoked by user. Let's test at least something.
129: ProjectOperations.notifyDeleting(project);
130: prjDir.getFileSystem().refresh(true);
131: assertNull(prjDir.getFileObject("build"));
132: }
133:
134: public void testOperationActions() throws Exception { // #72397
135: NbModuleProject project = generateStandaloneModule("module");
136: cgpi.setProject(project);
137: DialogDisplayerImpl dd = (DialogDisplayerImpl) Lookup
138: .getDefault().lookup(DialogDisplayer.class);
139: dd.setDialog(new JDialog() {
140: public @Override
141: void setVisible(boolean b) { /* do not show during test-run */
142: }
143: });
144: FileObject lock = FileUtil.createData(project
145: .getProjectDirectory(), "build/testuserdir/lock");
146: EventQueue.invokeAndWait(new Runnable() {
147: public void run() {
148: CommonProjectActions.deleteProjectAction()
149: .actionPerformed(null);
150: }
151: });
152: assertNotNull("warning message emitted", dd
153: .getLastNotifyDescriptor());
154: assertEquals("warning message emitted", dd
155: .getLastNotifyDescriptor().getMessage(), NbBundle
156: .getMessage(ModuleOperationsTest.class,
157: "ERR_ModuleIsBeingRun"));
158: dd.reset();
159: lock.delete();
160: EventQueue.invokeAndWait(new Runnable() {
161: public void run() {
162: CommonProjectActions.deleteProjectAction()
163: .actionPerformed(null);
164: }
165: });
166: assertNull("no warning message", dd.getLastNotifyDescriptor());
167: }
168:
169: static final class ContextGlobalProviderImpl implements
170: ContextGlobalProvider {
171:
172: private Lookup contextLookup;
173:
174: void setProject(final Project project) {
175: contextLookup = Lookups.singleton(project);
176: }
177:
178: public Lookup createGlobalContext() {
179: return contextLookup;
180: }
181:
182: }
183:
184: }
|