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.notification.RunListener;
23  import org.junit.runner.notification.RunNotifier;
24  
25  import java.util.ArrayList;
26  import java.util.Collection;
27  import java.util.Iterator;
28  import java.util.concurrent.atomic.AtomicInteger;
29  
30  import static org.apache.maven.surefire.util.internal.ConcurrencyUtils.countDownToZero;
31  
32  /**
33   * Extends {@link RunNotifier JUnit notifier},
34   * encapsulates several different types of {@link RunListener JUnit listeners}, and
35   * fires events to listeners.
36   *
37   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
38   * @since 2.19
39   */
40  public class Notifier
41      extends RunNotifier implements Stoppable
42  {
43      private final Collection<RunListener> listeners = new ArrayList<RunListener>();
44  
45      private final AtomicInteger skipAfterFailureCount;
46  
47      private final JUnit4RunListener reporter;
48  
49      public Notifier( JUnit4RunListener reporter, int skipAfterFailureCount )
50      {
51          addListener( reporter );
52          this.reporter = reporter;
53          this.skipAfterFailureCount = new AtomicInteger( skipAfterFailureCount );
54      }
55  
56      public void fireStopEvent()
57      {
58          if ( countDownToZero( skipAfterFailureCount ) )
59          {
60              pleaseStop();
61          }
62  
63          reporter.testExecutionSkippedByUser();
64      }
65  
66      @Override
67      public void addListener( RunListener listener )
68      {
69          listeners.add( listener );
70          super.addListener( listener );
71      }
72  
73      public Notifier addListeners( Collection<RunListener> given )
74      {
75          for ( RunListener listener : given )
76          {
77              addListener( listener );
78          }
79          return this;
80      }
81  
82      public Notifier addListeners( RunListener... given )
83      {
84          for ( RunListener listener : given )
85          {
86              addListener( listener );
87          }
88          return this;
89      }
90  
91      @Override
92      public void removeListener( RunListener listener )
93      {
94          listeners.remove( listener );
95          super.removeListener( listener );
96      }
97  
98      public void removeListeners()
99      {
100         for ( Iterator<RunListener> it = listeners.iterator(); it.hasNext(); )
101         {
102             RunListener listener = it.next();
103             it.remove();
104             super.removeListener( listener );
105         }
106     }
107 }