1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.junit;
16
17 import java.io.ByteArrayOutputStream;
18 import java.io.OutputStream;
19 import java.io.PrintStream;
20 import java.util.Optional;
21 import java.util.regex.Pattern;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.htmlunit.WebDriverTestCase;
25 import org.junit.jupiter.api.extension.AfterEachCallback;
26 import org.junit.jupiter.api.extension.BeforeEachCallback;
27 import org.junit.jupiter.api.extension.ExtensionContext;
28
29
30
31
32
33
34
35
36
37
38
39 public class ErrorOutputChecker implements BeforeEachCallback, AfterEachCallback {
40 private PrintStream originalErr_;
41 private final ByteArrayOutputStream baos_ = new ByteArrayOutputStream();
42 private static final Pattern[] PATTERNS = {
43
44 Pattern.compile(".*Logging initialized .* to org.eclipse.jetty.util.log.StdErrLog.*\r?\n"),
45
46
47 Pattern.compile("SLF4J\\(I\\): .*\r?\n"),
48
49
50 Pattern.compile(".*com.caucho.quercus.servlet.QuercusServlet initImpl\r?\n"),
51 Pattern.compile(".*QuercusServlet starting as QuercusServletImpl\r?\n"),
52 Pattern.compile(".*Quercus finished initialization in \\d*ms\r?\n"),
53 };
54
55 @Override
56 public void beforeEach(final ExtensionContext context) throws Exception {
57 wrapSystemErr();
58 }
59
60 @Override
61 public void afterEach(final ExtensionContext context) throws Exception {
62 try {
63 final Optional<Object> testInstance = context.getTestInstance();
64
65 if (testInstance.isPresent()
66 && testInstance.get() instanceof WebDriverTestCase) {
67 final WebDriverTestCase webDriverTestCase = (WebDriverTestCase) testInstance.get();
68 if (!webDriverTestCase.useRealBrowser()) {
69 verifyNoOutput();
70 }
71 }
72 else {
73 verifyNoOutput();
74 }
75 }
76 finally {
77 restoreSystemErr();
78 }
79 }
80
81 void verifyNoOutput() {
82 if (baos_.size() != 0) {
83 String output = baos_.toString();
84
85
86 for (final Pattern pattern : PATTERNS) {
87 output = pattern.matcher(output).replaceAll("");
88 }
89
90 if (!output.isEmpty()) {
91 if (output.contains("ChromeDriver")) {
92 throw new RuntimeException("Outdated Chrome driver version: " + output);
93 }
94 if (output.contains("geckodriver")) {
95 throw new RuntimeException("Outdated Gecko driver version: " + output);
96 }
97 output = StringUtils.replaceEach(output, new String[] {"\n", "\r"}, new String[]{"\\n", "\\r"});
98 throw new RuntimeException("Test has produced output to System.err: " + output);
99 }
100 }
101 }
102
103 private void wrapSystemErr() {
104 originalErr_ = System.err;
105 System.setErr(new NSAPrintStreamWrapper(originalErr_, baos_));
106 }
107
108 void restoreSystemErr() {
109 System.setErr(originalErr_);
110 }
111 }
112
113
114
115
116
117 class NSAPrintStreamWrapper extends PrintStream {
118 private PrintStream wrapped_;
119
120 NSAPrintStreamWrapper(final PrintStream original, final OutputStream spyOut) {
121 super(spyOut, true);
122 wrapped_ = original;
123 }
124
125
126
127
128 @Override
129 public int hashCode() {
130 return wrapped_.hashCode();
131 }
132
133
134
135
136 @Override
137 public boolean equals(final Object obj) {
138 return wrapped_.equals(obj);
139 }
140
141
142
143
144 @Override
145 public String toString() {
146 return wrapped_.toString();
147 }
148
149
150
151
152 @Override
153 public void flush() {
154 super.flush();
155 wrapped_.flush();
156 }
157
158
159
160
161 @Override
162 public void close() {
163 super.close();
164 wrapped_.close();
165 }
166
167
168
169
170 @Override
171 public boolean checkError() {
172 super.checkError();
173 return wrapped_.checkError();
174 }
175
176
177
178
179 @Override
180 public void write(final int b) {
181 super.write(b);
182 wrapped_.write(b);
183 }
184
185
186
187
188 @Override
189 public void write(final byte[] buf, final int off, final int len) {
190 super.write(buf, off, len);
191 wrapped_.write(buf, off, len);
192 }
193 }