KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > launcher > JUnitBaseLaunchConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.junit.launcher;
13
14 import java.io.File JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24
25 import org.eclipse.core.variables.VariablesPlugin;
26
27 import org.eclipse.swt.widgets.Display;
28 import org.eclipse.swt.widgets.Shell;
29
30 import org.eclipse.jface.dialogs.MessageDialog;
31
32 import org.eclipse.debug.core.ILaunch;
33 import org.eclipse.debug.core.ILaunchConfiguration;
34 import org.eclipse.debug.core.ILaunchManager;
35
36 import org.eclipse.jdt.core.IJavaElement;
37 import org.eclipse.jdt.core.IJavaProject;
38 import org.eclipse.jdt.core.IType;
39 import org.eclipse.jdt.core.JavaCore;
40
41 import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
42 import org.eclipse.jdt.launching.ExecutionArguments;
43 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
44 import org.eclipse.jdt.launching.IVMInstall;
45 import org.eclipse.jdt.launching.IVMRunner;
46 import org.eclipse.jdt.launching.SocketUtil;
47 import org.eclipse.jdt.launching.VMRunnerConfiguration;
48
49 import org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegate;
50
51 import org.eclipse.jdt.internal.junit.Messages;
52 import org.eclipse.jdt.internal.junit.ui.JUnitMessages;
53 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
54 import org.eclipse.jdt.internal.junit.util.IJUnitStatusConstants;
55 import org.eclipse.jdt.internal.junit.util.TestSearchEngine;
56
57 /**
58  * @deprecated Extend {@link JUnitLaunchConfigurationDelegate} instead
59  */

60 public abstract class JUnitBaseLaunchConfiguration extends AbstractJavaLaunchConfigurationDelegate {
61
62     public static final String JavaDoc NO_DISPLAY_ATTR = JUnitLaunchConfigurationConstants.ATTR_NO_DISPLAY;
63
64     public static final String JavaDoc RUN_QUIETLY_MODE =JUnitLaunchConfigurationConstants.MODE_RUN_QUIETLY_MODE;
65     
66     public static final String JavaDoc PORT_ATTR= JUnitLaunchConfigurationConstants.ATTR_PORT;
67     /**
68      * The single test type, or "" iff running a launch container.
69      */

70     public static final String JavaDoc TESTTYPE_ATTR= JUnitPlugin.PLUGIN_ID+".TESTTYPE"; //$NON-NLS-1$
71
/**
72      * The test method, or "" iff running the whole test type.
73      */

74     public static final String JavaDoc TESTNAME_ATTR= JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME;
75     public static final String JavaDoc ATTR_KEEPRUNNING = JUnitLaunchConfigurationConstants.ATTR_KEEPRUNNING;
76     /**
77      * The launch container, or "" iff running a single test type.
78      */

79     public static final String JavaDoc LAUNCH_CONTAINER_ATTR= JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER;
80
81     /**
82      * The name of the prioritized tests
83      */

84     public static final String JavaDoc FAILURES_FILENAME_ATTR= JUnitLaunchConfigurationConstants.ATTR_FAILURES_NAMES;
85     public static final String JavaDoc TEST_KIND_ATTR = JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND;
86     
87     public void launch(ILaunchConfiguration configuration, String JavaDoc mode, ILaunch launch, IProgressMonitor pm) throws CoreException {
88         if (mode.equals(RUN_QUIETLY_MODE)) {
89             launch.setAttribute(NO_DISPLAY_ATTR, "true"); //$NON-NLS-1$
90
mode = ILaunchManager.RUN_MODE;
91         }
92             
93         TestSearchResult testTypes = findTestTypes(configuration, pm);
94         IVMInstall install= getVMInstall(configuration);
95         IVMRunner runner = install.getVMRunner(mode);
96         if (runner == null) {
97             abort(Messages.format(JUnitMessages.JUnitBaseLaunchConfiguration_error_novmrunner, new String JavaDoc[]{install.getId()}), null, IJavaLaunchConfigurationConstants.ERR_VM_RUNNER_DOES_NOT_EXIST);
98         }
99         
100         int port= SocketUtil.findFreePort();
101         VMRunnerConfiguration runConfig= launchTypes(configuration, mode, testTypes, port);
102         setDefaultSourceLocator(launch, configuration);
103         
104         launch.setAttribute(PORT_ATTR, Integer.toString(port));
105         launch.setAttribute(TESTTYPE_ATTR, testTypes.getTypes()[0].getHandleIdentifier());
106         runner.run(runConfig, launch, pm);
107     }
108
109     protected final TestSearchResult findTestTypes(ILaunchConfiguration configuration, IProgressMonitor pm) throws CoreException {
110         IJavaProject javaProject= getJavaProject(configuration);
111         if ((javaProject == null) || !javaProject.exists()) {
112             informAndAbort(JUnitMessages.JUnitLaunchConfigurationDelegate_error_invalidproject, null, IJavaLaunchConfigurationConstants.ERR_NOT_A_JAVA_PROJECT);
113         }
114         if (!TestSearchEngine.hasTestCaseType(javaProject)) {
115             informAndAbort(JUnitMessages.JUnitLaunchConfigurationDelegate_error_junitnotonpath, null, IJUnitStatusConstants.ERR_JUNIT_NOT_ON_PATH);
116         }
117
118         ITestKind testKind= JUnitLaunchConfigurationConstants.getTestRunnerKind(configuration);
119         if (testKind.isNull()) {
120             testKind= TestKindRegistry.getDefault().getKind(TestKindRegistry.JUNIT3_TEST_KIND_ID); // backward compatible for launch configurations with no runner
121
}
122                 
123         boolean isJUnit4Configuration= TestKindRegistry.JUNIT4_TEST_KIND_ID.equals(testKind.getId());
124         if (isJUnit4Configuration && ! TestSearchEngine.hasTestAnnotation(javaProject)) {
125             informAndAbort(JUnitMessages.JUnitLaunchConfigurationDelegate_error_junit4notonpath, null, IJUnitStatusConstants.ERR_JUNIT_NOT_ON_PATH);
126         }
127         
128         IJavaElement testTarget= getTestTarget(configuration, javaProject);
129         HashSet JavaDoc result= new HashSet JavaDoc();
130         testKind.getFinder().findTestsInContainer(testTarget, result, pm);
131         if (result.isEmpty()) {
132             String JavaDoc msg= Messages.format(JUnitMessages.JUnitLaunchConfigurationDelegate_error_notests_kind, testKind.getDisplayName());
133             informAndAbort(msg, null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE);
134         }
135         IType[] types= (IType[]) result.toArray(new IType[result.size()]);
136         return new TestSearchResult(types, testKind);
137     }
138
139     private void informAndAbort(String JavaDoc message, Throwable JavaDoc exception, int code) throws CoreException {
140         IStatus status= new Status(IStatus.INFO, JUnitPlugin.PLUGIN_ID, code, message, exception);
141         if (showStatusMessage(status))
142             throw new CoreException(status);
143         abort(message, exception, code);
144     }
145
146     private final VMRunnerConfiguration launchTypes(ILaunchConfiguration configuration, String JavaDoc mode, TestSearchResult tests, int port) throws CoreException {
147         File JavaDoc workingDir = verifyWorkingDirectory(configuration);
148         String JavaDoc workingDirName = null;
149         if (workingDir != null)
150             workingDirName = workingDir.getAbsolutePath();
151         
152         // Program & VM args
153
String JavaDoc vmArgs= getVMArguments(configuration);
154         ExecutionArguments execArgs = new ExecutionArguments(vmArgs, ""); //$NON-NLS-1$
155
String JavaDoc[] envp= getEnvironment(configuration);
156
157         VMRunnerConfiguration runConfig= createVMRunner(configuration, tests, port, mode);
158         runConfig.setVMArguments(execArgs.getVMArgumentsArray());
159         runConfig.setWorkingDirectory(workingDirName);
160         runConfig.setEnvironment(envp);
161
162         Map JavaDoc vmAttributesMap = getVMSpecificAttributesMap(configuration);
163         runConfig.setVMSpecificAttributesMap(vmAttributesMap);
164
165         String JavaDoc[] bootpath = getBootpath(configuration);
166         runConfig.setBootClassPath(bootpath);
167         
168         return runConfig;
169     }
170
171     private final IJavaElement getTestTarget(ILaunchConfiguration configuration, IJavaProject javaProject) throws CoreException {
172         String JavaDoc containerHandle = configuration.getAttribute(LAUNCH_CONTAINER_ATTR, ""); //$NON-NLS-1$
173
if (containerHandle.length() != 0) {
174              IJavaElement element= JavaCore.create(containerHandle);
175              if (element == null || !element.exists()) {
176                  informAndAbort(JUnitMessages.JUnitLaunchConfigurationDelegate_error_input_element_deosn_not_exist, null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE);
177              }
178              return element;
179         }
180         String JavaDoc testTypeName= configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, ""); //$NON-NLS-1$
181
if (testTypeName.length() != 0) {
182             testTypeName= performStringSubstitution(testTypeName);
183             IType type= javaProject.findType(testTypeName);
184             if (type != null) {
185                 return type;
186             }
187         }
188         informAndAbort(JUnitMessages.JUnitLaunchConfigurationDelegate_input_type_does_not_exist, null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE);
189         return null; // not reachable
190
}
191     
192     private final String JavaDoc performStringSubstitution(String JavaDoc testTypeName) throws CoreException {
193         return VariablesPlugin.getDefault().getStringVariableManager().performStringSubstitution(testTypeName);
194     }
195
196     private boolean showStatusMessage(final IStatus status) {
197         final boolean[] success= new boolean[] { false };
198         getDisplay().syncExec(
199                 new Runnable JavaDoc() {
200                     public void run() {
201                         Shell shell= JUnitPlugin.getActiveWorkbenchShell();
202                         if (shell == null)
203                             shell= getDisplay().getActiveShell();
204                         if (shell != null) {
205                             MessageDialog.openInformation(shell, JUnitMessages.JUnitLaunchConfigurationDelegate_dialog_title, status.getMessage());
206                             success[0]= true;
207                         }
208                     }
209                 }
210         );
211         return success[0];
212     }
213     
214     private Display getDisplay() {
215         Display display;
216         display= Display.getCurrent();
217         if (display == null)
218             display= Display.getDefault();
219         return display;
220     }
221     
222     /* (non-Javadoc)
223      * @see org.eclipse.jdt.internal.junit.launcher.ITestFindingAbortHandler#abort(java.lang.String, java.lang.Throwable, int)
224      */

225     protected void abort(String JavaDoc message, Throwable JavaDoc exception, int code) throws CoreException {
226         throw new CoreException(new Status(IStatus.ERROR, JUnitPlugin.PLUGIN_ID, code, message, exception));
227     }
228     
229     /**
230      * Override to create a custom VMRunnerConfiguration for a launch configuration.
231      */

232     protected abstract VMRunnerConfiguration createVMRunner(ILaunchConfiguration configuration, TestSearchResult testTypes, int port, String JavaDoc runMode) throws CoreException;
233
234     protected boolean keepAlive(ILaunchConfiguration config) {
235         try {
236             return config.getAttribute(ATTR_KEEPRUNNING, false);
237         } catch(CoreException e) {
238         }
239         return false;
240     }
241
242     protected List JavaDoc getBasicArguments(ILaunchConfiguration configuration, int port, String JavaDoc runMode, TestSearchResult result) throws CoreException {
243         ArrayList JavaDoc argv = new ArrayList JavaDoc();
244         argv.add("-version"); //$NON-NLS-1$
245
argv.add("3"); //$NON-NLS-1$
246

247         argv.add("-port"); //$NON-NLS-1$
248
argv.add(Integer.toString(port));
249
250         if (keepAlive(configuration) && runMode.equals(ILaunchManager.DEBUG_MODE))
251             argv.add(0, "-keepalive"); //$NON-NLS-1$
252

253         String JavaDoc testLoaderId = result.getTestKind().getLoaderClassName();
254         argv.add("-testLoaderClass"); //$NON-NLS-1$
255
argv.add(testLoaderId);
256         
257         // TODO: allow the TestKind to add new options to the command line:
258
// result.getTestKind().addArguments(configuration, argv)
259

260         return argv;
261     }
262 }
263
Popular Tags