1 12 package org.eclipse.jdt.internal.junit.launcher; 13 14 import java.io.File ; 15 import java.util.ArrayList ; 16 import java.util.HashSet ; 17 import java.util.List ; 18 import java.util.Map ; 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 60 public abstract class JUnitBaseLaunchConfiguration extends AbstractJavaLaunchConfigurationDelegate { 61 62 public static final String NO_DISPLAY_ATTR = JUnitLaunchConfigurationConstants.ATTR_NO_DISPLAY; 63 64 public static final String RUN_QUIETLY_MODE =JUnitLaunchConfigurationConstants.MODE_RUN_QUIETLY_MODE; 65 66 public static final String PORT_ATTR= JUnitLaunchConfigurationConstants.ATTR_PORT; 67 70 public static final String TESTTYPE_ATTR= JUnitPlugin.PLUGIN_ID+".TESTTYPE"; 74 public static final String TESTNAME_ATTR= JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME; 75 public static final String ATTR_KEEPRUNNING = JUnitLaunchConfigurationConstants.ATTR_KEEPRUNNING; 76 79 public static final String LAUNCH_CONTAINER_ATTR= JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER; 80 81 84 public static final String FAILURES_FILENAME_ATTR= JUnitLaunchConfigurationConstants.ATTR_FAILURES_NAMES; 85 public static final String TEST_KIND_ATTR = JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND; 86 87 public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor pm) throws CoreException { 88 if (mode.equals(RUN_QUIETLY_MODE)) { 89 launch.setAttribute(NO_DISPLAY_ATTR, "true"); 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 []{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); } 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 result= new HashSet (); 130 testKind.getFinder().findTestsInContainer(testTarget, result, pm); 131 if (result.isEmpty()) { 132 String 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 message, Throwable 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 mode, TestSearchResult tests, int port) throws CoreException { 147 File workingDir = verifyWorkingDirectory(configuration); 148 String workingDirName = null; 149 if (workingDir != null) 150 workingDirName = workingDir.getAbsolutePath(); 151 152 String vmArgs= getVMArguments(configuration); 154 ExecutionArguments execArgs = new ExecutionArguments(vmArgs, ""); String [] 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 vmAttributesMap = getVMSpecificAttributesMap(configuration); 163 runConfig.setVMSpecificAttributesMap(vmAttributesMap); 164 165 String [] 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 containerHandle = configuration.getAttribute(LAUNCH_CONTAINER_ATTR, ""); 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 testTypeName= configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, ""); 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; } 191 192 private final String performStringSubstitution(String 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 () { 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 225 protected void abort(String message, Throwable exception, int code) throws CoreException { 226 throw new CoreException(new Status(IStatus.ERROR, JUnitPlugin.PLUGIN_ID, code, message, exception)); 227 } 228 229 232 protected abstract VMRunnerConfiguration createVMRunner(ILaunchConfiguration configuration, TestSearchResult testTypes, int port, String 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 getBasicArguments(ILaunchConfiguration configuration, int port, String runMode, TestSearchResult result) throws CoreException { 243 ArrayList argv = new ArrayList (); 244 argv.add("-version"); argv.add("3"); 247 argv.add("-port"); argv.add(Integer.toString(port)); 249 250 if (keepAlive(configuration) && runMode.equals(ILaunchManager.DEBUG_MODE)) 251 argv.add(0, "-keepalive"); 253 String testLoaderId = result.getTestKind().getLoaderClassName(); 254 argv.add("-testLoaderClass"); argv.add(testLoaderId); 256 257 260 return argv; 261 } 262 } 263 | Popular Tags |