KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > uka > ipd > coverage > plugin > launcher > CoverageLaunchConfiguration


1 /*
2  * Created on 07.10.2004
3  *
4  * written by Matthias Kempka
5  */

6 package de.uka.ipd.coverage.plugin.launcher;
7
8 import java.io.*;
9 import java.net.URL JavaDoc;
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 /**
28  * Created on 07.10.2004
29  * @author Matthias Kempka
30  */

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."); //$NON-NLS-1$
37
}
38
39     public void launch(ILaunchConfiguration configuration, String JavaDoc mode,
40             ILaunch launch, IProgressMonitor monitor) throws CoreException {
41         logger.debug("now launching..."); //$NON-NLS-1$
42

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 port = SocketUtil.findFreePort();
53
int coveragePort = SocketUtil.findFreePort();
54
55         // ======= BEGIN COPIED FROM JUnitBaseLaunchConfiguration
56

57         IType[] testTypes = getTestTypes(configuration, javaProject, monitor);
58         if (testTypes.length == 0) {
59             abort("", null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE); //$NON-NLS-1$
60
}
61
62         String JavaDoc testName= configuration.getAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, ""); //$NON-NLS-1$
63

64         // insert the program arguments
65
Vector appArgs = new Vector(10);
66         ExecutionArguments execArgs = new ExecutionArguments("", getProgramArguments(configuration)); //$NON-NLS-1$
67

68         String JavaDoc[] 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"); //$NON-NLS-1$
80
appArgs.add(Integer.toString(junitPort));
81         //argv("-debugging");
82

83         if (configuration.getAttribute(JUnitBaseLaunchConfiguration.ATTR_KEEPRUNNING, false) && mode.equals(ILaunchManager.DEBUG_MODE))
84             appArgs.add(0, "-keepalive"); //$NON-NLS-1$
85

86         // a testname was specified just run the single test
87
if (testName.length() > 0) {
88             appArgs.add("-test"); //$NON-NLS-1$
89
appArgs.add(testTypes[0].getFullyQualifiedName()+":"+testName); //$NON-NLS-1$
90
} else if (testTypes.length > 1) {
91             String JavaDoc fileName= createTestNamesFile(testTypes);
92             appArgs.add("-testNameFile"); //$NON-NLS-1$
93
appArgs.add(fileName);
94         } else {
95             appArgs.add("-classNames"); //$NON-NLS-1$
96
for (int i= 0; i < testTypes.length; i++)
97                 appArgs.add(testTypes[i].getFullyQualifiedName());
98         }
99         
100         // END BEGIN COPIED FROM JUnitBaseLaunchConfiguration
101

102         appArgs.add("-coveragePort"); //$NON-NLS-1$
103
appArgs.add("" + coveragePort); //$NON-NLS-1$
104

105 // VMRunnerConfiguration vmConfiguration =
106
// createVMRunner(configuration, types, port, runmode);
107
String JavaDoc[] classPath= createClassPath(configuration);
108         VMRunnerConfiguration vmConfiguration = new VMRunnerConfiguration("de.uka.ipd.coverage.plugin.remote.CoverageRemoteTestRunner", classPath); //$NON-NLS-1$
109
vmConfiguration.setProgramArguments((String JavaDoc[]) appArgs.toArray(new String JavaDoc[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."); //$NON-NLS-1$
116
}
117     
118     private static void startTestRunnerClient(int port) {
119         CoverageRemoteTestRunnerClient client = new CoverageRemoteTestRunnerClient();
120         client.listen(port);
121     }
122     
123 // protected VMRunnerConfiguration createVMRunner(ILaunchConfiguration configuration,
124
// IType[] testTypes, int port, String runMode) throws CoreException {
125
// String[] classPath= createClassPath(configuration);
126
// String progArgs= getProgramArguments(configuration);
127
// String testName= configuration.getAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, ""); //$NON-NLS-1$
128
//
129
// // insert the program arguments
130
//// Vector argv= new Vector(10);
131
//// ExecutionArguments execArgs = new ExecutionArguments("", progArgs); //$NON-NLS-1$
132
//// String[] pa= execArgs.getProgramArgumentsArray();
133
//// for (int i= 0; i < pa.length; i++) {
134
//// argv.add(pa[i]);
135
//// }
136
////
137
//// argv.add("-version"); //$NON-NLS-1$
138
//// argv.add("3"); //$NON-NLS-1$
139
////
140
//// argv.add("-port"); //$NON-NLS-1$
141
//// argv.add(Integer.toString(port));
142
//// //argv("-debugging");
143
////
144
//// if (keepAlive(configuration) && runMode.equals(ILaunchManager.DEBUG_MODE))
145
//// argv.add(0, "-keepalive"); //$NON-NLS-1$
146
////
147
//// // a testname was specified just run the single test
148
//// if (testName.length() > 0) {
149
//// argv.add("-test"); //$NON-NLS-1$
150
//// argv.add(testTypes[0].getFullyQualifiedName()+":"+testName); //$NON-NLS-1$
151
//// } else if (testTypes.length > 1) {
152
//// String fileName= createTestNamesFile(testTypes);
153
//// argv.add("-testNameFile"); //$NON-NLS-1$
154
//// argv.add(fileName);
155
//// } else {
156
//// argv.add("-classNames"); //$NON-NLS-1$
157
//// for (int i= 0; i < testTypes.length; i++)
158
//// argv.add(testTypes[i].getFullyQualifiedName());
159
//// }
160
//// String[] args= new String[argv.size()];
161
//// argv.copyInto(args);
162
//// vmConfig.setProgramArguments(args);
163
// return vmConfig;
164
// }
165

166     
167     /**
168      * If the LaunchContainter attribute is empty calls findSingleTest() or
169      * calls findTestsInContainter otherwise.
170      * (copied from JUnit Plugin)
171      */

172     public IType[] getTestTypes(ILaunchConfiguration configuration, IJavaProject javaProject, IProgressMonitor monitor) throws CoreException {
173         String JavaDoc testTypeName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, (String JavaDoc)null);
174         if (monitor == null)
175             monitor = new NullProgressMonitor();
176         
177         String JavaDoc containerHandle = configuration.getAttribute(JUnitBaseLaunchConfiguration.LAUNCH_CONTAINER_ATTR, ""); //$NON-NLS-1$
178
if (containerHandle.length() == 0) {
179             return findSingleTest(javaProject, testTypeName);
180         }
181         else
182             return findTestsInContainer(javaProject, containerHandle, monitor);
183     }
184
185     /**
186      * Find the specified (fully-qualified) type name in the specified java
187      * project.
188      * (copied from JUnit Plugin)
189      */

190     public IType[] findSingleTest(IJavaProject javaProject, String JavaDoc 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); //$NON-NLS-1$
196
}
197         if (type == null) {
198             abort(de.uka.ipd.coverage.plugin.launcher.JUnitMessages.getString("CoverageLaunchConfiguration.0"), null, IJavaLaunchConfigurationConstants.ERR_UNSPECIFIED_MAIN_TYPE); //$NON-NLS-1$
199
}
200         return new IType[]{type};
201     }
202     
203     /**
204      * Uses TestSearchEngine.doFindTests() to find tests.
205      * (copied from JUnit Plugin)
206      */

207     private IType[] findTestsInContainer(IJavaProject javaProject, String JavaDoc containerHandle, IProgressMonitor monitor) {
208         IJavaElement container= JavaCore.create(containerHandle);
209         Set result= new HashSet();
210         try {
211             TestSearchEngine.doFindTests(new Object JavaDoc[]{container}, result, monitor);
212         } catch (InterruptedException JavaDoc e) {
213             //
214
}
215         return (IType[]) result.toArray(new IType[result.size()]) ;
216     }
217
218     private String JavaDoc createTestNamesFile(IType[] testTypes) throws CoreException {
219         try {
220             File file= File.createTempFile("testNames", ".txt"); //$NON-NLS-1$ //$NON-NLS-2$
221
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 JavaDoc 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)); //$NON-NLS-1$
238
}
239     }
240     
241     private String JavaDoc[] createClassPath(ILaunchConfiguration configuration) throws CoreException {
242
243         URL JavaDoc runtimeURL= Platform.getBundle("org.eclipse.jdt.junit.runtime").getEntry("/"); //$NON-NLS-1$ //$NON-NLS-2$
244
URL JavaDoc coreURL= Platform.getBundle("org.eclipse.core.runtime").getEntry("/"); //$NON-NLS-1$ //$NON-NLS-2$
245
URL JavaDoc url= Platform.getBundle(JUnitPlugin.PLUGIN_ID).getEntry("/"); //$NON-NLS-1$
246
URL JavaDoc coverageURL = Platform.getBundle(CoveragePlugin.PLUGIN_ID).getEntry("/"); //$NON-NLS-1$
247
URL JavaDoc workbenchURL = Platform.getBundle("org.eclipse.ui.workbench").getEntry("/"); //$NON-NLS-1$ //$NON-NLS-2$
248
URL JavaDoc osgiURL = Platform.getBundle("org.eclipse.osgi").getEntry("/"); //$NON-NLS-1$ //$NON-NLS-2$
249
URL JavaDoc debugCoreURL = Platform.getBundle("org.eclipse.debug.core").getEntry("/"); //$NON-NLS-1$ //$NON-NLS-2$
250
URL JavaDoc jfaceURL = Platform.getBundle("org.eclipse.jface").getEntry("/"); //$NON-NLS-1$ //$NON-NLS-2$
251
URL JavaDoc jdtCoreURL = Platform.getBundle("org.eclipse.jdt.core").getEntry("/"); //$NON-NLS-1$ //$NON-NLS-2$
252

253         List classPath = new ArrayList();
254
255         String JavaDoc[] cp= getClasspath(configuration);
256         classPath.addAll(Arrays.asList(cp));
257
258         try {
259                 classPath.add(Platform.asLocalURL(new URL JavaDoc(url, "junitsupport.jar")).getFile()); //$NON-NLS-1$
260
classPath.add(Platform.asLocalURL(new URL JavaDoc(runtimeURL, "junitruntime.jar")).getFile()); //$NON-NLS-1$
261
if (Platform.inDevelopmentMode()) {
262                     classPath.add(Platform.asLocalURL(new URL JavaDoc(coverageURL, "bin")).getFile()); //$NON-NLS-1$
263
} else {
264                     classPath.add(Platform.asLocalURL(new URL JavaDoc(coverageURL, "coverlipse.jar")).getFile()); //$NON-NLS-1$
265
}
266                 classPath.add(Platform.asLocalURL(new URL JavaDoc(coverageURL, "bcel-5.1.jar")).getFile()); //$NON-NLS-1$
267
classPath.add(Platform.asLocalURL(new URL JavaDoc(coverageURL, "jakarta-regexp-1.3.jar")).getFile()); //$NON-NLS-1$
268
classPath.add(Platform.asLocalURL(new URL JavaDoc(coreURL, "runtime.jar")).getFile()); //$NON-NLS-1$
269
classPath.add(Platform.asLocalURL(new URL JavaDoc(workbenchURL, "workbench.jar")).getFile()); //$NON-NLS-1$
270
classPath.add(Platform.asLocalURL(new URL JavaDoc(osgiURL, "osgi.jar")).getFile()); //$NON-NLS-1$
271
classPath.add(Platform.asLocalURL(new URL JavaDoc(debugCoreURL, "dtcore.jar")).getFile()); //$NON-NLS-1$
272
classPath.add(Platform.asLocalURL(new URL JavaDoc(jfaceURL, "jface.jar")).getFile()); //$NON-NLS-1$
273
classPath.add(Platform.asLocalURL(new URL JavaDoc(jdtCoreURL, "jdtcore.jar")).getFile()); //$NON-NLS-1$
274
// }
275
} catch (IOException e) {
276             JUnitPlugin.log(e); // TODO abort run and inform user
277
}
278         
279         return (String JavaDoc[]) classPath.toArray(new String JavaDoc[classPath.size()]);
280     }
281     
282 }
283
Popular Tags