Browse Source

Fixed length mismatch.

Made use of shared constants in junit package.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@420337 13f79535-47bb-0310-9956-ffa450edef68
master
Jacobus Martinus Kruithof 19 years ago
parent
commit
d2e6dcf66d
3 changed files with 83 additions and 48 deletions
  1. +36
    -0
      src/main/org/apache/tools/ant/taskdefs/optional/junit/Constants.java
  2. +15
    -14
      src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java
  3. +32
    -34
      src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java

+ 36
- 0
src/main/org/apache/tools/ant/taskdefs/optional/junit/Constants.java View File

@@ -0,0 +1,36 @@
/*
* Copyright 2000-2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.ant.taskdefs.optional.junit;
/**
* Constants, like filenames shared between various classes in this package.
*/
public class Constants {
static final String HALT_ON_ERROR = "haltOnError=";
static final String HALT_ON_FAILURE = "haltOnFailure=";
static final String FILTERTRACE = "filtertrace=";
static final String CRASHFILE = "crashfile=";
static final String BEFORE_FIRST_TEST = "BeforeFirstTest";
static final String PROPSFILE = "propsfile=";
static final String SHOWOUTPUT = "showoutput=";
static final String FORMATTER = "formatter=";
static final String LOGTESTLISTENEREVENTS = "logtestlistenerevents=";
static final String TESTSFILE = "testsfile=";
static final String TERMINATED_SUCCESSFULLY = "terminated successfully";
}

+ 15
- 14
src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTask.java View File

@@ -125,6 +125,7 @@ import org.apache.tools.ant.util.LoaderUtils;
*/
public class JUnitTask extends Task {

private static final String CLASSPATH = "CLASSPATH=";
private CommandlineJava commandline;
private Vector tests = new Vector();
private Vector batchTests = new Vector();
@@ -883,22 +884,22 @@ public class JUnitTask extends Task {
cmd.createArgument().setValue(test.getName());
} else {
log("Running multiple tests in the same VM", Project.MSG_VERBOSE);
cmd.createArgument().setValue("testsfile=" + casesFile);
cmd.createArgument().setValue(Constants.TESTSFILE + casesFile);
}

cmd.createArgument().setValue("filtertrace=" + test.getFiltertrace());
cmd.createArgument().setValue("haltOnError=" + test.getHaltonerror());
cmd.createArgument().setValue("haltOnFailure="
cmd.createArgument().setValue(Constants.FILTERTRACE + test.getFiltertrace());
cmd.createArgument().setValue(Constants.HALT_ON_ERROR + test.getHaltonerror());
cmd.createArgument().setValue(Constants.HALT_ON_FAILURE
+ test.getHaltonfailure());
if (includeAntRuntime) {
Vector v = Execute.getProcEnvironment();
Enumeration e = v.elements();
while (e.hasMoreElements()) {
String s = (String) e.nextElement();
if (s.startsWith("CLASSPATH=")) {
if (s.startsWith(CLASSPATH)) {
cmd.createClasspath(getProject()).createPath()
.append(new Path(getProject(),
s.substring(10 // "CLASSPATH=".length()
s.substring(CLASSPATH.length()
)));
}
}
@@ -914,21 +915,21 @@ public class JUnitTask extends Task {
prefix = "OutErr";
}
cmd.createArgument()
.setValue("formatter"
+ "=org.apache.tools.ant.taskdefs.optional.junit."
.setValue(Constants.FORMATTER
+ "org.apache.tools.ant.taskdefs.optional.junit."
+ prefix + "SummaryJUnitResultFormatter");
}

cmd.createArgument().setValue("showoutput="
cmd.createArgument().setValue(Constants.SHOWOUTPUT
+ String.valueOf(showOutput));
cmd.createArgument().setValue("logtestlistenerevents=true"); // #31885
cmd.createArgument().setValue(Constants.LOGTESTLISTENEREVENTS+"true"); // #31885

StringBuffer formatterArg = new StringBuffer(STRING_BUFFER_SIZE);
final FormatterElement[] feArray = mergeFormatters(test);
for (int i = 0; i < feArray.length; i++) {
FormatterElement fe = feArray[i];
if (fe.shouldUse(this)) {
formatterArg.append("formatter=");
formatterArg.append(Constants.FORMATTER);
formatterArg.append(fe.getClassname());
File outFile = getOutput(fe, test);
if (outFile != null) {
@@ -941,10 +942,10 @@ public class JUnitTask extends Task {
}

File vmWatcher = createTempPropertiesFile("junitvmwatcher");
cmd.createArgument().setValue("crashfile="
cmd.createArgument().setValue(Constants.CRASHFILE
+ vmWatcher.getAbsolutePath());
File propsFile = createTempPropertiesFile("junit");
cmd.createArgument().setValue("propsfile="
cmd.createArgument().setValue(Constants.PROPSFILE
+ propsFile.getAbsolutePath());
Hashtable p = getProject().getProperties();
Properties props = new Properties();
@@ -1000,7 +1001,7 @@ public class JUnitTask extends Task {
if (watchdog != null && watchdog.killedProcess()) {
result.timedOut = true;
logTimeout(feArray, test, vmCrashString);
} else if (!"terminated successfully".equals(vmCrashString)) {
} else if (!Constants.TERMINATED_SUCCESSFULLY.equals(vmCrashString)) {
result.crashed = true;
logVmCrash(feArray, test, vmCrashString);
}


+ 32
- 34
src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java View File

@@ -21,7 +21,6 @@ import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
@@ -146,7 +145,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR

/** ClassLoader passed in in non-forked mode. */
private ClassLoader loader;
/** Do we print TestListener events? */
private boolean logTestListenerEvents = false;

@@ -158,7 +157,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
* File will be empty in case the build did not crash.
*/
private static String crashFile = null;
/**
* Constructor for fork=true or when the user hasn't specified a
* classpath.
@@ -227,7 +226,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
}

private PrintStream savedOut = null;
public void run() {
res = new TestResult();
res.addListener(wrapListener(this));
@@ -426,7 +425,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
String testName = JUnitVersionHelper.getTestCaseName(test);
logTestListenerEvent("endTest(" + testName + ")");
}
private void logTestListenerEvent(String msg) {
PrintStream out = forked ? savedOut : System.out;
if (logTestListenerEvents) {
@@ -481,9 +480,9 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
}

public void handleOutput(String output) {
if (!logTestListenerEvents && output.startsWith(JUnitTask.TESTLISTENER_PREFIX))
; // ignore
else if (systemOut != null) {
if (!logTestListenerEvents && output.startsWith(JUnitTask.TESTLISTENER_PREFIX)) {
// ignore
} else if (systemOut != null) {
systemOut.print(output);
}
}
@@ -589,37 +588,37 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
System.exit(ERRORS);
}

if (args[0].startsWith("testsfile=")) {
if (args[0].startsWith(Constants.TESTSFILE)) {
multipleTests = true;
args[0] = args[0].substring(10 /* "testsfile=".length() */);
args[0] = args[0].substring(Constants.TESTSFILE.length());
}

for (int i = 1; i < args.length; i++) {
if (args[i].startsWith("haltOnError=")) {
haltError = Project.toBoolean(args[i].substring(12));
} else if (args[i].startsWith("haltOnFailure=")) {
haltFail = Project.toBoolean(args[i].substring(14));
} else if (args[i].startsWith("filtertrace=")) {
stackfilter = Project.toBoolean(args[i].substring(12));
} else if (args[i].startsWith("crashfile=")) {
crashFile = args[i].substring(12);
registerTestCase("BeforeFirstTest");
} else if (args[i].startsWith("formatter=")) {
if (args[i].startsWith(Constants.HALT_ON_ERROR)) {
haltError = Project.toBoolean(args[i].substring(Constants.HALT_ON_ERROR.length()));
} else if (args[i].startsWith(Constants.HALT_ON_FAILURE)) {
haltFail = Project.toBoolean(args[i].substring(Constants.HALT_ON_FAILURE.length()));
} else if (args[i].startsWith(Constants.FILTERTRACE)) {
stackfilter = Project.toBoolean(args[i].substring(Constants.FILTERTRACE.length()));
} else if (args[i].startsWith(Constants.CRASHFILE)) {
crashFile = args[i].substring(Constants.CRASHFILE.length());
registerTestCase(Constants.BEFORE_FIRST_TEST);
} else if (args[i].startsWith(Constants.FORMATTER)) {
try {
createAndStoreFormatter(args[i].substring(10));
createAndStoreFormatter(args[i].substring(Constants.FORMATTER.length()));
} catch (BuildException be) {
System.err.println(be.getMessage());
System.exit(ERRORS);
}
} else if (args[i].startsWith("propsfile=")) {
} else if (args[i].startsWith(Constants.PROPSFILE)) {
FileInputStream in = new FileInputStream(args[i]
.substring(10));
.substring(Constants.PROPSFILE.length()));
props.load(in);
in.close();
} else if (args[i].startsWith("showoutput=")) {
showOut = Project.toBoolean(args[i].substring(11));
} else if (args[i].startsWith("logtestlistenerevents=")) {
logTestListenerEvents = Project.toBoolean(args[i].substring(22));
} else if (args[i].startsWith(Constants.SHOWOUTPUT)) {
showOut = Project.toBoolean(args[i].substring(Constants.SHOWOUTPUT.length()));
} else if (args[i].startsWith(Constants.LOGTESTLISTENEREVENTS)) {
logTestListenerEvents = Project.toBoolean(args[i].substring(Constants.LOGTESTLISTENEREVENTS.length()));
}
}

@@ -669,7 +668,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
}
} else {
returnCode = launch(new JUnitTest(args[0]), haltError,
stackfilter, haltFail, showOut,
stackfilter, haltFail, showOut,
logTestListenerEvents, props);
}

@@ -708,7 +707,6 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
}

public void startTest(Test arg0) {
System.out.println(this.getClass().getName() + ":" + arg0);
registerTestCase(JUnitVersionHelper.getTestCaseName(arg0));
}
});
@@ -779,7 +777,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
pw.println(line);
}
}
} catch (Exception IOException) {
} catch (Exception e) {
return stack; // return the stack unfiltered
}
return sw.toString();
@@ -821,7 +819,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
FileWriter out = null;
try {
out = new FileWriter(crashFile);
out.write("terminated successfully\n");
out.write(Constants.TERMINATED_SUCCESSFULLY + "\n");
out.flush();
} finally {
if (out != null) {
@@ -853,7 +851,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
/**
* Modifies a TestListener when running JUnit 4: treats AssertionFailedError
* as a failure not an error.
*
*
* @since Ant 1.7
*/
private TestListener wrapListener(final TestListener testListener) {
@@ -870,8 +868,8 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
// We would prefer to show "failure" for things that logically are.
try {
String msg = t.getMessage();
AssertionFailedError failure = msg != null ?
new AssertionFailedError(msg) : new AssertionFailedError();
AssertionFailedError failure = msg != null
? new AssertionFailedError(msg) : new AssertionFailedError();
// To compile on pre-JDK 4 (even though this should always succeed):
Method initCause = Throwable.class.getMethod("initCause", new Class[] {Throwable.class});
initCause.invoke(failure, new Object[] {t});


Loading…
Cancel
Save