View Javadoc

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      protected String getTestMethodName()
67      {
68          return JUnit4RunListener.extractMethodName( junitFailure.getDescription() );
69      }
70  
71      @SuppressWarnings( "ThrowableResultOfMethodCallIgnored" )
72      public String smartTrimmedStackTrace()
73      {
74          Throwable exception = junitFailure.getException();
75          if ( exception != null )
76          {
77              SmartStackTraceParser smartStackTraceParser = new SmartStackTraceParser( getTestClassName(), exception,
78                                                                                       getTestMethodName() );
79              return smartStackTraceParser.getString();
80          }
81          else
82          {
83              return junitFailure.getMessage();
84          }
85      }
86  
87      /**
88       * At the moment, returns the same as {@link #writeTraceToString()}.
89       *
90       * @see org.apache.maven.surefire.report.StackTraceWriter#writeTrimmedTraceToString()
91       */
92      public String writeTrimmedTraceToString()
93      {
94          String testClass = getTestClassName();
95          try
96          {
97              return SmartStackTraceParser.innerMostWithFocusOnClass( junitFailure.getException(), testClass );
98          }
99          catch ( Throwable t )
100         {
101             return SmartStackTraceParser.innerMostWithFocusOnClass( t, testClass );
102         }
103     }
104 
105     /**
106      * Returns the exception associated with this failure.
107      *
108      * @see org.apache.maven.surefire.report.StackTraceWriter#getThrowable()
109      */
110     public SafeThrowable getThrowable()
111     {
112         return new SafeThrowable( junitFailure.getException() );
113     }
114 
115 }