View Javadoc
1   package org.apache.maven.surefire.booter;
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.plugin.surefire.log.api.ConsoleLogger;
23  import org.apache.maven.surefire.report.ConsoleOutputReceiver;
24  import org.apache.maven.surefire.report.ConsoleStream;
25  import org.apache.maven.surefire.report.ReportEntry;
26  import org.apache.maven.surefire.report.RunListener;
27  import org.apache.maven.surefire.report.RunMode;
28  import org.apache.maven.surefire.report.TestSetReportEntry;
29  
30  import static org.apache.maven.surefire.report.RunMode.NORMAL_RUN;
31  import static java.util.Objects.requireNonNull;
32  
33  /**
34   * Encodes the full output of the test run to the stdout stream.
35   * <br>
36   * This class and the ForkClient contain the full definition of the
37   * "wire-level" protocol used by the forked process. The protocol
38   * is *not* part of any public api and may change without further
39   * notice.
40   * <br>
41   * This class is threadsafe.
42   * <br>
43   * The synchronization in the underlying PrintStream (target instance)
44   * is used to preserve thread safety of the output stream. To perform
45   * multiple writes/prints for a single request, they must
46   * synchronize on "target.out" variable in this class.
47   *
48   * @author Kristian Rosenvold
49   */
50  public class ForkingRunListener
51      implements RunListener, ConsoleLogger, ConsoleOutputReceiver, ConsoleStream
52  {
53      private final ForkedChannelEncoder target;
54  
55      private final boolean trim;
56  
57      private volatile RunMode runMode = NORMAL_RUN;
58  
59      public ForkingRunListener( ForkedChannelEncoder target, boolean trim )
60      {
61          this.target = target;
62          this.trim = trim;
63      }
64  
65      @Override
66      public void testSetStarting( TestSetReportEntry report )
67      {
68          target.testSetStarting( report, trim );
69      }
70  
71      @Override
72      public void testSetCompleted( TestSetReportEntry report )
73      {
74          target.sendSystemProperties( report.getSystemProperties() );
75          target.testSetCompleted( report, trim );
76      }
77  
78      @Override
79      public void testStarting( ReportEntry report )
80      {
81          target.testStarting( report, trim );
82      }
83  
84      @Override
85      public void testSucceeded( ReportEntry report )
86      {
87          target.testSucceeded( report, trim );
88      }
89  
90      @Override
91      public void testAssumptionFailure( ReportEntry report )
92      {
93          target.testAssumptionFailure( report, trim );
94      }
95  
96      @Override
97      public void testError( ReportEntry report )
98      {
99          target.testError( report, trim );
100     }
101 
102     @Override
103     public void testFailed( ReportEntry report )
104     {
105         target.testFailed( report, trim );
106     }
107 
108     @Override
109     public void testSkipped( ReportEntry report )
110     {
111         target.testSkipped( report, trim );
112     }
113 
114     @Override
115     public void testExecutionSkippedByUser()
116     {
117         target.stopOnNextTest();
118     }
119 
120     @Override
121     public RunMode markAs( RunMode currentRunMode )
122     {
123         RunMode runMode = this.runMode;
124         this.runMode = requireNonNull( currentRunMode );
125         return runMode;
126     }
127 
128     @Override
129     public void writeTestOutput( String output, boolean newLine, boolean stdout )
130     {
131         if ( stdout )
132         {
133             target.stdOut( output, newLine );
134         }
135         else
136         {
137             target.stdErr( output, newLine );
138         }
139     }
140 
141     @Override
142     public boolean isDebugEnabled()
143     {
144         return true;
145     }
146 
147     @Override
148     public void debug( String message )
149     {
150         target.consoleDebugLog( message );
151     }
152 
153     @Override
154     public boolean isInfoEnabled()
155     {
156         return true;
157     }
158 
159     @Override
160     public void info( String message )
161     {
162         target.consoleInfoLog( message );
163     }
164 
165     @Override
166     public boolean isWarnEnabled()
167     {
168         return true;
169     }
170 
171     @Override
172     public void warning( String message )
173     {
174         target.consoleWarningLog( message );
175     }
176 
177     @Override
178     public boolean isErrorEnabled()
179     {
180         return true;
181     }
182 
183     @Override
184     public void error( String message )
185     {
186         target.consoleErrorLog( message );
187     }
188 
189     @Override
190     public void error( String message, Throwable t )
191     {
192         target.consoleErrorLog( message, t );
193     }
194 
195     @Override
196     public void error( Throwable t )
197     {
198         error( null, t );
199     }
200 
201     @Override
202     public void println( String message )
203     {
204         writeTestOutput( message, true, true );
205     }
206 }