1 package org.apache.maven.surefire.common.junit4;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
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
26 import org.junit.runner.notification.Failure;
27
28 /**
29 * Writes out a specific {@link org.junit.runner.notification.Failure} for
30 * surefire as a stacktrace.
31 *
32 * @author Karl M. Davis
33 */
34 public class JUnit4StackTraceWriter
35 implements StackTraceWriter
36 {
37 // Member Variables
38 protected final Failure junitFailure;
39
40 /**
41 * Constructor.
42 *
43 * @param junitFailure the {@link Failure} that this will be operating on
44 */
45 public JUnit4StackTraceWriter( Failure junitFailure )
46 {
47 this.junitFailure = junitFailure;
48 }
49
50 /*
51 * (non-Javadoc)
52 *
53 * @see org.apache.maven.surefire.report.StackTraceWriter#writeTraceToString()
54 */
55 public String writeTraceToString()
56 {
57 return junitFailure.getTrace();
58 }
59
60
61 protected String getTestClassName()
62 {
63 return JUnit4RunListener.extractClassName( junitFailure.getDescription() );
64 }
65
66 @SuppressWarnings( "ThrowableResultOfMethodCallIgnored" )
67 public String smartTrimmedStackTrace()
68 {
69 Throwable exception = junitFailure.getException();
70 if ( exception != null )
71 {
72 SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( getTestClassName(), exception );
73 return smartStackTraceParser.getString();
74 }
75 else
76 {
77 return junitFailure.getMessage();
78 }
79 }
80
81 /**
82 * At the moment, returns the same as {@link #writeTraceToString()}.
83 *
84 * @see org.apache.maven.surefire.report.StackTraceWriter#writeTrimmedTraceToString()
85 */
86 public String writeTrimmedTraceToString()
87 {
88 String testClass = getTestClassName();
89 try
90 {
91 return SmartStackTraceParser.innerMostWithFocusOnClass( junitFailure.getException(), testClass );
92 }
93 catch ( Throwable t )
94 {
95 return SmartStackTraceParser.innerMostWithFocusOnClass( t, testClass );
96 }
97 }
98
99 /**
100 * Returns the exception associated with this failure.
101 *
102 * @see org.apache.maven.surefire.report.StackTraceWriter#getThrowable()
103 */
104 public SafeThrowable getThrowable()
105 {
106 return new SafeThrowable( junitFailure.getException() );
107 }
108
109 }