|
|
@@ -67,6 +67,7 @@ import org.apache.tools.ant.types.Environment; |
|
|
|
import org.apache.tools.ant.types.CommandlineJava; |
|
|
|
import org.apache.tools.ant.types.Path; |
|
|
|
import org.apache.tools.ant.types.EnumeratedAttribute; |
|
|
|
import org.apache.tools.ant.util.FileUtils; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.FileOutputStream; |
|
|
@@ -76,7 +77,6 @@ import java.io.OutputStream; |
|
|
|
import java.util.Enumeration; |
|
|
|
import java.util.Hashtable; |
|
|
|
import java.util.Properties; |
|
|
|
import java.util.Random; |
|
|
|
import java.util.Vector; |
|
|
|
|
|
|
|
import java.net.URL; |
|
|
@@ -88,7 +88,8 @@ import java.net.URL; |
|
|
|
* created by Erich Gamma and Kent Beck. JUnit can be found at <a |
|
|
|
* href="http://www.junit.org">http://www.junit.org</a>. |
|
|
|
* |
|
|
|
* <p> <code>JUnitTask</code> can run a single specific <code>JUnitTest</code> using the <code>test</code> element. |
|
|
|
* <p> <code>JUnitTask</code> can run a single specific |
|
|
|
* <code>JUnitTest</code> using the <code>test</code> element.</p> |
|
|
|
* For example, the following target <code><pre> |
|
|
|
* <target name="test-int-chars" depends="jar-test"> |
|
|
|
* <echo message="testing international characters"/> |
|
|
@@ -98,14 +99,19 @@ import java.net.URL; |
|
|
|
* <test name="org.apache.ecs.InternationalCharTest" /> |
|
|
|
* </junit> |
|
|
|
* </target> |
|
|
|
* </pre></code> runs a single junit test (<code>org.apache.ecs.InternationalCharTest</code>) |
|
|
|
* in the current VM using the path with id <code>classpath</code> as classpath |
|
|
|
* and presents the results formatted using the standard <code>plain</code> formatter on the command line. |
|
|
|
* </pre></code> |
|
|
|
* <p>runs a single junit test |
|
|
|
* (<code>org.apache.ecs.InternationalCharTest</code>) in the current |
|
|
|
* VM using the path with id <code>classpath</code> as classpath and |
|
|
|
* presents the results formatted using the standard |
|
|
|
* <code>plain</code> formatter on the command line.</p> |
|
|
|
* |
|
|
|
* <p> This task can also run batches of tests. |
|
|
|
* The <code>batchtest</code> element creates a <code>BatchTest</code> based on a fileset. |
|
|
|
* This allows, for example, all classes found in directory to be run as testcases. |
|
|
|
* For example, <code><pre> |
|
|
|
* <p> This task can also run batches of tests. The |
|
|
|
* <code>batchtest</code> element creates a <code>BatchTest</code> |
|
|
|
* based on a fileset. This allows, for example, all classes found in |
|
|
|
* directory to be run as testcases.</p> |
|
|
|
* |
|
|
|
* <p>For example,</p><code><pre> |
|
|
|
* <target name="run-tests" depends="dump-info,compile-tests" if="junit.present"> |
|
|
|
* <junit printsummary="no" haltonfailure="yes" fork="${junit.fork}"> |
|
|
|
* <jvmarg value="-classic"/> |
|
|
@@ -119,22 +125,21 @@ import java.net.URL; |
|
|
|
* </batchtest> |
|
|
|
* </junit> |
|
|
|
* </target> |
|
|
|
* </pre></code> this target finds any classes with a <code>test</code> directory anywhere in their path |
|
|
|
* (under the top <code>${tests.dir}</code>, of course) and creates <code>JUnitTest</code>'s for each one. |
|
|
|
* </pre></code> |
|
|
|
* <p>this target finds any classes with a <code>test</code> directory |
|
|
|
* anywhere in their path (under the top <code>${tests.dir}</code>, of |
|
|
|
* course) and creates <code>JUnitTest</code>'s for each one.</p> |
|
|
|
* |
|
|
|
* <p> Of course, <code><junit></code> and <code><batch></code> elements can be combined |
|
|
|
* for more complex tests. For an example, see the ant <code>build.xml</code> target <code>run-tests</code> |
|
|
|
* (the second example is an edited version). |
|
|
|
* <p> Of course, <code><junit></code> and |
|
|
|
* <code><batch></code> elements can be combined for more |
|
|
|
* complex tests. For an example, see the ant <code>build.xml</code> |
|
|
|
* target <code>run-tests</code> (the second example is an edited |
|
|
|
* version).</p> |
|
|
|
* |
|
|
|
* <p> To spawn a new Java VM to prevent interferences between |
|
|
|
* different testcases, you need to enable <code>fork</code>. |
|
|
|
* A number of attributes and elements allow you to set up how this JVM runs. |
|
|
|
* <ul> |
|
|
|
* <li>{@link #setTimeout} property sets the maximum time allowed before a test is 'timed out' |
|
|
|
* <li>{@link #setMaxmemory} property sets memory assignment for the forked jvm |
|
|
|
* <li>{@link #setJvm} property allows the jvm to be specified |
|
|
|
* <li>The <code><jvmarg></code> element sets arguements to be passed to the forked jvm |
|
|
|
* </ul> |
|
|
|
* different testcases, you need to enable <code>fork</code>. A |
|
|
|
* number of attributes and elements allow you to set up how this JVM |
|
|
|
* runs. |
|
|
|
* @author Thomas Haas |
|
|
|
* @author <a href="mailto:stefan.bodewig@epost.de">Stefan Bodewig</a> |
|
|
|
* @author <a href="mailto:sbailliez@imediation.com">Stephane Bailliez</a> |
|
|
@@ -143,6 +148,8 @@ import java.net.URL; |
|
|
|
* |
|
|
|
* @version $Revision$ |
|
|
|
* |
|
|
|
* @since Ant 1.2 |
|
|
|
* |
|
|
|
* @see JUnitTest |
|
|
|
* @see BatchTest |
|
|
|
*/ |
|
|
@@ -166,11 +173,16 @@ public class JUnitTask extends Task { |
|
|
|
private Path antRuntimeClasses = null; |
|
|
|
|
|
|
|
/** |
|
|
|
* Tells this task whether to smartly filter the stack frames of JUnit testcase |
|
|
|
* errors and failures before reporting them. This property is applied on all |
|
|
|
* BatchTest (batchtest) and JUnitTest (test) however it can possibly be |
|
|
|
* overridden by their own properties. |
|
|
|
* @param value <tt>false</tt> if it should not filter, otherwise <tt>true<tt> |
|
|
|
* Tells this task whether to smartly filter the stack frames of |
|
|
|
* JUnit testcase errors and failures before reporting them. |
|
|
|
* |
|
|
|
* <p>This property is applied on all BatchTest (batchtest) and |
|
|
|
* JUnitTest (test) however it can possibly be overridden by their |
|
|
|
* own properties.</p> |
|
|
|
* @param value <tt>false</tt> if it should not filter, otherwise |
|
|
|
* <tt>true<tt> |
|
|
|
* |
|
|
|
* @since Ant 1.5 |
|
|
|
*/ |
|
|
|
public void setFiltertrace(boolean value) { |
|
|
|
Enumeration enum = allTests(); |
|
|
@@ -181,10 +193,14 @@ public class JUnitTask extends Task { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Tells this task to halt when there is an error in a test. |
|
|
|
* this property is applied on all BatchTest (batchtest) and JUnitTest (test) |
|
|
|
* however it can possibly be overridden by their own properties. |
|
|
|
* @param value <tt>true</tt> if it should halt, otherwise <tt>false</tt> |
|
|
|
* Tells this task to halt when there is an error in a test. this |
|
|
|
* property is applied on all BatchTest (batchtest) and JUnitTest |
|
|
|
* (test) however it can possibly be overridden by their own |
|
|
|
* properties. |
|
|
|
* @param value <tt>true</tt> if it should halt, otherwise |
|
|
|
* <tt>false</tt> |
|
|
|
* |
|
|
|
* @since Ant 1.2 |
|
|
|
*/ |
|
|
|
public void setHaltonerror(boolean value) { |
|
|
|
Enumeration enum = allTests(); |
|
|
@@ -195,10 +211,16 @@ public class JUnitTask extends Task { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Tells this task to set the named property to "true" when there is a error in a test. |
|
|
|
* This property is applied on all BatchTest (batchtest) and JUnitTest (test), |
|
|
|
* however, it can possibly be overriden by their own properties. |
|
|
|
* @param propertyName the name of the property to set in the event of an error. |
|
|
|
* Tells this task to set the named property to "true" when there |
|
|
|
* is a error in a test. |
|
|
|
* |
|
|
|
* <p>This property is applied on all BatchTest (batchtest) and |
|
|
|
* JUnitTest (test), however, it can possibly be overriden by |
|
|
|
* their own properties.</p> |
|
|
|
* @param propertyName the name of the property to set in the |
|
|
|
* event of an error. |
|
|
|
* |
|
|
|
* @since Ant 1.4 |
|
|
|
*/ |
|
|
|
public void setErrorProperty(String propertyName) { |
|
|
|
Enumeration enum = allTests(); |
|
|
@@ -210,9 +232,13 @@ public class JUnitTask extends Task { |
|
|
|
|
|
|
|
/** |
|
|
|
* Tells this task to halt when there is a failure in a test. |
|
|
|
* this property is applied on all BatchTest (batchtest) and JUnitTest (test) |
|
|
|
* however it can possibly be overridden by their own properties. |
|
|
|
* @param value <tt>true</tt> if it should halt, otherwise <tt>false</tt> |
|
|
|
* this property is applied on all BatchTest (batchtest) and |
|
|
|
* JUnitTest (test) however it can possibly be overridden by their |
|
|
|
* own properties. |
|
|
|
* @param value <tt>true</tt> if it should halt, otherwise |
|
|
|
* <tt>false</tt> |
|
|
|
* |
|
|
|
* @since Ant 1.2 |
|
|
|
*/ |
|
|
|
public void setHaltonfailure(boolean value) { |
|
|
|
Enumeration enum = allTests(); |
|
|
@@ -223,10 +249,16 @@ public class JUnitTask extends Task { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Tells this task to set the named property to "true" when there is a failure in a test. |
|
|
|
* This property is applied on all BatchTest (batchtest) and JUnitTest (test), |
|
|
|
* however, it can possibly be overriden by their own properties. |
|
|
|
* @param propertyName the name of the property to set in the event of an failure. |
|
|
|
* Tells this task to set the named property to "true" when there |
|
|
|
* is a failure in a test. |
|
|
|
* |
|
|
|
* <p>This property is applied on all BatchTest (batchtest) and |
|
|
|
* JUnitTest (test), however, it can possibly be overriden by |
|
|
|
* their own properties.</p> |
|
|
|
* @param propertyName the name of the property to set in the |
|
|
|
* event of an failure. |
|
|
|
* |
|
|
|
* @since Ant 1.4 |
|
|
|
*/ |
|
|
|
public void setFailureProperty(String propertyName) { |
|
|
|
Enumeration enum = allTests(); |
|
|
@@ -237,12 +269,17 @@ public class JUnitTask extends Task { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Tells whether a JVM should be forked for each testcase. It avoids interference |
|
|
|
* between testcases and possibly avoids hanging the build. |
|
|
|
* this property is applied on all BatchTest (batchtest) and JUnitTest (test) |
|
|
|
* however it can possibly be overridden by their own properties. |
|
|
|
* @param value <tt>true</tt> if a JVM should be forked, otherwise <tt>false</tt> |
|
|
|
* Tells whether a JVM should be forked for each testcase. |
|
|
|
* |
|
|
|
* <p>It avoids interference between testcases and possibly avoids |
|
|
|
* hanging the build. this property is applied on all BatchTest |
|
|
|
* (batchtest) and JUnitTest (test) however it can possibly be |
|
|
|
* overridden by their own properties.</p> |
|
|
|
* @param value <tt>true</tt> if a JVM should be forked, otherwise |
|
|
|
* <tt>false</tt> |
|
|
|
* @see #setTimeout |
|
|
|
* |
|
|
|
* @since Ant 1.2 |
|
|
|
*/ |
|
|
|
public void setFork(boolean value) { |
|
|
|
Enumeration enum = allTests(); |
|
|
@@ -253,11 +290,14 @@ public class JUnitTask extends Task { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Tells whether the task should print a short summary of the task. |
|
|
|
* Tells whether the task should print a short summary of the |
|
|
|
* task. |
|
|
|
* @param value <tt>true</tt> to print a summary, |
|
|
|
* <tt>withOutAndErr</tt> to include the test's output as |
|
|
|
* well, <tt>false</tt> otherwise. |
|
|
|
* <tt>withOutAndErr</tt> to include the test's output as |
|
|
|
* well, <tt>false</tt> otherwise. |
|
|
|
* @see SummaryJUnitResultFormatter |
|
|
|
* |
|
|
|
* @since Ant 1.2 |
|
|
|
*/ |
|
|
|
public void setPrintsummary(SummaryAttribute value) { |
|
|
|
summaryValue = value.getValue(); |
|
|
@@ -283,11 +323,15 @@ public class JUnitTask extends Task { |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Set the timeout value (in milliseconds). If the test is running for more than this |
|
|
|
* value, the test will be canceled. (works only when in 'fork' mode). |
|
|
|
* @param value the maximum time (in milliseconds) allowed before declaring the test |
|
|
|
* as 'timed-out' |
|
|
|
* Set the timeout value (in milliseconds). |
|
|
|
* |
|
|
|
* <p>If the test is running for more than this value, the test |
|
|
|
* will be canceled. (works only when in 'fork' mode).</p> |
|
|
|
* @param value the maximum time (in milliseconds) allowed before |
|
|
|
* declaring the test as 'timed-out' |
|
|
|
* @see #setFork(boolean) |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
public void setTimeout(Integer value) { |
|
|
|
timeout = value; |
|
|
@@ -297,19 +341,20 @@ public class JUnitTask extends Task { |
|
|
|
* Set the maximum memory to be used by all forked JVMs. |
|
|
|
* @param max the value as defined by <tt>-mx</tt> or <tt>-Xmx</tt> |
|
|
|
* in the java command line options. |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
public void setMaxmemory(String max) { |
|
|
|
if (Project.getJavaVersion().startsWith("1.1")) { |
|
|
|
createJvmarg().setValue("-mx"+max); |
|
|
|
} else { |
|
|
|
createJvmarg().setValue("-Xmx"+max); |
|
|
|
} |
|
|
|
commandline.setMaxmemory(max); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Set a new VM to execute the testcase. Default is <tt>java</tt>. Ignored if no JVM is forked. |
|
|
|
* Set a new VM to execute the testcase. Default is |
|
|
|
* <tt>java</tt>. Ignored if no JVM is forked. |
|
|
|
* @param value the new VM to use instead of <tt>java</tt> |
|
|
|
* @see #setFork(boolean) |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
public void setJvm(String value) { |
|
|
|
commandline.setVm(value); |
|
|
@@ -317,8 +362,11 @@ public class JUnitTask extends Task { |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a new JVM argument. Ignored if no JVM is forked. |
|
|
|
* @return create a new JVM argument so that any argument can be passed to the JVM. |
|
|
|
* @return create a new JVM argument so that any argument can be |
|
|
|
* passed to the JVM. |
|
|
|
* @see #setFork(boolean) |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
public Commandline.Argument createJvmarg() { |
|
|
|
return commandline.createVmArgument(); |
|
|
@@ -328,6 +376,8 @@ public class JUnitTask extends Task { |
|
|
|
* The directory to invoke the VM in. Ignored if no JVM is forked. |
|
|
|
* @param dir the directory to invoke the JVM from. |
|
|
|
* @see #setFork(boolean) |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
public void setDir(File dir) { |
|
|
|
this.dir = dir; |
|
|
@@ -336,6 +386,8 @@ public class JUnitTask extends Task { |
|
|
|
/** |
|
|
|
* Add a nested sysproperty element. This might be useful to tranfer |
|
|
|
* Ant properties to the testcases when JVM forking is not enabled. |
|
|
|
* |
|
|
|
* @since 1.3 |
|
|
|
*/ |
|
|
|
public void addSysproperty(Environment.Variable sysp) { |
|
|
|
commandline.addSysproperty(sysp); |
|
|
@@ -343,6 +395,8 @@ public class JUnitTask extends Task { |
|
|
|
|
|
|
|
/** |
|
|
|
* <code><classpath></code> allows classpath to be set for tests. |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
public Path createClasspath() { |
|
|
|
return commandline.createClasspath(project).createPath(); |
|
|
@@ -351,9 +405,9 @@ public class JUnitTask extends Task { |
|
|
|
/** |
|
|
|
* Add a nested env element - an environment variable. |
|
|
|
* |
|
|
|
* <p>Will be ignored if we are not forking a new VM. |
|
|
|
* <p>Will be ignored if we are not forking a new VM.</p> |
|
|
|
* |
|
|
|
* @since 1.33, Ant 1.5 |
|
|
|
* @since Ant 1.5 |
|
|
|
*/ |
|
|
|
public void addEnv(Environment.Variable var) { |
|
|
|
env.addVariable(var); |
|
|
@@ -362,9 +416,9 @@ public class JUnitTask extends Task { |
|
|
|
/** |
|
|
|
* Use a completely new environment. |
|
|
|
* |
|
|
|
* <p>Will be ignored if we are not forking a new VM. |
|
|
|
* <p>Will be ignored if we are not forking a new VM.</p> |
|
|
|
* |
|
|
|
* @since 1.33, Ant 1.5 |
|
|
|
* @since Ant 1.5 |
|
|
|
*/ |
|
|
|
public void setNewenvironment(boolean newenv) { |
|
|
|
newEnvironment = newenv; |
|
|
@@ -374,15 +428,20 @@ public class JUnitTask extends Task { |
|
|
|
* Add a new single testcase. |
|
|
|
* @param test a new single testcase |
|
|
|
* @see JUnitTest |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
public void addTest(JUnitTest test) { |
|
|
|
tests.addElement(test); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Create a new set of testcases (also called ..batchtest) and add it to the list. |
|
|
|
* Create a new set of testcases (also called ..batchtest) and add |
|
|
|
* it to the list. |
|
|
|
* @return a new instance of a batch test. |
|
|
|
* @see BatchTest |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
public BatchTest createBatchTest() { |
|
|
|
BatchTest test = new BatchTest(project); |
|
|
@@ -392,6 +451,8 @@ public class JUnitTask extends Task { |
|
|
|
|
|
|
|
/** |
|
|
|
* Add a new formatter to all tests of this task. |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
public void addFormatter(FormatterElement fe) { |
|
|
|
formatters.addElement(fe); |
|
|
@@ -400,7 +461,7 @@ public class JUnitTask extends Task { |
|
|
|
/** |
|
|
|
* Whether to include ant.jar, optional.jar and junit.jar in the forked VM. |
|
|
|
* |
|
|
|
* @since 1.37, Ant 1.5 |
|
|
|
* @since Ant 1.5 |
|
|
|
*/ |
|
|
|
public void setIncludeantruntime(boolean b) { |
|
|
|
includeAntRuntime = b; |
|
|
@@ -408,15 +469,20 @@ public class JUnitTask extends Task { |
|
|
|
|
|
|
|
/** |
|
|
|
* Creates a new JUnitRunner and enables fork of a new Java VM. |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
public JUnitTask() throws Exception { |
|
|
|
commandline.setClassname("org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner"); |
|
|
|
commandline |
|
|
|
.setClassname("org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Adds the jars or directories containing Ant, this task and |
|
|
|
* JUnit to the classpath - this should make the forked JVM work |
|
|
|
* without having to specify them directly. |
|
|
|
* |
|
|
|
* @since 1.4 |
|
|
|
*/ |
|
|
|
public void init() { |
|
|
|
antRuntimeClasses = new Path(getProject()); |
|
|
@@ -427,6 +493,8 @@ public class JUnitTask extends Task { |
|
|
|
|
|
|
|
/** |
|
|
|
* Runs the testcase. |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
public void execute() throws BuildException { |
|
|
|
Enumeration list = getIndividualTests(); |
|
|
@@ -441,7 +509,8 @@ public class JUnitTask extends Task { |
|
|
|
/** |
|
|
|
* Run the tests. |
|
|
|
*/ |
|
|
|
protected void execute(JUnitTest test) throws BuildException { |
|
|
|
protected void execute(JUnitTest arg) throws BuildException { |
|
|
|
JUnitTest test = (JUnitTest) arg.clone(); |
|
|
|
// set the default values if not specified |
|
|
|
//@todo should be moved to the test class instead. |
|
|
|
if (test.getTodir() == null) { |
|
|
@@ -466,8 +535,8 @@ public class JUnitTask extends Task { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// if there is an error/failure and that it should halt, stop everything otherwise |
|
|
|
// just log a statement |
|
|
|
// if there is an error/failure and that it should halt, stop |
|
|
|
// everything otherwise just log a statement |
|
|
|
boolean errorOccurredHere = exitValue == JUnitTestRunner.ERRORS; |
|
|
|
boolean failureOccurredHere = exitValue != JUnitTestRunner.SUCCESS; |
|
|
|
if (errorOccurredHere || failureOccurredHere) { |
|
|
@@ -480,10 +549,10 @@ public class JUnitTask extends Task { |
|
|
|
log("TEST "+test.getName()+" FAILED" |
|
|
|
+ (wasKilled ? " (timeout)" : ""), Project.MSG_ERR); |
|
|
|
if (errorOccurredHere && test.getErrorProperty() != null) { |
|
|
|
project.setProperty(test.getErrorProperty(), "true"); |
|
|
|
project.setNewProperty(test.getErrorProperty(), "true"); |
|
|
|
} |
|
|
|
if (failureOccurredHere && test.getFailureProperty() != null) { |
|
|
|
project.setProperty(test.getFailureProperty(), "true"); |
|
|
|
project.setNewProperty(test.getFailureProperty(), "true"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@@ -498,14 +567,17 @@ public class JUnitTask extends Task { |
|
|
|
* exceeds a certain amount of time. Can be <tt>null</tt>, in this case |
|
|
|
* the test could probably hang forever. |
|
|
|
*/ |
|
|
|
private int executeAsForked(JUnitTest test, ExecuteWatchdog watchdog) throws BuildException { |
|
|
|
private int executeAsForked(JUnitTest test, ExecuteWatchdog watchdog) |
|
|
|
throws BuildException { |
|
|
|
|
|
|
|
CommandlineJava cmd = (CommandlineJava) commandline.clone(); |
|
|
|
|
|
|
|
cmd.setClassname("org.apache.tools.ant.taskdefs.optional.junit.JUnitTestRunner"); |
|
|
|
cmd.createArgument().setValue(test.getName()); |
|
|
|
cmd.createArgument().setValue("filtertrace=" + test.getFiltertrace()); |
|
|
|
cmd.createArgument().setValue("haltOnError=" + test.getHaltonerror()); |
|
|
|
cmd.createArgument().setValue("haltOnFailure=" + test.getHaltonfailure()); |
|
|
|
cmd.createArgument().setValue("haltOnFailure=" |
|
|
|
+ test.getHaltonfailure()); |
|
|
|
if (includeAntRuntime) { |
|
|
|
log("Implicitly adding "+antRuntimeClasses+" to CLASSPATH", |
|
|
|
Project.MSG_VERBOSE); |
|
|
@@ -515,7 +587,8 @@ public class JUnitTask extends Task { |
|
|
|
|
|
|
|
if (summary) { |
|
|
|
log("Running " + test.getName(), Project.MSG_INFO); |
|
|
|
cmd.createArgument().setValue("formatter=org.apache.tools.ant.taskdefs.optional.junit.SummaryJUnitResultFormatter"); |
|
|
|
cmd.createArgument() |
|
|
|
.setValue("formatter=org.apache.tools.ant.taskdefs.optional.junit.SummaryJUnitResultFormatter"); |
|
|
|
} |
|
|
|
|
|
|
|
StringBuffer formatterArg = new StringBuffer(128); |
|
|
@@ -533,9 +606,13 @@ public class JUnitTask extends Task { |
|
|
|
formatterArg.setLength(0); |
|
|
|
} |
|
|
|
|
|
|
|
// Create a temporary file to pass the Ant properties to the forked test |
|
|
|
File propsFile = new File("junit" + (new Random(System.currentTimeMillis())).nextLong() + ".properties"); |
|
|
|
cmd.createArgument().setValue("propsfile=" + propsFile.getAbsolutePath()); |
|
|
|
// Create a temporary file to pass the Ant properties to the |
|
|
|
// forked test |
|
|
|
File propsFile = |
|
|
|
FileUtils.newFileUtils().createTempFile("junit", ".properties", |
|
|
|
project.getBaseDir()); |
|
|
|
cmd.createArgument().setValue("propsfile=" |
|
|
|
+ propsFile.getAbsolutePath()); |
|
|
|
Hashtable p = project.getProperties(); |
|
|
|
Properties props = new Properties(); |
|
|
|
for (Enumeration enum = p.keys(); enum.hasMoreElements(); ) { |
|
|
@@ -547,10 +624,15 @@ public class JUnitTask extends Task { |
|
|
|
props.save(outstream,"Ant JUnitTask generated properties file"); |
|
|
|
outstream.close(); |
|
|
|
} catch (java.io.IOException e) { |
|
|
|
throw new BuildException("Error creating temporary properties file.", e, location); |
|
|
|
propsFile.delete(); |
|
|
|
throw new BuildException("Error creating temporary properties " |
|
|
|
+ "file.", e, location); |
|
|
|
} |
|
|
|
|
|
|
|
Execute execute = new Execute(new LogStreamHandler(this, Project.MSG_INFO, Project.MSG_WARN), watchdog); |
|
|
|
Execute execute = new Execute(new LogStreamHandler(this, |
|
|
|
Project.MSG_INFO, |
|
|
|
Project.MSG_WARN), |
|
|
|
watchdog); |
|
|
|
execute.setCommandline(cmd.getCommandline()); |
|
|
|
execute.setAntRun(project); |
|
|
|
if (dir != null) { |
|
|
@@ -574,58 +656,73 @@ public class JUnitTask extends Task { |
|
|
|
} catch (IOException e) { |
|
|
|
throw new BuildException("Process fork failed.", e, location); |
|
|
|
} finally { |
|
|
|
if (! propsFile.delete()) { |
|
|
|
throw new BuildException("Could not delete temporary properties file."); |
|
|
|
} |
|
|
|
if (!propsFile.delete()) { |
|
|
|
throw new BuildException("Could not delete temporary " |
|
|
|
+ "properties file."); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return retVal; |
|
|
|
} |
|
|
|
|
|
|
|
// in VM is not very nice since it could probably hang the |
|
|
|
// whole build. IMHO this method should be avoided and it would be best |
|
|
|
// to remove it in future versions. TBD. (SBa) |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Pass output sent to System.out to the TestRunner so it can |
|
|
|
* collect ot for the formatters. |
|
|
|
* |
|
|
|
* @since Ant 1.5 |
|
|
|
*/ |
|
|
|
protected void handleOutput(String line) { |
|
|
|
if (runner != null) { |
|
|
|
runner.handleOutput(line); |
|
|
|
} |
|
|
|
else { |
|
|
|
} else { |
|
|
|
super.handleOutput(line); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Pass output sent to System.err to the TestRunner so it can |
|
|
|
* collect ot for the formatters. |
|
|
|
* |
|
|
|
* @since Ant 1.5 |
|
|
|
*/ |
|
|
|
protected void handleErrorOutput(String line) { |
|
|
|
if (runner != null) { |
|
|
|
runner.handleErrorOutput(line); |
|
|
|
} |
|
|
|
else { |
|
|
|
} else { |
|
|
|
super.handleErrorOutput(line); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// in VM is not very nice since it could probably hang the |
|
|
|
// whole build. IMHO this method should be avoided and it would be best |
|
|
|
// to remove it in future versions. TBD. (SBa) |
|
|
|
|
|
|
|
/** |
|
|
|
* Execute inside VM. |
|
|
|
*/ |
|
|
|
private int executeInVM(JUnitTest test) throws BuildException { |
|
|
|
private int executeInVM(JUnitTest arg) throws BuildException { |
|
|
|
JUnitTest test = (JUnitTest) arg.clone(); |
|
|
|
test.setProperties(project.getProperties()); |
|
|
|
if (dir != null) { |
|
|
|
log("dir attribute ignored if running in the same VM", Project.MSG_WARN); |
|
|
|
log("dir attribute ignored if running in the same VM", |
|
|
|
Project.MSG_WARN); |
|
|
|
} |
|
|
|
|
|
|
|
if (newEnvironment || null != env.getVariables()) { |
|
|
|
log("Changes to environment variables are ignored if running in the same VM.", |
|
|
|
Project.MSG_WARN); |
|
|
|
log("Changes to environment variables are ignored if running in " |
|
|
|
+ "the same VM.", Project.MSG_WARN); |
|
|
|
} |
|
|
|
|
|
|
|
CommandlineJava.SysProperties sysProperties = commandline.getSystemProperties(); |
|
|
|
CommandlineJava.SysProperties sysProperties = |
|
|
|
commandline.getSystemProperties(); |
|
|
|
if (sysProperties != null) { |
|
|
|
sysProperties.setSystem(); |
|
|
|
} |
|
|
|
AntClassLoader cl = null; |
|
|
|
try { |
|
|
|
log("Using System properties " + System.getProperties(), Project.MSG_VERBOSE); |
|
|
|
log("Using System properties " + System.getProperties(), |
|
|
|
Project.MSG_VERBOSE); |
|
|
|
Path classpath = (Path) commandline.getClasspath().clone(); |
|
|
|
if (includeAntRuntime) { |
|
|
|
log("Implicitly adding "+antRuntimeClasses+" to CLASSPATH", |
|
|
@@ -644,13 +741,16 @@ public class JUnitTask extends Task { |
|
|
|
cl.addSystemPackageRoot("org.apache.tools.ant"); |
|
|
|
cl.setThreadContextLoader(); |
|
|
|
} |
|
|
|
runner = new JUnitTestRunner(test, test.getHaltonerror(), test.getFiltertrace(), test.getHaltonfailure(), cl); |
|
|
|
runner = new JUnitTestRunner(test, test.getHaltonerror(), |
|
|
|
test.getFiltertrace(), |
|
|
|
test.getHaltonfailure(), cl); |
|
|
|
if (summary) { |
|
|
|
log("Running " + test.getName(), Project.MSG_INFO); |
|
|
|
|
|
|
|
SummaryJUnitResultFormatter f = |
|
|
|
new SummaryJUnitResultFormatter(); |
|
|
|
f.setWithOutAndErr( "withoutanderr".equalsIgnoreCase( summaryValue )); |
|
|
|
SummaryJUnitResultFormatter f = |
|
|
|
new SummaryJUnitResultFormatter(); |
|
|
|
f.setWithOutAndErr("withoutanderr" |
|
|
|
.equalsIgnoreCase( summaryValue )); |
|
|
|
f.setOutput( getDefaultOutput() ); |
|
|
|
runner.addFormatter(f); |
|
|
|
} |
|
|
@@ -680,6 +780,8 @@ public class JUnitTask extends Task { |
|
|
|
/** |
|
|
|
* @return <tt>null</tt> if there is a timeout value, otherwise the |
|
|
|
* watchdog instance. |
|
|
|
* |
|
|
|
* @since 1.2 |
|
|
|
*/ |
|
|
|
protected ExecuteWatchdog createWatchdog() throws BuildException { |
|
|
|
if (timeout == null){ |
|
|
@@ -690,6 +792,8 @@ public class JUnitTask extends Task { |
|
|
|
|
|
|
|
/** |
|
|
|
* Get the default output for a formatter. |
|
|
|
* |
|
|
|
* @since 1.3 |
|
|
|
*/ |
|
|
|
protected OutputStream getDefaultOutput(){ |
|
|
|
return new LogOutputStream(this, Project.MSG_INFO); |
|
|
@@ -698,6 +802,8 @@ public class JUnitTask extends Task { |
|
|
|
/** |
|
|
|
* Merge all individual tests from the batchtest with all individual tests |
|
|
|
* and return an enumeration over all <tt>JUnitTest</tt>. |
|
|
|
* |
|
|
|
* @since 1.3 |
|
|
|
*/ |
|
|
|
protected Enumeration getIndividualTests(){ |
|
|
|
final int count = batchTests.size(); |
|
|
@@ -710,11 +816,17 @@ public class JUnitTask extends Task { |
|
|
|
return Enumerations.fromCompound(enums); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @since 1.3 |
|
|
|
*/ |
|
|
|
protected Enumeration allTests() { |
|
|
|
Enumeration[] enums = { tests.elements(), batchTests.elements() }; |
|
|
|
return Enumerations.fromCompound(enums); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @since 1.3 |
|
|
|
*/ |
|
|
|
private FormatterElement[] mergeFormatters(JUnitTest test){ |
|
|
|
Vector feVector = (Vector)formatters.clone(); |
|
|
|
test.addFormattersTo(feVector); |
|
|
@@ -723,7 +835,12 @@ public class JUnitTask extends Task { |
|
|
|
return feArray; |
|
|
|
} |
|
|
|
|
|
|
|
/** return the file or null if does not use a file */ |
|
|
|
/** |
|
|
|
* If the formatter sends output to a file, return that file. |
|
|
|
* null otherwise. |
|
|
|
* |
|
|
|
* @since 1.3 |
|
|
|
*/ |
|
|
|
protected File getOutput(FormatterElement fe, JUnitTest test){ |
|
|
|
if (fe.getUseFile()) { |
|
|
|
String filename = test.getOutfile() + fe.getExtension(); |
|
|
@@ -740,6 +857,8 @@ public class JUnitTask extends Task { |
|
|
|
* |
|
|
|
* <p>Doesn't work for archives in JDK 1.1 as the URL returned by |
|
|
|
* getResource doesn't contain the name of the archive.</p> |
|
|
|
* |
|
|
|
* @since 1.4 |
|
|
|
*/ |
|
|
|
protected void addClasspathEntry(String resource) { |
|
|
|
URL url = getClass().getResource(resource); |
|
|
@@ -749,12 +868,16 @@ public class JUnitTask extends Task { |
|
|
|
int pling = u.indexOf("!"); |
|
|
|
String jarName = u.substring(9, pling); |
|
|
|
log("Found "+jarName, Project.MSG_DEBUG); |
|
|
|
antRuntimeClasses.createPath().setLocation(new File((new File(jarName)).getAbsolutePath())); |
|
|
|
antRuntimeClasses.createPath() |
|
|
|
.setLocation(new File((new File(jarName)) |
|
|
|
.getAbsolutePath())); |
|
|
|
} else if (u.startsWith("file:")) { |
|
|
|
int tail = u.indexOf(resource); |
|
|
|
String dirName = u.substring(5, tail); |
|
|
|
log("Found "+dirName, Project.MSG_DEBUG); |
|
|
|
antRuntimeClasses.createPath().setLocation(new File((new File(dirName)).getAbsolutePath())); |
|
|
|
antRuntimeClasses.createPath() |
|
|
|
.setLocation(new File((new File(dirName)) |
|
|
|
.getAbsolutePath())); |
|
|
|
} else { |
|
|
|
log("Don\'t know how to handle resource URL "+u, |
|
|
|
Project.MSG_DEBUG); |
|
|
|