KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > ModuleOperationsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.ui;
21
22 import java.awt.EventQueue JavaDoc;
23 import java.util.Arrays JavaDoc;
24 import javax.swing.JDialog JavaDoc;
25 import org.apache.tools.ant.module.api.support.ActionUtils;
26 import org.netbeans.api.project.Project;
27 import org.netbeans.modules.apisupport.project.DialogDisplayerImpl;
28 import org.netbeans.modules.apisupport.project.InstalledFileLocatorImpl;
29 import org.netbeans.modules.apisupport.project.NbModuleProject;
30 import org.netbeans.modules.apisupport.project.TestBase;
31 import org.netbeans.modules.apisupport.project.layers.LayerTestBase;
32 import org.netbeans.spi.project.ActionProvider;
33 import org.netbeans.spi.project.support.ProjectOperations;
34 import org.netbeans.spi.project.support.ant.GeneratedFilesHelper;
35 import org.netbeans.spi.project.ui.support.CommonProjectActions;
36 import org.openide.DialogDescriptor;
37 import org.openide.DialogDisplayer;
38 import org.openide.filesystems.FileObject;
39 import org.openide.filesystems.FileUtil;
40 import org.openide.util.ContextGlobalProvider;
41 import org.openide.util.Lookup;
42 import org.openide.util.NbBundle;
43 import org.openide.util.lookup.Lookups;
44
45 /**
46  * Test ModuleOperations.
47  *
48  * @author Martin Krauskopf
49  */

50 public class ModuleOperationsTest extends TestBase {
51     
52     private static ContextGlobalProviderImpl cgpi = new ContextGlobalProviderImpl();
53     
54     static {
55         System.setProperty("org.netbeans.core.startup.ModuleSystem.CULPRIT", "true");
56         LayerTestBase.Lkp.setLookup(new Object JavaDoc[] {
57             cgpi,
58         });
59         DialogDisplayerImpl.returnFromNotify(DialogDescriptor.NO_OPTION);
60     }
61     
62     public ModuleOperationsTest(String JavaDoc name) {
63         super(name);
64     }
65     
66     protected void setUp() throws Exception JavaDoc {
67         super.setUp();
68         InstalledFileLocatorImpl.registerDestDir(destDirF);
69     }
70     
71     public void testDelete() throws Exception JavaDoc {
72         NbModuleProject project = generateStandaloneModule("module");
73         project.open();
74         ActionProvider ap = (ActionProvider) project.getLookup().lookup(ActionProvider.class);
75         assertNotNull("have an action provider", ap);
76         assertTrue("delete action is enabled", ap.isActionEnabled(ActionProvider.COMMAND_DELETE, null));
77         
78         FileObject prjDir = project.getProjectDirectory();
79         
80         FileObject buildXML = prjDir.getFileObject(GeneratedFilesHelper.BUILD_XML_PATH);
81         prjDir.createData(".cvsignore");
82         
83         // build project
84
ActionUtils.runTarget(buildXML, new String JavaDoc[] { "compile" }, null).waitFinished();
85         assertNotNull("project was build", prjDir.getFileObject("build"));
86         
87         FileObject[] expectedMetadataFiles = new FileObject[] {
88             buildXML,
89             prjDir.getFileObject("manifest.mf"),
90             prjDir.getFileObject("nbproject"),
91             prjDir.getFileObject(".cvsignore"),
92         };
93         assertEquals("correct metadata files", Arrays.asList(expectedMetadataFiles), ProjectOperations.getMetadataFiles(project));
94         
95         FileObject[] expectedDataFiles = new FileObject[] {
96             prjDir.getFileObject("src"),
97             prjDir.getFileObject("test"),
98         };
99         assertEquals("correct data files", Arrays.asList(expectedDataFiles), ProjectOperations.getDataFiles(project));
100         
101         // It is hard to simulate exact scenario invoked by user. Let's test at least something.
102
ProjectOperations.notifyDeleting(project);
103         prjDir.getFileSystem().refresh(true);
104         assertNull(prjDir.getFileObject("build"));
105     }
106     
107     public void testOperationActions() throws Exception JavaDoc { // #72397
108
NbModuleProject project = generateStandaloneModule("module");
109         cgpi.setProject(project);
110         DialogDisplayerImpl dd = (DialogDisplayerImpl) Lookup.getDefault().lookup(DialogDisplayer.class);
111         dd.setDialog(new JDialog JavaDoc() {
112             public void setVisible(boolean b) { /* do not show during test-run */ }
113         });
114         FileObject lock = FileUtil.createData(project.getProjectDirectory(), "build/testuserdir/lock");
115         EventQueue.invokeAndWait(new Runnable JavaDoc() {
116             public void run() {
117                 CommonProjectActions.deleteProjectAction().actionPerformed(null);
118             }
119         });
120         assertNotNull("warning message emitted", dd.getLastNotifyDescriptor());
121         assertEquals("warning message emitted", dd.getLastNotifyDescriptor().getMessage(),
122                 NbBundle.getMessage(ModuleOperationsTest.class, "ERR_ModuleIsBeingRun"));
123         dd.reset();
124         lock.delete();
125         EventQueue.invokeAndWait(new Runnable JavaDoc() {
126             public void run() {
127                 CommonProjectActions.deleteProjectAction().actionPerformed(null);
128             }
129         });
130         assertNull("no warning message", dd.getLastNotifyDescriptor());
131     }
132     
133     static final class ContextGlobalProviderImpl implements ContextGlobalProvider {
134         
135         private Lookup contextLookup;
136         
137         void setProject(final Project project) {
138             contextLookup = Lookups.singleton(project);
139         }
140         
141         public Lookup createGlobalContext() {
142             return contextLookup;
143         }
144         
145     }
146     
147 }
148
Popular Tags