You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by ti...@apache.org on 2015/09/13 15:35:23 UTC

maven-surefire git commit: [SUREFIRE] refactoring

Repository: maven-surefire
Updated Branches:
  refs/heads/master e87f8807d -> 34d445be3


[SUREFIRE] refactoring


Project: http://git-wip-us.apache.org/repos/asf/maven-surefire/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven-surefire/commit/34d445be
Tree: http://git-wip-us.apache.org/repos/asf/maven-surefire/tree/34d445be
Diff: http://git-wip-us.apache.org/repos/asf/maven-surefire/diff/34d445be

Branch: refs/heads/master
Commit: 34d445be3c6ef5be7245db031039751105777e3f
Parents: e87f880
Author: Tibor17 <ti...@lycos.com>
Authored: Sun Sep 13 15:34:49 2015 +0200
Committer: Tibor17 <ti...@lycos.com>
Committed: Sun Sep 13 15:34:49 2015 +0200

----------------------------------------------------------------------
 .../surefire/report/FileReporterUtils.java      |  7 +-
 .../plugin/surefire/report/ReporterUtils.java   | 45 ++++++++++++
 .../plugin/surefire/report/TestSetStats.java    | 75 ++++++--------------
 .../surefire/report/WrappedReportEntry.java     | 43 ++++-------
 .../maven/surefire/junitcore/LogicalStream.java | 14 ++--
 .../maven/surefire/junitcore/TestMethod.java    | 60 +++++++++-------
 6 files changed, 123 insertions(+), 121 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/34d445be/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/FileReporterUtils.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/FileReporterUtils.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/FileReporterUtils.java
index f829121..36bc269 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/FileReporterUtils.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/FileReporterUtils.java
@@ -24,8 +24,13 @@ package org.apache.maven.plugin.surefire.report;
  *
  * @author Andreas Gudian
  */
-public class FileReporterUtils
+public final class FileReporterUtils
 {
+    private FileReporterUtils()
+    {
+        throw new IllegalStateException( "non instantiable constructor" );
+    }
+
     public static String stripIllegalFilenameChars( String original )
     {
         String result = original;

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/34d445be/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ReporterUtils.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ReporterUtils.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ReporterUtils.java
new file mode 100644
index 0000000..c9cac33
--- /dev/null
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/ReporterUtils.java
@@ -0,0 +1,45 @@
+package org.apache.maven.plugin.surefire.report;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+/**
+ * Utility for reporter classes.
+ *
+ * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
+ * @since 2.19
+ */
+final class ReporterUtils
+{
+    private static final int MS_PER_SEC = 1000;
+
+    private ReporterUtils()
+    {
+        throw new IllegalStateException( "non instantiable constructor" );
+    }
+
+    public static String formatElapsedTime( double runTime )
+    {
+        NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH );
+        return numberFormat.format( runTime / MS_PER_SEC );
+    }
+}

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/34d445be/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java
index 95e92d4..87c9310 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetStats.java
@@ -19,11 +19,9 @@ package org.apache.maven.plugin.surefire.report;
  * under the License.
  */
 
-import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
-import java.util.Locale;
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
 
@@ -52,8 +50,6 @@ public class TestSetStats
 
     private long lastStartAt;
 
-    private long elapsedForTestSet;
-
     public TestSetStats( boolean trimStackTrace, boolean plainFormat )
     {
         this.trimStackTrace = trimStackTrace;
@@ -62,26 +58,12 @@ public class TestSetStats
 
     public int getElapsedSinceTestSetStart()
     {
-        if ( testSetStartAt > 0 )
-        {
-            return (int) ( System.currentTimeMillis() - testSetStartAt );
-        }
-        else
-        {
-            return 0;
-        }
+        return testSetStartAt > 0 ? (int) ( System.currentTimeMillis() - testSetStartAt ) : 0;
     }
 
     public int getElapsedSinceLastStart()
     {
-        if ( lastStartAt > 0 )
-        {
-            return (int) ( System.currentTimeMillis() - lastStartAt );
-        }
-        else
-        {
-            return 0;
-        }
+        return lastStartAt > 0 ? (int) ( System.currentTimeMillis() - lastStartAt ) : 0;
     }
 
     public void testSetStart()
@@ -107,9 +89,7 @@ public class TestSetStats
         {
             testStartAt = testEndAt;
         }
-        long elapsedForThis = reportEntry.getElapsed() != null ? reportEntry.getElapsed() : testEndAt - testStartAt;
-        elapsedForTestSet += elapsedForThis;
-        return elapsedForThis;
+        return reportEntry.getElapsed() != null ? reportEntry.getElapsed() : testEndAt - testStartAt;
     }
 
     public void testSucceeded( WrappedReportEntry reportEntry )
@@ -122,7 +102,6 @@ public class TestSetStats
     {
         errors += 1;
         finishTest( reportEntry );
-
     }
 
     public void testFailure( WrappedReportEntry reportEntry )
@@ -143,7 +122,6 @@ public class TestSetStats
         errors = 0;
         failures = 0;
         skipped = 0;
-        elapsedForTestSet = 0;
 
         for ( WrappedReportEntry entry : reportEntries )
         {
@@ -176,18 +154,7 @@ public class TestSetStats
 
     public String elapsedTimeAsString( long runTime )
     {
-        return numberFormat.format( (double) runTime / MS_PER_SEC );
-    }
-
-    private static final String TEST_SET_COMPLETED_PREFIX = "Tests run: ";
-
-    private final NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH );
-
-    private static final int MS_PER_SEC = 1000;
-
-    public String getElapsedForTestSet()
-    {
-        return elapsedTimeAsString( elapsedForTestSet );
+        return ReporterUtils.formatElapsedTime( runTime );
     }
 
     private void incrementCompletedCount()
@@ -197,31 +164,29 @@ public class TestSetStats
 
     public String getTestSetSummary( WrappedReportEntry reportEntry )
     {
-        StringBuilder buf = new StringBuilder();
-
-        buf.append( TEST_SET_COMPLETED_PREFIX );
-        buf.append( completedCount );
-        buf.append( ", Failures: " );
-        buf.append( failures );
-        buf.append( ", Errors: " );
-        buf.append( errors );
-        buf.append( ", Skipped: " );
-        buf.append( skipped );
-        buf.append( ", Time elapsed: " );
-        buf.append( reportEntry.elapsedTimeAsString() );
-        buf.append( " sec" );
+        String summary = "Tests run: ";
+        summary += completedCount;
+        summary += ", Failures: ";
+        summary += failures;
+        summary += ", Errors: ";
+        summary += errors;
+        summary += ", Skipped: ";
+        summary += skipped;
+        summary += ", ";
+        summary += reportEntry.getElapsedTimeVerbose();
+        summary += " sec";
 
         if ( failures > 0 || errors > 0 )
         {
-            buf.append( " <<< FAILURE!" );
+            summary += " <<< FAILURE!";
         }
 
-        buf.append( " - in " );
-        buf.append( reportEntry.getNameWithGroup() );
+        summary += " - in ";
+        summary += reportEntry.getNameWithGroup();
 
-        buf.append( "\n" );
+        summary += "\n";
 
-        return buf.toString();
+        return summary;
     }
 
     public List<String> getTestResults()

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/34d445be/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java
----------------------------------------------------------------------
diff --git a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java
index f62a917..3e99306 100644
--- a/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java
+++ b/maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/WrappedReportEntry.java
@@ -19,9 +19,6 @@ package org.apache.maven.plugin.surefire.report;
  * under the License.
  */
 
-import java.text.NumberFormat;
-import java.util.Locale;
-
 import org.apache.maven.surefire.report.ReportEntry;
 import org.apache.maven.surefire.report.StackTraceWriter;
 
@@ -31,6 +28,8 @@ import org.apache.maven.surefire.report.StackTraceWriter;
 public class WrappedReportEntry
     implements ReportEntry
 {
+    private static final String NL = System.getProperty( "line.separator" );
+
     private final ReportEntry original;
 
     private final ReportEntryType reportEntryType;
@@ -41,12 +40,6 @@ public class WrappedReportEntry
 
     private final Utf8RecodingDeferredFileOutputStream stdErr;
 
-    private final NumberFormat numberFormat = NumberFormat.getInstance( Locale.ENGLISH );
-
-    private static final int MS_PER_SEC = 1000;
-
-    static final String NL = System.getProperty( "line.separator" );
-
     public WrappedReportEntry( ReportEntry original, ReportEntryType reportEntryType, Integer estimatedElapsed,
                                Utf8RecodingDeferredFileOutputStream stdout,
                                Utf8RecodingDeferredFileOutputStream stdErr )
@@ -110,12 +103,8 @@ public class WrappedReportEntry
 
     public String getStackTrace( boolean trimStackTrace )
     {
-        StackTraceWriter writer = original.getStackTraceWriter();
-        if ( writer == null )
-        {
-            return null;
-        }
-        return trimStackTrace ? writer.writeTrimmedTraceToString() : writer.writeTraceToString();
+        StackTraceWriter w = original.getStackTraceWriter();
+        return w == null ? null : ( trimStackTrace ? w.writeTrimmedTraceToString() : w.writeTraceToString() );
     }
 
     public String elapsedTimeAsString()
@@ -125,7 +114,7 @@ public class WrappedReportEntry
 
     String elapsedTimeAsString( long runTime )
     {
-        return numberFormat.format( (double) runTime / MS_PER_SEC );
+        return ReporterUtils.formatElapsedTime( runTime );
     }
 
     public String getReportName()
@@ -141,26 +130,18 @@ public class WrappedReportEntry
 
     public String getOutput( boolean trimStackTrace )
     {
-        StringBuilder buf = new StringBuilder();
-
-        buf.append( getElapsedTimeSummary() );
-
-        buf.append( "  <<< " ).append( getReportEntryType().toString().toUpperCase() ).append( "!" ).append( NL );
-
-        buf.append( getStackTrace( trimStackTrace ) );
+        return getElapsedTimeSummary() + "  <<< " + getReportEntryType().toString().toUpperCase() + "!" + NL
+            + getStackTrace( trimStackTrace );
+    }
 
-        return buf.toString();
+    public String getElapsedTimeVerbose()
+    {
+        return "Time elapsed: " + elapsedTimeAsString() + " sec";
     }
 
     public String getElapsedTimeSummary()
     {
-        StringBuilder reportContent = new StringBuilder();
-        reportContent.append( getName() );
-        reportContent.append( "  Time elapsed: " );
-        reportContent.append( elapsedTimeAsString() );
-        reportContent.append( " sec" );
-
-        return reportContent.toString();
+        return getName() + "  " + getElapsedTimeVerbose();
     }
 
     public boolean isErrorOrFailure()

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/34d445be/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/LogicalStream.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/LogicalStream.java b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/LogicalStream.java
index c5e27e0..41e09b8 100644
--- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/LogicalStream.java
+++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/LogicalStream.java
@@ -19,19 +19,20 @@ package org.apache.maven.surefire.junitcore;
  * under the License.
  */
 
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Collection;
+import java.util.concurrent.ConcurrentLinkedQueue;
+
 import org.apache.maven.surefire.report.ConsoleOutputReceiver;
 import org.apache.maven.surefire.util.internal.ByteBuffer;
 
 /**
  * A stream-like object that preserves ordering between stdout/stderr
  */
-public class LogicalStream
+public final class LogicalStream
 {
-    private final List<Entry> output = new ArrayList<Entry>();
+    private final Collection<Entry> output = new ConcurrentLinkedQueue<Entry>();
 
-    class Entry
+    static final class Entry
     {
         final boolean stdout;
 
@@ -49,7 +50,6 @@ public class LogicalStream
             this.len = len;
         }
 
-
         public void writeDetails( ConsoleOutputReceiver outputReceiver )
         {
             outputReceiver.writeTestOutput( b, off, len, stdout );
@@ -83,6 +83,4 @@ public class LogicalStream
             entry.writeDetails( outputReceiver );
         }
     }
-
-
 }

http://git-wip-us.apache.org/repos/asf/maven-surefire/blob/34d445be/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestMethod.java
----------------------------------------------------------------------
diff --git a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestMethod.java b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestMethod.java
index a448dfb..eb388c6 100644
--- a/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestMethod.java
+++ b/surefire-providers/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/TestMethod.java
@@ -25,6 +25,8 @@ import org.apache.maven.surefire.report.ConsoleOutputReceiverForCurrentThread;
 import org.apache.maven.surefire.report.ReportEntry;
 import org.apache.maven.surefire.report.RunListener;
 
+import java.util.concurrent.atomic.AtomicReference;
+
 /**
  * Represents the test-state of a single test method that is run.
  * <p/>
@@ -34,6 +36,10 @@ import org.apache.maven.surefire.report.RunListener;
 class TestMethod
     implements ConsoleOutputReceiver
 {
+    private static final InheritableThreadLocal<TestMethod> TEST_METHOD = new InheritableThreadLocal<TestMethod>();
+
+    private final AtomicReference<LogicalStream> output = new AtomicReference<LogicalStream>();
+
     private final ReportEntry description;
 
     private final TestSet testSet;
@@ -48,10 +54,6 @@ class TestMethod
 
     private volatile ReportEntry ignored;
 
-    private static final InheritableThreadLocal<TestMethod> TEST_METHOD = new InheritableThreadLocal<TestMethod>();
-
-    private volatile LogicalStream output;
-
     TestMethod( ReportEntry description, TestSet testSet )
     {
         this.description = description;
@@ -104,31 +106,32 @@ class TestMethod
 
     void replay( RunListener reporter )
     {
-
         if ( ignored != null )
         {
             reporter.testSkipped( createReportEntry( ignored ) );
-            return;
-        }
-
-        ReportEntry descriptionReport = createReportEntry( description );
-        reporter.testStarting( descriptionReport );
-        if ( output != null )
-        {
-            output.writeDetails( ( (ConsoleOutputReceiver) reporter ) );
-        }
-
-        if ( testFailure != null )
-        {
-            reporter.testFailed( createReportEntry( testFailure ) );
-        }
-        else if ( testError != null )
-        {
-            reporter.testError( createReportEntry( testError ) );
         }
         else
         {
-            reporter.testSucceeded( descriptionReport );
+            ReportEntry descriptionReport = createReportEntry( description );
+            reporter.testStarting( descriptionReport );
+            LogicalStream ls = output.get();
+            if ( ls != null )
+            {
+                ls.writeDetails( (ConsoleOutputReceiver) reporter );
+            }
+
+            if ( testFailure != null )
+            {
+                reporter.testFailed( createReportEntry( testFailure ) );
+            }
+            else if ( testError != null )
+            {
+                reporter.testError( createReportEntry( testError ) );
+            }
+            else
+            {
+                reporter.testSucceeded( descriptionReport );
+            }
         }
     }
 
@@ -157,11 +160,16 @@ class TestMethod
 
     LogicalStream getLogicalStream()
     {
-        if ( output == null )
+        LogicalStream ls = output.get();
+        if ( ls == null )
         {
-            output = new LogicalStream();
+            ls = new LogicalStream();
+            if ( !output.compareAndSet( null, ls ) )
+            {
+                ls = output.get();
+            }
         }
-        return output;
+        return ls;
     }
 
     public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )