Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SystemStreamCapturer |
|
| 1.8;1.8 |
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 org.apache.maven.surefire.util.TeeStream; | |
23 | import org.codehaus.plexus.util.IOUtil; | |
24 | ||
25 | import java.io.ByteArrayOutputStream; | |
26 | import java.io.PrintStream; | |
27 | ||
28 | /** | |
29 | * Captures System.out/System.err streams to buffers. | |
30 | * <p/> | |
31 | * Please note that this design is inherently single-threaded test-linear, and is intended only | |
32 | * for use with ReporterManager, which is also test-linear. While it will capture | |
33 | * output in a multi-threaded scenario, there's no way to associate ouput with the correct | |
34 | * test/thread. | |
35 | * <p/> | |
36 | * Note; this class does not need synchronization because all of these methods are serially invoked on | |
37 | * the same thread. Or maybe not. See notes inside ReporterManager about the general improperness | |
38 | * of this design in multithreading. | |
39 | */ | |
40 | public class SystemStreamCapturer | |
41 | { | |
42 | private final PrintStream oldOut; | |
43 | ||
44 | private final PrintStream oldErr; | |
45 | ||
46 | private final PrintStream newErr; | |
47 | ||
48 | private final PrintStream newOut; | |
49 | ||
50 | private final ByteArrayOutputStream stdOut; | |
51 | ||
52 | private final ByteArrayOutputStream stdErr; | |
53 | ||
54 | public SystemStreamCapturer() | |
55 | 0 | { |
56 | 0 | stdOut = new ByteArrayOutputStream(); |
57 | ||
58 | 0 | newOut = new PrintStream( stdOut ); |
59 | ||
60 | 0 | oldOut = System.out; |
61 | ||
62 | 0 | TeeStream tee = new TeeStream( oldOut, newOut ); |
63 | 0 | System.setOut( tee ); |
64 | ||
65 | 0 | stdErr = new ByteArrayOutputStream(); |
66 | ||
67 | 0 | newErr = new PrintStream( stdErr ); |
68 | ||
69 | 0 | oldErr = System.err; |
70 | ||
71 | 0 | tee = new TeeStream( oldErr, newErr ); |
72 | 0 | System.setErr( tee ); |
73 | 0 | } |
74 | ||
75 | ||
76 | public void restoreStreams() | |
77 | { | |
78 | // Note that the fields can be null if the test hasn't even started yet (an early error) | |
79 | 0 | if ( oldOut != null ) |
80 | { | |
81 | 0 | System.setOut( oldOut ); |
82 | } | |
83 | 0 | if ( oldErr != null ) |
84 | { | |
85 | 0 | System.setErr( oldErr ); |
86 | } | |
87 | ||
88 | 0 | IOUtil.close( newOut ); |
89 | 0 | IOUtil.close( newErr ); |
90 | 0 | } |
91 | ||
92 | public void clearCapturedContent() | |
93 | { | |
94 | 0 | if ( stdOut != null ) |
95 | { | |
96 | 0 | stdOut.reset(); |
97 | } | |
98 | 0 | if ( stdErr != null ) |
99 | { | |
100 | 0 | stdErr.reset(); |
101 | } | |
102 | 0 | } |
103 | ||
104 | public String getStdOutLog() | |
105 | { | |
106 | // Note that the fields can be null if the test hasn't even started yet (an early error) | |
107 | 0 | return stdOut != null ? stdOut.toString() : ""; |
108 | } | |
109 | ||
110 | public String getStdErrLog() | |
111 | { | |
112 | // Note that the fields can be null if the test hasn't even started yet (an early error) | |
113 | 0 | return stdErr != null ? stdErr.toString() : ""; |
114 | } | |
115 | } |