View Javadoc
1   package org.apache.maven.surefire.junitcore;
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  /*
23   * Licensed to the Apache Software Foundation (ASF) under one
24   * or more contributor license agreements.  See the NOTICE file
25   * distributed with this work for additional information
26   * regarding copyright ownership.  The ASF licenses this file
27   * to you under the Apache License, Version 2.0 (the
28   * "License"); you may not use this file except in compliance
29   * with the License.  You may obtain a copy of the License at
30   *
31   *     http://www.apache.org/licenses/LICENSE-2.0
32   *
33   * Unless required by applicable law or agreed to in writing,
34   * software distributed under the License is distributed on an
35   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
36   * KIND, either express or implied.  See the License for the
37   * specific language governing permissions and limitations
38   * under the License.
39   *
40   * Also licensed under CPL http://junit.sourceforge.net/cpl-v10.html
41   */
42  
43  import java.util.concurrent.atomic.AtomicInteger;
44  
45  import org.junit.runner.Description;
46  import org.junit.runner.Result;
47  import org.junit.runner.notification.Failure;
48  import org.junit.runner.notification.RunListener;
49  
50  /**
51   * @author Kristian Rosenvold, kristianAzeniorD0Tno
52   */
53  public class DiagnosticRunListener
54      extends RunListener
55  {
56      private final AtomicInteger numTestStarted = new AtomicInteger();
57  
58      private final AtomicInteger numTestFailed = new AtomicInteger();
59  
60      private final AtomicInteger numTestAssumptionsFailed = new AtomicInteger();
61  
62      private final AtomicInteger numTestFinished = new AtomicInteger();
63  
64      private final AtomicInteger numTestIgnored = new AtomicInteger();
65  
66      private final boolean printToConsole;
67  
68      private final RunListener target;
69  
70  
71      private void print( String event, Description description )
72      {
73          if ( printToConsole )
74          {
75              System.out.println( Thread.currentThread().toString() + ", event = " + event + ", " + description );
76          }
77      }
78  
79      private void print( String event, Result description )
80      {
81          if ( printToConsole )
82          {
83              System.out.println( Thread.currentThread().toString() + ", event = " + event + ", " + description );
84          }
85      }
86  
87      private void print( String event, Failure description )
88      {
89          if ( printToConsole )
90          {
91              System.out.println( Thread.currentThread().toString() + ", event = " + event + ", " + description );
92          }
93      }
94  
95      public DiagnosticRunListener( boolean printToConsole, RunListener target )
96      {
97          this.printToConsole = printToConsole;
98          this.target = target;
99      }
100 
101     @Override
102     public void testRunStarted( Description description )
103         throws Exception
104     {
105         print( "testRunStarted", description );
106         if ( target != null )
107         {
108             target.testRunStarted( description );
109         }
110     }
111 
112     @Override
113     public void testRunFinished( Result result )
114         throws Exception
115     {
116         print( "testRunFinished", result );
117         if ( target != null )
118         {
119             target.testRunFinished( result );
120         }
121     }
122 
123     @Override
124     public void testStarted( Description description )
125         throws Exception
126     {
127         numTestStarted.incrementAndGet();
128         print( "testStarted", description );
129         if ( target != null )
130         {
131             target.testStarted( description );
132         }
133     }
134 
135     @Override
136     public void testFinished( Description description )
137         throws Exception
138     {
139         numTestFinished.incrementAndGet();
140         print( "testFinished", description );
141         if ( target != null )
142         {
143             target.testFinished( description );
144         }
145     }
146 
147     @Override
148     public void testFailure( Failure failure )
149         throws Exception
150     {
151         numTestFailed.incrementAndGet();
152         print( "testFailure", failure );
153         if ( target != null )
154         {
155             target.testFailure( failure );
156         }
157     }
158 
159     @Override
160     public void testAssumptionFailure( Failure failure )
161     {
162         numTestAssumptionsFailed.incrementAndGet();
163         print( "testAssumptionFailure", failure );
164         if ( target != null )
165         {
166             target.testAssumptionFailure( failure );
167         }
168     }
169 
170     @Override
171     public void testIgnored( Description description )
172         throws Exception
173     {
174         numTestIgnored.incrementAndGet();
175         print( "testIgnored", description );
176         if ( target != null )
177         {
178             target.testIgnored( description );
179         }
180     }
181 
182     @Override
183     public String toString()
184     {
185         return "DiagnosticRunListener{" + "numTestIgnored=" + numTestIgnored + ", numTestStarted=" + numTestStarted
186             + ", numTestFailed=" + numTestFailed + ", numTestAssumptionsFailed=" + numTestAssumptionsFailed
187             + ", numTestFinished=" + numTestFinished + '}';
188     }
189 }