1 package org.apache.maven.surefire.common.junit4;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.surefire.report.SafeThrowable;
23 import org.apache.maven.surefire.report.SmartStackTraceParser;
24 import org.apache.maven.surefire.report.StackTraceWriter;
25 import org.junit.runner.notification.Failure;
26
27 import static org.apache.maven.surefire.util.internal.TestClassMethodNameUtils.extractClassName;
28 import static org.apache.maven.surefire.util.internal.TestClassMethodNameUtils.extractMethodName;
29 import static org.apache.maven.surefire.report.SmartStackTraceParser.stackTraceWithFocusOnClassAsString;
30
31
32
33
34
35
36
37 public class JUnit4StackTraceWriter
38 implements StackTraceWriter
39 {
40
41 protected final Failure junitFailure;
42
43
44
45
46
47
48 public JUnit4StackTraceWriter( Failure junitFailure )
49 {
50 this.junitFailure = junitFailure;
51 }
52
53
54
55
56
57
58 @Override
59 public String writeTraceToString()
60 {
61 Throwable t = junitFailure.getException();
62 if ( t != null )
63 {
64 String originalTrace = junitFailure.getTrace();
65 if ( isMultiLineExceptionMessage( t ) )
66 {
67
68 StringBuilder builder = new StringBuilder( originalTrace );
69 String exc = t.getClass().getName() + ": ";
70 if ( originalTrace.startsWith( exc ) )
71 {
72 builder.insert( exc.length(), '\n' );
73 }
74 return builder.toString();
75 }
76 return originalTrace;
77 }
78 return "";
79 }
80
81 protected String getTestClassName()
82 {
83 return extractClassName( junitFailure.getDescription().getDisplayName() );
84 }
85
86 protected String getTestMethodName()
87 {
88 return extractMethodName( junitFailure.getDescription().getDisplayName() );
89 }
90
91 @Override
92 @SuppressWarnings( "ThrowableResultOfMethodCallIgnored" )
93 public String smartTrimmedStackTrace()
94 {
95 Throwable exception = junitFailure.getException();
96 return exception == null
97 ? junitFailure.getMessage()
98 : new SmartStackTraceParser( getTestClassName(), exception, getTestMethodName() ).getString();
99 }
100
101
102
103
104
105
106 @Override
107 public String writeTrimmedTraceToString()
108 {
109 String testClass = getTestClassName();
110 try
111 {
112 Throwable e = junitFailure.getException();
113 return stackTraceWithFocusOnClassAsString( e, testClass );
114 }
115 catch ( Throwable t )
116 {
117 return stackTraceWithFocusOnClassAsString( t, testClass );
118 }
119 }
120
121
122
123
124
125
126 @Override
127 public SafeThrowable getThrowable()
128 {
129 return new SafeThrowable( junitFailure.getException() );
130 }
131
132 private static boolean isMultiLineExceptionMessage( Throwable t )
133 {
134 String msg = t.getLocalizedMessage();
135 if ( msg != null )
136 {
137 int countNewLines = 0;
138 for ( int i = 0, length = msg.length(); i < length; i++ )
139 {
140 if ( msg.charAt( i ) == '\n' )
141 {
142 if ( ++countNewLines == 2 )
143 {
144 break;
145 }
146 }
147 }
148 return countNewLines > 1 || countNewLines == 1 && !msg.trim().endsWith( "\n" );
149 }
150 return false;
151 }
152
153 }