Permalink
Browse files

[SUREFIRE-935] Implement trimStackTrace for JUnit4

[SUREFIRE-936] Make it easier to see which tests fail
  • Loading branch information...
krosenvold committed Dec 10, 2012
1 parent cafad89 commit fff9e32febbc21602b6a393ce448d6caddf62fb5
Showing with 953 additions and 158 deletions.
  1. +9 −1 .../main/java/org/apache/maven/plugin/surefire/booterclient/output/DeserializedStacktraceWriter.java
  2. +4 −2 ...urefire-common/src/main/java/org/apache/maven/plugin/surefire/booterclient/output/ForkClient.java
  3. +0 −1 ...surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/DefaultReporterFactory.java
  4. +2 −2 maven-surefire-common/src/main/java/org/apache/maven/plugin/surefire/report/TestSetRunListener.java
  5. +14 −27 maven-surefire-common/src/main/java/org/apache/maven/surefire/report/RunStatistics.java
  6. +3 −3 ...re-common/src/test/java/org/apache/maven/plugin/surefire/booterclient/ForkingRunListenerTest.java
  7. +2 −2 ...refire-common/src/test/java/org/apache/maven/plugin/surefire/report/StatelessXMLReporterTest.java
  8. +13 −25 maven-surefire-common/src/test/java/org/apache/maven/surefire/report/RunStatisticsTest.java
  9. +2 −1 surefire-api/src/main/java/org/apache/maven/surefire/booter/ForkingRunListener.java
  10. +53 −5 ...va/org/apache/maven/surefire/report/{PojoStackTraceWriter.java → LegacyPojoStackTraceWriter.java}
  11. +5 −0 surefire-api/src/main/java/org/apache/maven/surefire/report/SafeThrowable.java
  12. +7 −0 surefire-api/src/main/java/org/apache/maven/surefire/report/StackTraceWriter.java
  13. +3 −4 surefire-api/src/main/java/org/apache/maven/surefire/util/DefaultRunOrderCalculator.java
  14. +6 −22 ...pache/maven/surefire/report/{PojoStackTraceWriterTest.java → LegacyPojoStackTraceWriterTest.java}
  15. +5 −5 surefire-booter/src/main/java/org/apache/maven/surefire/booter/ForkedBooter.java
  16. +1 −1 ...integration-tests/src/test/java/org/apache/maven/surefire/its/jiras/Surefire697NiceSummaryIT.java
  17. +75 −0 surefire-providers/common-java5/pom.xml
  18. +73 −0 ...e-providers/common-java5/src/main/java/org/apache/maven/surefire/report/PojoStackTraceWriter.java
  19. +189 −0 ...-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java
  20. +63 −0 surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/ATestClass.java
  21. +63 −0 ...oviders/common-java5/src/test/java/org/apache/maven/surefire/report/PojoStackTraceWriterTest.java
  22. +218 −0 ...viders/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java
  23. +5 −0 surefire-providers/common-junit4/pom.xml
  24. +8 −2 ...viders/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4RunListener.java
  25. +26 −4 ...s/common-junit4/src/main/java/org/apache/maven/surefire/common/junit4/JUnit4StackTraceWriter.java
  26. +5 −0 surefire-providers/common-junit48/pom.xml
  27. +55 −0 ...ommon-junit48/src/main/java/org/apache/maven/surefire/common/junit48/JUnit46StackTraceWriter.java
  28. +1 −0 surefire-providers/pom.xml
  29. +0 −36 ...roviders/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/JUnitStackTraceWriter.java
  30. +9 −9 surefire-providers/surefire-junit3/src/main/java/org/apache/maven/surefire/junit/PojoTestSet.java
  31. +3 −2 .../surefire-junit3/src/main/java/org/apache/maven/surefire/junit/TestListenerInvocationHandler.java
  32. +7 −0 surefire-providers/surefire-junit4/pom.xml
  33. +6 −0 surefire-providers/surefire-junit47/pom.xml
  34. +8 −0 ...ders/surefire-junit47/src/main/java/org/apache/maven/surefire/junitcore/JUnitCoreRunListener.java
  35. +5 −0 surefire-providers/surefire-testng/pom.xml
  36. +5 −4 ...ders/surefire-testng/src/main/java/org/apache/maven/surefire/testng/TestNGDirectoryTestSuite.java
@@ -37,10 +37,18 @@

private final String stackTrace;

public DeserializedStacktraceWriter( String message, String stackTrace )
private final String smartTrimmed;

public DeserializedStacktraceWriter( String message, String smartTrimmed, String stackTrace )
{
this.message = message;
this.stackTrace = stackTrace;
this.smartTrimmed = smartTrimmed;
}

public String smartTrimmedStackTrace()
{
return smartTrimmed;
}

// Trimming or not is decided on the forking side
@@ -215,9 +215,11 @@ private StackTraceWriter deserializeStackStraceWriter( StringTokenizer tokens )
{
StackTraceWriter stackTraceWriter;
String stackTraceMessage = nullableCsv( tokens.nextToken() );
String smartStackTrace = nullableCsv( tokens.nextToken() );
String stackTrace = tokens.hasMoreTokens() ? nullableCsv( tokens.nextToken() ) : null;
stackTraceWriter =
stackTrace != null ? new DeserializedStacktraceWriter( stackTraceMessage, stackTrace ) : null;
stackTraceWriter = stackTrace != null
? new DeserializedStacktraceWriter( stackTraceMessage, smartStackTrace, stackTrace )
: null;
return stackTraceWriter;
}

@@ -22,7 +22,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.maven.plugin.surefire.StartupReportConfiguration;
import org.apache.maven.plugin.surefire.runorder.StatisticsReporter;
import org.apache.maven.surefire.report.DefaultDirectConsoleReporter;
@@ -173,7 +173,7 @@ public void testError( ReportEntry reportEntry )
{
statisticsReporter.testError( reportEntry );
}
globalStatistics.addErrorSource( reportEntry.getName(), reportEntry.getStackTraceWriter() );
globalStatistics.addErrorSource( reportEntry.getStackTraceWriter() );
clearCapture();
}

@@ -185,7 +185,7 @@ public void testFailed( ReportEntry reportEntry )
{
statisticsReporter.testFailed( reportEntry );
}
globalStatistics.addFailureSource( reportEntry.getName(), reportEntry.getStackTraceWriter() );
globalStatistics.addFailureSource( reportEntry.getStackTraceWriter() );
clearCapture();
}

@@ -24,7 +24,6 @@
import java.util.Collections;
import org.apache.maven.plugin.surefire.report.TestSetStats;
import org.apache.maven.surefire.suite.RunResult;
import org.apache.maven.surefire.util.internal.StringUtils;

/**
* @author Kristian Rosenvold
@@ -50,14 +49,22 @@
private int skipped;


public void addErrorSource( String errorSource, StackTraceWriter stackTraceWriter )
public void addErrorSource( StackTraceWriter stackTraceWriter )
{
errorSources.addSource( errorSource, stackTraceWriter );
if ( stackTraceWriter == null )
{
throw new IllegalArgumentException( "Cant be null" );
}
errorSources.addSource( stackTraceWriter );
}

public void addFailureSource( String failureSource, StackTraceWriter stackTraceWriter )
public void addFailureSource( StackTraceWriter stackTraceWriter )
{
failureSources.addSource( failureSource, stackTraceWriter );
if ( stackTraceWriter == null )
{
throw new IllegalArgumentException( "Cant be null" );
}
failureSources.addSource( stackTraceWriter );
}

public Collection<String> getErrorSources()
@@ -122,18 +129,9 @@ void addSource( String source )
}
}

void addSource( String source, StackTraceWriter stackTraceWriter )
{
String message = getMessageOfThrowable( stackTraceWriter );
String extendedSource =
StringUtils.isBlank( message ) ? source : source + ": " + trimToSingleLine( message );
addSource( extendedSource );
}

private String trimToSingleLine( String str )
void addSource( StackTraceWriter stackTraceWriter )
{
int i = str.indexOf( "\n" );
return i >= 0 ? str.substring( 0, i ) + "(..)" : str;
addSource( stackTraceWriter.smartTrimmedStackTrace() );
}

Collection<String> getListOfSources()
@@ -143,16 +141,5 @@ private String trimToSingleLine( String str )
return Collections.unmodifiableCollection( listOfSources );
}
}

private String getMessageOfThrowable( StackTraceWriter stackTraceWriter )
{
//noinspection ThrowableResultOfMethodCallIgnored
return stackTraceWriter != null ? getMessageOfThrowable( stackTraceWriter.getThrowable() ) : "";
}

private String getMessageOfThrowable( SafeThrowable throwable )
{
return throwable != null ? throwable.getLocalizedMessage() : "";
}
}
}
@@ -30,7 +30,7 @@
import org.apache.maven.surefire.report.CategorizedReportEntry;
import org.apache.maven.surefire.report.ConsoleLogger;
import org.apache.maven.surefire.report.ConsoleOutputReceiver;
import org.apache.maven.surefire.report.PojoStackTraceWriter;
import org.apache.maven.surefire.report.LegacyPojoStackTraceWriter;
import org.apache.maven.surefire.report.ReportEntry;
import org.apache.maven.surefire.report.ReporterException;
import org.apache.maven.surefire.report.RunListener;
@@ -296,7 +296,7 @@ private SimpleReportEntry createReportEntryWithStackTrace()
catch ( RuntimeException e )
{
StackTraceWriter stackTraceWriter =
new PojoStackTraceWriter( "org.apache.tests.TestClass", "testMethod11", e );
new LegacyPojoStackTraceWriter( "org.apache.tests.TestClass", "testMethod11", e );
return new CategorizedReportEntry( "com.abc.TestClass", "testMethod", "aGroup", stackTraceWriter, 77 );
}
}
@@ -310,7 +310,7 @@ private SimpleReportEntry createReportEntryWithSpecialMessage( String message )
catch ( RuntimeException e )
{
StackTraceWriter stackTraceWriter =
new PojoStackTraceWriter( "org.apache.tests.TestClass", "testMethod11", e );
new LegacyPojoStackTraceWriter( "org.apache.tests.TestClass", "testMethod11", e );
return new CategorizedReportEntry( "com.abc.TestClass", "testMethod", "aGroup", stackTraceWriter, 77 );
}
}
@@ -20,7 +20,7 @@
*/

import java.io.File;
import org.apache.maven.surefire.report.PojoStackTraceWriter;
import org.apache.maven.surefire.report.LegacyPojoStackTraceWriter;
import org.apache.maven.surefire.report.ReportEntry;
import org.apache.maven.surefire.report.SimpleReportEntry;

@@ -45,7 +45,7 @@ protected void setUp()
super.setUp();
message = "junit.framework.AssertionFailedError";
reportEntry = new SimpleReportEntry( this.getClass().getName(), "StatelessXMLReporterTest",
new PojoStackTraceWriter( "", "", new AssertionFailedError() ), 17 );
new LegacyPojoStackTraceWriter( "", "", new AssertionFailedError() ), 17 );
stats = new TestSetStats( false, true );
}

@@ -24,30 +24,25 @@
public class RunStatisticsTest
extends TestCase
{
private static final String DUMMY_ERROR_SOURCE = "dummy error source";
private static final String Method = "AClass#AMethod";

private static final String DUMMY_ERROR_SOURCE = Method + " RuntimeException";

private static final String DUMMY_FAILURE_SOURCE = "dummy failure source";

private static final String DUMMY_MESSAGE = "dummy message";
private static final String DUMMY_MESSAGE = "foo";

public void testAddErrorSourceWithThrowableMessage()
{
RuntimeException throwable = new RuntimeException( DUMMY_MESSAGE );
RunStatistics statistics = createRunStatisticsAndAddErrorSourceWithThrowable( throwable );
assertRunStatisticsHasErrorSource( statistics, DUMMY_ERROR_SOURCE + ": " + DUMMY_MESSAGE );
}

public void testAddErrorSourceWithoutStackTraceWriter()
{
RunStatistics statistics = new RunStatistics();
statistics.addErrorSource( DUMMY_ERROR_SOURCE, null );
assertRunStatisticsHasErrorSource( statistics, DUMMY_ERROR_SOURCE );
assertRunStatisticsHasErrorSource( statistics, DUMMY_ERROR_SOURCE + " " + DUMMY_MESSAGE );
}

public void testAddErrorSourceWithoutThrowable()
{
RunStatistics statistics = createRunStatisticsAndAddErrorSourceWithThrowable( null );
assertRunStatisticsHasErrorSource( statistics, DUMMY_ERROR_SOURCE );
assertRunStatisticsHasErrorSource( statistics, Method );
}

public void testAddErrorSourceWithThrowableWithoutMessage()
@@ -61,43 +56,36 @@ public void testAddFailureSourceWithThrowableMessage()
{
RuntimeException throwable = new RuntimeException( DUMMY_MESSAGE );
RunStatistics statistics = createRunStatisticsAndAddFailureSourceWithThrowable( throwable );
assertRunStatisticsHasFailureSource( statistics, DUMMY_FAILURE_SOURCE + ": " + DUMMY_MESSAGE );
}

public void testAddFailureSourceWithoutStackTraceWriter()
{
RunStatistics statistics = new RunStatistics();
statistics.addFailureSource( DUMMY_FAILURE_SOURCE, null );
assertRunStatisticsHasFailureSource( statistics, DUMMY_FAILURE_SOURCE );
assertRunStatisticsHasFailureSource( statistics, DUMMY_ERROR_SOURCE + " " + DUMMY_MESSAGE );
}

public void testAddFailureSourceWithoutThrowable()
{
RunStatistics statistics = createRunStatisticsAndAddFailureSourceWithThrowable( null );
assertRunStatisticsHasFailureSource( statistics, DUMMY_FAILURE_SOURCE );
assertRunStatisticsHasFailureSource( statistics, Method );
}

public void testAddFailureSourceWithThrowableWithoutMessage()
{
RuntimeException throwable = new RuntimeException();
RunStatistics statistics = createRunStatisticsAndAddFailureSourceWithThrowable( throwable );
assertRunStatisticsHasFailureSource( statistics, DUMMY_FAILURE_SOURCE );
assertRunStatisticsHasFailureSource( statistics, DUMMY_ERROR_SOURCE );
}

private RunStatistics createRunStatisticsAndAddErrorSourceWithThrowable( Throwable throwable )
{
StackTraceWriter stackTraceWriter = new PojoStackTraceWriter( null, null, throwable );
StackTraceWriter stackTraceWriter = new LegacyPojoStackTraceWriter( "AClass", "AMethod", throwable );
RunStatistics statistics = new RunStatistics();
statistics.addErrorSource( DUMMY_ERROR_SOURCE, stackTraceWriter );
statistics.addErrorSource( stackTraceWriter );

return statistics;
}

private RunStatistics createRunStatisticsAndAddFailureSourceWithThrowable( Throwable throwable )
{
StackTraceWriter stackTraceWriter = new PojoStackTraceWriter( null, null, throwable );
StackTraceWriter stackTraceWriter = new LegacyPojoStackTraceWriter( "AClass", "AMethod", throwable );
RunStatistics statistics = new RunStatistics();
statistics.addFailureSource( DUMMY_FAILURE_SOURCE, stackTraceWriter );
statistics.addFailureSource( stackTraceWriter );

return statistics;
}
@@ -325,7 +325,8 @@ public static void encode( StringBuffer stringBuffer, StackTraceWriter stackTrac
nullableEncoding( stringBuffer, message );
}
comma( stringBuffer );

nullableEncoding( stringBuffer, stackTraceWriter.smartTrimmedStackTrace() );
comma( stringBuffer );
nullableEncoding( stringBuffer, trimStackTraces
? stackTraceWriter.writeTrimmedTraceToString()
: stackTraceWriter.writeTraceToString() );
@@ -25,20 +25,21 @@
import org.apache.maven.surefire.util.internal.StringUtils;

/**
* Write the trace out for a POJO test.
* Write the trace out for a POJO test. Java 1.3 compatible.
*
* @author <a href="mailto:brett@apache.org">Brett Porter</a>
* @noinspection ThrowableResultOfMethodCallIgnored
*/
public class PojoStackTraceWriter
public class LegacyPojoStackTraceWriter
implements StackTraceWriter
{
private final Throwable t;

protected final String testClass;
private final String testClass;

protected final String testMethod;
private final String testMethod;

public PojoStackTraceWriter( String testClass, String testMethod, Throwable t )
public LegacyPojoStackTraceWriter( String testClass, String testMethod, Throwable t )
{
this.testClass = testClass;
this.testMethod = testMethod;
@@ -56,6 +57,53 @@ public String writeTraceToString()
return w.toString();
}

public String smartTrimmedStackTrace()
{
StringBuffer result = new StringBuffer();
result.append( testClass );
result.append( "#" );
result.append( testMethod );
SafeThrowable throwable = getThrowable();
if ( throwable.getTarget() instanceof AssertionError )
{
result.append( " " );
result.append( getTruncatedMessage( throwable.getMessage(), 77 - result.length() ) );
}
else
{
Throwable target = throwable.getTarget();
if ( target != null )
{
result.append( " " );
result.append( target.getClass().getSimpleName() );
result.append( getTruncatedMessage( throwable.getMessage(), 77 - result.length() ) );
}
}
return result.toString();
}

private static String getTruncatedMessage( String msg, int i )
{
if ( i < 0 )
{
return "";
}
if ( msg == null )
{
return "";
}
String substring = msg.substring( 0, Math.min( i, msg.length() ) );
if ( i < msg.length() )
{
return " " + substring + "...";
}
else
{
return " " + substring;
}
}


public String writeTrimmedTraceToString()
{
String text = writeTraceToString();
@@ -53,4 +53,9 @@ public String getMessage()
return t.getMessage();
}
}

public Throwable getTarget()
{
return target;
}
}
@@ -40,6 +40,13 @@
*/
String writeTrimmedTraceToString();

/**
* Get the "smart" trimmed (1-2 lines) stacktrace.
*
* @return the trace
*/
String smartTrimmedStackTrace();

/**
* Retrieve the throwable for this writer.
*
Oops, something went wrong.

0 comments on commit fff9e32

Please sign in to comment.