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  import org.apache.maven.surefire.common.junit4.JUnit4RunListener;
23  import org.apache.maven.surefire.report.ConsoleOutputReceiver;
24  import org.apache.maven.surefire.report.RunListener;
25  import org.apache.maven.surefire.report.SimpleReportEntry;
26  import org.apache.maven.surefire.testset.TestSetFailedException;
27  
28  import org.junit.runner.Description;
29  import org.junit.runner.Result;
30  import org.junit.runner.notification.Failure;
31  
32  /**
33   * A class to be used when there is no JUnit parallelism (methods or/and class). This allow to workaround JUnit
34   * limitation a la Junit4 provider. Specifically, we can redirect properly the output even if we don't have class
35   * demarcation in JUnit. It works when if there is a JVM instance per test run, i.e. with forkMode=always or perthread.
36   */
37  public class NonConcurrentRunListener
38      extends JUnit4RunListener
39      implements ConsoleOutputReceiver
40  {
41  
42      private java.lang.Class<?> currentTestClass;
43  
44      private Description lastFinishedDescription;
45  
46      public NonConcurrentRunListener( RunListener reporter )
47          throws TestSetFailedException
48      {
49          super( reporter );
50      }
51  
52      public synchronized void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
53      {
54          // We can write immediately: no parallelism and a single class.
55          ( (ConsoleOutputReceiver) reporter ).writeTestOutput( buf, off, len, stdout );
56      }
57  
58      protected SimpleReportEntry createReportEntry( Description description )
59      {
60          return new SimpleReportEntry( description.getClassName(), description.getDisplayName()/*,
61                                         (int) ( System.currentTimeMillis() - startTime ) */);
62      }
63  
64      protected SimpleReportEntry createReportEntryForTestSet( Description description )
65      {
66          return new SimpleReportEntry( description.getClassName(), description.getClassName() /*,
67                                         (int) ( System.currentTimeMillis() - startTime ) */);
68      }
69  
70      @Override
71      public void testStarted( Description description )
72          throws Exception
73      {
74          finishLastTestSetIfNeccessary( description );
75          super.testStarted( description );
76      }
77  
78      private void finishLastTestSetIfNeccessary( Description description )
79      {
80          if ( !description.getTestClass().equals( currentTestClass ) )
81          {
82              currentTestClass = description.getTestClass();
83              if ( lastFinishedDescription != null )
84              {
85                  reporter.testSetCompleted( createReportEntryForTestSet( lastFinishedDescription ) );
86                  lastFinishedDescription = null;
87              }
88              reporter.testSetStarting( createReportEntryForTestSet( description ) );
89          }
90      }
91  
92      @Override
93      public void testFinished( Description description )
94          throws Exception
95      {
96          super.testFinished( description );
97          this.lastFinishedDescription = description;
98      }
99  
100     @Override
101     public void testIgnored( Description description )
102         throws Exception
103     {
104         finishLastTestSetIfNeccessary( description );
105 
106         super.testIgnored( description );
107         this.lastFinishedDescription = description;
108     }
109 
110     @Override
111     public void testFailure( Failure failure )
112         throws Exception
113     {
114         super.testFailure( failure );
115         this.lastFinishedDescription = failure.getDescription();
116     }
117 
118     @Override
119     public void testAssumptionFailure( Failure failure )
120     {
121         super.testAssumptionFailure( failure );
122         this.lastFinishedDescription = failure.getDescription();
123     }
124 
125     @Override
126     public void testRunStarted( Description description )
127         throws Exception
128     {
129     }
130 
131     @Override
132     public void testRunFinished( Result result )
133         throws Exception
134     {
135         if ( lastFinishedDescription != null )
136         {
137             reporter.testSetCompleted( createReportEntryForTestSet( lastFinishedDescription ) );
138             lastFinishedDescription = null;
139         }
140     }
141 }