View Javadoc

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