1 6 package de.uka.ipd.coverage.plugin.launcher; 7 8 import java.io.*; 9 import java.net.URL ; 10 import java.util.*; 11 12 import org.eclipse.core.runtime.*; 13 import org.eclipse.debug.core.ILaunch; 14 import org.eclipse.debug.core.ILaunchConfiguration; 15 import org.eclipse.debug.core.ILaunchManager; 16 import org.eclipse.jdt.core.*; 17 import org.eclipse.jdt.internal.junit.launcher.JUnitBaseLaunchConfiguration; 18 import org.eclipse.jdt.internal.junit.ui.JUnitMessages; 19 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 20 import org.eclipse.jdt.internal.junit.util.TestSearchEngine; 21 import org.eclipse.jdt.launching.*; 22 import org.osgi.framework.BundleException; 23 24 import de.uka.ipd.coverage.plugin.CoveragePlugin; 25 import de.uka.ipd.coverage.utils.Logger; 26 27 31 public class CoverageLaunchConfiguration extends AbstractJavaLaunchConfigurationDelegate { 32 33 private Logger logger = new Logger(this); 34 35 public CoverageLaunchConfiguration() { 36 logger.debug(this.getClass().getName() + " instanciated."); } 38 39 public void launch(ILaunchConfiguration configuration, String mode, 40 ILaunch launch, IProgressMonitor monitor) throws CoreException { 41 logger.debug("now launching..."); 43 try { 44 Platform.getBundle(JUnitPlugin.PLUGIN_ID).start(); 45 Platform.getBundle(CoveragePlugin.PLUGIN_ID).start(); 46 } catch (BundleException e) { 47 logger.log(e); 48 e.printStackTrace(); 49 } 50 IJavaProject javaProject = getJavaProject(configuration); 51 CoveragePlugin.getDefault().setJavaProject(javaProject); 52 int coveragePort = SocketUtil.findFreePort(); 54 55 57 IType[] testTypes = getTestTypes(configuration, javaProject, monitor); 58 if (testTypes.length == 0) { 59 abort("", null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE); } 61 62 String testName= configuration.getAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, ""); 64 Vector appArgs = new Vector(10); 66 ExecutionArguments execArgs = new ExecutionArguments("", getProgramArguments(configuration)); 68 String [] programArguments = execArgs.getProgramArgumentsArray(); 69 for (int i= 0; i < programArguments.length; i++) { 70 appArgs.add(programArguments[i]); 71 } 72 73 int junitPort= SocketUtil.findFreePort(); 74 setDefaultSourceLocator(launch, configuration); 75 76 launch.setAttribute(JUnitBaseLaunchConfiguration.PORT_ATTR, Integer.toString(junitPort)); 77 launch.setAttribute(JUnitBaseLaunchConfiguration.TESTTYPE_ATTR, testTypes[0].getHandleIdentifier()); 78 79 appArgs.add("-port"); appArgs.add(Integer.toString(junitPort)); 81 83 if (configuration.getAttribute(JUnitBaseLaunchConfiguration.ATTR_KEEPRUNNING, false) && mode.equals(ILaunchManager.DEBUG_MODE)) 84 appArgs.add(0, "-keepalive"); 86 if (testName.length() > 0) { 88 appArgs.add("-test"); appArgs.add(testTypes[0].getFullyQualifiedName()+":"+testName); } else if (testTypes.length > 1) { 91 String fileName= createTestNamesFile(testTypes); 92 appArgs.add("-testNameFile"); appArgs.add(fileName); 94 } else { 95 appArgs.add("-classNames"); for (int i= 0; i < testTypes.length; i++) 97 appArgs.add(testTypes[i].getFullyQualifiedName()); 98 } 99 100 102 appArgs.add("-coveragePort"); appArgs.add("" + coveragePort); 105 String [] classPath= createClassPath(configuration); 108 VMRunnerConfiguration vmConfiguration = new VMRunnerConfiguration("de.uka.ipd.coverage.plugin.remote.CoverageRemoteTestRunner", classPath); vmConfiguration.setProgramArguments((String []) appArgs.toArray(new String [appArgs.size()])); 110 IVMInstall vm = verifyVMInstall(configuration); 111 IVMRunner runner = vm.getVMRunner(mode); 112 setDefaultSourceLocator(launch, configuration); 113 startTestRunnerClient(coveragePort); 114 runner.run(vmConfiguration, launch, monitor); 115 logger.debug("exiting."); } 117 118 private static void startTestRunnerClient(int port) { 119 CoverageRemoteTestRunnerClient client = new CoverageRemoteTestRunnerClient(); 120 client.listen(port); 121 } 122 123 166 167 172 public IType[] getTestTypes(ILaunchConfiguration configuration, IJavaProject javaProject, IProgressMonitor monitor) throws CoreException { 173 String testTypeName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String )null); 174 if (monitor == null) 175 monitor = new NullProgressMonitor(); 176 177 String containerHandle = configuration.getAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, ""); if (containerHandle.length() == 0) { 179 return findSingleTest(javaProject, testTypeName); 180 } 181 else 182 return findTestsInContainer(javaProject, containerHandle, monitor); 183 } 184 185 190 public IType[] findSingleTest(IJavaProject javaProject, String testName) throws CoreException { 191 IType type = null; 192 try { 193 type = javaProject.findType(testName); 194 } catch (JavaModelException jme) { 195 abort(de.uka.ipd.coverage.plugin.launcher.JUnitMessages.getString("CoverageLaunchConfiguration.0"), null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE); } 197 if (type == null) { 198 abort(de.uka.ipd.coverage.plugin.launcher.JUnitMessages.getString("CoverageLaunchConfiguration.0"), null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE); } 200 return new IType[]{type}; 201 } 202 203 207 private IType[] findTestsInContainer(IJavaProject javaProject, String containerHandle, IProgressMonitor monitor) { 208 IJavaElement container= JavaCore.create(containerHandle); 209 Set result= new HashSet(); 210 try { 211 TestSearchEngine.doFindTests(new Object []{container}, result, monitor); 212 } catch (InterruptedException e) { 213 } 215 return (IType[]) result.toArray(new IType[result.size()]) ; 216 } 217 218 private String createTestNamesFile(IType[] testTypes) throws CoreException { 219 try { 220 File file= File.createTempFile("testNames", ".txt"); file.deleteOnExit(); 222 BufferedWriter bw= null; 223 try { 224 bw= new BufferedWriter(new FileWriter(file)); 225 for (int i= 0; i < testTypes.length; i++) { 226 String testName= testTypes[i].getFullyQualifiedName(); 227 bw.write(testName); 228 bw.newLine(); 229 } 230 } finally { 231 if (bw != null) { 232 bw.close(); 233 } 234 } 235 return file.getAbsolutePath(); 236 } catch (IOException e) { 237 throw new CoreException(new Status(IStatus.ERROR, JUnitPlugin.PLUGIN_ID, IStatus.ERROR, "", e)); } 239 } 240 241 private String [] createClassPath(ILaunchConfiguration configuration) throws CoreException { 242 243 URL runtimeURL= Platform.getBundle("org.eclipse.jdt.junit.runtime").getEntry("/"); URL coreURL= Platform.getBundle("org.eclipse.core.runtime").getEntry("/"); URL url= Platform.getBundle(JUnitPlugin.PLUGIN_ID).getEntry("/"); URL coverageURL = Platform.getBundle(CoveragePlugin.PLUGIN_ID).getEntry("/"); URL workbenchURL = Platform.getBundle("org.eclipse.ui.workbench").getEntry("/"); URL osgiURL = Platform.getBundle("org.eclipse.osgi").getEntry("/"); URL debugCoreURL = Platform.getBundle("org.eclipse.debug.core").getEntry("/"); URL jfaceURL = Platform.getBundle("org.eclipse.jface").getEntry("/"); URL jdtCoreURL = Platform.getBundle("org.eclipse.jdt.core").getEntry("/"); 253 List classPath = new ArrayList(); 254 255 String [] cp= getClasspath(configuration); 256 classPath.addAll(Arrays.asList(cp)); 257 258 try { 259 classPath.add(Platform.asLocalURL(new URL (url, "junitsupport.jar")).getFile()); classPath.add(Platform.asLocalURL(new URL (runtimeURL, "junitruntime.jar")).getFile()); if (Platform.inDevelopmentMode()) { 262 classPath.add(Platform.asLocalURL(new URL (coverageURL, "bin")).getFile()); } else { 264 classPath.add(Platform.asLocalURL(new URL (coverageURL, "coverlipse.jar")).getFile()); } 266 classPath.add(Platform.asLocalURL(new URL (coverageURL, "bcel-5.1.jar")).getFile()); classPath.add(Platform.asLocalURL(new URL (coverageURL, "jakarta-regexp-1.3.jar")).getFile()); classPath.add(Platform.asLocalURL(new URL (coreURL, "runtime.jar")).getFile()); classPath.add(Platform.asLocalURL(new URL (workbenchURL, "workbench.jar")).getFile()); classPath.add(Platform.asLocalURL(new URL (osgiURL, "osgi.jar")).getFile()); classPath.add(Platform.asLocalURL(new URL (debugCoreURL, "dtcore.jar")).getFile()); classPath.add(Platform.asLocalURL(new URL (jfaceURL, "jface.jar")).getFile()); classPath.add(Platform.asLocalURL(new URL (jdtCoreURL, "jdtcore.jar")).getFile()); } catch (IOException e) { 276 JUnitPlugin.log(e); } 278 279 return (String []) classPath.toArray(new String [classPath.size()]); 280 } 281 282 } 283 | Popular Tags |