KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > launcher > JUnitLaunchConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  * David Saff (saff@mit.edu) - bug 102632: [JUnit] Support for JUnit 4.
11  *******************************************************************************/

12 package org.eclipse.jdt.internal.junit.launcher;
13
14 import java.io.BufferedWriter JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileWriter JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.net.MalformedURLException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Vector JavaDoc;
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 /**
44  * Launch configuration delegate for a plain JUnit test.
45  */

46 public class JUnitLaunchConfiguration extends JUnitBaseLaunchConfiguration {
47
48     public static final String JavaDoc ID_JUNIT_APPLICATION= "org.eclipse.jdt.junit.launchconfig"; //$NON-NLS-1$
49
/**
50      * Add a VMRunner with a class path that includes org.eclipse.jdt.junit plugin.
51      * In addition it adds the port for the RemoteTestRunner as an argument
52      */

53     protected VMRunnerConfiguration createVMRunner(ILaunchConfiguration configuration, TestSearchResult testTypes, int port, String JavaDoc runMode) throws CoreException {
54         String JavaDoc[] classPath = createClassPath(configuration, testTypes.getTestKind());
55         VMRunnerConfiguration vmConfig= new VMRunnerConfiguration("org.eclipse.jdt.internal.junit.runner.RemoteTestRunner", classPath); //$NON-NLS-1$
56
Vector JavaDoc argv= getVMArgs(configuration, testTypes, port, runMode);
57         String JavaDoc[] args= new String JavaDoc[argv.size()];
58         argv.copyInto(args);
59         vmConfig.setProgramArguments(args);
60         return vmConfig;
61     }
62
63     public Vector JavaDoc getVMArgs(ILaunchConfiguration configuration, TestSearchResult result, int port, String JavaDoc runMode) throws CoreException {
64         String JavaDoc progArgs= getProgramArguments(configuration);
65         String JavaDoc testName= configuration.getAttribute(JUnitBaseLaunchConfiguration.TESTNAME_ATTR, ""); //$NON-NLS-1$
66
String JavaDoc testFailureNames= configuration.getAttribute(JUnitBaseLaunchConfiguration.FAILURES_FILENAME_ATTR, ""); //$NON-NLS-1$
67

68         // insert the program arguments
69
Vector JavaDoc argv= new Vector JavaDoc(10);
70         ExecutionArguments execArgs = new ExecutionArguments("", progArgs); //$NON-NLS-1$
71
String JavaDoc[] 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         // a testname was specified just run the single test
81
if (testName.length() > 0) {
82             argv.add("-test"); //$NON-NLS-1$
83
argv.add(testTypes[0].getFullyQualifiedName()+":"+testName); //$NON-NLS-1$
84
} else if (testTypes.length > 1) {
85             String JavaDoc fileName= createTestNamesFile(testTypes);
86             argv.add("-testNameFile"); //$NON-NLS-1$
87
argv.add(fileName);
88         } else {
89             argv.add("-classNames"); //$NON-NLS-1$
90
for (int i= 0; i < testTypes.length; i++)
91                 argv.add(testTypes[i].getFullyQualifiedName());
92         }
93         if (testFailureNames.length() > 0) {
94             argv.add("-testfailures"); //$NON-NLS-1$
95
argv.add(testFailureNames);
96         }
97
98         return argv;
99     }
100
101     private String JavaDoc createTestNamesFile(IType[] testTypes) throws CoreException {
102         try {
103             File JavaDoc file= File.createTempFile("testNames", ".txt"); //$NON-NLS-1$ //$NON-NLS-2$
104
file.deleteOnExit();
105             BufferedWriter JavaDoc bw= null;
106             try {
107                 bw= new BufferedWriter JavaDoc(new FileWriter JavaDoc(file));
108                 for (int i= 0; i < testTypes.length; i++) {
109                     String JavaDoc 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 JavaDoc e) {
120             throw new CoreException(new Status(IStatus.ERROR, JUnitPlugin.PLUGIN_ID, IStatus.ERROR, "", e)); //$NON-NLS-1$
121
}
122     }
123     
124     public String JavaDoc[] createClassPath(ILaunchConfiguration configuration, ITestKind kind) throws CoreException {
125         String JavaDoc[] cp= getClasspath(configuration);
126                 
127         List JavaDoc junitEntries = new ClasspathLocalizer(Platform.inDevelopmentMode()).localizeClasspath(kind);
128                 
129         String JavaDoc[] classPath= new String JavaDoc[cp.length + junitEntries.size()];
130         Object JavaDoc[] 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 JavaDoc localizeClasspath(ITestKind kind) {
150         JUnitRuntimeClasspathEntry[] entries= kind.getClasspathEntries();
151         List JavaDoc junitEntries= new ArrayList JavaDoc();
152         
153         for (int i= 0; i < entries.length; i++) {
154             try {
155                 addEntry(junitEntries, entries[i]);
156             } catch (IOException JavaDoc e) {
157                 Assert.isTrue(false, entries[i].getPluginId() + " is available (required JAR)"); //$NON-NLS-1$
158
}
159         }
160         return junitEntries;
161     }
162
163     private void addEntry(List JavaDoc junitEntries, final JUnitRuntimeClasspathEntry entry) throws IOException JavaDoc, MalformedURLException JavaDoc {
164         String JavaDoc entryString= entryString(entry);
165         if (entryString != null)
166             junitEntries.add(entryString);
167     }
168
169     private String JavaDoc entryString(final JUnitRuntimeClasspathEntry entry) throws IOException JavaDoc, MalformedURLException JavaDoc {
170         if (inDevelopmentMode()) {
171             try {
172                 return localURL(entry.developmentModeEntry());
173             } catch (IOException JavaDoc e3) {
174                 // fall through and try default
175
}
176         }
177         return localURL(entry);
178     }
179
180     private boolean inDevelopmentMode() {
181         return fInDevelopmentMode;
182     }
183     
184     private String JavaDoc localURL(JUnitRuntimeClasspathEntry jar) throws IOException JavaDoc, MalformedURLException JavaDoc {
185         Bundle bundle= JUnitPlugin.getDefault().getBundle(jar.getPluginId());
186         URL JavaDoc url;
187         if (jar.getPluginRelativePath() == null)
188             url= bundle.getEntry("/"); //$NON-NLS-1$
189
else
190             url= bundle.getEntry(jar.getPluginRelativePath());
191         if (url == null)
192             throw new IOException JavaDoc();
193         return FileLocator.toFileURL(url).getFile();
194     }
195 }
196
197
Popular Tags