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 java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  import org.apache.maven.surefire.report.ReportEntry;
25  import org.apache.maven.surefire.report.RunListener;
26  import org.apache.maven.surefire.report.SimpleReportEntry;
27  
28  import org.junit.runner.Description;
29  import org.junit.runner.notification.Failure;
30  
31  public class JUnit4RunListener
32      extends org.junit.runner.notification.RunListener
33  {
34      private static final Pattern PARENS = Pattern.compile( "^" + "[^\\(\\)]+" //non-parens
35                                                                 + "\\((" // then an open-paren (start matching a group)
36                                                                 + "[^\\\\(\\\\)]+" //non-parens
37                                                                 + ")\\)" + "$" ); // then a close-paren (end group match)
38  
39      protected final RunListener reporter;
40  
41      /**
42       * This flag is set after a failure has occurred so that a <code>testSucceeded</code> event is not fired.
43       * This is necessary because JUnit4 always fires a <code>testRunFinished</code> event-- even if there was a failure.
44       */
45      private final ThreadLocal<Boolean> failureFlag = new InheritableThreadLocal<Boolean>();
46  
47      /**
48       * Constructor.
49       *
50       * @param reporter the reporter to log testing events to
51       */
52      public JUnit4RunListener( RunListener reporter )
53      {
54          this.reporter = reporter;
55      }
56  
57      // Testrun methods are not invoked when using the runner
58  
59      /**
60       * Called when a specific test has been skipped (for whatever reason).
61       *
62       * @see org.junit.runner.notification.RunListener#testIgnored(org.junit.runner.Description)
63       */
64      public void testIgnored( Description description )
65          throws Exception
66      {
67          reporter.testSkipped( createReportEntry( description ) );
68      }
69  
70      /**
71       * Called when a specific test has started.
72       *
73       * @see org.junit.runner.notification.RunListener#testStarted(org.junit.runner.Description)
74       */
75      public void testStarted( Description description )
76          throws Exception
77      {
78          reporter.testStarting( createReportEntry( description ) );
79          failureFlag.remove();
80      }
81  
82      /**
83       * Called when a specific test has failed.
84       *
85       * @see org.junit.runner.notification.RunListener#testFailure(org.junit.runner.notification.Failure)
86       */
87      @SuppressWarnings( { "ThrowableResultOfMethodCallIgnored" } )
88      public void testFailure( Failure failure )
89          throws Exception
90      {
91          ReportEntry report =
92              new SimpleReportEntry( extractClassName( failure.getDescription() ), failure.getTestHeader(),
93                                     new JUnit4StackTraceWriter( failure ) );
94  
95          if ( failure.getException() instanceof AssertionError )
96          {
97              this.reporter.testFailed( report );
98          }
99          else
100         {
101             this.reporter.testError( report );
102         }
103         failureFlag.set( Boolean.TRUE );
104     }
105 
106     @SuppressWarnings( { "UnusedDeclaration" } )
107     public void testAssumptionFailure( Failure failure )
108     {
109         this.reporter.testAssumptionFailure( createReportEntry( failure.getDescription() ) );
110         failureFlag.set( Boolean.TRUE );
111     }
112 
113 
114     /**
115      * Called after a specific test has finished.
116      *
117      * @see org.junit.runner.notification.RunListener#testFinished(org.junit.runner.Description)
118      */
119     public void testFinished( Description description )
120         throws Exception
121     {
122         Boolean failure = failureFlag.get();
123         if ( failure == null )
124         {
125             reporter.testSucceeded( createReportEntry( description ) );
126         }
127     }
128 
129     private SimpleReportEntry createReportEntry( Description description )
130     {
131         return new SimpleReportEntry( extractClassName( description ), description.getDisplayName() );
132     }
133 
134 
135     String extractClassName( Description description )
136     {
137         String displayName = description.getDisplayName();
138         Matcher m = PARENS.matcher( displayName );
139         if ( !m.find() )
140         {
141             return displayName;
142         }
143         return m.group( 1 );
144     }
145 }