1 12 package org.eclipse.pde.internal.ui.launcher; 13 14 import java.io.File ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.net.URL ; 17 import java.util.ArrayList ; 18 import java.util.Map ; 19 import java.util.TreeMap ; 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 CORE_APPLICATION = "org.eclipse.pde.junit.runtime.coretestapplication"; public static final String UI_APPLICATION = "org.eclipse.pde.junit.runtime.uitestapplication"; 71 protected static IPluginModelBase[] registryPlugins; 72 protected File fConfigDir = null; 73 74 public void launch( 75 ILaunchConfiguration configuration, 76 String mode, 77 ILaunch launch, 78 IProgressMonitor monitor) 79 throws CoreException { 80 try { 81 fConfigDir = null; 82 monitor.beginTask("", 7); 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 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 mode) throws CoreException { 125 IVMInstall launcher = VMHelper.createLauncher(configuration); 126 return launcher.getVMRunner(mode); 127 } 128 131 protected VMRunnerConfiguration createVMRunner( 132 ILaunchConfiguration configuration, 133 TestSearchResult testTypes, 134 int port, 135 String runMode) 136 throws CoreException { 137 138 String [] 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)); 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 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 message, Throwable 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 [] getProgramArgumentsArray( 178 ILaunchConfiguration configuration, 179 TestSearchResult testSearchResult, 180 int port, 181 String runMode) 182 throws CoreException { 183 ArrayList programArgs = new ArrayList (); 184 185 Map 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 e) { 194 } catch (InterruptedException 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 () { 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 programArgs.add("-application"); programArgs.add(getApplicationName(pluginMap, configuration)); 217 218 if (configuration.getAttribute(IPDELauncherConstants.USE_PRODUCT, false)) { 220 programArgs.add("-product"); programArgs.add(configuration.getAttribute(IPDELauncherConstants.PRODUCT, "")); } else { 223 String testApplication = configuration.getAttribute(IPDELauncherConstants.APP_TO_TEST, (String )null); 225 if (testApplication != null && testApplication.length() > 0) { 226 programArgs.add("-testApplication"); programArgs.add(testApplication); 228 } 229 } 230 231 String targetWorkspace = LaunchArgumentsHelper.getWorkspaceLocation(configuration); 233 if (targetWorkspace.length() > 0) { 234 programArgs.add("-data"); programArgs.add(targetWorkspace); 236 } 237 238 String 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"); programArgs.add("file:" + new Path(getConfigDir(configuration).getPath()).addTrailingSeparator().toString()); 250 programArgs.add("-dev"); programArgs.add(ClasspathHelper.getDevEntriesProperties(getConfigDir(configuration).toString() + "/dev.properties", pluginMap)); 254 if (pluginMap.containsKey(PDECore.getPluginId())) 257 programArgs.add("-pdelaunch"); 259 if (configuration.getAttribute(IPDELauncherConstants.TRACING, false) 261 && !IPDELauncherConstants.TRACING_NONE.equals(configuration.getAttribute( 262 IPDELauncherConstants.TRACING_CHECKED, (String ) null))) { 263 programArgs.add("-debug"); String path = getConfigDir(configuration).getPath() + IPath.SEPARATOR + ".options"; programArgs.add(LaunchArgumentsHelper.getTracingFileArgument(configuration, path)); 266 } 267 268 String [] userArgs = LaunchArgumentsHelper.getUserProgramArgumentArray(configuration); 270 for (int i = 0; i < userArgs.length; i++) { 271 if (userArgs[i].equals("-debug") && programArgs.contains("-debug")) continue; 275 programArgs.add(userArgs[i]); 276 } 277 278 if (!programArgs.contains("-os")) { programArgs.add("-os"); programArgs.add(TargetPlatform.getOS()); 281 } 282 if (!programArgs.contains("-ws")) { programArgs.add("-ws"); programArgs.add(TargetPlatform.getWS()); 285 } 286 if (!programArgs.contains("-arch")) { programArgs.add("-arch"); programArgs.add(TargetPlatform.getOSArch()); 289 } 290 291 programArgs.add("-testpluginname"); programArgs.add(getTestPluginId(configuration)); 293 294 programArgs.add("-loaderpluginname"); programArgs.add(testSearchResult.getTestKind().getLoaderPluginId()); 296 297 String testFailureNames = configuration.getAttribute(JUnitBaseLaunchConfiguration.FAILURES_FILENAME_ATTR, ""); if (testFailureNames.length() > 0) { 299 programArgs.add("-testfailures"); programArgs.add(testFailureNames); 301 } 302 303 IType[] testTypes = testSearchResult.getTypes(); 305 String testName = 306 configuration.getAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, ""); if (testName.length() > 0) { 308 programArgs.add("-test"); programArgs.add(testTypes[0].getFullyQualifiedName() + ":" + testName); } else { 311 programArgs.add("-classnames"); for (int i = 0; i < testTypes.length; i++) 313 programArgs.add(testTypes[i].getFullyQualifiedName()); 314 } 315 return (String []) programArgs.toArray(new String [programArgs.size()]); 316 } 317 318 protected IPluginModelBase[] addRequiredPlugins(Map pluginMap, TestSearchResult result) 319 throws CoreException { 320 addRequiredPlugin(pluginMap, "org.eclipse.pde.junit.runtime"); addRequiredPlugin(pluginMap, "org.eclipse.jdt.junit.runtime"); addRequiredPlugin(pluginMap, "org.junit"); addRequiredPlugin(pluginMap, result.getTestKind().getLoaderPluginId()); 324 return (IPluginModelBase[]) pluginMap.values().toArray( 325 new IPluginModelBase[pluginMap.size()]); 326 } 327 328 private void addRequiredPlugin(Map pluginMap, final String pluginId) throws CoreException { 329 if (!pluginMap.containsKey(pluginId)) { pluginMap.put(pluginId, findPlugin(pluginId)); } 332 } 333 334 protected IPluginModelBase findPlugin(String 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 [] 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 [] getVMArgumentsArray(ILaunchConfiguration configuration) throws CoreException { 358 return new ExecutionArguments(getVMArguments(configuration),"").getVMArgumentsArray(); } 360 361 public String getProgramArguments(ILaunchConfiguration configuration) 362 throws CoreException { 363 return LaunchArgumentsHelper.getUserProgramArguments(configuration); 364 } 365 366 public String getVMArguments(ILaunchConfiguration configuration) 367 throws CoreException { 368 return LaunchArgumentsHelper.getUserVMArguments(configuration); 369 } 370 371 public String [] getEnvironment(ILaunchConfiguration configuration) throws CoreException { 372 return DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration); 373 } 374 375 public String [] getClasspath(ILaunchConfiguration configuration) throws CoreException { 376 String [] 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 getWorkingDirectory(ILaunchConfiguration configuration) throws CoreException { 384 return LaunchArgumentsHelper.getWorkingDirectory(configuration); 385 } 386 387 public Map 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 getDefaultWorkspace(ILaunchConfiguration config) throws CoreException { 396 if (config.getAttribute(IPDELauncherConstants.APPLICATION, UI_APPLICATION).equals(UI_APPLICATION)) 397 return "${workspace_loc}/../junit-workbench-workspace"; return "${workspace_loc}/../junit-core-workspace"; } 400 401 protected String getApplicationName(Map pluginMap, ILaunchConfiguration configuration) { 402 try { 403 String application = configuration.getAttribute(IPDELauncherConstants.APPLICATION, (String )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 getPluginID(ILaunchConfiguration configuration) { 412 try { 413 String projectID = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME, ""); 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 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())) return true; 435 } 436 return false; 437 } 438 return true; 439 } 440 441 public static IPluginModelBase[] getPluginAndPrereqs(String id) { 442 TreeMap map = new TreeMap (); 443 addPluginAndPrereqs(id, map); 444 return (IPluginModelBase[])map.values().toArray(new IPluginModelBase[map.size()]); 445 } 446 447 protected static void addPluginAndPrereqs(String id, TreeMap 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())) addPluginAndPrereqs(fragment.getId(), map); 472 } 473 } 474 } 475 476 477 protected File getConfigDir(ILaunchConfiguration config) { 478 if (fConfigDir == null) 479 fConfigDir = LaunchConfigurationHelper.getConfigurationArea(config); 480 481 return fConfigDir; 482 } 483 484 487 protected IProject[] getBuildOrder(ILaunchConfiguration configuration, 488 String mode) throws CoreException { 489 return computeBuildOrder(LaunchPluginValidator.getAffectedProjects(configuration)); 490 } 491 492 495 protected IProject[] getProjectsForProblemSearch( 496 ILaunchConfiguration configuration, String mode) 497 throws CoreException { 498 return LaunchPluginValidator.getAffectedProjects(configuration); 499 } 500 501 502 } 503 | Popular Tags |