Skip to content

Commit

Permalink
[SUREFIRE-1992] Do not abbreviate test error/failure messages to 78 c…
Browse files Browse the repository at this point in the history
…haracters
  • Loading branch information
spannm authored and Tibor17 committed Feb 8, 2022
1 parent 9ba1733 commit 89cffc2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 69 deletions.
Expand Up @@ -21,6 +21,7 @@


import org.apache.maven.surefire.api.util.internal.StringUtils;
import static org.apache.maven.surefire.shared.utils.StringUtils.isNotEmpty;

import java.io.PrintWriter;
import java.io.StringWriter;
Expand All @@ -33,8 +34,6 @@
public class LegacyPojoStackTraceWriter
implements StackTraceWriter
{
private static final int MAX_LINE_LENGTH = 77;

private final Throwable t;

private final String testClass;
Expand Down Expand Up @@ -81,19 +80,19 @@ public String smartTrimmedStackTrace()
result.append( "#" );
result.append( testMethod );
SafeThrowable throwable = getThrowable();
if ( throwable.getTarget() instanceof AssertionError )
{
result.append( " " );
result.append( getTruncatedMessage( throwable.getMessage(), MAX_LINE_LENGTH - result.length() ) );
}
else
Throwable target = throwable.getTarget();
if ( target != null )
{
Throwable target = throwable.getTarget();
if ( target != null )
if ( ! ( target instanceof AssertionError ) )
{
result.append( ' ' )
.append( target.getClass().getSimpleName() );
}
final String msg = throwable.getMessage();
if ( isNotEmpty( msg ) )
{
result.append( " " );
result.append( target.getClass().getSimpleName() );
result.append( getTruncatedMessage( throwable.getMessage(), MAX_LINE_LENGTH - result.length() ) );
result.append( ' ' )
.append( msg );
}
}
return result.toString();
Expand All @@ -120,28 +119,6 @@ private static boolean isMultiLineExceptionMessage( Throwable t )
return false;
}

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;
}
}


@Override
public String writeTrimmedTraceToString()
{
Expand Down
Expand Up @@ -25,7 +25,6 @@
import java.util.Collections;
import java.util.List;

import static java.lang.Math.min;
import static java.util.Arrays.asList;
import static java.util.Collections.reverse;
import static org.apache.maven.surefire.shared.utils.StringUtils.chompLast;
Expand All @@ -37,8 +36,6 @@
@SuppressWarnings( "ThrowableResultOfMethodCallIgnored" )
public class SmartStackTraceParser
{
private static final int MAX_LINE_LENGTH = 77;

private final SafeThrowable throwable;

private final StackTraceElement[] stackTrace;
Expand Down Expand Up @@ -133,23 +130,19 @@ public String getString()
final String excClassName = excType.getName();
final String msg = throwable.getMessage();

if ( target instanceof AssertionError
if ( ! ( target instanceof AssertionError
|| "junit.framework.AssertionFailedError".equals( excClassName )
|| "junit.framework.ComparisonFailure".equals( excClassName )
|| excClassName.startsWith( "org.opentest4j." ) )
{
if ( isNotEmpty( msg ) )
{
result.append( ' ' )
.append( msg );
}
}
else
|| excClassName.startsWith( "org.opentest4j." ) ) )
{
result.append( rootIsInclass() ? " " : " » " )
.append( toMinimalThrowableMiniMessage( excType ) );
.append( toMinimalThrowableMiniMessage( excType ) );
}

result.append( truncateMessage( msg, MAX_LINE_LENGTH - result.length() ) );
if ( isNotEmpty( msg ) )
{
result.append( ' ' )
.append( msg );
}
return result.toString();
}
Expand All @@ -168,22 +161,6 @@ private static String toMinimalThrowableMiniMessage( Class<?> excType )
return name;
}

private static String truncateMessage( String msg, int i )
{
StringBuilder truncatedMessage = new StringBuilder();
if ( i >= 0 && msg != null )
{
truncatedMessage.append( ' ' )
.append( msg, 0, min( i, msg.length() ) );

if ( i < msg.length() )
{
truncatedMessage.append( "..." );
}
}
return truncatedMessage.toString();
}

private boolean rootIsInclass()
{
return stackTrace != null && stackTrace.length > 0 && stackTrace[0].getClassName().equals( testClassName );
Expand Down
Expand Up @@ -60,7 +60,7 @@ public void nestedNpeOutsideTest()

public void aLongTestErrorMessage()
{
throw new RuntimeException( "This message will be truncated, somewhere over the rainbow. "
throw new RuntimeException( "This message won't be truncated, somewhere over the rainbow. "
+ "Gangnam style, Gangnam style, Gangnam style, , Gangnam style, Gangnam style" );
}

Expand Down
Expand Up @@ -136,7 +136,7 @@ public void testNestedNpeOutsideTest()
}
}

public void testLongMessageTruncation()
public void testLongMessageHandling()
{
ATestClass aTestClass = new ATestClass();
try
Expand All @@ -148,7 +148,7 @@ public void testLongMessageTruncation()
SmartStackTraceParser smartStackTraceParser =
new SmartStackTraceParser( ATestClass.class.getName(), e, null );
String res = smartStackTraceParser.getString();
assertEquals( "ATestClass.aLongTestErrorMessage:63 Runtime This message will be truncated, so...",
assertEquals( "ATestClass.aLongTestErrorMessage:63 Runtime " + e.getMessage(),
res );
}
}
Expand Down

0 comments on commit 89cffc2

Please sign in to comment.