View Javadoc

1   package org.apache.maven.surefire.report;
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 java.util.Collections;
23  import java.util.HashMap;
24  import java.util.Map;
25  
26  /**
27   * Attaches the currently executing test method to the thread, allowing
28   * test output to be directed to the proper test set.
29   *
30   * @author Kristian Rosenvold
31   */
32  public abstract class TestConsoleOutputRunListener
33      implements RunListener, ConsoleOutputReceiver
34  {
35      private final ReporterFactory reporterFactory;
36  
37      protected TestConsoleOutputRunListener( ReporterFactory reporterFactory )
38      {
39          this.reporterFactory = reporterFactory;
40      }
41  
42      public static TestConsoleOutputRunListener createInstance( ReporterFactory reporterFactory,
43                                                                 boolean oneThreadPerClass )
44      {
45          return oneThreadPerClass ? (TestConsoleOutputRunListener) new OneThreadPerClassConsoleOutputRunListener(
46              reporterFactory ) : new UnknownThreadPerClassConsoleOutputRunListener( reporterFactory );
47      }
48  
49      protected abstract RunListener getTestSetRunListener( ReportEntry reportEntry );
50  
51      protected abstract void clearTestSetRunListener( ReportEntry reportEntry );
52  
53      protected abstract RunListener getTestMethodRunListener( ReportEntry report );
54  
55      protected abstract void clearTestMethodRunListener( ReportEntry reportEntry );
56  
57      protected abstract ConsoleOutputReceiver getConsoleOutputReceiver();
58  
59      protected ReporterFactory getReporterFactory()
60      {
61          return reporterFactory;
62      }
63  
64  
65      public void writeTestOutput( byte[] buf, int off, int len, boolean stdout )
66      {
67          getConsoleOutputReceiver().writeTestOutput( buf, off, len, stdout );
68      }
69  
70      public void testSetStarting( ReportEntry report )
71          throws ReporterException
72      {
73          getTestSetRunListener( report ).testSetStarting( report );
74      }
75  
76      public void testSetCompleted( ReportEntry report )
77          throws ReporterException
78      {
79          getTestSetRunListener( report ).testSetCompleted( report );
80          clearTestSetRunListener( report );
81      }
82  
83      public void testStarting( ReportEntry report )
84      {
85          getTestMethodRunListener( report ).testStarting( report );
86      }
87  
88      public void testSucceeded( ReportEntry report )
89      {
90          getTestMethodRunListener( report ).testSucceeded( report );
91          clearTestMethodRunListener( report );
92      }
93  
94      public void testAssumptionFailure( ReportEntry report )
95      {
96          getTestMethodRunListener( report ).testAssumptionFailure( report );
97          clearTestMethodRunListener( report );
98      }
99  
100     public void testError( ReportEntry report )
101     {
102         getTestMethodRunListener( report ).testError( report );
103         clearTestMethodRunListener( report );
104     }
105 
106     public void testFailed( ReportEntry report )
107     {
108         getTestMethodRunListener( report ).testFailed( report );
109         clearTestMethodRunListener( report );
110     }
111 
112     public void testSkipped( ReportEntry report )
113     {
114         getTestMethodRunListener( report ).testSkipped( report );
115         clearTestMethodRunListener( report );
116     }
117 
118     public static class OneThreadPerClassConsoleOutputRunListener
119         extends TestConsoleOutputRunListener
120     {
121         private final ThreadLocal currentTestMethodListener = new InheritableThreadLocal();
122 
123         public OneThreadPerClassConsoleOutputRunListener( ReporterFactory reporterFactory )
124         {
125             super( reporterFactory );
126         }
127 
128         protected RunListener getTestSetRunListener( ReportEntry reportEntry )
129         {
130             return getTestMethodRunListener( reportEntry );
131         }
132 
133         protected void clearTestSetRunListener( ReportEntry reportEntry )
134         {
135             currentTestMethodListener.remove();
136         }
137 
138         protected void clearTestMethodRunListener( ReportEntry reportEntry )
139         {
140             // Dont clear, we do this in testset.
141         }
142 
143         protected RunListener getTestMethodRunListener( ReportEntry report )
144         {
145             RunListener runListener = (RunListener) currentTestMethodListener.get();
146             if ( runListener == null )
147             {
148                 runListener = getReporterFactory().createReporter();
149                 currentTestMethodListener.set( runListener );
150             }
151             return runListener;
152         }
153 
154         protected ConsoleOutputReceiver getConsoleOutputReceiver()
155         {
156             return (ConsoleOutputReceiver) currentTestMethodListener.get();
157         }
158 
159 
160     }
161 
162     public static class UnknownThreadPerClassConsoleOutputRunListener
163         extends TestConsoleOutputRunListener
164     {
165         private final ThreadLocal currentTestMethodListener = new InheritableThreadLocal();
166 
167         private final ThreadLocal currentTestSetListener = new InheritableThreadLocal();
168 
169         private final Map testSetToRunListener = Collections.synchronizedMap( new HashMap() );
170 
171         public UnknownThreadPerClassConsoleOutputRunListener( ReporterFactory reporterFactory )
172         {
173             super( reporterFactory );
174         }
175 
176 
177         protected RunListener getTestSetRunListener( ReportEntry reportEntry )
178         {
179             RunListener result = (RunListener) testSetToRunListener.get( reportEntry.getSourceName() );
180             if ( result == null )
181             {
182                 result = getReporterFactory().createReporter();
183                 testSetToRunListener.put( reportEntry.getSourceName(), result );
184             }
185             currentTestSetListener.set( result );
186             return result;
187         }
188 
189         protected void clearTestSetRunListener( ReportEntry reportEntry )
190         {
191             currentTestSetListener.remove();
192         }
193 
194         protected RunListener getTestMethodRunListener( ReportEntry report )
195         {
196             RunListener runListener;
197             runListener = (RunListener) testSetToRunListener.get( report.getSourceName() );
198             if ( runListener == null )
199             {
200                 runListener = getReporterFactory().createReporter();
201                 testSetToRunListener.put( report.getSourceName(), runListener );
202             }
203             currentTestMethodListener.set( runListener );
204             return runListener;
205         }
206 
207         protected void clearTestMethodRunListener( ReportEntry reportEntry )
208         {
209             currentTestMethodListener.remove();
210         }
211 
212         protected ConsoleOutputReceiver getConsoleOutputReceiver()
213         {
214             ConsoleOutputReceiver consoleOutputReceiver = (ConsoleOutputReceiver) currentTestMethodListener.get();
215             if ( consoleOutputReceiver == null )
216             {
217                 consoleOutputReceiver = (ConsoleOutputReceiver) currentTestSetListener.get();
218             }
219             return consoleOutputReceiver;
220         }
221 
222 
223     }
224 
225 }