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.junit.runner.Description;
23  import org.junit.runner.notification.Failure;
24  import org.junit.runner.notification.RunListener;
25  import org.junit.runner.notification.RunNotifier;
26  import org.junit.runner.notification.StoppedByUserException;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.Iterator;
31  import java.util.Queue;
32  import java.util.concurrent.ConcurrentLinkedQueue;
33  import java.util.concurrent.atomic.AtomicInteger;
34  
35  import static org.apache.maven.surefire.common.junit4.JUnit4ProviderUtil.cutTestClassAndMethod;
36  import static org.apache.maven.surefire.util.internal.ConcurrencyUtils.countDownToZero;
37  
38  /**
39   * Extends {@link RunNotifier JUnit notifier},
40   * encapsulates several different types of {@link RunListener JUnit listeners}, and
41   * fires events to listeners.
42   *
43   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
44   * @since 2.19
45   */
46  public class Notifier
47      extends RunNotifier
48  {
49      private final Collection<RunListener> listeners = new ArrayList<RunListener>();
50  
51      private final Queue<String> testClassNames = new ConcurrentLinkedQueue<String>();
52  
53      private final AtomicInteger skipAfterFailureCount;
54  
55      private final JUnit4RunListener reporter;
56  
57      private volatile boolean failFast;
58  
59      public Notifier( JUnit4RunListener reporter, int skipAfterFailureCount )
60      {
61          addListener( reporter );
62          this.reporter = reporter;
63          this.skipAfterFailureCount = new AtomicInteger( skipAfterFailureCount );
64      }
65  
66      private Notifier()
67      {
68          reporter = null;
69          skipAfterFailureCount = null;
70      }
71  
72      public static Notifier pureNotifier()
73      {
74          return new Notifier()
75          {
76              @Override
77              public void asFailFast( @SuppressWarnings( { "unused", "checkstyle:hiddenfieldcheck" } ) boolean failFast )
78              {
79                  throw new UnsupportedOperationException( "pure notifier" );
80              }
81          };
82      }
83  
84      public void asFailFast( boolean enableFailFast )
85      {
86          failFast = enableFailFast;
87      }
88  
89      public final boolean isFailFast()
90      {
91          return failFast;
92      }
93  
94      @Override
95      @SuppressWarnings( "checkstyle:redundantthrowscheck" ) // checkstyle is wrong here, see super.fireTestStarted()
96      public final void fireTestStarted( Description description ) throws StoppedByUserException
97      {
98          // If fireTestStarted() throws exception (== skipped test), the class must not be removed from testClassNames.
99          // Therefore this class will be removed only if test class started with some test method.
100         super.fireTestStarted( description );
101         if ( !testClassNames.isEmpty() )
102         {
103             testClassNames.remove( cutTestClassAndMethod( description ).getClazz() );
104         }
105     }
106 
107     @Override
108     public final void fireTestFailure( Failure failure )
109     {
110         if ( failFast )
111         {
112             fireStopEvent();
113         }
114         super.fireTestFailure( failure );
115     }
116 
117     @Override
118     public final void addListener( RunListener listener )
119     {
120         listeners.add( listener );
121         super.addListener( listener );
122     }
123 
124     public final Notifier addListeners( Collection<RunListener> given )
125     {
126         for ( RunListener listener : given )
127         {
128             addListener( listener );
129         }
130         return this;
131     }
132 
133     @SuppressWarnings( "unused" )
134     public final Notifier addListeners( RunListener... given )
135     {
136         for ( RunListener listener : given )
137         {
138             addListener( listener );
139         }
140         return this;
141     }
142 
143     @Override
144     public final void removeListener( RunListener listener )
145     {
146         listeners.remove( listener );
147         super.removeListener( listener );
148     }
149 
150     public final void removeListeners()
151     {
152         for ( Iterator<RunListener> it = listeners.iterator(); it.hasNext(); )
153         {
154             RunListener listener = it.next();
155             it.remove();
156             super.removeListener( listener );
157         }
158     }
159 
160     public final Queue<String> getRemainingTestClasses()
161     {
162         return failFast ? testClassNames : null;
163     }
164 
165     public final void copyListenersTo( Notifier copyTo )
166     {
167         copyTo.addListeners( listeners );
168     }
169 
170     /**
171      * Fire stop even to plugin process and/or call {@link org.junit.runner.notification.RunNotifier#pleaseStop()}.
172      */
173     private void fireStopEvent()
174     {
175         if ( countDownToZero( skipAfterFailureCount ) )
176         {
177             pleaseStop();
178         }
179 
180         reporter.testExecutionSkippedByUser();
181     }
182 }