KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > j2seproject > J2SEActionProviderTest


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.java.j2seproject;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.PrintWriter JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Collections JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Properties JavaDoc;
31 import org.netbeans.api.fileinfo.NonRecursiveFolder;
32 import org.netbeans.api.java.project.JavaProjectConstants;
33 import org.netbeans.api.project.Project;
34 import org.netbeans.api.project.ProjectManager;
35 import org.netbeans.junit.NbTestCase;
36 import org.netbeans.modules.java.j2seproject.applet.AppletSupport;
37 import org.netbeans.modules.java.j2seproject.ui.customizer.MainClassChooser;
38 import org.netbeans.spi.project.ProjectConfiguration;
39 import org.netbeans.spi.project.ProjectConfigurationProvider;
40 import org.netbeans.spi.project.support.ant.PropertyEvaluator;
41 import org.openide.filesystems.FileLock;
42 import org.openide.filesystems.FileObject;
43 import org.netbeans.api.project.TestUtil;
44 import org.netbeans.spi.project.ActionProvider;
45 import org.netbeans.spi.project.support.ant.AntProjectHelper;
46 import org.openide.filesystems.FileUtil;
47 import org.openide.filesystems.URLMapper;
48 import org.openide.loaders.DataFolder;
49 import org.openide.loaders.DataObject;
50 import org.openide.modules.SpecificationVersion;
51 import org.openide.util.Lookup;
52 import org.openide.util.Mutex;
53 import org.openide.util.lookup.Lookups;
54
55 /**
56  * Tests for J2SEActionProvider
57  *
58  * @author David Konecny
59  */

60 public class J2SEActionProviderTest extends NbTestCase {
61     
62     public J2SEActionProviderTest(String JavaDoc testName) {
63         super(testName);
64     }
65
66     private FileObject scratch;
67     private FileObject projdir;
68     private FileObject sources;
69     private FileObject build;
70     private FileObject tests;
71     private ProjectManager pm;
72     private Project pp;
73     private AntProjectHelper helper;
74     private J2SEActionProvider actionProvider;
75     private DataFolder sourcePkg1;
76     private DataFolder sourcePkg2;
77     private DataFolder testPkg1;
78     private DataFolder testPkg2;
79     private DataObject someSource1;
80     private DataObject someSource2;
81     private DataObject someSource3;
82     private DataObject someTest1;
83     private DataObject someTest2;
84     
85     protected void setUp() throws Exception JavaDoc {
86         super.setUp();
87         TestUtil.setLookup(new Object JavaDoc[] {
88             new org.netbeans.modules.java.j2seproject.J2SEProjectType(),
89             new org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation(),
90             new SimplePlatformProvider (),
91         });
92         scratch = TestUtil.makeScratchDir(this);
93         projdir = scratch.createFolder("proj");
94         J2SEProjectGenerator.setDefaultSourceLevel(new SpecificationVersion ("1.4")); //NOI18N
95
helper = J2SEProjectGenerator.createProject(FileUtil.toFile(projdir),"proj","foo.Main","manifest.mf"); //NOI18N
96
J2SEProjectGenerator.setDefaultSourceLevel(null);
97         pm = ProjectManager.getDefault();
98         pp = pm.findProject(projdir);
99         actionProvider = (J2SEActionProvider)pp.getLookup().lookup(J2SEActionProvider.class);
100         sources = projdir.getFileObject("src");
101         tests = projdir.getFileObject("test");
102 // projdir.createData("build.xml");
103
build = projdir.createFolder("build");
104         build.createFolder("classes");
105         FileObject pkg = sources.createFolder("foo");
106         FileObject fo = pkg.createData("Bar.java");
107         sourcePkg1 = DataFolder.findFolder (pkg);
108         pkg = sources.createFolder("foo2");
109         sourcePkg2 = DataFolder.findFolder (pkg);
110         someSource1 = DataObject.find(fo);
111         fo = sources.getFileObject("foo").createData("Main.java");
112         createMain(fo);
113         someSource2 = DataObject.find(fo);
114         fo = sources.getFileObject("foo").createData("Third.java");
115         someSource3 = DataObject.find(fo);
116         pkg = tests.createFolder("foo");
117         fo = pkg.createData("BarTest.java");
118         testPkg1 = DataFolder.findFolder (pkg);
119         pkg = tests.createFolder("foo2");
120         testPkg2 = DataFolder.findFolder (pkg);
121         someTest1 = DataObject.find(fo);
122         fo = tests.getFileObject("foo").createData("MainTest.java");
123         someTest2 = DataObject.find(fo);
124         assertNotNull(someSource1);
125         assertNotNull(someSource2);
126         assertNotNull(someTest1);
127         assertNotNull(someTest2);
128     }
129
130     protected void tearDown() throws Exception JavaDoc {
131         scratch = null;
132         projdir = null;
133         pm = null;
134         TestUtil.setLookup(Lookup.EMPTY);
135         super.tearDown();
136     }
137     
138     private void createMain(FileObject fo) throws Exception JavaDoc {
139         FileLock lock = fo.lock();
140         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(fo.getOutputStream(lock));
141         pw.println("package foo;");
142         pw.println("public class Main { public static void main(String[] args){}; };");
143         pw.flush();
144         pw.close();
145         lock.releaseLock();
146     }
147     
148     public void testGetTargetNames() throws Exception JavaDoc {
149         implTestGetTargetNames();
150     }
151
152     public void testGetTargetNamesMultiRoots () throws Exception JavaDoc {
153         SourceRootsTest.addSourceRoot(helper, projdir, "src.other.dir","other");
154         implTestGetTargetNames();
155     }
156
157     public void implTestGetTargetNames () throws Exception JavaDoc {
158         Properties JavaDoc p;
159         Lookup context;
160         String JavaDoc[] targets;
161
162         // test COMMAND_COMPILE_SINGLE
163

164         p = new Properties JavaDoc();
165         context = Lookups.fixed(new DataObject[] {someSource1});
166         targets = actionProvider.getTargetNames(ActionProvider.COMMAND_COMPILE_SINGLE, context, p);
167         assertNotNull("Must found some targets for COMMAND_COMPILE_SINGLE", targets);
168         assertEquals("There must be one target for COMMAND_COMPILE_SINGLE", 1, targets.length);
169         assertEquals("Unexpected target name", "compile-single", targets[0]);
170         assertEquals("There must be one target parameter", 1, p.keySet().size());
171         assertEquals("There must be be target parameter", "foo/Bar.java", p.getProperty("javac.includes"));
172         p = new Properties JavaDoc();
173         context = Lookups.fixed(new DataObject[] {someTest1,someTest2});
174         targets = actionProvider.getTargetNames(ActionProvider.COMMAND_COMPILE_SINGLE, context, p);
175         assertNotNull("Must found some targets for COMMAND_COMPILE_SINGLE", targets);
176         assertEquals("There must be one target for COMMAND_COMPILE_SINGLE", 1, targets.length);
177         assertEquals("Unexpected target name", "compile-test-single", targets[0]);
178         assertEquals("There must be one target parameter", 1, p.keySet().size());
179         assertEquals("There must be be target parameter", "foo/BarTest.java,foo/MainTest.java", p.getProperty("javac.includes"));
180         p = new Properties JavaDoc();
181         context = Lookups.fixed(new DataObject[] {sourcePkg1});
182         targets = actionProvider.getTargetNames(ActionProvider.COMMAND_COMPILE_SINGLE, context, p);
183         assertNotNull("Must found some targets for COMMAND_COMPILE_SINGLE", targets);
184         assertEquals("There must be one target for COMMAND_COMPILE_SINGLE", 1, targets.length);
185         assertEquals("Unexpected target name", "compile-single", targets[0]);
186         assertEquals("There must be one target parameter", 1, p.keySet().size());
187         assertEquals("There must be be target parameter", "foo/**", p.getProperty("javac.includes"));
188         p = new Properties JavaDoc();
189         context = Lookups.fixed(new DataObject[] {sourcePkg1, sourcePkg2});
190         targets = actionProvider.getTargetNames(ActionProvider.COMMAND_COMPILE_SINGLE, context, p);
191         assertNotNull("Must found some targets for COMMAND_COMPILE_SINGLE", targets);
192         assertEquals("There must be one target for COMMAND_COMPILE_SINGLE", 1, targets.length);
193         assertEquals("Unexpected target name", "compile-single", targets[0]);
194         assertEquals("There must be one target parameter", 1, p.keySet().size());
195         assertEquals("There must be be target parameter", "foo/**,foo2/**", p.getProperty("javac.includes"));
196         p = new Properties JavaDoc();
197         context = Lookups.fixed(new DataObject[] {DataFolder.findFolder(sources)});
198         targets = actionProvider.getTargetNames(ActionProvider.COMMAND_COMPILE_SINGLE, context, p);
199         assertNotNull("Must found some targets for COMMAND_COMPILE_SINGLE", targets);
200         assertEquals("There must be one target for COMMAND_COMPILE_SINGLE", 1, targets.length);
201         assertEquals("Unexpected target name", "compile-single", targets[0]);
202         assertEquals("There must be one target parameter", 1, p.keySet().size());
203         assertEquals("There must be be target parameter", "**", p.getProperty("javac.includes"));
204         
205         p = new Properties JavaDoc();
206         context = Lookups.fixed(new Object JavaDoc[] {sourcePkg1, new NonRecursiveFolderImpl (sourcePkg1)});
207         targets = actionProvider.getTargetNames(ActionProvider.COMMAND_COMPILE_SINGLE, context, p);
208         assertNotNull("Must found some targets for COMMAND_COMPILE_SINGLE", targets);
209         assertEquals("There must be one target for COMMAND_COMPILE_SINGLE", 1, targets.length);
210         assertEquals("Unexpected target name", "compile-single", targets[0]);
211         assertEquals("There must be one target parameter", 1, p.keySet().size());
212         assertEquals("There must be be target parameter", "foo/*", p.getProperty("javac.includes"));
213         p = new Properties JavaDoc();
214         context = Lookups.fixed(new Object JavaDoc[] {sourcePkg1, sourcePkg2, new NonRecursiveFolderImpl(sourcePkg1), new NonRecursiveFolderImpl(sourcePkg2)});
215         targets = actionProvider.getTargetNames(ActionProvider.COMMAND_COMPILE_SINGLE, context, p);
216         assertNotNull("Must found some targets for COMMAND_COMPILE_SINGLE", targets);
217         assertEquals("There must be one target for COMMAND_COMPILE_SINGLE", 1, targets.length);
218         assertEquals("Unexpected target name", "compile-single", targets[0]);
219         assertEquals("There must be one target parameter", 1, p.keySet().size());
220         assertEquals("There must be be target parameter", "foo/*,foo2/*", p.getProperty("javac.includes"));
221         p = new Properties JavaDoc();
222         context = Lookups.fixed(new Object JavaDoc[] {DataFolder.findFolder(sources), new NonRecursiveFolderImpl(sources)});
223         targets = actionProvider.getTargetNames(ActionProvider.COMMAND_COMPILE_SINGLE, context, p);
224         assertNotNull("Must found some targets for COMMAND_COMPILE_SINGLE", targets);
225         assertEquals("There must be one target for COMMAND_COMPILE_SINGLE", 1, targets.length);
226         assertEquals("Unexpected target name", "compile-single", targets[0]);
227         assertEquals("There must be one target parameter", 1, p.keySet().size());
228         assertEquals("There must be be target parameter", "*", p.getProperty("javac.includes"));
229         
230         // test COMMAND_TEST_SINGLE
231

232         p = new Properties JavaDoc();
233         context = Lookups.fixed(new DataObject[] {someSource1});
234         targets = actionProvider.getTargetNames(ActionProvider.COMMAND_TEST_SINGLE, context, p);
235         assertNotNull("Must found some targets for COMMAND_TEST_SINGLE", targets);
236         assertEquals("There must be one target for COMMAND_TEST_SINGLE", 1, targets.length);
237         assertEquals("Unexpected target name", "test-single", targets[0]);
238         assertEquals("There must be one target parameter", 2, p.keySet().size());
239         assertEquals("There must be be target parameter", "foo/BarTest.java", p.getProperty("javac.includes"));
240         assertEquals("There must be be target parameter", "foo/BarTest.java", p.getProperty("test.includes"));
241         p = new Properties JavaDoc();
242         context = Lookups.fixed(new DataObject[] {someSource1,someSource2});
243         targets = actionProvider.getTargetNames(ActionProvider.COMMAND_TEST_SINGLE, context, p);
244         assertNotNull("Must found some targets for COMMAND_TEST_SINGLE", targets);
245         assertEquals("There must be one target for COMMAND_TEST_SINGLE", 1, targets.length);
246         assertEquals("Unexpected target name", "test-single", targets[0]);
247         assertEquals("There must be one target parameter", 2, p.keySet().size());
248         assertEquals("There must be be target parameter", "foo/BarTest.java,foo/MainTest.java", p.getProperty("javac.includes"));
249         assertEquals("There must be be target parameter", "foo/BarTest.java,foo/MainTest.java", p.getProperty("test.includes"));
250
251         // test COMMAND_DEBUG_TEST_SINGLE
252

253         p = new Properties JavaDoc();
254         context = Lookups.fixed(new DataObject[] {someSource1});
255         targets = actionProvider.getTargetNames(ActionProvider.COMMAND_DEBUG_TEST_SINGLE, context, p);
256         assertNotNull("Must found some targets for COMMAND_DEBUG_TEST_SINGLE", targets);
257         assertEquals("There must be one target for COMMAND_DEBUG_TEST_SINGLE", 1, targets.length);
258         assertEquals("Unexpected target name", "debug-test", targets[0]);
259         assertEquals("There must be one target parameter", 1, p.keySet().size());
260         assertEquals("There must be be target parameter", "foo.BarTest", p.getProperty("test.class"));
261
262         // test COMMAND_DEBUG_FIX
263

264         p = new Properties JavaDoc();
265         context = Lookups.fixed(new DataObject[] {someSource1});
266         targets = actionProvider.getTargetNames(JavaProjectConstants.COMMAND_DEBUG_FIX, context, p);
267         assertNotNull("Must found some targets for COMMAND_DEBUG_FIX", targets);
268         assertEquals("There must be one target for COMMAND_DEBUG_FIX", 1, targets.length);
269         assertEquals("Unexpected target name", "debug-fix", targets[0]);
270         assertEquals("There must be one target parameter", 1, p.keySet().size());
271         assertEquals("There must be be target parameter", "foo/Bar", p.getProperty("fix.includes"));
272         p = new Properties JavaDoc();
273         context = Lookups.fixed(new DataObject[] {someTest1});
274         targets = actionProvider.getTargetNames(JavaProjectConstants.COMMAND_DEBUG_FIX, context, p);
275         assertNotNull("Must found some targets for COMMAND_DEBUG_FIX", targets);
276         assertEquals("There must be one target for COMMAND_DEBUG_FIX", 1, targets.length);
277         assertEquals("Unexpected target name", "debug-fix-test", targets[0]);
278         assertEquals("There must be one target parameter", 1, p.keySet().size());
279         assertEquals("There must be be target parameter", "foo/BarTest", p.getProperty("fix.includes"));
280
281         // test COMMAND_RUN_SINGLE
282

283         p = new Properties JavaDoc();
284         context = Lookups.fixed(new DataObject[] {someSource2});
285         MainClassChooser.unitTestingSupport_hasMainMethodResult = Boolean.TRUE;
286         try {
287             targets = actionProvider.getTargetNames(ActionProvider.COMMAND_RUN_SINGLE, context, p);
288         } finally {
289             MainClassChooser.unitTestingSupport_hasMainMethodResult = null;
290         }
291         assertNotNull("Must found some targets for COMMAND_RUN_SINGLE", targets);
292         assertEquals("There must be one target for COMMAND_RUN_SINGLE", 1, targets.length);
293         assertEquals("Unexpected target name", "run-single", targets[0]);
294         assertEquals("There must be one target parameter", 2, p.keySet().size());
295         assertEquals("There must be be target parameter", "foo/Main.java", p.getProperty("javac.includes"));
296         assertEquals("There must be be target parameter", "foo.Main", p.getProperty("run.class"));
297         p = new Properties JavaDoc();
298         context = Lookups.fixed(new DataObject[] {someSource2});
299         MainClassChooser.unitTestingSupport_hasMainMethodResult = Boolean.FALSE;
300         AppletSupport.unitTestingSupport_isApplet = Boolean.TRUE;
301         try {
302             targets = actionProvider.getTargetNames(ActionProvider.COMMAND_RUN_SINGLE, context, p);
303         } finally {
304             MainClassChooser.unitTestingSupport_hasMainMethodResult = null;
305             AppletSupport.unitTestingSupport_isApplet = null;
306         }
307         assertNotNull("Must found some targets for COMMAND_RUN_SINGLE", targets);
308         assertEquals("There must be one target for COMMAND_RUN_SINGLE", 1, targets.length);
309         assertEquals("Unexpected target name", "run-applet", targets[0]);
310         assertEquals("There must be one target parameter", 2, p.keySet().size());
311         assertEquals("There must be be target parameter", "foo/Main.java", p.getProperty("javac.includes"));
312         FileObject appletHtml = build.getFileObject("Main", "html");
313         assertNotNull("Applet HTML page must be generated", appletHtml);
314         URL JavaDoc appletUrl = URLMapper.findURL(appletHtml, URLMapper.EXTERNAL);
315         assertEquals("There must be be target parameter", appletUrl.toExternalForm(), p.getProperty("applet.url"));
316         p = new Properties JavaDoc();
317         context = Lookups.fixed(new DataObject[] {someTest1});
318         MainClassChooser.unitTestingSupport_hasMainMethodResult = Boolean.TRUE;
319         AppletSupport.unitTestingSupport_isApplet = Boolean.TRUE;
320         try {
321             targets = actionProvider.getTargetNames(ActionProvider.COMMAND_RUN_SINGLE, context, p);
322         } finally {
323             MainClassChooser.unitTestingSupport_hasMainMethodResult = null;
324             AppletSupport.unitTestingSupport_isApplet = null;
325         }
326         assertNotNull("Must found some targets for COMMAND_RUN_SINGLE", targets);
327         assertEquals("There must be one target for COMMAND_RUN_SINGLE", 1, targets.length);
328         assertEquals("Unexpected target name", "test-single", targets[0]);
329         assertEquals("There must be one target parameter", 2, p.keySet().size());
330         assertEquals("There must be be target parameter", "foo/BarTest.java", p.getProperty("javac.includes"));
331         assertEquals("There must be be target parameter", "foo/BarTest.java", p.getProperty("test.includes"));
332
333         // test COMMAND_DEBUG_SINGLE
334

335         p = new Properties JavaDoc();
336         context = Lookups.fixed(new DataObject[] {someSource2});
337         MainClassChooser.unitTestingSupport_hasMainMethodResult = Boolean.TRUE;
338         try {
339             targets = actionProvider.getTargetNames(ActionProvider.COMMAND_DEBUG_SINGLE, context, p);
340         } finally {
341             MainClassChooser.unitTestingSupport_hasMainMethodResult = null;
342         }
343         assertNotNull("Must found some targets for COMMAND_DEBUG_SINGLE", targets);
344         assertEquals("There must be one target for COMMAND_DEBUG_SINGLE", 1, targets.length);
345         assertEquals("Unexpected target name", "debug-single", targets[0]);
346         assertEquals("There must be one target parameter", 2, p.keySet().size());
347         assertEquals("There must be be target parameter", "foo/Main.java", p.getProperty("javac.includes"));
348         assertEquals("There must be be target parameter", "foo.Main", p.getProperty("debug.class"));
349         p = new Properties JavaDoc();
350         context = Lookups.fixed(new DataObject[] {someSource2});
351         MainClassChooser.unitTestingSupport_hasMainMethodResult = Boolean.FALSE;
352         AppletSupport.unitTestingSupport_isApplet = Boolean.TRUE;
353         try {
354             targets = actionProvider.getTargetNames(ActionProvider.COMMAND_DEBUG_SINGLE, context, p);
355         } finally {
356             MainClassChooser.unitTestingSupport_hasMainMethodResult = null;
357             AppletSupport.unitTestingSupport_isApplet = null;
358         }
359         assertNotNull("Must found some targets for COMMAND_DEBUG_SINGLE", targets);
360         assertEquals("There must be one target for COMMAND_DEBUG_SINGLE", 1, targets.length);
361         assertEquals("Unexpected target name", "debug-applet", targets[0]);
362         assertEquals("There must be one target parameter", 3, p.keySet().size());
363         assertEquals("There must be be target parameter", "foo/Main.java", p.getProperty("javac.includes"));
364         appletHtml = build.getFileObject("Main", "html");
365         assertNotNull("Applet HTML page must be generated", appletHtml);
366         appletUrl = URLMapper.findURL(appletHtml, URLMapper.EXTERNAL);
367         assertEquals("There must be be target parameter", appletUrl.toExternalForm(), p.getProperty("applet.url"));
368         p = new Properties JavaDoc();
369         context = Lookups.fixed(new DataObject[] {someTest1});
370         MainClassChooser.unitTestingSupport_hasMainMethodResult = Boolean.TRUE;
371         AppletSupport.unitTestingSupport_isApplet = Boolean.TRUE;
372         try {
373             targets = actionProvider.getTargetNames(ActionProvider.COMMAND_DEBUG_SINGLE, context, p);
374         } finally {
375             MainClassChooser.unitTestingSupport_hasMainMethodResult = null;
376             AppletSupport.unitTestingSupport_isApplet = null;
377         }
378         assertNotNull("Must found some targets for COMMAND_DEBUG_SINGLE", targets);
379         assertEquals("There must be one target for COMMAND_DEBUG_SINGLE", 1, targets.length);
380         assertEquals("Unexpected target name", "debug-test", targets[0]);
381         assertEquals("There must be one target parameter", 1, p.keySet().size());
382         assertEquals("There must be be target parameter", "foo.BarTest", p.getProperty("test.class"));
383
384         // test COMMAND_RUN
385

386         p = new Properties JavaDoc();
387         context = Lookup.EMPTY;
388         MainClassChooser.unitTestingSupport_hasMainMethodResult = Boolean.TRUE;
389         try {
390             targets = actionProvider.getTargetNames(ActionProvider.COMMAND_RUN, context, p);
391         } finally {
392             MainClassChooser.unitTestingSupport_hasMainMethodResult = null;
393         }
394         assertNotNull("Must found some targets for COMMAND_RUN", targets);
395         assertEquals("There must be one target for COMMAND_RUN", 1, targets.length);
396         assertEquals("Unexpected target name", "run", targets[0]);
397         //The project is saved after the main.class property was added into the project's properties,
398
//it is no more needed to pass the main.class in the properties.
399
//See issue #61244: Main class setting not saved for J2SE Project during IDE session
400
assertEquals("There must be no target parameter", 0, p.keySet().size());
401         
402         // test COMMAND_DEBUG
403

404         p = new Properties JavaDoc();
405         context = Lookup.EMPTY;
406         MainClassChooser.unitTestingSupport_hasMainMethodResult = Boolean.TRUE;
407         try {
408             targets = actionProvider.getTargetNames(ActionProvider.COMMAND_DEBUG, context, p);
409         } finally {
410             MainClassChooser.unitTestingSupport_hasMainMethodResult = null;
411         }
412         assertNotNull("Must found some targets for COMMAND_DEBUG", targets);
413         assertEquals("There must be one target for COMMAND_DEBUG", 1, targets.length);
414         assertEquals("Unexpected target name", "debug", targets[0]);
415         //The project is saved after the main.class property was added into the project's properties,
416
//it is no more needed to pass it in the properties.
417
//See issue #61244: Main class setting not saved for J2SE Project during IDE session
418
assertEquals("There must be one target parameter", 1, p.keySet().size());
419         assertEquals("There must be be target parameter", "foo.Main", p.getProperty("debug.class"));
420
421         // test COMMAND_DEBUG_STEP_INTO
422

423         p = new Properties JavaDoc();
424         context = Lookup.EMPTY;
425         MainClassChooser.unitTestingSupport_hasMainMethodResult = Boolean.TRUE;
426         try {
427             targets = actionProvider.getTargetNames(ActionProvider.COMMAND_DEBUG_STEP_INTO, context, p);
428         } finally {
429             MainClassChooser.unitTestingSupport_hasMainMethodResult = null;
430         }
431         assertNotNull("Must found some targets for COMMAND_DEBUG_STEP_INTO", targets);
432         assertEquals("There must be one target for COMMAND_DEBUG_STEP_INTO", 1, targets.length);
433         assertEquals("Unexpected target name", "debug-stepinto", targets[0]);
434         //The project is saved after the main.class property was added into the project's properties,
435
//it is no more needed to pass it in the properties.
436
//See issue #61244: Main class setting not saved for J2SE Project during IDE session
437
assertEquals("There must be one target parameter", 1, p.keySet().size());
438         assertEquals("There must be be target parameter", "foo.Main", p.getProperty("debug.class"));
439     }
440     
441     public void testGetTargetNamesFromConfig() throws Exception JavaDoc {
442         final FileObject projdirFO = scratch.createFolder("projectwithconfigs");
443         J2SEProjectGenerator.createProject(FileUtil.toFile(projdirFO), "projectwithconfigs", null, null);
444         final J2SEProject proj = (J2SEProject) ProjectManager.getDefault().findProject(projdirFO);
445         final ProjectConfigurationProvider pcp = proj.getLookup().lookup(ProjectConfigurationProvider.class);
446         ProjectManager.mutex().writeAccess(new Mutex.ExceptionAction<Void JavaDoc>() {
447             public Void JavaDoc run() throws Exception JavaDoc {
448                 Properties JavaDoc props = new Properties JavaDoc();
449                 props.setProperty("main.class", "foo.Bar");
450                 props.setProperty("$target.build", "");
451                 props.setProperty("$target.run", "runtarget");
452                 props.setProperty("$target.debug", "debugtarget1 debugtarget2");
453                 write(props, projdirFO, "nbproject/configs/test.properties");
454                 props = new Properties JavaDoc();
455                 write(props, projdirFO, "nbproject/private/configs/test.properties");
456                 props = new Properties JavaDoc();
457                 props.setProperty("config", "test");
458                 write(props, projdirFO, "nbproject/private/config.properties");
459                 ProjectManager.getDefault().saveProject(proj);
460                 List JavaDoc<ProjectConfiguration> configs = new ArrayList JavaDoc<ProjectConfiguration>(pcp.getConfigurations());
461                 pcp.setActiveConfiguration(configs.get(1));
462                 return null;
463             }
464         });
465         J2SEActionProvider actionProvider = (J2SEActionProvider) proj.getLookup().lookup(J2SEActionProvider.class);
466         PropertyEvaluator eval = proj.evaluator();
467         String JavaDoc config = eval.getProperty("config");
468         assertEquals("Name of active config from Evaluator is test", "test", config);
469         FileObject sources = projdirFO.getFileObject("src");
470         FileObject pkg = sources.createFolder("foo");
471         FileObject file = pkg.createData("Bar.java");
472         DataObject srcDO = DataObject.find(file);
473         Lookup context = Lookups.fixed(new DataObject[] { srcDO });
474         MainClassChooser.unitTestingSupport_hasMainMethodResult = Boolean.TRUE;
475         try {
476             // test of targets defined in config
477
String JavaDoc[] targets = actionProvider.getTargetNames(ActionProvider.COMMAND_DEBUG, context, new Properties JavaDoc());
478             assertEquals("There must be two Debug targets in test config", 2, targets.length);
479             assertEquals("First Debug target name is debugtarget1", "debugtarget1", targets[0]);
480             assertEquals("Second Debug target name is debugtarget2", "debugtarget2", targets[1]);
481             targets = actionProvider.getTargetNames(ActionProvider.COMMAND_BUILD, context, new Properties JavaDoc());
482             assertEquals("There must be 1 Build target in test config", 1, targets.length);
483             // target is not in fact from the config, config contains empty string
484
assertEquals("Build target name is jar", "jar", targets[0]);
485             targets = actionProvider.getTargetNames(ActionProvider.COMMAND_RUN, context, new Properties JavaDoc());
486             assertEquals("There must be 1 Run target in test config", 1, targets.length);
487             assertEquals("Run target name is runtarget", "runtarget", targets[0]);
488             // test of targets not in config
489
targets = actionProvider.getTargetNames(ActionProvider.COMMAND_CLEAN, context, new Properties JavaDoc());
490             assertEquals("There must be 1 Clean target", 1, targets.length);
491             assertEquals("Clean target name is runtarget", "clean", targets[0]);
492         } finally {
493             MainClassChooser.unitTestingSupport_hasMainMethodResult = null;
494         }
495     }
496     
497     public void testIsActionEnabled() throws Exception JavaDoc {
498         implTestIsActionEnabled();
499     }
500
501     public void testIsActionEnabledMultiRoot() throws Exception JavaDoc {
502         FileObject newRoot = SourceRootsTest.addSourceRoot(helper, projdir, "src.other.dir","other");
503         implTestIsActionEnabled();
504         Lookup context = Lookups.fixed(new DataObject[] {sourcePkg1, DataFolder.findFolder(newRoot)});
505         boolean enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
506         assertFalse ("COMMAND_COMPILE_SINGLE must be disabled on multiple src packages from different roots", enabled);
507     }
508
509     private void implTestIsActionEnabled () throws Exception JavaDoc {
510         Lookup context;
511         boolean enabled;
512
513         // test COMMAND_COMPILE_SINGLE
514

515         context = Lookups.fixed(new DataObject[] {someSource1});
516         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
517         assertTrue("COMMAND_COMPILE_SINGLE must be enabled on one source", enabled);
518
519         context = Lookups.fixed(new DataObject[] {someSource1, someSource2});
520         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
521         assertTrue("COMMAND_COMPILE_SINGLE must be enabled on multiple sources", enabled);
522
523         context = Lookups.fixed(new DataObject[] {someTest1, someTest2});
524         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
525         assertTrue("COMMAND_COMPILE_SINGLE must be enabled on multiple tests", enabled);
526
527         context = Lookups.fixed(new DataObject[] {someSource1, someTest1});
528         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
529         assertFalse("COMMAND_COMPILE_SINGLE must be disabled on mixed files", enabled);
530
531         context = Lookups.fixed(new DataObject[] {sourcePkg1});
532         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
533         assertTrue ("COMMAND_COMPILE_SINGLE must be enabled on one src package", enabled);
534
535         context = Lookups.fixed(new DataObject[] {sourcePkg1, sourcePkg2});
536         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
537         assertTrue ("COMMAND_COMPILE_SINGLE must be enabled on multiple src packages", enabled);
538
539         context = Lookups.fixed(new DataObject[] {sourcePkg1, someSource1});
540         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
541         assertTrue ("COMMAND_COMPILE_SINGLE must be enabled on mixed src packages/files", enabled);
542
543
544         context = Lookups.fixed(new DataObject[] {testPkg1});
545         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
546         assertTrue ("COMMAND_COMPILE_SINGLE must be enabled on one test package", enabled);
547
548         context = Lookups.fixed(new DataObject[] {testPkg1, testPkg2});
549         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
550         assertTrue ("COMMAND_COMPILE_SINGLE must be enabled on multiple test packages", enabled);
551
552         context = Lookups.fixed(new DataObject[] {testPkg1, someTest1});
553         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
554         assertTrue ("COMMAND_COMPILE_SINGLE must be enabled on mixed test packages/files", enabled);
555
556         context = Lookups.fixed(new DataObject[] {DataFolder.findFolder(projdir)});
557         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
558         assertFalse ("COMMAND_COMPILE_SINGLE must not be enabled on non source folder", enabled);
559
560
561         context = Lookups.fixed(new DataObject[] {sourcePkg1, testPkg1});
562         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_COMPILE_SINGLE, context);
563         assertFalse ("COMMAND_COMPILE_SINGLE must not be enabled on non mixed packages", enabled);
564
565         // test COMMAND_TEST_SINGLE
566

567         context = Lookups.fixed(new DataObject[] {someTest1});
568         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_TEST_SINGLE, context);
569         assertFalse("COMMAND_TEST_SINGLE must be disabled on one test", enabled);
570
571         context = Lookups.fixed(new DataObject[] {someTest1, someTest2});
572         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_TEST_SINGLE, context);
573         assertFalse("COMMAND_TEST_SINGLE must be disabled on multiple tests", enabled);
574
575         context = Lookups.fixed(new DataObject[] {someSource3});
576         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_TEST_SINGLE, context);
577         assertFalse("COMMAND_TEST_SINGLE must be disabled on non-test file which does not have associated test", enabled);
578
579         context = Lookups.fixed(new DataObject[] {someSource2});
580         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_TEST_SINGLE, context);
581         assertTrue("COMMAND_TEST_SINGLE must be enabled on source file which has associated test", enabled);
582
583         context = Lookups.fixed(new DataObject[] {someSource1, someSource2});
584         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_TEST_SINGLE, context);
585         assertTrue("COMMAND_TEST_SINGLE must be enabled on source files which has associated tests", enabled);
586
587         context = Lookups.fixed(new DataObject[] {someSource1, someSource3});
588         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_TEST_SINGLE, context);
589         assertFalse("COMMAND_TEST_SINGLE must be disabled on mixture of source files when some files do not have tests", enabled);
590
591         context = Lookups.fixed(new DataObject[] {someSource1, someTest1});
592         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_TEST_SINGLE, context);
593         assertFalse("COMMAND_TEST_SINGLE must be disabled on mixture of source files and test files", enabled);
594
595         // test COMMAND_DEBUG_TEST_SINGLE
596

597         context = Lookups.fixed(new DataObject[] {someTest1});
598         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_DEBUG_TEST_SINGLE, context);
599         assertFalse("COMMAND_DEBUG_TEST_SINGLE must be disabled on test files", enabled);
600
601         context = Lookups.fixed(new DataObject[] {someTest1, someTest2});
602         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_DEBUG_TEST_SINGLE, context);
603         assertFalse("COMMAND_DEBUG_TEST_SINGLE must be disabled on multiple tests", enabled);
604
605         context = Lookups.fixed(new DataObject[] {someSource3});
606         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_DEBUG_TEST_SINGLE, context);
607         assertFalse("COMMAND_DEBUG_TEST_SINGLE must be disabled on non-test file which does not have associated test", enabled);
608
609         context = Lookups.fixed(new DataObject[] {someSource2});
610         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_DEBUG_TEST_SINGLE, context);
611         assertTrue("COMMAND_DEBUG_TEST_SINGLE must be enabled on source file which has associated test", enabled);
612
613         context = Lookups.fixed(new DataObject[] {someSource1, someSource2});
614         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_DEBUG_TEST_SINGLE, context);
615         assertFalse("COMMAND_DEBUG_TEST_SINGLE must be disabled on multiple source files", enabled);
616
617         // test COMMAND_DEBUG_FIX
618

619         context = Lookups.fixed(new DataObject[] {someTest1});
620         enabled = actionProvider.isActionEnabled(JavaProjectConstants.COMMAND_DEBUG_FIX, context);
621         assertTrue("COMMAND_DEBUG_FIX must be enabled on one test", enabled);
622
623         context = Lookups.fixed(new DataObject[] {someTest1, someTest2});
624         enabled = actionProvider.isActionEnabled(JavaProjectConstants.COMMAND_DEBUG_FIX, context);
625         assertFalse("COMMAND_DEBUG_FIX must be disabled on multiple tests", enabled);
626
627         context = Lookups.fixed(new DataObject[] {someSource1});
628         enabled = actionProvider.isActionEnabled(JavaProjectConstants.COMMAND_DEBUG_FIX, context);
629         assertTrue("COMMAND_DEBUG_FIX must be enabled on one source", enabled);
630
631         context = Lookups.fixed(new DataObject[] {someSource1, someSource2});
632         enabled = actionProvider.isActionEnabled(JavaProjectConstants.COMMAND_DEBUG_FIX, context);
633         assertFalse("COMMAND_DEBUG_FIX must be disabled on multiple source files", enabled);
634
635         context = Lookups.fixed(new DataObject[] {someSource1, someTest1});
636         enabled = actionProvider.isActionEnabled(JavaProjectConstants.COMMAND_DEBUG_FIX, context);
637         assertFalse("COMMAND_DEBUG_FIX must be disabled on multiple mixed files", enabled);
638
639         // test COMMAND_RUN_SINGLE
640

641         context = Lookups.fixed(new DataObject[] {someSource1});
642         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_RUN_SINGLE, context);
643         assertTrue("COMMAND_RUN_SINGLE must be enabled on one source", enabled);
644
645         context = Lookups.fixed(new DataObject[] {someSource1, someSource2});
646         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_RUN_SINGLE, context);
647         assertFalse("COMMAND_RUN_SINGLE must be disabled on multiple sources", enabled);
648
649         context = Lookups.fixed(new DataObject[] {someTest1});
650         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_RUN_SINGLE, context);
651         assertTrue("COMMAND_RUN_SINGLE must be enabled on test file", enabled);
652
653         context = Lookups.fixed(new DataObject[] {someTest1, someTest2});
654         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_RUN_SINGLE, context);
655         assertFalse("COMMAND_RUN_SINGLE must be disabled on multiple test files", enabled);
656
657         context = Lookups.fixed(new DataObject[] {someSource1, someTest1});
658         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_RUN_SINGLE, context);
659         assertFalse("COMMAND_RUN_SINGLE must be disabled on mixed multiple test files", enabled);
660
661         // test COMMAND_DEBUG_SINGLE
662

663         context = Lookups.fixed(new DataObject[] {someSource1});
664         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_DEBUG_SINGLE, context);
665         assertTrue("COMMAND_DEBUG_SINGLE must be enabled on one source", enabled);
666
667         context = Lookups.fixed(new DataObject[] {someSource1, someSource2});
668         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_DEBUG_SINGLE, context);
669         assertFalse("COMMAND_DEBUG_SINGLE must be disabled on multiple sources", enabled);
670
671         context = Lookups.fixed(new DataObject[] {someTest1});
672         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_DEBUG_SINGLE, context);
673         assertTrue("COMMAND_DEBUG_SINGLE must be enabled on test file", enabled);
674
675         context = Lookups.fixed(new DataObject[] {someTest1, someTest2});
676         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_DEBUG_SINGLE, context);
677         assertFalse("COMMAND_DEBUG_SINGLE must be disabled on multiple test files", enabled);
678
679         context = Lookups.fixed(new DataObject[] {someSource1, someTest1});
680         enabled = actionProvider.isActionEnabled(ActionProvider.COMMAND_DEBUG_SINGLE, context);
681         assertFalse("COMMAND_DEBUG_SINGLE must be disabled on mixed multiple test files", enabled);
682     }
683     
684     
685     private static final class NonRecursiveFolderImpl implements NonRecursiveFolder {
686         
687         private FileObject fobj;
688         
689         public NonRecursiveFolderImpl (DataObject dobj) {
690             assert dobj != null;
691             this.fobj = dobj.getPrimaryFile();
692         }
693         
694         public NonRecursiveFolderImpl (FileObject fobj) {
695             assert fobj != null;
696             this.fobj = fobj;
697         }
698                 
699         public FileObject getFolder() {
700             return this.fobj;
701         }
702     }
703     
704     private static class SimplePlatformProvider implements org.netbeans.modules.java.platform.JavaPlatformProvider {
705         
706         public void removePropertyChangeListener(java.beans.PropertyChangeListener JavaDoc listener) {
707         }
708
709         public void addPropertyChangeListener(java.beans.PropertyChangeListener JavaDoc listener) {
710         }
711
712         public org.netbeans.api.java.platform.JavaPlatform[] getInstalledPlatforms() {
713             return new org.netbeans.api.java.platform.JavaPlatform[] {
714                 getDefaultPlatform ()
715             };
716         }
717
718         public org.netbeans.api.java.platform.JavaPlatform getDefaultPlatform() {
719             return new TestDefaultPlatform ();
720         }
721         
722     }
723     
724     private static class TestDefaultPlatform extends org.netbeans.api.java.platform.JavaPlatform {
725         
726         public TestDefaultPlatform () {
727             
728         }
729
730         public FileObject findTool(String JavaDoc toolName) {
731             return null;
732         }
733
734         public String JavaDoc getDisplayName() {
735             return "Default Platform";
736         }
737
738         public org.netbeans.api.java.classpath.ClassPath getBootstrapLibraries() {
739             return null;
740         }
741
742         public java.util.Collection JavaDoc getInstallFolders() {
743             return null;
744         }
745
746         public org.netbeans.api.java.classpath.ClassPath getStandardLibraries() {
747             return null;
748         }
749
750         public String JavaDoc getVendor() {
751             return null;
752         }
753
754         public org.netbeans.api.java.platform.Specification getSpecification() {
755             return new org.netbeans.api.java.platform.Specification ("j2se", new SpecificationVersion ("1.4"));
756         }
757
758         public org.netbeans.api.java.classpath.ClassPath getSourceFolders() {
759             return null;
760         }
761
762         public java.util.List JavaDoc getJavadocFolders() {
763             return null;
764         }
765
766         public java.util.Map JavaDoc getProperties() {
767             return Collections.singletonMap("platform.ant.name","default_platform");
768         }
769         
770     }
771     
772     private void write(Properties JavaDoc p, FileObject d, String JavaDoc path) throws IOException JavaDoc {
773         FileObject f = FileUtil.createData(d, path);
774         OutputStream JavaDoc os = f.getOutputStream();
775         p.store(os, null);
776         os.close();
777     }
778     
779     private static Collection JavaDoc<? extends ProjectConfiguration> getConfigurations(ProjectConfigurationProvider<?> pcp) {
780         return pcp.getConfigurations();
781     }
782
783     @SuppressWarnings JavaDoc("unchecked")
784     private static void setActiveConfiguration(ProjectConfigurationProvider<?> pcp, ProjectConfiguration pc) throws IOException JavaDoc {
785         ProjectConfigurationProvider _pcp = pcp;
786         _pcp.setActiveConfiguration(pc);
787     }
788
789 }
790
Popular Tags