@@ -23,6 +23,7 @@ import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
@@ -99,25 +100,22 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
*/
private Permissions perm = null;
private static final String JUNIT_4_TEST_ADAPTER
= "junit.framework.JUnit4TestAdapter";
private static final String[] DEFAULT_TRACE_FILTERS = new String[] {
"junit.framework.TestCase",
"junit.framework.TestResult",
"junit.framework.TestSuite",
"junit.framework.Assert.", // don't filter AssertionFailure
"junit.swingui.TestRunner",
"junit.awtui.TestRunner",
"junit.textui.TestRunner",
"java.lang.reflect.Method.invoke(",
"sun.reflect.",
"org.apache.tools.ant.",
// JUnit 4 support:
"org.junit.",
"junit.framework.JUnit4TestAdapter",
" more",
};
private static final String JUNIT_4_TEST_ADAPTER = "junit.framework.JUnit4TestAdapter";
private static final String[] DEFAULT_TRACE_FILTERS = new String[] {"junit.framework.TestCase",
"junit.framework.TestResult",
"junit.framework.TestSuite",
"junit.framework.Assert.", // don't filter AssertionFailure
"junit.swingui.TestRunner",
"junit.awtui.TestRunner",
"junit.textui.TestRunner",
"java.lang.reflect.Method.invoke(",
"sun.reflect.",
"org.apache.tools.ant.",
// JUnit 4 support:
"org.junit.",
"junit.framework.JUnit4TestAdapter",
" more"};
/**
@@ -459,12 +457,10 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
useSingleMethodAdapter = true;
}
} else {
junit4TestAdapterClass =
Class.forName(JUNIT_4_TEST_ADAPTER,
junit4TestAdapterClass = Class.forName(JUNIT_4_TEST_ADAPTER,
true, loader);
if (testMethodsSpecified) {
junit4TestAdapterClass =
Class.forName(
junit4TestAdapterClass = Class.forName(
"org.apache.tools.ant.taskdefs.optional.junit.JUnit4TestMethodAdapter",
true, loader);
useSingleMethodAdapter = true;
@@ -491,13 +487,13 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
formalParams = new Class[] {Class.class, String[].class};
actualParams = new Object[] {testClass, methods};
} else {
formalParams = new Class[] {Class.class, Class.forName("junit.framework.JUnit4TestAdapterCache")};
actualParams = new Object[] {testClass, junit4TestAdapterCacheClass.getMethod("getInstance").invoke(null)};
formalParams = new Class[] {Class.class, Class.forName(
"junit.framework.JUnit4TestAdapterCache")};
actualParams = new Object[] {testClass, junit4TestAdapterCacheClass
.getMethod("getInstance").invoke(null)};
}
suite =
(Test) junit4TestAdapterClass
.getConstructor(formalParams).
newInstance(actualParams);
suite = (Test) junit4TestAdapterClass.getConstructor(formalParams)
.newInstance(actualParams);
} else {
// Use JUnit 3.
@@ -532,8 +528,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
if (exception != null) { // had an exception constructing suite
final int formatterSize = formatters.size();
for (int i = 0; i < formatterSize; i++) {
((TestListener) formatters.elementAt(i))
.addError(null, exception);
((TestListener) formatters.elementAt(i)).addError(null, exception);
}
junitTest.setCounts(1, 0, 1, 0);
junitTest.setRunTime(0);
@@ -542,10 +537,10 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
logTestListenerEvent("tests to run: " + suite.countTestCases());
suite.run(res);
} finally {
if (junit4 ||
suite.getClass().getName().equals(JUNIT_4_TEST_ADAPTER)) {
if (junit4 || suite.getClass().getName().equals(JUNIT_4_TEST_ADAPTER)) {
final int[] cnts = findJUnit4FailureErrorCount(res);
junitTest.setCounts(res.runCount() + res.ignoredCount(), cnts[0], cnts[1], res.ignoredCount() + res.skippedCount());
junitTest.setCounts(res.runCount() + res.ignoredCount(), cnts[0], cnts[1],
res.ignoredCount() + res.skippedCount());
} else {
junitTest.setCounts(res.runCount() + res.ignoredCount(), res.failureCount(),
res.errorCount(), res.ignoredCount() + res.skippedCount());
@@ -610,12 +605,12 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
try {
suiteAnnotation = Class.forName("org.junit.Suite.SuiteClasses");
} catch(final ClassNotFoundException ex) {
} catch (final ClassNotFoundException ex) {
// ignore - we don't have this annotation so make sure we don't check for it
}
try {
runWithAnnotation = Class.forName("org.junit.runner.RunWith");
} catch(final ClassNotFoundException ex) {
} catch (final ClassNotFoundException ex) {
// also ignore as this annotation doesn't exist so tests can't use it
}
@@ -643,12 +638,13 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
return true;
}
if (runWithAnnotation != null && testClass.getAnnotation(runWithAnnotation) != null) {
/* Class is marked with @RunWith. If this class is badly written (no test methods, multiple
* constructors, private constructor etc) then the class is automatically run and fails in the
* IDEs I've tried... so I'm happy handing the class to JUnit to try and run, and let JUnit
* report a failure if a bad test case is provided. Trying to do anything else is likely to
* result in us filtering out cases that could be valid for future versions of JUnit so would
* just increase future maintenance work.
/* Class is marked with @RunWith. If this class is badly written (no test methods,
* multiple constructors, private constructor etc) then the class is automatically
* run and fails in the IDEs I've tried... so I'm happy handing the class to JUnit
* to try and run, and let JUnit report a failure if a bad test case is provided.
* Trying to do anything else is likely to result in us filtering out cases that
* could be valid for future versions of JUnit so would just increase future
* maintenance work.
*/
return true;
}
@@ -661,7 +657,8 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
return true;
}
} else {
// check if JUnit3 class have public or protected no-args methods starting with names starting with test
// check if JUnit3 class have public or protected no-args methods starting with
// names starting with test
if (m.getName().startsWith("test") && m.getParameterTypes().length == 0
&& (Modifier.isProtected(m.getModifiers()) || Modifier.isPublic(m.getModifiers()))) {
return true;
@@ -713,6 +710,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
private void logTestListenerEvent(String msg) {
if (logTestListenerEvents) {
@SuppressWarnings("resource")
final PrintStream out = savedOut != null ? savedOut : System.out;
out.flush();
if (msg == null) {
@@ -720,8 +718,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
}
final StringTokenizer msgLines = new StringTokenizer(msg, "\r\n", false);
while (msgLines.hasMoreTokens()) {
out.println(JUnitTask.TESTLISTENER_PREFIX
+ msgLines.nextToken());
out.println(JUnitTask.TESTLISTENER_PREFIX + msgLines.nextToken());
}
out.flush();
}
@@ -840,16 +837,14 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
private void fireStartTestSuite() {
final int size = formatters.size();
for (int i = 0; i < size; i++) {
((JUnitResultFormatter) formatters.elementAt(i))
.startTestSuite(junitTest);
((JUnitResultFormatter) formatters.elementAt(i)).startTestSuite(junitTest);
}
}
private void fireEndTestSuite() {
final int size = formatters.size();
for (int i = 0; i < size; i++) {
((JUnitResultFormatter) formatters.elementAt(i))
.endTestSuite(junitTest);
((JUnitResultFormatter) formatters.elementAt(i)).endTestSuite(junitTest);
}
}
@@ -915,7 +910,8 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
boolean logFailedTests = true;
boolean logTestListenerEvents = false;
boolean skipNonTests = false;
int antThreadID = 0; /* Ant id of thread running this unit test, 0 in single-threaded mode */
/* Ant id of thread running this unit test, 0 in single-threaded mode */
int antThreadID = 0;
if (args.length == 0) {
System.err.println("required argument TestClassName missing");
@@ -954,7 +950,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
}
} else if (args[i].startsWith(Constants.PROPSFILE)) {
final FileInputStream in = new FileInputStream(args[i]
.substring(Constants.PROPSFILE.length()));
.substring(Constants.PROPSFILE.length()));
props.load(in);
in.close();
} else if (args[i].startsWith(Constants.SHOWOUTPUT)) {
@@ -986,8 +982,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
int returnCode = SUCCESS;
if (multipleTests) {
try {
final java.io.BufferedReader reader =
new java.io.BufferedReader(new java.io.FileReader(args[0]));
final BufferedReader reader = new BufferedReader(new FileReader(args[0]));
String testCaseName;
String[] testMethodNames;
int code = 0;
@@ -1020,8 +1015,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
errorOccurred = (code == ERRORS);
failureOccurred = (code != SUCCESS);
if (errorOccurred || failureOccurred) {
if ((errorOccurred && haltError)
|| (failureOccurred && haltFail)) {
if ((errorOccurred && haltError) || (failureOccurred && haltFail)) {
registerNonCrash();
System.exit(code);
} else {
@@ -1029,8 +1023,7 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
returnCode = code;
}
if (logFailedTests) {
System.out.println("TEST " + t.getName()
+ " FAILED");
System.out.println("TEST " + t.getName() + " FAILED");
}
}
}
@@ -1043,9 +1036,8 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
t.setThread(antThreadID);
t.setProperties(props);
t.setSkipNonTests(skipNonTests);
returnCode = launch(
t, methods, haltError, stackfilter, haltFail,
showOut, outputToFormat, logTestListenerEvents);
returnCode = launch(t, methods, haltError, stackfilter, haltFail, showOut,
outputToFormat, logTestListenerEvents);
}
registerNonCrash();
@@ -1090,9 +1082,8 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
for (int i = 0; i < size; i++) {
final FormatterElement fe = (FormatterElement) fromCmdLine.elementAt(i);
if (multipleTests && fe.getUseFile()) {
final File destFile =
new File(test.getTodir(),
test.getOutfile() + fe.getExtension());
final File destFile = new File(test.getTodir(),
test.getOutfile() + fe.getExtension());
fe.setOutfile(destFile);
}
runner.addFormatter((JUnitResultFormatter) fe.createFormatter());
@@ -1119,9 +1110,8 @@ public class JUnitTestRunner implements TestListener, JUnitTaskMirror.JUnitTestR
} else {
final int fName = line.indexOf(IGNORED_FILE_NAME);
if (fName > -1) {
fe.setExtension(line
.substring(fName
+ IGNORED_FILE_NAME.length()));
fe.setExtension(line.substring(fName
+ IGNORED_FILE_NAME.length()));
}
}
}