You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by jh...@apache.org on 2007/09/11 13:36:54 UTC

svn commit: r574554 - in /ant/core/trunk: docs/manual/OptionalTasks/junit.html src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java

Author: jhm
Date: Tue Sep 11 04:36:53 2007
New Revision: 574554

URL: http://svn.apache.org/viewvc?rev=574554&view=rev
Log:
* 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

Modified:
    ant/core/trunk/docs/manual/OptionalTasks/junit.html
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java

Modified: ant/core/trunk/docs/manual/OptionalTasks/junit.html
URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/OptionalTasks/junit.html?rev=574554&r1=574553&r2=574554&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/OptionalTasks/junit.html (original)
+++ ant/core/trunk/docs/manual/OptionalTasks/junit.html Tue Sep 11 04:36:53 2007
@@ -342,7 +342,7 @@
 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 @@
 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 @@
   <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>

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java?rev=574554&r1=574553&r2=574554&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/optional/junit/FailureRecorder.java Tue Sep 11 04:36:53 2007
@@ -24,8 +24,9 @@
 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 @@
  * // 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 @@
  * 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 @@
         = 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 @@
         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 @@
             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 @@
     }
 
     /**
-     * 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 @@
         // 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;
+            }
+        }
     }
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org