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