KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > ui > launcher > JUnitLaunchConfigurationDelegate


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.ui.launcher;
13
14 import java.io.File JavaDoc;
15 import java.util.List JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.IPath;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.core.runtime.Status;
25 import org.eclipse.core.runtime.SubProgressMonitor;
26 import org.eclipse.debug.core.DebugPlugin;
27 import org.eclipse.debug.core.ILaunch;
28 import org.eclipse.debug.core.ILaunchConfiguration;
29 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
30 import org.eclipse.jdt.core.IJavaProject;
31 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
32 import org.eclipse.jdt.launching.IVMInstall;
33 import org.eclipse.jdt.launching.IVMRunner;
34 import org.eclipse.osgi.service.resolver.BundleDescription;
35 import org.eclipse.osgi.util.NLS;
36 import org.eclipse.pde.core.plugin.IFragmentModel;
37 import org.eclipse.pde.core.plugin.IPluginModelBase;
38 import org.eclipse.pde.core.plugin.PluginRegistry;
39 import org.eclipse.pde.core.plugin.TargetPlatform;
40 import org.eclipse.pde.internal.core.ClasspathHelper;
41 import org.eclipse.pde.internal.core.PDECore;
42 import org.eclipse.pde.internal.core.TargetPlatformHelper;
43 import org.eclipse.pde.internal.core.util.CoreUtility;
44 import org.eclipse.pde.internal.core.util.VersionUtil;
45 import org.eclipse.pde.internal.ui.IPDEUIConstants;
46 import org.eclipse.pde.internal.ui.PDEPlugin;
47 import org.eclipse.pde.internal.ui.PDEUIMessages;
48 import org.eclipse.pde.internal.ui.launcher.EclipsePluginValidationOperation;
49 import org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper;
50 import org.eclipse.pde.internal.ui.launcher.LaunchConfigurationHelper;
51 import org.eclipse.pde.internal.ui.launcher.LaunchPluginValidator;
52 import org.eclipse.pde.internal.ui.launcher.LauncherUtils;
53 import org.eclipse.pde.internal.ui.launcher.VMHelper;
54 import org.osgi.framework.Version;
55
56 /**
57  * A launch delegate for launching JUnit Plug-in tests.
58  *
59  * @since 3.3
60  */

61 public class JUnitLaunchConfigurationDelegate extends org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegate {
62
63     private static String JavaDoc[] REQUIRED_PLUGINS = {"org.junit", "org.eclipse.jdt.junit.runtime", "org.eclipse.pde.junit.runtime"}; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
64

65     protected File JavaDoc fConfigDir = null;
66     
67     private Map JavaDoc fPluginMap;
68     
69     /*
70      * (non-Javadoc)
71      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getVMRunner(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
72      */

73     public IVMRunner getVMRunner(ILaunchConfiguration configuration, String JavaDoc mode) throws CoreException {
74         IVMInstall launcher = VMHelper.createLauncher(configuration);
75         return launcher.getVMRunner(mode);
76     }
77     
78     /* (non-Javadoc)
79      * @see org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegate#verifyMainTypeName(org.eclipse.debug.core.ILaunchConfiguration)
80      */

81     public String JavaDoc verifyMainTypeName(ILaunchConfiguration configuration) throws CoreException {
82         if (TargetPlatformHelper.getTargetVersion() >= 3.3)
83             return "org.eclipse.equinox.launcher.Main"; //$NON-NLS-1$
84
return "org.eclipse.core.launcher.Main"; //$NON-NLS-1$
85
}
86     
87     private String JavaDoc getTestPluginId(ILaunchConfiguration configuration)
88         throws CoreException {
89         IJavaProject javaProject = getJavaProject(configuration);
90         IPluginModelBase model =
91             PluginRegistry.findModel(javaProject.getProject());
92         if (model == null)
93             abort(NLS.bind(PDEUIMessages.JUnitLaunchConfiguration_error_notaplugin,
94                             javaProject.getProject().getName()),
95                     null, IStatus.OK);
96         if (model instanceof IFragmentModel)
97             return ((IFragmentModel)model).getFragment().getPluginId();
98
99         return model.getPluginBase().getId();
100     }
101     
102     /*
103      * (non-Javadoc)
104      * @see org.eclipse.jdt.internal.junit.launcher.JUnitBaseLaunchConfiguration#abort(java.lang.String, java.lang.Throwable, int)
105      */

106     protected void abort(String JavaDoc message, Throwable JavaDoc exception, int code)
107         throws CoreException {
108         throw new CoreException(new Status(IStatus.ERROR, IPDEUIConstants.PLUGIN_ID, code, message, exception));
109     }
110
111     /* (non-Javadoc)
112      * @see org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegate#collectExecutionArguments(org.eclipse.debug.core.ILaunchConfiguration, java.util.List, java.util.List)
113      */

114     protected void collectExecutionArguments(ILaunchConfiguration configuration, List JavaDoc/*String*/ vmArguments, List JavaDoc/*String*/ programArgs) throws CoreException {
115         super.collectExecutionArguments(configuration, vmArguments, programArgs);
116         
117         // Specify the JUnit Plug-in test application to launch
118
programArgs.add("-application"); //$NON-NLS-1$
119
String JavaDoc application = null;
120         try {
121             // if application is set, it must be a headless app.
122
application = configuration.getAttribute(IPDELauncherConstants.APPLICATION, (String JavaDoc)null);
123         } catch (CoreException e) {
124         }
125         
126         // if application is not set, we should launch the default UI test app
127
// Check to see if we should launch the legacy UI app
128
if (application == null) {
129             IPluginModelBase model = (IPluginModelBase)fPluginMap.get("org.eclipse.pde.junit.runtime"); //$NON-NLS-1$
130
BundleDescription desc = model != null ? model.getBundleDescription() : null;
131             if (desc != null) {
132                 Version version = desc.getVersion();
133                 int major = version.getMajor();
134                 // launch legacy UI app only if we are launching a target that does
135
// not use the new application model and we are launching with a
136
// org.eclipse.pde.junit.runtime whose version is >= 3.3
137
if (major >= 3 && version.getMinor() >= 3 && !TargetPlatformHelper.usesNewApplicationModel()) {
138                     application = IPDEUIConstants.LEGACY_UI_TEST_APPLICATION;
139                 }
140             }
141         }
142         
143         // launch the UI test application
144
if (application == null)
145             application = IPDEUIConstants.UI_TEST_APPLICATION;
146         
147         programArgs.add(application);
148         
149         // If a product is specified, then add it to the program args
150
if (configuration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) {
151             programArgs.add("-product"); //$NON-NLS-1$
152
programArgs.add(configuration.getAttribute(IPDELauncherConstants.PRODUCT, "")); //$NON-NLS-1$
153
} else {
154             // Specify the application to test
155
String JavaDoc defaultApplication = IPDEUIConstants.CORE_TEST_APPLICATION.equals(application) ? null : TargetPlatform.getDefaultApplication();
156             String JavaDoc testApplication = configuration.getAttribute(IPDELauncherConstants.APP_TO_TEST, defaultApplication);
157             if (testApplication != null) {
158                 programArgs.add("-testApplication"); //$NON-NLS-1$
159
programArgs.add(testApplication);
160             }
161         }
162         
163         // Specify the location of the runtime workbench
164
String JavaDoc targetWorkspace = LaunchArgumentsHelper.getWorkspaceLocation(configuration);
165         if (targetWorkspace.length() > 0) {
166             programArgs.add("-data"); //$NON-NLS-1$
167
programArgs.add(targetWorkspace);
168         }
169         
170         // Create the platform configuration for the runtime workbench
171
String JavaDoc productID = LaunchConfigurationHelper.getProductID(configuration);
172         LaunchConfigurationHelper.createConfigIniFile(configuration,
173                 productID, fPluginMap, getConfigurationDirectory(configuration));
174         String JavaDoc brandingId = LaunchConfigurationHelper.getContributingPlugin(productID);
175         TargetPlatform.createPlatformConfiguration(
176                 getConfigurationDirectory(configuration),
177                 (IPluginModelBase[])fPluginMap.values().toArray(new IPluginModelBase[fPluginMap.size()]),
178                 brandingId != null ? (IPluginModelBase) fPluginMap.get(brandingId) : null);
179         TargetPlatformHelper.checkPluginPropertiesConsistency(fPluginMap, getConfigurationDirectory(configuration));
180         
181         programArgs.add("-configuration"); //$NON-NLS-1$
182
programArgs.add("file:" + new Path(getConfigurationDirectory(configuration).getPath()).addTrailingSeparator().toString()); //$NON-NLS-1$
183

184         // Specify the output folder names
185
programArgs.add("-dev"); //$NON-NLS-1$
186
programArgs.add(ClasspathHelper.getDevEntriesProperties(getConfigurationDirectory(configuration).toString() + "/dev.properties", fPluginMap)); //$NON-NLS-1$
187

188         // necessary for PDE to know how to load plugins when target platform = host platform
189
// see PluginPathFinder.getPluginPaths()
190
IPluginModelBase base = findPlugin(PDECore.PLUGIN_ID);
191         if (base != null && VersionUtil.compareMacroMinorMicro(base.getBundleDescription().getVersion(), new Version("3.3.1")) < 0) //$NON-NLS-1$
192
programArgs.add("-pdelaunch"); //$NON-NLS-1$
193

194         // Create the .options file if tracing is turned on
195
if (configuration.getAttribute(IPDELauncherConstants.TRACING, false)
196                 && !IPDELauncherConstants.TRACING_NONE.equals(configuration.getAttribute(
197                         IPDELauncherConstants.TRACING_CHECKED, (String JavaDoc) null))) {
198             programArgs.add("-debug"); //$NON-NLS-1$
199
String JavaDoc path = getConfigurationDirectory(configuration).getPath() + IPath.SEPARATOR + ".options"; //$NON-NLS-1$
200
programArgs.add(LaunchArgumentsHelper.getTracingFileArgument(configuration, path));
201         }
202         
203         // add the program args specified by the user
204
String JavaDoc[] userArgs = LaunchArgumentsHelper.getUserProgramArgumentArray(configuration);
205         for (int i = 0; i < userArgs.length; i++) {
206             // be forgiving if people have tracing turned on and forgot
207
// to remove the -debug from the program args field.
208
if (userArgs[i].equals("-debug") && programArgs.contains("-debug")) //$NON-NLS-1$ //$NON-NLS-2$
209
continue;
210             programArgs.add(userArgs[i]);
211         }
212         
213         if (!configuration.getAttribute(IPDEUIConstants.APPEND_ARGS_EXPLICITLY, false)) {
214             if (!programArgs.contains("-os")) { //$NON-NLS-1$
215
programArgs.add("-os"); //$NON-NLS-1$
216
programArgs.add(TargetPlatform.getOS());
217             }
218             if (!programArgs.contains("-ws")) { //$NON-NLS-1$
219
programArgs.add("-ws"); //$NON-NLS-1$
220
programArgs.add(TargetPlatform.getWS());
221             }
222             if (!programArgs.contains("-arch")) { //$NON-NLS-1$
223
programArgs.add("-arch"); //$NON-NLS-1$
224
programArgs.add(TargetPlatform.getOSArch());
225             }
226         }
227             
228         programArgs.add("-testpluginname"); //$NON-NLS-1$
229
programArgs.add(getTestPluginId(configuration));
230     }
231         
232     private IPluginModelBase findPlugin(String JavaDoc id) throws CoreException {
233         IPluginModelBase model = PluginRegistry.findModel(id);
234         if (model == null)
235             model = PDECore.getDefault().findPluginInHost(id);
236         if (model == null)
237             abort(
238                 NLS.bind(PDEUIMessages.JUnitLaunchConfiguration_error_missingPlugin, id),
239                 null,
240                 IStatus.OK);
241         return model;
242     }
243         
244     /*
245      * (non-Javadoc)
246      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getProgramArguments(org.eclipse.debug.core.ILaunchConfiguration)
247      */

248     public String JavaDoc getProgramArguments(ILaunchConfiguration configuration)
249         throws CoreException {
250         return LaunchArgumentsHelper.getUserProgramArguments(configuration);
251     }
252     
253     /*
254      * (non-Javadoc)
255      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getVMArguments(org.eclipse.debug.core.ILaunchConfiguration)
256      */

257     public String JavaDoc getVMArguments(ILaunchConfiguration configuration)
258         throws CoreException {
259         String JavaDoc vmArgs = LaunchArgumentsHelper.getUserVMArguments(configuration);
260         
261         // necessary for PDE to know how to load plugins when target platform = host platform
262
// see PluginPathFinder.getPluginPaths() and PluginPathFinder.isDevLaunchMode()
263
IPluginModelBase base = (IPluginModelBase)LaunchPluginValidator.getPluginsToRun(configuration).get(PDECore.PLUGIN_ID);
264         if (base != null && VersionUtil.compareMacroMinorMicro(base.getBundleDescription().getVersion(), new Version("3.3.1")) >= 0) { //$NON-NLS-1$
265
if (vmArgs.length() > 0 && !vmArgs.endsWith(" ")) //$NON-NLS-1$
266
vmArgs = vmArgs.concat(" "); //$NON-NLS-1$
267
vmArgs = vmArgs.concat("-Declipse.pde.launch=true"); //$NON-NLS-1$
268
}
269         return vmArgs;
270     }
271     
272     /*
273      * (non-Javadoc)
274      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getEnvironment(org.eclipse.debug.core.ILaunchConfiguration)
275      */

276     public String JavaDoc[] getEnvironment(ILaunchConfiguration configuration) throws CoreException {
277         return DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
278     }
279     
280     /*
281      * (non-Javadoc)
282      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getClasspath(org.eclipse.debug.core.ILaunchConfiguration)
283      */

284     public String JavaDoc[] getClasspath(ILaunchConfiguration configuration) throws CoreException {
285         String JavaDoc[] classpath = LaunchArgumentsHelper.constructClasspath(configuration);
286         if (classpath == null) {
287             abort(PDEUIMessages.WorkbenchLauncherConfigurationDelegate_noStartup, null, IStatus.OK);
288         }
289         return classpath;
290     }
291     
292     /*
293      * (non-Javadoc)
294      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getWorkingDirectory(org.eclipse.debug.core.ILaunchConfiguration)
295      */

296     public File JavaDoc getWorkingDirectory(ILaunchConfiguration configuration) throws CoreException {
297         return LaunchArgumentsHelper.getWorkingDirectory(configuration);
298     }
299     
300     /*
301      * (non-Javadoc)
302      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getVMSpecificAttributesMap(org.eclipse.debug.core.ILaunchConfiguration)
303      */

304     public Map JavaDoc getVMSpecificAttributesMap(ILaunchConfiguration configuration) throws CoreException {
305         return LaunchArgumentsHelper.getVMSpecificAttributesMap(configuration);
306     }
307
308     /*
309      * (non-Javadoc)
310      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#setDefaultSourceLocator(org.eclipse.debug.core.ILaunch, org.eclipse.debug.core.ILaunchConfiguration)
311      */

312     protected void setDefaultSourceLocator(ILaunch launch, ILaunchConfiguration configuration) throws CoreException {
313         ILaunchConfigurationWorkingCopy wc = null;
314         if (configuration.isWorkingCopy()) {
315             wc = (ILaunchConfigurationWorkingCopy) configuration;
316         } else {
317             wc = configuration.getWorkingCopy();
318         }
319         String JavaDoc id = configuration.getAttribute(
320                 IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER,
321                 (String JavaDoc) null);
322         if (!PDESourcePathProvider.ID.equals(id)) {
323             wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_SOURCE_PATH_PROVIDER,
324                             PDESourcePathProvider.ID);
325             wc.doSave();
326         }
327         
328         manageLaunch(launch);
329     }
330     
331     /**
332      * Returns the location of the configuration area
333      *
334      * @param configuration
335      * the launch configuration
336      * @return a directory where the configuration area is located
337      */

338     protected File JavaDoc getConfigurationDirectory(ILaunchConfiguration configuration) {
339         if (fConfigDir == null)
340             fConfigDir = LaunchConfigurationHelper.getConfigurationArea(configuration);
341         return fConfigDir;
342     }
343     
344     /*
345      * (non-Javadoc)
346      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getBuildOrder(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
347      */

348     protected IProject[] getBuildOrder(ILaunchConfiguration configuration,
349             String JavaDoc mode) throws CoreException {
350         return computeBuildOrder(LaunchPluginValidator.getAffectedProjects(configuration));
351     }
352     
353     /*
354      * (non-Javadoc)
355      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getProjectsForProblemSearch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String)
356      */

357     protected IProject[] getProjectsForProblemSearch(
358             ILaunchConfiguration configuration, String JavaDoc mode)
359             throws CoreException {
360         return LaunchPluginValidator.getAffectedProjects(configuration);
361     }
362     
363     /**
364      * Adds a listener to the launch to be notified at interesting launch lifecycle
365      * events such as when the launch terminates.
366      *
367      * @param launch
368      * the launch
369      */

370     protected void manageLaunch(ILaunch launch) {
371         PDEPlugin.getDefault().getLaunchListener().manage(launch);
372     }
373     
374     /* (non-Javadoc)
375      * @see org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegate#preLaunchCheck(org.eclipse.debug.core.ILaunchConfiguration, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
376      */

377     protected void preLaunchCheck(ILaunchConfiguration configuration, ILaunch launch, IProgressMonitor monitor) throws CoreException {
378         // Get the list of plug-ins to run
379
fPluginMap = LaunchPluginValidator.getPluginsToRun(configuration);
380         
381         // implicitly add the plug-ins required for JUnit testing if necessary
382
for (int i = 0; i < REQUIRED_PLUGINS.length; i++) {
383             String JavaDoc id = REQUIRED_PLUGINS[i];
384             if (!fPluginMap.containsKey(id)) {
385                 fPluginMap.put(id, findPlugin(id));
386             }
387         }
388     
389         boolean autoValidate = configuration.getAttribute(IPDELauncherConstants.AUTOMATIC_VALIDATE, false);
390         monitor.beginTask("", autoValidate ? 3 : 4); //$NON-NLS-1$
391
if (autoValidate)
392             validatePluginDependencies(configuration, new SubProgressMonitor(monitor, 1));
393         validateProjectDependencies(configuration, new SubProgressMonitor(monitor, 1));
394         clear(configuration, new SubProgressMonitor(monitor, 1));
395         launch.setAttribute(IPDELauncherConstants.CONFIG_LOCATION, getConfigurationDirectory(configuration).toString());
396         synchronizeManifests(configuration, new SubProgressMonitor(monitor, 1));
397     }
398     /**
399      * Checks for old-style plugin.xml files that have become stale since the last launch.
400      * For any stale plugin.xml files found, the corresponding MANIFEST.MF is deleted
401      * from the runtime configuration area so that it gets regenerated upon startup.
402      *
403      * @param configuration
404      * the launch configuration
405      * @param monitor
406      * the progress monitor
407      */

408     protected void synchronizeManifests(ILaunchConfiguration configuration, IProgressMonitor monitor) {
409         LaunchConfigurationHelper.synchronizeManifests(configuration, getConfigurationDirectory(configuration));
410         monitor.done();
411     }
412
413     /**
414      * Clears the workspace prior to launching if the workspace exists and the option to
415      * clear it is turned on. Also clears the configuration area if that option is chosen.
416      *
417      * @param configuration
418      * the launch configuration
419      * @param monitor
420      * the progress monitor
421      * @throws CoreException
422      * if unable to retrieve launch attribute values
423      * @since 3.3
424      */

425     protected void clear(ILaunchConfiguration configuration, IProgressMonitor monitor) throws CoreException {
426         String JavaDoc workspace = LaunchArgumentsHelper.getWorkspaceLocation(configuration);
427         // Clear workspace and prompt, if necessary
428
if (!LauncherUtils.clearWorkspace(configuration, workspace, new SubProgressMonitor(monitor, 1))) {
429             monitor.setCanceled(true);
430             return;
431         }
432
433         // clear config area, if necessary
434
if (configuration.getAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA, false))
435             CoreUtility.deleteContent(getConfigurationDirectory(configuration));
436     }
437
438     /**
439      * Checks if the Automated Management of Dependencies option is turned on.
440      * If so, it makes aure all manifests are updated with the correct dependencies.
441      *
442      * @param configuration
443      * the launch configuration
444      * @param monitor
445      * a progress monitor
446      */

447     protected void validateProjectDependencies(ILaunchConfiguration configuration, IProgressMonitor monitor) {
448         LauncherUtils.validateProjectDependencies(configuration, monitor);
449     }
450     
451     /**
452      * Validates inter-bundle dependencies automatically prior to launching
453      * if that option is turned on.
454      *
455      * @param configuration
456      * the launch configuration
457      * @param monitor
458      * a progress monitor
459      */

460     protected void validatePluginDependencies(ILaunchConfiguration configuration, IProgressMonitor monitor) throws CoreException {
461         EclipsePluginValidationOperation op = new EclipsePluginValidationOperation(configuration);
462         LaunchPluginValidator.runValidationOperation(op, monitor);
463     }
464 }
465
Popular Tags