Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUREFIRE-1887] Trim stacktraces to include relevant lines #574

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void testWriteTrimmedTraceToString()
{
String stackTrace = "junit.framework.AssertionFailedError: blah\n"
+ " at junit.framework.Assert.fail(Assert.java:47)\n"
+ " at AnotherClass.execute(AnotherClass.java:60)\n"
+ " at TestSurefire3.testQuote(TestSurefire3.java:23)\n"
+ " at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n"
+ " at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n"
Expand Down Expand Up @@ -64,14 +65,18 @@ public void testWriteTrimmedTraceToString()
MockThrowable t = new MockThrowable( stackTrace );
LegacyPojoStackTraceWriter w = new LegacyPojoStackTraceWriter( "TestSurefire3", "testQuote", t );
String out = w.writeTrimmedTraceToString();
String expected = "junit.framework.AssertionFailedError: blah\n" + " at junit.framework.Assert.fail(Assert.java:47)\n" + " at TestSurefire3.testQuote(TestSurefire3.java:23)\n";
String expected = "junit.framework.AssertionFailedError: blah\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused. This seems to add an extra line to the output and not remove anything. Is there a test that shows these changes making stack traces simpler?

+ " at junit.framework.Assert.fail(Assert.java:47)\n"
+ " at AnotherClass.execute(AnotherClass.java:60)\n"
+ " at TestSurefire3.testQuote(TestSurefire3.java:23)\n";
assertEquals( expected, out );
}

@SuppressWarnings( "checkstyle:linelength" )
public void testCausedBy()
{
String stackTrace = "java.lang.RuntimeException: blah\n"
+ " at AnotherClass.execute(AnotherClass.java:60)\n"
+ " at TestSurefire3.testBlah(TestSurefire3.java:45)\n"
+ " at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n"
+ " at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)\n"
Expand Down Expand Up @@ -109,6 +114,7 @@ public void testCausedBy()
LegacyPojoStackTraceWriter w = new LegacyPojoStackTraceWriter( "TestSurefire3", "testBlah", t );
String out = w.writeTrimmedTraceToString();
String expected = "java.lang.RuntimeException: blah\n"
+ " at AnotherClass.execute(AnotherClass.java:60)\n"
+ " at TestSurefire3.testBlah(TestSurefire3.java:45)\n"
+ "Caused by: junit.framework.AssertionFailedError: \"\n"
+ " at junit.framework.Assert.fail(Assert.java:47)\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public String smartTrimmedStackTrace()
@Override
public String writeTrimmedTraceToString()
{
return t == null ? "" : SmartStackTraceParser.stackTraceWithFocusOnClassAsString( t, testClass );
return t == null ? "" : new SmartStackTraceParser( testClass, t, testMethod ).getTrimmedStackTrace();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,15 @@ static Throwable findTopmostWithClass( final Throwable t, StackTraceFilter filte
return t;
}

public String getTrimmedStackTrace( )
{
StackTraceFilter filter = new ClassNameStackTraceFilter( testClassName );
Throwable topmost = findTopmostWithClass( throwable.getTarget(), filter );
List<StackTraceElement> stackTraceElements = asList( topmost.getStackTrace() );
String s = causeToString( topmost.getCause(), filter );
return toTrimmedString( throwable.getTarget(), stackTraceElements, filter ) + s;
}

public static String stackTraceWithFocusOnClassAsString( Throwable t, String className )
{
StackTraceFilter filter = new ClassNameStackTraceFilter( className );
Expand Down Expand Up @@ -258,6 +267,44 @@ private static String causeToString( Throwable cause, StackTraceFilter filter )
return resp.toString();
}

private static String toTrimmedString( Throwable t, Iterable<StackTraceElement> elements, StackTraceFilter filter )
{
StringBuilder result = new StringBuilder();
if ( t != null )
{
result.append( t.getClass().getName() );
String msg = t.getMessage();
if ( msg != null )
{
result.append( ": " );
if ( isMultiLine( msg ) )
{
// SUREFIRE-986
result.append( '\n' );
}
result.append( msg );
}
result.append( '\n' );
}

boolean matched = false;
for ( StackTraceElement element : elements )
{
if ( filter.matches( element ) )
{
matched = true;
}

if ( filter.matches( element ) || !matched )
{
result.append( "\tat " )
.append( element )
.append( '\n' );
}
}
return result.toString();
}

private static String toString( Throwable t, Iterable<StackTraceElement> elements, StackTraceFilter filter )
{
StringBuilder result = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

import junit.framework.TestCase;

import static org.assertj.core.api.Assertions.assertThat;

/**
*
*/
Expand Down Expand Up @@ -56,6 +58,37 @@ public void testMultiLineMessage()
}
}

public void testTrimmedStackTrace( )
{
Throwable throwable = new Exception( "" );

try
{
throwExceptionFromTestClass( );
}
catch ( Throwable t )
{
throwable = t;
}
PojoStackTraceWriter a = new PojoStackTraceWriter(
"org.apache.maven.surefire.report.PojoStackTraceWriterTest", null, throwable
);
String result = a.writeTrimmedTraceToString();

assertThat( result ).contains( "org.apache.maven.surefire.report.PojoStackTraceWriterTest."
+ "throwExceptionFromTestClass(PojoStackTraceWriterTest.java" );
assertThat( result ).contains( "org.apache.maven.surefire.report.PojoStackTraceWriterTest."
+ "testTrimmedStackTrace(PojoStackTraceWriterTest.java" );
assertThat( result ).hasLineCount( 8 );
assertThat( result ).contains( "Caused by: java.lang.Exception: Hey ho, hey ho, a throwable we throw!" );
}

public void throwExceptionFromTestClass() throws Exception
{
Object call = new RunnableTestClass1().call();
throw new IllegalStateException( "illegal state", (Throwable) call );
}

static class ATestClass
{
static class AnotherTestClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.junit.runner.notification.Failure;

import static org.apache.maven.surefire.common.junit4.JUnit4ProviderUtil.toClassMethod;
import static org.apache.maven.surefire.report.SmartStackTraceParser.stackTraceWithFocusOnClassAsString;

/**
* Writes out a specific {@link org.junit.runner.notification.Failure} for
Expand Down Expand Up @@ -99,11 +98,11 @@ public String writeTrimmedTraceToString()
try
{
Throwable e = junitFailure.getException();
return stackTraceWithFocusOnClassAsString( e, testClass );
return new SmartStackTraceParser( testClass, e, null ).getTrimmedStackTrace();
}
catch ( Throwable t )
{
return stackTraceWithFocusOnClassAsString( t, testClass );
return new SmartStackTraceParser( testClass, t, null ).getTrimmedStackTrace();
}
}

Expand Down