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.io.IOException;
23  import java.io.OutputStream;
24  import java.io.PrintStream;
25  
26  import static java.lang.System.setErr;
27  import static java.lang.System.setOut;
28  
29  /**
30   * Deals with system.out/err.
31   * <br>
32   */
33  public final class ConsoleOutputCapture
34  {
35      public static void startCapture( ConsoleOutputReceiver target )
36      {
37          setOut( new ForwardingPrintStream( true, target ) );
38          setErr( new ForwardingPrintStream( false, target ) );
39      }
40  
41      private static final class ForwardingPrintStream
42          extends PrintStream
43      {
44          private final boolean isStdout;
45          private final ConsoleOutputReceiver target;
46  
47          ForwardingPrintStream( boolean stdout, ConsoleOutputReceiver target )
48          {
49              super( new NullOutputStream() );
50              isStdout = stdout;
51              this.target = target;
52          }
53  
54          @Override
55          public void write( byte[] buf, int off, int len )
56          {
57              target.writeTestOutput( new String( buf, off, len ), false, isStdout );
58          }
59  
60          @Override
61          public void write( byte[] b )
62              throws IOException
63          {
64              write( b, 0, b.length );
65          }
66  
67          @Override
68          public void write( int b )
69          {
70              try
71              {
72                  write( new byte[] { (byte) b } );
73              }
74              catch ( IOException e )
75              {
76                  setError();
77              }
78          }
79  
80          @Override
81          public void println( boolean x )
82          {
83              println( x ? "true" : "false" );
84          }
85  
86          @Override
87          public void println( char x )
88          {
89              println( String.valueOf( x ) );
90          }
91  
92          @Override
93          public void println( int x )
94          {
95              println( String.valueOf( x ) );
96          }
97  
98          @Override
99          public void println( long x )
100         {
101             println( String.valueOf( x ) );
102         }
103 
104         @Override
105         public void println( float x )
106         {
107             println( String.valueOf( x ) );
108         }
109 
110         @Override
111         public void println( double x )
112         {
113             println( String.valueOf( x ) );
114         }
115 
116         @Override
117         public void println( char[] x )
118         {
119             println( String.valueOf( x ) );
120         }
121 
122         @Override
123         public void println( Object x )
124         {
125             println( String.valueOf( x ) );
126         }
127 
128         @Override
129         public void println( String s )
130         {
131             target.writeTestOutput( s == null ? "null" : s, true, isStdout );
132         }
133 
134         @Override
135         public void println()
136         {
137             target.writeTestOutput( "", true, isStdout );
138         }
139 
140         @Override
141         public void print( boolean x )
142         {
143             print( x ? "true" : "false" );
144         }
145 
146         @Override
147         public void print( char x )
148         {
149             print( String.valueOf( x ) );
150         }
151 
152         @Override
153         public void print( int x )
154         {
155             print( String.valueOf( x ) );
156         }
157 
158         @Override
159         public void print( long x )
160         {
161             print( String.valueOf( x ) );
162         }
163 
164         @Override
165         public void print( float x )
166         {
167             print( String.valueOf( x ) );
168         }
169 
170         @Override
171         public void print( double x )
172         {
173             print( String.valueOf( x ) );
174         }
175 
176         @Override
177         public void print( char[] x )
178         {
179             print( String.valueOf( x ) );
180         }
181 
182         @Override
183         public void print( Object x )
184         {
185             print( String.valueOf( x ) );
186         }
187 
188         @Override
189         public void print( String s )
190         {
191             target.writeTestOutput( s == null ? "null" : s, false, isStdout );
192         }
193 
194         @Override
195         public PrintStream append( CharSequence csq )
196         {
197             print( csq == null ? "null" : csq.toString() );
198             return this;
199         }
200 
201         @Override
202         public PrintStream append( CharSequence csq, int start, int end )
203         {
204             CharSequence s = csq == null ? "null" : csq;
205             print( s.subSequence( start, end ).toString() );
206             return this;
207         }
208 
209         @Override
210         public PrintStream append( char c )
211         {
212             print( c );
213             return this;
214         }
215 
216         @Override
217         public void close()
218         {
219         }
220 
221         @Override
222         public void flush()
223         {
224         }
225     }
226 
227     private static final class NullOutputStream
228             extends OutputStream
229     {
230         @Override
231         public void write( int b )
232         {
233         }
234     }
235 }