1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.surefire.api.report;
20
21 import javax.annotation.Nonnull;
22
23 import java.io.IOException;
24 import java.io.OutputStream;
25 import java.io.PrintStream;
26
27 import static java.lang.System.setErr;
28 import static java.lang.System.setOut;
29 import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdErr;
30 import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdErrln;
31 import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdOut;
32 import static org.apache.maven.surefire.api.report.TestOutputReportEntry.stdOutln;
33
34
35
36
37
38 public final class ConsoleOutputCapture {
39 public static void startCapture(TestOutputReceiver<OutputReportEntry> target) {
40 setOut(new ForwardingPrintStream(true, target));
41 setErr(new ForwardingPrintStream(false, target));
42 }
43
44 private static final class ForwardingPrintStream extends PrintStream {
45 private final boolean isStdout;
46 private final TestOutputReceiver<OutputReportEntry> target;
47
48 ForwardingPrintStream(boolean stdout, TestOutputReceiver<OutputReportEntry> target) {
49 super(new NullOutputStream());
50 isStdout = stdout;
51 this.target = target;
52 }
53
54 @Override
55 public void write(@Nonnull byte[] buf, int off, int len) {
56 String log = new String(buf, off, len);
57 target.writeTestOutput(isStdout ? stdOut(log) : stdErr(log));
58 }
59
60 public void write(@Nonnull byte[] b) throws IOException {
61 write(b, 0, b.length);
62 }
63
64 @Override
65 public void write(int b) {
66 try {
67 write(new byte[] {(byte) b});
68 } catch (IOException e) {
69 setError();
70 }
71 }
72
73 @Override
74 public void println(boolean x) {
75 println(x ? "true" : "false");
76 }
77
78 @Override
79 public void println(char x) {
80 println(String.valueOf(x));
81 }
82
83 @Override
84 public void println(int x) {
85 println(String.valueOf(x));
86 }
87
88 @Override
89 public void println(long x) {
90 println(String.valueOf(x));
91 }
92
93 @Override
94 public void println(float x) {
95 println(String.valueOf(x));
96 }
97
98 @Override
99 public void println(double x) {
100 println(String.valueOf(x));
101 }
102
103 @Override
104 public void println(@Nonnull char[] x) {
105 println(String.valueOf(x));
106 }
107
108 @Override
109 public void println(Object x) {
110 println(String.valueOf(x));
111 }
112
113 @Override
114 public void println(String s) {
115 String log = s == null ? "null" : s;
116 target.writeTestOutput(isStdout ? stdOutln(log) : stdErrln(log));
117 }
118
119 @Override
120 public void println() {
121 target.writeTestOutput(isStdout ? stdOutln("") : stdErrln(""));
122 }
123
124 @Override
125 public void print(boolean x) {
126 print(x ? "true" : "false");
127 }
128
129 @Override
130 public void print(char x) {
131 print(String.valueOf(x));
132 }
133
134 @Override
135 public void print(int x) {
136 print(String.valueOf(x));
137 }
138
139 @Override
140 public void print(long x) {
141 print(String.valueOf(x));
142 }
143
144 @Override
145 public void print(float x) {
146 print(String.valueOf(x));
147 }
148
149 @Override
150 public void print(double x) {
151 print(String.valueOf(x));
152 }
153
154 @Override
155 public void print(@Nonnull char[] x) {
156 print(String.valueOf(x));
157 }
158
159 @Override
160 public void print(Object x) {
161 print(String.valueOf(x));
162 }
163
164 @Override
165 public void print(String s) {
166 String log = s == null ? "null" : s;
167 target.writeTestOutput(isStdout ? stdOut(log) : stdErr(log));
168 }
169
170 @Override
171 public PrintStream append(CharSequence csq) {
172 print(csq == null ? "null" : csq.toString());
173 return this;
174 }
175
176 @Override
177 public PrintStream append(CharSequence csq, int start, int end) {
178 CharSequence s = csq == null ? "null" : csq;
179 print(s.subSequence(start, end).toString());
180 return this;
181 }
182
183 @Override
184 public PrintStream append(char c) {
185 print(c);
186 return this;
187 }
188
189 @Override
190 public void close() {}
191
192 @Override
193 public void flush() {}
194 }
195
196 private static final class NullOutputStream extends OutputStream {
197 @Override
198 public void write(int b) {}
199 }
200 }