KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > JUnitLaunchConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2003, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * David Saff (saff@mit.edu) - bug 102632: [JUnit] Support for JUnit 4.
11  *******************************************************************************/

12 package org.eclipse.pde.internal.ui.launcher;
13
14 import java.io.File JavaDoc;
15 import java.lang.reflect.InvocationTargetException JavaDoc;
16 import java.net.URL JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.TreeMap JavaDoc;
20
21 import org.eclipse.core.resources.IProject;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.runtime.CoreException;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.core.runtime.IProgressMonitor;
26 import org.eclipse.core.runtime.IStatus;
27 import org.eclipse.core.runtime.NullProgressMonitor;
28 import org.eclipse.core.runtime.Path;
29 import org.eclipse.core.runtime.Status;
30 import org.eclipse.core.runtime.SubProgressMonitor;
31 import org.eclipse.debug.core.DebugPlugin;
32 import org.eclipse.debug.core.ILaunch;
33 import org.eclipse.debug.core.ILaunchConfiguration;
34 import org.eclipse.jdt.core.IJavaProject;
35 import org.eclipse.jdt.core.IType;
36 import org.eclipse.jdt.internal.junit.launcher.JUnitBaseLaunchConfiguration;
37 import org.eclipse.jdt.internal.junit.launcher.TestSearchResult;
38 import org.eclipse.jdt.launching.ExecutionArguments;
39 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
40 import org.eclipse.jdt.launching.IVMInstall;
41 import org.eclipse.jdt.launching.IVMRunner;
42 import org.eclipse.jdt.launching.SocketUtil;
43 import org.eclipse.jdt.launching.VMRunnerConfiguration;
44 import org.eclipse.jface.dialogs.IDialogConstants;
45 import org.eclipse.osgi.util.NLS;
46 import org.eclipse.pde.core.plugin.IFragment;
47 import org.eclipse.pde.core.plugin.IFragmentModel;
48 import org.eclipse.pde.core.plugin.IPluginImport;
49 import org.eclipse.pde.core.plugin.IPluginModelBase;
50 import org.eclipse.pde.internal.core.ClasspathHelper;
51 import org.eclipse.pde.internal.core.ModelEntry;
52 import org.eclipse.pde.internal.core.PDECore;
53 import org.eclipse.pde.internal.core.PDEManager;
54 import org.eclipse.pde.internal.core.PDEState;
55 import org.eclipse.pde.internal.core.PluginModelManager;
56 import org.eclipse.pde.internal.core.TargetPlatform;
57 import org.eclipse.pde.internal.core.util.CoreUtility;
58 import org.eclipse.pde.internal.ui.IPDEUIConstants;
59 import org.eclipse.pde.internal.ui.PDEPlugin;
60 import org.eclipse.pde.internal.ui.PDEUIMessages;
61 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
62 import org.eclipse.swt.widgets.Display;
63 import org.eclipse.update.configurator.ConfiguratorUtils;
64
65
66 public class JUnitLaunchConfiguration extends JUnitBaseLaunchConfiguration {
67
68     public static final String JavaDoc CORE_APPLICATION = "org.eclipse.pde.junit.runtime.coretestapplication"; //$NON-NLS-1$
69
public static final String JavaDoc UI_APPLICATION = "org.eclipse.pde.junit.runtime.uitestapplication"; //$NON-NLS-1$
70

71     protected static IPluginModelBase[] registryPlugins;
72     protected File JavaDoc fConfigDir = null;
73
74     public void launch(
75         ILaunchConfiguration configuration,
76         String JavaDoc mode,
77         ILaunch launch,
78         IProgressMonitor monitor)
79         throws CoreException {
80         try {
81             fConfigDir = null;
82             monitor.beginTask("", 7); //$NON-NLS-1$
83
TestSearchResult testSearchResult = findTestTypes(configuration, monitor);
84             IType[] testTypes = testSearchResult.getTypes();
85             monitor.worked(1);
86             
87             LauncherUtils.validateProjectDependencies(configuration, new SubProgressMonitor(monitor, 1));
88             
89             String JavaDoc workspace = LaunchArgumentsHelper.getWorkspaceLocation(configuration);
90             if (!LauncherUtils.clearWorkspace(configuration, workspace, new SubProgressMonitor(monitor, 1))) {
91                 monitor.setCanceled(true);
92                 return;
93             }
94
95             if (configuration.getAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA, false))
96                 CoreUtility.deleteContent(getConfigDir(configuration));
97             launch.setAttribute(IPDELauncherConstants.CONFIG_LOCATION, getConfigDir(configuration).toString());
98             
99             int port = SocketUtil.findFreePort();
100             VMRunnerConfiguration runnerConfig = createVMRunner(configuration, testSearchResult, port, mode);
101             if (runnerConfig == null) {
102                 monitor.setCanceled(true);
103                 return;
104             }
105             monitor.worked(1);
106             
107             setDefaultSourceLocator(launch, configuration);
108             LaunchConfigurationHelper.synchronizeManifests(configuration, getConfigDir(configuration));
109             launch.setAttribute(PORT_ATTR, Integer.toString(port));
110             launch.setAttribute(TESTTYPE_ATTR, testTypes[0].getHandleIdentifier());
111             PDEPlugin.getDefault().getLaunchListener().manage(launch);
112             IVMRunner runner = getVMRunner(configuration, mode);
113             if (runner != null)
114                 runner.run(runnerConfig, launch, monitor);
115             else
116                 monitor.setCanceled(true);
117             monitor.worked(1);
118         } catch (CoreException e) {
119             monitor.setCanceled(true);
120             throw e;
121         }
122     }
123     
124     public IVMRunner getVMRunner(ILaunchConfiguration configuration, String JavaDoc mode) throws CoreException {
125         IVMInstall launcher = VMHelper.createLauncher(configuration);
126         return launcher.getVMRunner(mode);
127     }
128     /*
129      * @see JUnitBaseLauncherDelegate#configureVM(IType[], int, String)
130      */

131     protected VMRunnerConfiguration createVMRunner(
132         ILaunchConfiguration configuration,
133         TestSearchResult testTypes,
134         int port,
135         String JavaDoc runMode)
136         throws CoreException {
137
138         // Program arguments
139
String JavaDoc[] programArgs = getProgramArgumentsArray(configuration, testTypes, port, runMode);
140         if (programArgs == null)
141             return null;
142
143         VMRunnerConfiguration runnerConfig =
144             new VMRunnerConfiguration("org.eclipse.core.launcher.Main", getClasspath(configuration)); //$NON-NLS-1$
145
runnerConfig.setVMArguments(getVMArgumentsArray(configuration));
146         runnerConfig.setProgramArguments(programArgs);
147         runnerConfig.setEnvironment(getEnvironment(configuration));
148         runnerConfig.setWorkingDirectory(getWorkingDirectory(configuration).getAbsolutePath());
149         runnerConfig.setVMSpecificAttributesMap(getVMSpecificAttributesMap(configuration));
150         return runnerConfig;
151     }
152
153     protected String JavaDoc getTestPluginId(ILaunchConfiguration configuration)
154         throws CoreException {
155         IJavaProject javaProject = getJavaProject(configuration);
156         IPluginModelBase model =
157             PDECore.getDefault().getModelManager().findModel(javaProject.getProject());
158         if (model == null)
159             throw new CoreException(
160                 new Status(
161                     IStatus.ERROR,
162                     IPDEUIConstants.PLUGIN_ID,
163                     IStatus.ERROR,
164                     PDEUIMessages.JUnitLaunchConfiguration_error_notaplugin,
165                     null));
166         if (model instanceof IFragmentModel)
167             return ((IFragmentModel)model).getFragment().getPluginId();
168
169         return model.getPluginBase().getId();
170     }
171     
172     protected void abort(String JavaDoc message, Throwable JavaDoc exception, int code)
173         throws CoreException {
174         throw new CoreException(new Status(IStatus.ERROR, IPDEUIConstants.PLUGIN_ID, code, message, exception));
175     }
176     
177     public String JavaDoc[] getProgramArgumentsArray(
178         ILaunchConfiguration configuration,
179         TestSearchResult testSearchResult,
180         int port,
181         String JavaDoc runMode)
182         throws CoreException {
183         ArrayList JavaDoc programArgs = new ArrayList JavaDoc();
184         
185         // Get the list of plug-ins to run
186
Map JavaDoc pluginMap = LaunchPluginValidator.getPluginsToRun(configuration);
187         
188         if (configuration.getAttribute(IPDELauncherConstants.AUTOMATIC_VALIDATE, false)) {
189             IPluginModelBase[] models = (IPluginModelBase[])pluginMap.values().toArray(new IPluginModelBase[pluginMap.size()]);
190             final PluginValidationOperation op = new PluginValidationOperation(models);
191             try {
192                 op.run(new NullProgressMonitor());
193             } catch (InvocationTargetException JavaDoc e) {
194             } catch (InterruptedException JavaDoc e) {
195             } finally {
196                 if (op.hasErrors()) {
197                     final int[] result = new int[1];
198                     final Display display = LauncherUtils.getDisplay();
199                     display.syncExec(new Runnable JavaDoc() {
200                         public void run() {
201                             result[0] = new PluginValidationDialog(display.getActiveShell(), op).open();
202                     }});
203                     if (result[0] == IDialogConstants.CANCEL_ID) {
204                         return null;
205                     }
206                 }
207             }
208         }
209
210         addRequiredPlugins(pluginMap, testSearchResult);
211         
212         programArgs.addAll(getBasicArguments(configuration, port, runMode, testSearchResult));
213         
214         // Specify the application to launch based on the list of plug-ins to run.
215
programArgs.add("-application"); //$NON-NLS-1$
216
programArgs.add(getApplicationName(pluginMap, configuration));
217         
218         // If a product is specified, then add it to the program args
219
if (configuration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) {
220             programArgs.add("-product"); //$NON-NLS-1$
221
programArgs.add(configuration.getAttribute(IPDELauncherConstants.PRODUCT, "")); //$NON-NLS-1$
222
} else {
223             // Specify the application to test
224
String JavaDoc testApplication = configuration.getAttribute(IPDELauncherConstants.APP_TO_TEST, (String JavaDoc)null);
225             if (testApplication != null && testApplication.length() > 0) {
226                 programArgs.add("-testApplication"); //$NON-NLS-1$
227
programArgs.add(testApplication);
228             }
229         }
230         
231         // Specify the location of the runtime workbench
232
String JavaDoc targetWorkspace = LaunchArgumentsHelper.getWorkspaceLocation(configuration);
233         if (targetWorkspace.length() > 0) {
234             programArgs.add("-data"); //$NON-NLS-1$
235
programArgs.add(targetWorkspace);
236         }
237         
238         // Create the platform configuration for the runtime workbench
239
String JavaDoc productID = LaunchConfigurationHelper.getProductID(configuration);
240         LaunchConfigurationHelper.createConfigIniFile(configuration,
241                 productID, pluginMap, getConfigDir(configuration));
242         TargetPlatform.createPlatformConfigurationArea(
243                 pluginMap,
244                 getConfigDir(configuration),
245                 LaunchConfigurationHelper.getContributingPlugin(productID));
246         
247         programArgs.add("-configuration"); //$NON-NLS-1$
248
programArgs.add("file:" + new Path(getConfigDir(configuration).getPath()).addTrailingSeparator().toString()); //$NON-NLS-1$
249

250         // Specify the output folder names
251
programArgs.add("-dev"); //$NON-NLS-1$
252
programArgs.add(ClasspathHelper.getDevEntriesProperties(getConfigDir(configuration).toString() + "/dev.properties", pluginMap)); //$NON-NLS-1$
253

254         // necessary for PDE to know how to load plugins when target platform = host platform
255
// see PluginPathFinder.getPluginPaths()
256
if (pluginMap.containsKey(PDECore.getPluginId()))
257             programArgs.add("-pdelaunch"); //$NON-NLS-1$
258

259         // Create the .options file if tracing is turned on
260
if (configuration.getAttribute(IPDELauncherConstants.TRACING, false)
261                 && !IPDELauncherConstants.TRACING_NONE.equals(configuration.getAttribute(
262                         IPDELauncherConstants.TRACING_CHECKED, (String JavaDoc) null))) {
263             programArgs.add("-debug"); //$NON-NLS-1$
264
String JavaDoc path = getConfigDir(configuration).getPath() + IPath.SEPARATOR + ".options"; //$NON-NLS-1$
265
programArgs.add(LaunchArgumentsHelper.getTracingFileArgument(configuration, path));
266         }
267         
268         // add the program args specified by the user
269
String JavaDoc[] userArgs = LaunchArgumentsHelper.getUserProgramArgumentArray(configuration);
270         for (int i = 0; i < userArgs.length; i++) {
271             // be forgiving if people have tracing turned on and forgot
272
// to remove the -debug from the program args field.
273
if (userArgs[i].equals("-debug") && programArgs.contains("-debug")) //$NON-NLS-1$ //$NON-NLS-2$
274
continue;
275             programArgs.add(userArgs[i]);
276         }
277         
278         if (!programArgs.contains("-os")) { //$NON-NLS-1$
279
programArgs.add("-os"); //$NON-NLS-1$
280
programArgs.add(TargetPlatform.getOS());
281         }
282         if (!programArgs.contains("-ws")) { //$NON-NLS-1$
283
programArgs.add("-ws"); //$NON-NLS-1$
284
programArgs.add(TargetPlatform.getWS());
285         }
286         if (!programArgs.contains("-arch")) { //$NON-NLS-1$
287
programArgs.add("-arch"); //$NON-NLS-1$
288
programArgs.add(TargetPlatform.getOSArch());
289         }
290             
291         programArgs.add("-testpluginname"); //$NON-NLS-1$
292
programArgs.add(getTestPluginId(configuration));
293         
294         programArgs.add("-loaderpluginname"); //$NON-NLS-1$
295
programArgs.add(testSearchResult.getTestKind().getLoaderPluginId());
296         
297         String JavaDoc testFailureNames = configuration.getAttribute(JUnitBaseLaunchConfiguration.FAILURES_FILENAME_ATTR, ""); //$NON-NLS-1$
298
if (testFailureNames.length() > 0) {
299             programArgs.add("-testfailures"); //$NON-NLS-1$
300
programArgs.add(testFailureNames);
301         }
302
303         // a testname was specified just run the single test
304
IType[] testTypes = testSearchResult.getTypes();
305         String JavaDoc testName =
306             configuration.getAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, ""); //$NON-NLS-1$
307
if (testName.length() > 0) {
308             programArgs.add("-test"); //$NON-NLS-1$
309
programArgs.add(testTypes[0].getFullyQualifiedName() + ":" + testName); //$NON-NLS-1$
310
} else {
311             programArgs.add("-classnames"); //$NON-NLS-1$
312
for (int i = 0; i < testTypes.length; i++)
313             programArgs.add(testTypes[i].getFullyQualifiedName());
314         }
315         return (String JavaDoc[]) programArgs.toArray(new String JavaDoc[programArgs.size()]);
316     }
317         
318     protected IPluginModelBase[] addRequiredPlugins(Map JavaDoc pluginMap, TestSearchResult result)
319         throws CoreException {
320         addRequiredPlugin(pluginMap, "org.eclipse.pde.junit.runtime"); //$NON-NLS-1$
321
addRequiredPlugin(pluginMap, "org.eclipse.jdt.junit.runtime"); //$NON-NLS-1$
322
addRequiredPlugin(pluginMap, "org.junit"); //$NON-NLS-1$
323
addRequiredPlugin(pluginMap, result.getTestKind().getLoaderPluginId());
324         return (IPluginModelBase[]) pluginMap.values().toArray(
325             new IPluginModelBase[pluginMap.size()]);
326     }
327     
328     private void addRequiredPlugin(Map JavaDoc pluginMap, final String JavaDoc pluginId) throws CoreException {
329         if (!pluginMap.containsKey(pluginId)) { //$NON-NLS-1$
330
pluginMap.put(pluginId, findPlugin(pluginId)); //$NON-NLS-1$ //$NON-NLS-2$
331
}
332     }
333     
334     protected IPluginModelBase findPlugin(String JavaDoc id) throws CoreException {
335         PluginModelManager manager = PDECore.getDefault().getModelManager();
336         IPluginModelBase model = manager.findModel(id);
337         if (model != null)
338             return model;
339
340         if (registryPlugins == null) {
341             URL JavaDoc[] pluginPaths = ConfiguratorUtils.getCurrentPlatformConfiguration().getPluginPath();
342             PDEState state = new PDEState(pluginPaths, false, new NullProgressMonitor());
343             registryPlugins = state.getTargetModels();
344         }
345
346         for (int i = 0; i < registryPlugins.length; i++) {
347             if (registryPlugins[i].getPluginBase().getId().equals(id))
348                 return registryPlugins[i];
349         }
350         abort(
351             NLS.bind(PDEUIMessages.JUnitLaunchConfiguration_error_missingPlugin, id),
352             null,
353             IStatus.OK);
354         return null;
355     }
356     
357     public String JavaDoc[] getVMArgumentsArray(ILaunchConfiguration configuration) throws CoreException {
358         return new ExecutionArguments(getVMArguments(configuration),"").getVMArgumentsArray(); //$NON-NLS-1$
359
}
360     
361     public String JavaDoc getProgramArguments(ILaunchConfiguration configuration)
362         throws CoreException {
363         return LaunchArgumentsHelper.getUserProgramArguments(configuration);
364     }
365     
366     public String JavaDoc getVMArguments(ILaunchConfiguration configuration)
367         throws CoreException {
368         return LaunchArgumentsHelper.getUserVMArguments(configuration);
369     }
370     
371     public String JavaDoc[] getEnvironment(ILaunchConfiguration configuration) throws CoreException {
372         return DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
373     }
374     
375     public String JavaDoc[] getClasspath(ILaunchConfiguration configuration) throws CoreException {
376         String JavaDoc[] classpath = LaunchArgumentsHelper.constructClasspath(configuration);
377         if (classpath == null) {
378             abort(PDEUIMessages.WorkbenchLauncherConfigurationDelegate_noStartup, null, IStatus.OK);
379         }
380         return classpath;
381     }
382     
383     public File JavaDoc getWorkingDirectory(ILaunchConfiguration configuration) throws CoreException {
384         return LaunchArgumentsHelper.getWorkingDirectory(configuration);
385     }
386     
387     public Map JavaDoc getVMSpecificAttributesMap(ILaunchConfiguration configuration) throws CoreException {
388         return LaunchArgumentsHelper.getVMSpecificAttributesMap(configuration);
389     }
390
391     protected void setDefaultSourceLocator(ILaunch launch, ILaunchConfiguration configuration) throws CoreException {
392         LauncherUtils.setDefaultSourceLocator(configuration);
393     }
394     
395     protected String JavaDoc getDefaultWorkspace(ILaunchConfiguration config) throws CoreException {
396         if (config.getAttribute(IPDELauncherConstants.APPLICATION, UI_APPLICATION).equals(UI_APPLICATION))
397             return "${workspace_loc}/../junit-workbench-workspace"; //$NON-NLS-1$
398
return "${workspace_loc}/../junit-core-workspace"; //$NON-NLS-1$
399
}
400     
401     protected String JavaDoc getApplicationName(Map JavaDoc pluginMap, ILaunchConfiguration configuration) {
402         try {
403             String JavaDoc application = configuration.getAttribute(IPDELauncherConstants.APPLICATION, (String JavaDoc)null);
404             if (CORE_APPLICATION.equals(application))
405                 return CORE_APPLICATION;
406         } catch (CoreException e) {
407         }
408         return UI_APPLICATION;
409     }
410     
411     public static String JavaDoc getPluginID(ILaunchConfiguration configuration) {
412         try {
413             String JavaDoc projectID = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
414
if (projectID.length() > 0) {
415                 IResource project = PDEPlugin.getWorkspace().getRoot().findMember(projectID);
416                 if (project instanceof IProject) {
417                     IPluginModelBase model = PDECore.getDefault().getModelManager().findModel((IProject)project);
418                     if (model != null) {
419                         return model.getPluginBase().getId();
420                     }
421                 }
422             }
423         } catch (CoreException e) {
424         }
425         return null;
426     }
427     
428     public static boolean requiresUI(ILaunchConfiguration configuration) {
429         String JavaDoc id = getPluginID(configuration);
430         if (id != null) {
431             IPluginModelBase[] models = getPluginAndPrereqs(id);
432             for (int i = 0; i < models.length; i++) {
433                 if ("org.eclipse.swt".equals(models[i].getPluginBase().getId())) //$NON-NLS-1$
434
return true;
435             }
436             return false;
437         }
438         return true;
439     }
440     
441     public static IPluginModelBase[] getPluginAndPrereqs(String JavaDoc id) {
442         TreeMap JavaDoc map = new TreeMap JavaDoc();
443         addPluginAndPrereqs(id, map);
444         return (IPluginModelBase[])map.values().toArray(new IPluginModelBase[map.size()]);
445     }
446     
447     protected static void addPluginAndPrereqs(String JavaDoc id, TreeMap JavaDoc map) {
448         if (map.containsKey(id))
449             return;
450         
451         ModelEntry entry = PDECore.getDefault().getModelManager().findEntry(id);
452         if (entry == null)
453             return;
454         
455         IPluginModelBase model = entry.getActiveModel();
456         
457         map.put(id, model);
458         
459         IPluginImport[] imports = model.getPluginBase().getImports();
460         for (int i = 0; i < imports.length; i++) {
461             addPluginAndPrereqs(imports[i].getId(), map);
462         }
463         
464         if (model instanceof IFragmentModel) {
465             addPluginAndPrereqs(((IFragmentModel) model).getFragment().getPluginId(), map);
466         } else {
467             IFragmentModel[] fragments = PDEManager.findFragmentsFor(model);
468             for (int i = 0; i < fragments.length; i++) {
469                 IFragment fragment = fragments[i].getFragment();
470                 if (!"org.eclipse.ui.workbench.compatibility".equals(fragment.getId())) //$NON-NLS-1$
471
addPluginAndPrereqs(fragment.getId(), map);
472             }
473         }
474     }
475
476     
477     protected File JavaDoc getConfigDir(ILaunchConfiguration config) {
478         if (fConfigDir == null)
479             fConfigDir = LaunchConfigurationHelper.getConfigurationArea(config);
480     
481         return fConfigDir;
482     }
483     
484     /* (non-Javadoc)
485      * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getBuildOrder(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
486      */

487     protected IProject[] getBuildOrder(ILaunchConfiguration configuration,
488             String JavaDoc mode) throws CoreException {
489         return computeBuildOrder(LaunchPluginValidator.getAffectedProjects(configuration));
490     }
491     
492     /* (non-Javadoc)
493      * @see org.eclipse.debug.core.model.LaunchConfigurationDelegate#getProjectsForProblemSearch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
494      */

495     protected IProject[] getProjectsForProblemSearch(
496             ILaunchConfiguration configuration, String JavaDoc mode)
497             throws CoreException {
498         return LaunchPluginValidator.getAffectedProjects(configuration);
499     }
500
501
502 }
503
Popular Tags