This is an automated email from the ASF dual-hosted git repository. tibordigana pushed a commit to branch SUREFIRE-1017 in repository https://gitbox.apache.org/repos/asf/maven-surefire.git
commit df64ff2cf080306def286d39a24d8cce00827a89 Author: Tibor17 <[email protected]> AuthorDate: Wed Dec 12 00:20:00 2018 +0100 added more unit tests in SmartStackTraceParserTest and refactoring --- .../surefire/report/SmartStackTraceParser.java | 112 +++++++++------------ .../maven/surefire/report/OutermostClass.java | 27 +++++ .../surefire/report/SmartStackTraceParserTest.java | 76 +++++++++----- 3 files changed, 129 insertions(+), 86 deletions(-) diff --git a/surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java b/surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java index 6ebb2d4..6081a75 100644 --- a/surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java +++ b/surefire-providers/common-java5/src/main/java/org/apache/maven/surefire/report/SmartStackTraceParser.java @@ -20,10 +20,11 @@ package org.apache.maven.surefire.report; */ import java.util.ArrayList; -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.shared.utils.StringUtils.chompLast; import static org.apache.maven.shared.utils.StringUtils.isNotEmpty; @@ -39,35 +40,27 @@ public class SmartStackTraceParser private final StackTraceElement[] stackTrace; - private final String simpleName; - private final String testClassName; - private final Class testClass; + private final Class<?> testClass; private final String testMethodName; - public SmartStackTraceParser( Class testClass, Throwable throwable ) - { - this( testClass.getName(), throwable, null ); - } - public SmartStackTraceParser( String testClassName, Throwable throwable, String testMethodName ) { this.testMethodName = testMethodName; this.testClassName = testClassName; - testClass = getClass( testClassName ); - simpleName = testClassName.substring( testClassName.lastIndexOf( "." ) + 1 ); + testClass = toClass( testClassName ); this.throwable = new SafeThrowable( throwable ); stackTrace = throwable.getStackTrace(); } - private static Class getClass( String name ) + private static Class<?> toClass( String name ) { try { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); - return classLoader != null ? classLoader.loadClass( name ) : null; + return classLoader == null ? null : classLoader.loadClass( name ); } catch ( ClassNotFoundException e ) { @@ -75,7 +68,7 @@ public class SmartStackTraceParser } } - private static String getSimpleName( String className ) + private static String toSimpleClassName( String className ) { int i = className.lastIndexOf( "." ); return className.substring( i + 1 ); @@ -91,10 +84,11 @@ public class SmartStackTraceParser final StringBuilder result = new StringBuilder(); final List<StackTraceElement> stackTraceElements = focusOnClass( stackTrace, testClass ); - Collections.reverse( stackTraceElements ); + reverse( stackTraceElements ); + final String testClassSimpleName = toSimpleClassName( testClassName ); if ( stackTraceElements.isEmpty() ) { - result.append( simpleName ); + result.append( testClassSimpleName ); if ( isNotEmpty( testMethodName ) ) { result.append( "." ) @@ -103,64 +97,62 @@ public class SmartStackTraceParser } else { - for ( int i = 0; i < stackTraceElements.size(); i++ ) + for ( int i = 0, size = stackTraceElements.size(); i < size; i++ ) { final StackTraceElement stackTraceElement = stackTraceElements.get( i ); + final boolean isTestClassName = stackTraceElement.getClassName().equals( testClassName ); if ( i == 0 ) { - result.append( simpleName ); - if ( !stackTraceElement.getClassName().equals( testClassName ) ) - { - result.append( ">" ); - } - else - { - result.append( "." ); - } + result.append( testClassSimpleName ) + .append( isTestClassName ? '.' : '>' ); } - if ( !stackTraceElement.getClassName().equals( testClassName ) ) + + if ( !isTestClassName ) { - result.append( getSimpleName( stackTraceElement.getClassName() ) ) // Add the name of the superclas - .append( "." ); + result.append( toSimpleClassName( stackTraceElement.getClassName() ) ) + .append( '.' ); } + result.append( stackTraceElement.getMethodName() ) - .append( ":" ) + .append( ':' ) .append( stackTraceElement.getLineNumber() ) .append( "->" ); } if ( result.length() >= 2 ) { - result.deleteCharAt( result.length() - 1 ) - .deleteCharAt( result.length() - 1 ); + result.setLength( result.length() - 2 ); } } - Throwable target = throwable.getTarget(); - String exception = target.getClass().getName(); + final Throwable target = throwable.getTarget(); + final Class<?> excType = target.getClass(); + final String excClassName = excType.getName(); + final String msg = throwable.getMessage(); + if ( target instanceof AssertionError - || "junit.framework.AssertionFailedError".equals( exception ) - || "junit.framework.ComparisonFailure".equals( exception ) ) + || "junit.framework.AssertionFailedError".equals( excClassName ) + || "junit.framework.ComparisonFailure".equals( excClassName ) ) { - String msg = throwable.getMessage(); if ( isNotEmpty( msg ) ) { - result.append( " " ) + result.append( ' ' ) .append( msg ); } } else { - result.append( rootIsInclass() ? " " : " » " ); - result.append( getMinimalThrowableMiniMessage( target ) ); - result.append( getTruncatedMessage( MAX_LINE_LENGTH - result.length() ) ); + result.append( rootIsInclass() ? " " : " » " ) + .append( toMinimalThrowableMiniMessage( excType ) ); + + result.append( truncateMessage( msg, MAX_LINE_LENGTH - result.length() ) ); } return result.toString(); } - private static String getMinimalThrowableMiniMessage( Throwable throwable ) + private static String toMinimalThrowableMiniMessage( Class<?> excType ) { - String name = throwable.getClass().getSimpleName(); + String name = excType.getSimpleName(); if ( name.endsWith( "Exception" ) ) { return chompLast( name, "Exception" ); @@ -172,26 +164,20 @@ public class SmartStackTraceParser return name; } - private String getTruncatedMessage( int i ) + private static String truncateMessage( String msg, int i ) { - if ( i < 0 ) - { - return ""; - } - String msg = throwable.getMessage(); - if ( msg == null ) - { - return ""; - } - String substring = msg.substring( 0, Math.min( i, msg.length() ) ); - if ( i < msg.length() ) + StringBuilder truncatedMessage = new StringBuilder(); + if ( i >= 0 && msg != null ) { - return " " + substring + "..."; - } - else - { - return " " + substring; + truncatedMessage.append( ' ' ) + .append( msg.substring( 0, min( i, msg.length() ) ) ); + + if ( i < msg.length() ) + { + truncatedMessage.append( "..." ); + } } + return truncatedMessage.toString(); } private boolean rootIsInclass() @@ -199,7 +185,7 @@ public class SmartStackTraceParser return stackTrace.length > 0 && stackTrace[0].getClassName().equals( testClassName ); } - static List<StackTraceElement> focusOnClass( StackTraceElement[] stackTrace, Class clazz ) + private static List<StackTraceElement> focusOnClass( StackTraceElement[] stackTrace, Class<?> clazz ) { List<StackTraceElement> result = new ArrayList<>(); for ( StackTraceElement element : stackTrace ) @@ -212,7 +198,7 @@ public class SmartStackTraceParser return result; } - private static boolean isInSupers( Class testClass, String lookFor ) + private static boolean isInSupers( Class<?> testClass, String lookFor ) { if ( lookFor.startsWith( "junit.framework." ) ) { @@ -263,7 +249,7 @@ public class SmartStackTraceParser return result; } - static boolean containsClassName( StackTraceElement[] stackTrace, StackTraceFilter filter ) + private static boolean containsClassName( StackTraceElement[] stackTrace, StackTraceFilter filter ) { for ( StackTraceElement element : stackTrace ) { diff --git a/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/OutermostClass.java b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/OutermostClass.java new file mode 100644 index 0000000..be9525e --- /dev/null +++ b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/OutermostClass.java @@ -0,0 +1,27 @@ +package org.apache.maven.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. + */ + +class OutermostClass { + void junit() + { + new ATestClass().failInAssert(); + } +} diff --git a/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java index d450219..f057ce5 100644 --- a/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java +++ b/surefire-providers/common-java5/src/test/java/org/apache/maven/surefire/report/SmartStackTraceParserTest.java @@ -36,7 +36,6 @@ public class SmartStackTraceParserTest extends TestCase { public void testGetString() - throws Exception { ATestClass aTestClass = new ATestClass(); try @@ -45,16 +44,45 @@ public class SmartStackTraceParserTest } catch ( AssertionError e ) { - SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e ); + SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class.getName(), e, null ); String res = smartStackTraceParser.getString(); assertEquals( "ATestClass.failInAssert:30 X is not Z", res ); + } + } + public void testGetStringFromNested() + { + OutermostClass aTestClass = new OutermostClass(); + try + { + aTestClass.junit(); } + catch ( AssertionError e ) + { + SmartStackTraceParser smartStackTraceParser = + new SmartStackTraceParser( ATestClass.class.getName(), e, null ); + String res = smartStackTraceParser.getString(); + assertEquals( "ATestClass.failInAssert:30 X is not Z", res ); + } + } + public void testGetStringWithMethod() + { + OutermostClass aTestClass = new OutermostClass(); + try + { + aTestClass.junit(); + } + catch ( AssertionError e ) + { + SmartStackTraceParser smartStackTraceParser = + new SmartStackTraceParser( InnerATestClass.class.getName(), e, "myMethod" ); + String res = smartStackTraceParser.getString(); + assertEquals( "InnerATestClass.myMethod X is not Z", res ); + } } public void testNestedFailure() - throws Exception { ATestClass aTestClass = new ATestClass(); try @@ -63,14 +91,14 @@ public class SmartStackTraceParserTest } catch ( AssertionError e ) { - SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e ); + SmartStackTraceParser smartStackTraceParser = + new SmartStackTraceParser( ATestClass.class.getName(), e, null ); String res = smartStackTraceParser.getString(); assertEquals( "ATestClass.nestedFailInAssert:35->failInAssert:30 X is not Z", res ); } } public void testNestedNpe() - throws Exception { ATestClass aTestClass = new ATestClass(); try @@ -79,15 +107,14 @@ public class SmartStackTraceParserTest } catch ( NullPointerException e ) { - SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e ); + SmartStackTraceParser smartStackTraceParser = + new SmartStackTraceParser( ATestClass.class.getName(), e, null ); String res = smartStackTraceParser.getString(); assertEquals( "ATestClass.nestedNpe:45->npe:40 NullPointer It was null", res ); - } } public void testNestedNpeOutsideTest() - throws Exception { ATestClass aTestClass = new ATestClass(); try @@ -96,15 +123,14 @@ public class SmartStackTraceParserTest } catch ( NullPointerException e ) { - SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e ); + SmartStackTraceParser smartStackTraceParser = + new SmartStackTraceParser( ATestClass.class.getName(), e, null ); String res = smartStackTraceParser.getString(); assertEquals( "ATestClass.nestedNpeOutsideTest:55->npeOutsideTest:50 » NullPointer", res ); - } } public void testLongMessageTruncation() - throws Exception { ATestClass aTestClass = new ATestClass(); try @@ -113,15 +139,15 @@ public class SmartStackTraceParserTest } catch ( RuntimeException e ) { - SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e ); + SmartStackTraceParser smartStackTraceParser = + new SmartStackTraceParser( ATestClass.class.getName(), e, null ); String res = smartStackTraceParser.getString(); - assertEquals( "ATestClass.aLongTestErrorMessage:60 Runtime This message will be truncated, so...", res ); - + assertEquals( "ATestClass.aLongTestErrorMessage:60 Runtime This message will be truncated, so...", + res ); } } public void testFailureInBaseClass() - throws Exception { ASubClass aTestClass = new ASubClass(); try @@ -130,14 +156,14 @@ public class SmartStackTraceParserTest } catch ( NullPointerException e ) { - SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ASubClass.class, e ); + SmartStackTraceParser smartStackTraceParser = + new SmartStackTraceParser( ASubClass.class.getName(), e, null ); String res = smartStackTraceParser.getString(); assertEquals( "ASubClass>ABaseClass.npe:27 » NullPointer It was null", res ); } } public void testClassThatWillFail() - throws Exception { CaseThatWillFail aTestClass = new CaseThatWillFail(); try @@ -146,13 +172,14 @@ public class SmartStackTraceParserTest } catch ( ComparisonFailure e ) { - SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( CaseThatWillFail.class, e ); + SmartStackTraceParser smartStackTraceParser = + new SmartStackTraceParser( CaseThatWillFail.class.getName(), e, null ); String res = smartStackTraceParser.getString(); assertEquals( "CaseThatWillFail.testThatWillFail:29 expected:<[abc]> but was:<[def]>", res ); } } - public Throwable getAThrownException() + private static Throwable getAThrownException() { try { @@ -186,7 +213,8 @@ public class SmartStackTraceParserTest } catch ( ComparisonFailure e ) { - SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( AssertionNoMessage.class, e ); + SmartStackTraceParser smartStackTraceParser = + new SmartStackTraceParser( AssertionNoMessage.class.getName(), e, null ); String res = smartStackTraceParser.getString(); assertEquals( "AssertionNoMessage.testThrowSomething:29 expected:<[abc]> but was:<[xyz]>", res ); } @@ -200,7 +228,8 @@ public class SmartStackTraceParserTest } catch ( AssertionFailedError e ) { - SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( FailWithFail.class, e ); + SmartStackTraceParser smartStackTraceParser = + new SmartStackTraceParser( FailWithFail.class.getName(), e, null ); String res = smartStackTraceParser.getString(); assertEquals( "FailWithFail.testThatWillFail:29 abc", res ); } @@ -243,7 +272,8 @@ public class SmartStackTraceParserTest } catch ( AssertionError e ) { - SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( ATestClass.class, e ); + SmartStackTraceParser smartStackTraceParser = + new SmartStackTraceParser( ATestClass.class.getName(), e, null ); Field stackTrace = SmartStackTraceParser.class.getDeclaredField( "stackTrace" ); stackTrace.setAccessible( true ); stackTrace.set( smartStackTraceParser, new StackTraceElement[0] ); @@ -303,7 +333,7 @@ public class SmartStackTraceParserTest } } - public ExecutionException getSingleNested() + private ExecutionException getSingleNested() { FutureTask<Object> futureTask = new FutureTask<>( new RunnableTestClass2() ); DaemonThreadFactory.newDaemonThread( futureTask ).start();
