maven-surefire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From br...@apache.org
Subject svn commit: r398538 - in /maven/surefire/trunk/surefire-api/src: main/java/org/apache/maven/surefire/report/ test/java/org/apache/maven/surefire/report/
Date Mon, 01 May 2006 06:43:57 GMT
Author: brett
Date: Sun Apr 30 23:43:56 2006
New Revision: 398538

URL: http://svn.apache.org/viewcvs?rev=398538&view=rev
Log:
[MSUREFIRE-96] make XML reporter thread safe

Modified:
    maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractFileReporter.java
    maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractReporter.java
    maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractTextReporter.java
    maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/Reporter.java
    maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/XMLReporter.java
    maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/report/XMLReporterTest.java

Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractFileReporter.java
URL: http://svn.apache.org/viewcvs/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractFileReporter.java?rev=398538&r1=398537&r2=398538&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractFileReporter.java
(original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractFileReporter.java
Sun Apr 30 23:43:56 2006
@@ -68,6 +68,7 @@
     }
 
     public void testSetCompleted( ReportEntry report )
+        throws ReporterException
     {
         super.testSetCompleted( report );
 

Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractReporter.java
URL: http://svn.apache.org/viewcvs/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractReporter.java?rev=398538&r1=398537&r2=398538&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractReporter.java
(original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractReporter.java
Sun Apr 30 23:43:56 2006
@@ -80,6 +80,7 @@
     }
 
     public void testSetCompleted( ReportEntry report )
+        throws ReporterException
     {
     }
 

Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractTextReporter.java
URL: http://svn.apache.org/viewcvs/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractTextReporter.java?rev=398538&r1=398537&r2=398538&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractTextReporter.java
(original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/AbstractTextReporter.java
Sun Apr 30 23:43:56 2006
@@ -111,6 +111,7 @@
     }
 
     public void testSetCompleted( ReportEntry report )
+        throws ReporterException
     {
         super.testSetCompleted( report );
 

Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/Reporter.java
URL: http://svn.apache.org/viewcvs/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/Reporter.java?rev=398538&r1=398537&r2=398538&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/Reporter.java
(original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/Reporter.java
Sun Apr 30 23:43:56 2006
@@ -18,7 +18,7 @@
 
 /**
  * Contract between the different implementations of the Surefire reporters
- *  
+ *
  * @version $Id$
  */
 public interface Reporter
@@ -40,40 +40,41 @@
     void testSetStarting( ReportEntry report )
         throws ReporterException;
 
-    void testSetCompleted( ReportEntry report );
+    void testSetCompleted( ReportEntry report )
+        throws ReporterException;
 
     void testSetAborted( ReportEntry report );
 
     // Tests
 
     /**
-     * Event fired when a test is about to start 
-     * 
+     * Event fired when a test is about to start
+     *
      * @param report
      */
     void testStarting( ReportEntry report );
 
     /**
      * Event fired when a test ended successfully
-     * 
+     *
      * @param report
      */
     void testSucceeded( ReportEntry report );
 
     /**
      * Event fired when a test ended with an error (non anticipated problem)
-     * 
+     *
      * @param report
-     * @param stdOut standard output from the test case 
+     * @param stdOut standard output from the test case
      * @param stdErr error output from the test case
      */
     void testError( ReportEntry report, String stdOut, String stdErr );
 
     /**
      * Event fired when a test ended with a failure (anticipated problem)
-     * 
+     *
      * @param report
-     * @param stdOut standard output from the test case 
+     * @param stdOut standard output from the test case
      * @param stdErr error output from the test case
      */
     void testFailed( ReportEntry report, String stdOut, String stdErr );
@@ -85,28 +86,28 @@
 
     /**
      * Get the number of errors
-     * 
+     *
      * @return
      */
     int getNumErrors();
 
     /**
      * Get the number of failures
-     * 
+     *
      * @return
      */
     int getNumFailures();
 
     /**
      * Get the number of tests
-     * 
+     *
      * @return
      */
     int getNumTests();
 
     /**
      * Get the number of tests skipped
-     * 
+     *
      * @return
      */
     int getNumSkipped();

Modified: maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/XMLReporter.java
URL: http://svn.apache.org/viewcvs/maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/XMLReporter.java?rev=398538&r1=398537&r2=398538&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/XMLReporter.java
(original)
+++ maven/surefire/trunk/surefire-api/src/main/java/org/apache/maven/surefire/report/XMLReporter.java
Sun Apr 30 23:43:56 2006
@@ -29,7 +29,11 @@
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.UnsupportedEncodingException;
+import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Enumeration;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Properties;
 import java.util.StringTokenizer;
 
@@ -43,40 +47,45 @@
 public class XMLReporter
     extends AbstractReporter
 {
-
     private static final String LS = System.getProperty( "line.separator" );
 
-    private PrintWriter writer;
-
-    private Xpp3Dom testSuite;
-
-    private Xpp3Dom testCase;
-
     private File reportsDirectory;
 
+    private List results = Collections.synchronizedList( new ArrayList() );
+
     public XMLReporter( File reportsDirectory )
     {
         this.reportsDirectory = reportsDirectory;
     }
 
-    public void setTestCase( Xpp3Dom testCase )
-    {
-        this.testCase = testCase;
-    }
-
-    public Xpp3Dom getTestCase()
-    {
-        return testCase;
-    }
-
     public void writeMessage( String message )
     {
     }
 
-    public void testSetStarting( ReportEntry report )
+    public void testSetCompleted( ReportEntry report )
         throws ReporterException
     {
-        super.testSetStarting( report );
+        super.testSetCompleted( report );
+
+        long runTime = System.currentTimeMillis() - testSetStartTime;
+
+        Xpp3Dom testSuite = createTestElement( "testsuite", report, runTime );
+
+        showProperties( testSuite );
+
+        testSuite.setAttribute( "tests", String.valueOf( this.getNumTests() ) );
+
+        testSuite.setAttribute( "errors", String.valueOf( this.getNumErrors() ) );
+
+        testSuite.setAttribute( "skipped", String.valueOf( this.getNumSkipped() ) );
+
+        testSuite.setAttribute( "failures", String.valueOf( this.getNumFailures() ) );
+
+        for ( Iterator i = results.iterator(); i.hasNext(); )
+        {
+            Xpp3Dom testcase = (Xpp3Dom) i.next();
+            testSuite.addChild( testcase );
+        }
 
         File reportFile = new File( reportsDirectory, "TEST-" + report.getName() + ".xml"
);
 
@@ -84,10 +93,16 @@
 
         reportDir.mkdirs();
 
+        PrintWriter writer = null;
+
         try
         {
             writer = new PrintWriter(
                 new BufferedWriter( new OutputStreamWriter( new FileOutputStream( reportFile
), "UTF-8" ) ) );
+
+            writer.write( "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + LS );
+
+            Xpp3DomWriter.write( new PrettyPrintXMLWriter( writer ), testSuite );
         }
         catch ( UnsupportedEncodingException e )
         {
@@ -98,42 +113,14 @@
             throw new ReporterException( "Unable to create file: " + e.getMessage(), e );
         }
 
-        writer.write( "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" + LS );
-
-        testSuite = createTestElement( new Xpp3Dom( "testsuite" ), report.getName(), report
);
-        showProperties();
-    }
-
-    public void testSetCompleted( ReportEntry report )
-    {
-        super.testSetCompleted( report );
-
-        testSuite.setAttribute( "tests", String.valueOf( this.getNumTests() ) );
-
-        testSuite.setAttribute( "errors", String.valueOf( this.getNumErrors() ) );
-
-        testSuite.setAttribute( "skipped", String.valueOf( this.getNumSkipped() ) );
-
-        testSuite.setAttribute( "failures", String.valueOf( this.getNumFailures() ) );
-
-        long runTime = System.currentTimeMillis() - testSetStartTime;
-
-        testSuite.setAttribute( "time", elapsedTimeAsString( runTime ) );
-
-        try
-        {
-            Xpp3DomWriter.write( new PrettyPrintXMLWriter( writer ), testSuite );
-        }
         finally
         {
             IOUtil.close( writer );
         }
     }
 
-    public void testStarting( ReportEntry report )
+    private String getReportName( ReportEntry report )
     {
-        super.testStarting( report );
-
         String reportName;
 
         if ( report.getName().indexOf( "(" ) > 0 )
@@ -144,18 +131,7 @@
         {
             reportName = report.getName();
         }
-
-        this.testCase = createTestElement( createElement( testSuite, "testcase" ), reportName,
report );
-    }
-
-    private Xpp3Dom createTestElement( Xpp3Dom element, String reportName, ReportEntry report
)
-    {
-        element.setAttribute( "name", reportName );
-        if ( report.getGroup() != null )
-        {
-            element.setAttribute( "group", report.getGroup() );
-        }
-        return element;
+        return reportName;
     }
 
     public void testSucceeded( ReportEntry report )
@@ -164,7 +140,21 @@
 
         long runTime = this.endTime - this.startTime;
 
+        Xpp3Dom testCase = createTestElement( "testcase", report, runTime );
+
+        results.add( testCase );
+    }
+
+    private Xpp3Dom createTestElement( String name, ReportEntry report, long runTime )
+    {
+        Xpp3Dom testCase = new Xpp3Dom( name );
+        testCase.setAttribute( "name", getReportName( report ) );
+        if ( report.getGroup() != null )
+        {
+            testCase.setAttribute( "group", report.getGroup() );
+        }
         testCase.setAttribute( "time", elapsedTimeAsString( runTime ) );
+        return testCase;
     }
 
     public void testError( ReportEntry report, String stdOut, String stdErr )
@@ -183,11 +173,9 @@
 
     private void writeTestProblems( ReportEntry report, String stdOut, String stdErr, String
name )
     {
-        if ( testCase == null )
-        {
-            // This can occur if the error happens before the test starts
-            testStarting( report );
-        }
+        long runTime = endTime - startTime;
+
+        Xpp3Dom testCase = createTestElement( "testcase", report, runTime );
 
         Xpp3Dom element = createElement( testCase, name );
 
@@ -214,16 +202,14 @@
 
         element.setValue( stackTrace );
 
-        addOutputStreamElement( stdOut, "system-out" );
-
-        addOutputStreamElement( stdErr, "system-err" );
+        addOutputStreamElement( stdOut, "system-out", testCase );
 
-        long runTime = endTime - startTime;
+        addOutputStreamElement( stdErr, "system-err", testCase );
 
-        testCase.setAttribute( "time", elapsedTimeAsString( runTime ) );
+        results.add( testCase );
     }
 
-    private void addOutputStreamElement( String stdOut, String name )
+    private void addOutputStreamElement( String stdOut, String name, Xpp3Dom testCase )
     {
         if ( stdOut != null && stdOut.trim().length() > 0 )
         {
@@ -242,8 +228,10 @@
 
     /**
      * Adds system properties to the XML report.
+     *
+     * @param testSuite
      */
-    private void showProperties()
+    private void showProperties( Xpp3Dom testSuite )
     {
         Xpp3Dom properties = createElement( testSuite, "properties" );
 
@@ -281,5 +269,10 @@
         s = StringUtils.replace( s, ">", "&gt;" );
         return s;
 
+    }
+
+    public Iterator getResults()
+    {
+        return results.iterator();
     }
 }

Modified: maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/report/XMLReporterTest.java
URL: http://svn.apache.org/viewcvs/maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/report/XMLReporterTest.java?rev=398538&r1=398537&r2=398538&view=diff
==============================================================================
--- maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/report/XMLReporterTest.java
(original)
+++ maven/surefire/trunk/surefire-api/src/test/java/org/apache/maven/surefire/report/XMLReporterTest.java
Sun Apr 30 23:43:56 2006
@@ -37,7 +37,6 @@
         reporter = new XMLReporter( null );
         message = "junit.framework.AssertionFailedError";
         reportEntry = new ReportEntry( this, "XMLReporterTest", message, new AssertionFailedError()
);
-        reporter.setTestCase( new Xpp3Dom( "" ) );
     }
 
     /*
@@ -60,7 +59,7 @@
 
     private void assertResult( XMLReporter reporter, String message )
     {
-        Xpp3Dom result = reporter.getTestCase();
+        Xpp3Dom result = (Xpp3Dom) reporter.getResults().next();
         Xpp3Dom child = result.getChild( "error" );
         assertEquals( message, child.getAttribute( "type" ) );
     }



Mime
View raw message