@@ -277,7 +277,7 @@ public class JUnitTask extends Task {
}
}
/**
/**
* Set the bah vior when {@link #setFork fork} fork has been enabled.
* Set the beh avior when {@link #setFork fork} fork has been enabled.
*
*
* <p>Possible values are "once", "perTest" and "perBatch". If
* <p>Possible values are "once", "perTest" and "perBatch". If
* set to "once", only a single Java VM will be forked for all
* set to "once", only a single Java VM will be forked for all
@@ -608,6 +608,12 @@ public class JUnitTask extends Task {
* @since Ant 1.6
* @since Ant 1.6
*/
*/
public void setTempdir(File tmpDir) {
public void setTempdir(File tmpDir) {
if(tmpDir!=null) {
if(!tmpDir.exists() || !tmpDir.isDirectory()) {
throw new BuildException(tmpDir.toString()
+" is not a valid temp directory");
}
}
this.tmpDir = tmpDir;
this.tmpDir = tmpDir;
}
}
@@ -699,11 +705,7 @@ public class JUnitTask extends Task {
JUnitTest test = null;
JUnitTest test = null;
// Create a temporary file to pass the test cases to run to
// Create a temporary file to pass the test cases to run to
// the runner (one test case per line)
// the runner (one test case per line)
File casesFile =
FileUtils.newFileUtils().createTempFile("junittestcases",
".properties",
getProject().getBaseDir());
casesFile.deleteOnExit();
File casesFile = createTempPropertiesFile("junittestcases");
PrintWriter writer = null;
PrintWriter writer = null;
try {
try {
writer =
writer =
@@ -834,12 +836,8 @@ public class JUnitTask extends Task {
}
}
}
}
// Create a temporary file to pass the Ant properties to the
// forked test
File propsFile =
FileUtils.newFileUtils().createTempFile("junit", ".properties",
tmpDir != null ? tmpDir : getProject().getBaseDir());
propsFile.deleteOnExit();
File propsFile = createTempPropertiesFile("junit");
cmd.createArgument().setValue("propsfile="
cmd.createArgument().setValue("propsfile="
+ propsFile.getAbsolutePath());
+ propsFile.getAbsolutePath());
Hashtable p = getProject().getProperties();
Hashtable p = getProject().getProperties();
@@ -898,6 +896,22 @@ public class JUnitTask extends Task {
return retVal;
return retVal;
}
}
/**
* Create a temporary file to pass the properties to a new process.
* Will auto-delete on (graceful) exit.
* The file will be in the project basedir unless tmpDir declares
* something else.
* @param prefix
* @return
*/
private File createTempPropertiesFile(String prefix) {
File propsFile =
FileUtils.newFileUtils().createTempFile(prefix, ".properties",
tmpDir != null ? tmpDir : getProject().getBaseDir());
propsFile.deleteOnExit();
return propsFile;
}
/**
/**
* Pass output sent to System.out to the TestRunner so it can
* Pass output sent to System.out to the TestRunner so it can
@@ -1080,7 +1094,7 @@ public class JUnitTask extends Task {
if (timeout == null) {
if (timeout == null) {
return null;
return null;
}
}
return new ExecuteWatchdog(timeout.intValue());
return new ExecuteWatchdog((long) timeout.intValue());
}
}
/**
/**
@@ -1229,6 +1243,10 @@ public class JUnitTask extends Task {
OutputStream out) {
OutputStream out) {
formatter.setOutput(out);
formatter.setOutput(out);
formatter.startTestSuite(test);
formatter.startTestSuite(test);
//the trick to integrating test output to the formatter, is to
//create a special test class that asserts a timout occurred,
//and tell the formatter that it raised.
Test t = new Test() {
Test t = new Test() {
public int countTestCases() { return 1; }
public int countTestCases() { return 1; }
public void run(TestResult r) {
public void run(TestResult r) {
@@ -1281,6 +1299,7 @@ public class JUnitTask extends Task {
}
}
/**
/**
* Forked test support
* @since Ant 1.6.2
* @since Ant 1.6.2
*/
*/
private final class ForkedTestConfiguration {
private final class ForkedTestConfiguration {
@@ -1289,6 +1308,15 @@ public class JUnitTask extends Task {
private boolean haltOnFailure;
private boolean haltOnFailure;
private String errorProperty;
private String errorProperty;
private String failureProperty;
private String failureProperty;
/**
* constructor for forked test configuration
* @param filterTrace
* @param haltOnError
* @param haltOnFailure
* @param errorProperty
* @param failureProperty
*/
ForkedTestConfiguration(boolean filterTrace, boolean haltOnError,
ForkedTestConfiguration(boolean filterTrace, boolean haltOnError,
boolean haltOnFailure, String errorProperty,
boolean haltOnFailure, String errorProperty,
String failureProperty) {
String failureProperty) {
@@ -1299,6 +1327,23 @@ public class JUnitTask extends Task {
this.failureProperty = failureProperty;
this.failureProperty = failureProperty;
}
}
/**
* configure from a test; sets member variables to attributes of the test
* @param test
*/
public ForkedTestConfiguration(JUnitTest test) {
this(test.getFiltertrace(),
test.getHaltonerror(),
test.getHaltonfailure(),
test.getErrorProperty(),
test.getFailureProperty());
}
/**
* equality test checks all the member variables
* @param other
* @return true if everything is equal
*/
public boolean equals(Object other) {
public boolean equals(Object other) {
if (other == null
if (other == null
|| other.getClass() != ForkedTestConfiguration.class) {
|| other.getClass() != ForkedTestConfiguration.class) {
@@ -1318,6 +1363,11 @@ public class JUnitTask extends Task {
&& failureProperty.equals(o.failureProperty)));
&& failureProperty.equals(o.failureProperty)));
}
}
/**
* hashcode is based only on the boolean members, and returns a value
* in the range 0-7.
* @return
*/
public int hashCode() {
public int hashCode() {
return (filterTrace ? 1 : 0)
return (filterTrace ? 1 : 0)
+ (haltOnError ? 2 : 0)
+ (haltOnError ? 2 : 0)
@@ -1326,11 +1376,22 @@ public class JUnitTask extends Task {
}
}
/**
/**
* These are the different forking options
* @since 1.6.2
* @since 1.6.2
*/
*/
public static final class ForkStyle extends EnumeratedAttribute {
public static final class ForkStyle extends EnumeratedAttribute {
/**
* fork once only
*/
public static final String ONCE = "once";
public static final String ONCE = "once";
/**
* fork once per test class
*/
public static final String PER_TEST = "perTest";
public static final String PER_TEST = "perTest";
/**
* fork once per batch of tests
*/
public static final String PER_BATCH = "perBatch";
public static final String PER_BATCH = "perBatch";
public ForkStyle() {
public ForkStyle() {
@@ -1365,11 +1426,7 @@ public class JUnitTask extends Task {
execute(test);
execute(test);
} else {
} else {
ForkedTestConfiguration c =
ForkedTestConfiguration c =
new ForkedTestConfiguration(test.getFiltertrace(),
test.getHaltonerror(),
test.getHaltonfailure(),
test.getErrorProperty(),
test.getFailureProperty());
new ForkedTestConfiguration(test);
List l = (List) testConfigurations.get(c);
List l = (List) testConfigurations.get(c);
if (l == null) {
if (l == null) {
l = new ArrayList();
l = new ArrayList();