1 12 package org.eclipse.jdt.internal.junit.launcher; 13 14 import java.io.BufferedWriter ; 15 import java.io.File ; 16 import java.io.FileWriter ; 17 import java.io.IOException ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 import java.util.Vector ; 23 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.FileLocator; 26 import org.eclipse.core.runtime.IStatus; 27 import org.eclipse.core.runtime.Platform; 28 import org.eclipse.core.runtime.Status; 29 30 import org.eclipse.jface.util.Assert; 31 32 import org.eclipse.debug.core.ILaunchConfiguration; 33 34 import org.eclipse.jdt.core.IType; 35 36 import org.eclipse.jdt.launching.ExecutionArguments; 37 import org.eclipse.jdt.launching.VMRunnerConfiguration; 38 39 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 40 41 import org.osgi.framework.Bundle; 42 43 46 public class JUnitLaunchConfiguration extends JUnitBaseLaunchConfiguration { 47 48 public static final String ID_JUNIT_APPLICATION= "org.eclipse.jdt.junit.launchconfig"; 53 protected VMRunnerConfiguration createVMRunner(ILaunchConfiguration configuration, TestSearchResult testTypes, int port, String runMode) throws CoreException { 54 String [] classPath = createClassPath(configuration, testTypes.getTestKind()); 55 VMRunnerConfiguration vmConfig= new VMRunnerConfiguration("org.eclipse.jdt.internal.junit.runner.RemoteTestRunner", classPath); Vector argv= getVMArgs(configuration, testTypes, port, runMode); 57 String [] args= new String [argv.size()]; 58 argv.copyInto(args); 59 vmConfig.setProgramArguments(args); 60 return vmConfig; 61 } 62 63 public Vector getVMArgs(ILaunchConfiguration configuration, TestSearchResult result, int port, String runMode) throws CoreException { 64 String progArgs= getProgramArguments(configuration); 65 String testName= configuration.getAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, ""); String testFailureNames= configuration.getAttribute(JUnitBaseLaunchConfiguration.FAILURES_FILENAME_ATTR, ""); 68 Vector argv= new Vector (10); 70 ExecutionArguments execArgs = new ExecutionArguments("", progArgs); String [] pa= execArgs.getProgramArgumentsArray(); 72 for (int i= 0; i < pa.length; i++) { 73 argv.add(pa[i]); 74 } 75 76 argv.addAll(getBasicArguments(configuration, port, runMode, result)); 77 78 IType[] testTypes = result.getTypes(); 79 80 if (testName.length() > 0) { 82 argv.add("-test"); argv.add(testTypes[0].getFullyQualifiedName()+":"+testName); } else if (testTypes.length > 1) { 85 String fileName= createTestNamesFile(testTypes); 86 argv.add("-testNameFile"); argv.add(fileName); 88 } else { 89 argv.add("-classNames"); for (int i= 0; i < testTypes.length; i++) 91 argv.add(testTypes[i].getFullyQualifiedName()); 92 } 93 if (testFailureNames.length() > 0) { 94 argv.add("-testfailures"); argv.add(testFailureNames); 96 } 97 98 return argv; 99 } 100 101 private String createTestNamesFile(IType[] testTypes) throws CoreException { 102 try { 103 File file= File.createTempFile("testNames", ".txt"); file.deleteOnExit(); 105 BufferedWriter bw= null; 106 try { 107 bw= new BufferedWriter (new FileWriter (file)); 108 for (int i= 0; i < testTypes.length; i++) { 109 String testName= testTypes[i].getFullyQualifiedName(); 110 bw.write(testName); 111 bw.newLine(); 112 } 113 } finally { 114 if (bw != null) { 115 bw.close(); 116 } 117 } 118 return file.getAbsolutePath(); 119 } catch (IOException e) { 120 throw new CoreException(new Status(IStatus.ERROR, JUnitPlugin.PLUGIN_ID, IStatus.ERROR, "", e)); } 122 } 123 124 public String [] createClassPath(ILaunchConfiguration configuration, ITestKind kind) throws CoreException { 125 String [] cp= getClasspath(configuration); 126 127 List junitEntries = new ClasspathLocalizer(Platform.inDevelopmentMode()).localizeClasspath(kind); 128 129 String [] classPath= new String [cp.length + junitEntries.size()]; 130 Object [] jea= junitEntries.toArray(); 131 System.arraycopy(cp, 0, classPath, 0, cp.length); 132 System.arraycopy(jea, 0, classPath, cp.length, jea.length); 133 return classPath; 134 } 135 } 136 137 class ClasspathLocalizer { 138 139 private boolean fInDevelopmentMode; 140 141 protected ClasspathLocalizer() { 142 this(false); 143 } 144 145 public ClasspathLocalizer(boolean inDevelopmentMode) { 146 fInDevelopmentMode = inDevelopmentMode; 147 } 148 149 protected List localizeClasspath(ITestKind kind) { 150 JUnitRuntimeClasspathEntry[] entries= kind.getClasspathEntries(); 151 List junitEntries= new ArrayList (); 152 153 for (int i= 0; i < entries.length; i++) { 154 try { 155 addEntry(junitEntries, entries[i]); 156 } catch (IOException e) { 157 Assert.isTrue(false, entries[i].getPluginId() + " is available (required JAR)"); } 159 } 160 return junitEntries; 161 } 162 163 private void addEntry(List junitEntries, final JUnitRuntimeClasspathEntry entry) throws IOException , MalformedURLException { 164 String entryString= entryString(entry); 165 if (entryString != null) 166 junitEntries.add(entryString); 167 } 168 169 private String entryString(final JUnitRuntimeClasspathEntry entry) throws IOException , MalformedURLException { 170 if (inDevelopmentMode()) { 171 try { 172 return localURL(entry.developmentModeEntry()); 173 } catch (IOException e3) { 174 } 176 } 177 return localURL(entry); 178 } 179 180 private boolean inDevelopmentMode() { 181 return fInDevelopmentMode; 182 } 183 184 private String localURL(JUnitRuntimeClasspathEntry jar) throws IOException , MalformedURLException { 185 Bundle bundle= JUnitPlugin.getDefault().getBundle(jar.getPluginId()); 186 URL url; 187 if (jar.getPluginRelativePath() == null) 188 url= bundle.getEntry("/"); else 190 url= bundle.getEntry(jar.getPluginRelativePath()); 191 if (url == null) 192 throw new IOException (); 193 return FileLocator.toFileURL(url).getFile(); 194 } 195 } 196 197 | Popular Tags |