View Javadoc

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