Browse Source

* Refactoring of FailureRecorder: use of a helper class instead of helper methods

* FailureRecorder creates better names in the generated testcase
* just put in into Ant 1.8.0
* sorting of the failed tests
* javadoc
* update the manual


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@574554 13f79535-47bb-0310-9956-ffa450edef68
master
Jan Materne 18 years ago
parent
commit
f02f90064b
2 changed files with 77 additions and 48 deletions
  1. +4
    -3
      docs/manual/OptionalTasks/junit.html
  2. +73
    -45
      src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java

+ 4
- 3
docs/manual/OptionalTasks/junit.html View File

@@ -342,7 +342,7 @@ The name of the file is determined by the
name of the test and can be set by the <code>outfile</code> attribute
of <code>&lt;test&gt;</code>.</p>

<p>There are three predefined formatters - one prints the test results
<p>There are four predefined formatters - one prints the test results
in XML format, the other emits plain text. The formatter named
<code>brief</code> will only print detailed information for testcases
that failed, while <code>plain</code> gives a little statistics line
@@ -354,7 +354,8 @@ can be specified.</p>
that your tests have written as some characters are illegal in XML
documents and will be dropped.</p>

<p>The formatter named <code>failure</code> collects all failing <code>testXXX()</code>
<p>The fourth formatter named <code>failure</code> (since Ant 1.8.0)
collects all failing <code>testXXX()</code>
methods and creates a new <code>TestCase</code> which delegates only these
failing methods. The name and the location can be specified via Java System property
<code>ant.junit.failureCollector</code>. The value has to point to the directory and
@@ -375,7 +376,7 @@ time.</p>
<tr>
<td valign="top">type</td>
<td valign="top">Use a predefined formatter (either
<code>xml</code>, <code>plain</code>, or <code>brief</code>).</td>
<code>xml</code>, <code>plain</code>, <code>brief</code> or <code>failure</code>).</td>
<td align="center" rowspan="2">Exactly one of these.</td>
</tr>
<tr>


+ 73
- 45
src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java View File

@@ -24,8 +24,9 @@ import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
@@ -42,8 +43,8 @@ import org.apache.tools.ant.util.FileUtils;
* // generated on: 2007.08.06 09:42:34,555
* import junit.framework.*;
* public class FailedTests extends TestCase {
* public FailedTests(String s) {
* super(s);
* public FailedTests(String testname) {
* super(testname);
* }
* public static Test suite() {
* TestSuite suite = new TestSuite();
@@ -59,7 +60,7 @@ import org.apache.tools.ant.util.FileUtils;
* the failing test cases in a static list. Because we dont have a finalizer
* method in the formatters "lifecycle", we regenerate the new java source
* at each end of a test suite. The last run will contain all failed tests.
* @since Ant 1.7.1
* @since Ant 1.8.0
*/
public class FailureRecorder implements JUnitResultFormatter {
@@ -78,7 +79,7 @@ public class FailureRecorder implements JUnitResultFormatter {
= System.getProperty("java.io.tmpdir") + "FailedTests";
/** Class names of failed tests without duplicates. */
private static HashSet/*<Test>*/ failedTests = new HashSet();
private static SortedSet/*<TestInfos>*/ failedTests = new TreeSet();
/** A writer for writing the generated source to. */
private PrintWriter writer;
@@ -99,7 +100,7 @@ public class FailureRecorder implements JUnitResultFormatter {
return locationName;
}
// CheckStyle:LineLengthCheck OFF - see is long
// CheckStyle:LineLengthCheck OFF - @see is long
/**
* After each test suite, the whole new JUnit class will be regenerated.
* @param suite the test suite
@@ -117,14 +118,7 @@ public class FailureRecorder implements JUnitResultFormatter {
writer = new PrintWriter(new FileOutputStream(sourceFile));
createClassHeader();
createTestSuiteHeader();
for (Iterator iter = failedTests.iterator(); iter.hasNext();) {
Test test = (Test) iter.next();
if (test != null) {
createAddTestToSuite(test);
}
}
createTestSuiteFooter();
createSuiteMethod();
createClassFooter();
FileUtils.close(writer);
@@ -134,19 +128,21 @@ public class FailureRecorder implements JUnitResultFormatter {
}
/**
* Not used
* {@inheritDoc}
* Add the failed test to the list.
* @see junit.framework.TestListener#addError(junit.framework.Test, java.lang.Throwable)
*/
public void addError(Test test, Throwable throwable) {
failedTests.add(test);
failedTests.add(new TestInfos(test));
}
// CheckStyle:LineLengthCheck OFF - @see is long
/**
* Not used
* {@inheritDoc}
* Add the failed test to the list.
* @see junit.framework.TestListener#addFailure(junit.framework.Test, junit.framework.AssertionFailedError)
*/
// CheckStyle:LineLengthCheck ON
public void addFailure(Test test, AssertionFailedError error) {
failedTests.add(test);
failedTests.add(new TestInfos(test));
}
/**
@@ -215,42 +211,74 @@ public class FailureRecorder implements JUnitResultFormatter {
// no-arg constructor
writer.print(" public ");
writer.print(className);
writer.println("(String s) {");
writer.println(" super(s);");
writer.println("(String testname) {");
writer.println(" super(testname);");
writer.println(" }");
}
private void createTestSuiteHeader() {
private void createSuiteMethod() {
writer.println(" public static Test suite() {");
writer.println(" TestSuite suite = new TestSuite();");
}
private void createAddTestToSuite(Test test) {
writer.print(" suite.addTest( new ");
writer.print(getClassName(test));
writer.print("(\"");
writer.print(getMethodName(test));
writer.println("\") );");
}
private void createTestSuiteFooter() {
for (Iterator iter = failedTests.iterator(); iter.hasNext();) {
TestInfos testInfos = (TestInfos) iter.next();
writer.print(" suite.addTest(");
writer.print(testInfos);
writer.println(");");
}
writer.println(" return suite;");
writer.println(" }");
}
private void createClassFooter() {
writer.println("}");
}
// Helper methods
private String getMethodName(Test test) {
String methodName = test.toString();
return methodName.substring(0, methodName.indexOf('('));
}
private String getClassName(Test test) {
return test.getClass().getName();
// Helper classes
/**
* TestInfos holds information about a given test for later use.
*/
public class TestInfos implements Comparable {
/** The class name of the test. */
String className;
/** The method name of the testcase. */
String methodName;
/**
* This constructor extracts the needed information from the given test.
* @param test Test to analyze
*/
public TestInfos(Test test) {
className = test.getClass().getName();
methodName = test.toString();
methodName = methodName.substring(0, methodName.indexOf('('));
}
/**
* This String-Representation can directly be used for instantiation of
* the JUnit testcase.
* @see java.lang.Object#toString()
* @see FailureRecorder#createSuiteMethod()
*/
public String toString() {
return "new " + className + "(\"" + methodName + "\")";
}
/*
* The SortedMap needs comparable elements.
* @see java.lang.Comparable#compareTo(T)
* @see SortedSet#comparator()
*/
public int compareTo(Object other) {
if (other instanceof TestInfos) {
TestInfos otherInfos = (TestInfos) other;
return toString().compareTo(otherInfos.toString());
} else {
return -1;
}
}
}
}

Loading…
Cancel
Save