1 /*
2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package jdk.testlibrary;
25
26 import java.io.IOException;
27 import java.util.regex.Matcher;
28 import java.util.regex.Pattern;
29
30 /**
31 * Utility class for verifying output and exit value from a {@code Process}.
32 */
33 public final class OutputAnalyzer {
34
35 private final String stdout;
36 private final String stderr;
37 private final int exitValue;
38
39 /**
40 * Create an OutputAnalyzer, a utility class for verifying output and exit
41 * value from a Process
42 *
43 * @param process
44 * Process to analyze
45 * @throws IOException
46 * If an I/O error occurs.
68 * @param stdout
69 * stdout buffer to analyze
70 * @param stderr
71 * stderr buffer to analyze
72 */
73 public OutputAnalyzer(String stdout, String stderr) {
74 this.stdout = stdout;
75 this.stderr = stderr;
76 exitValue = -1;
77 }
78
79 /**
80 * Verify that the stdout and stderr contents of output buffer contains the
81 * string
82 *
83 * @param expectedString
84 * String that buffer should contain
85 * @throws RuntimeException
86 * If the string was not found
87 */
88 public void shouldContain(String expectedString) {
89 if (!stdout.contains(expectedString)
90 && !stderr.contains(expectedString)) {
91 reportDiagnosticSummary();
92 throw new RuntimeException("'" + expectedString
93 + "' missing from stdout/stderr \n");
94 }
95 }
96
97 /**
98 * Verify that the stdout contents of output buffer contains the string
99 *
100 * @param expectedString
101 * String that buffer should contain
102 * @throws RuntimeException
103 * If the string was not found
104 */
105 public void stdoutShouldContain(String expectedString) {
106 if (!stdout.contains(expectedString)) {
107 reportDiagnosticSummary();
108 throw new RuntimeException("'" + expectedString
109 + "' missing from stdout \n");
110 }
111 }
112
113 /**
114 * Verify that the stderr contents of output buffer contains the string
115 *
116 * @param expectedString
117 * String that buffer should contain
118 * @throws RuntimeException
119 * If the string was not found
120 */
121 public void stderrShouldContain(String expectedString) {
122 if (!stderr.contains(expectedString)) {
123 reportDiagnosticSummary();
124 throw new RuntimeException("'" + expectedString
125 + "' missing from stderr \n");
126 }
127 }
128
129 /**
130 * Verify that the stdout and stderr contents of output buffer does not
131 * contain the string
132 *
133 * @param expectedString
134 * String that the buffer should not contain
135 * @throws RuntimeException
136 * If the string was found
137 */
138 public void shouldNotContain(String notExpectedString) {
139 if (stdout.contains(notExpectedString)) {
140 reportDiagnosticSummary();
141 throw new RuntimeException("'" + notExpectedString
142 + "' found in stdout \n");
143 }
144 if (stderr.contains(notExpectedString)) {
145 reportDiagnosticSummary();
146 throw new RuntimeException("'" + notExpectedString
147 + "' found in stderr \n");
148 }
149 }
150
151 /**
152 * Verify that the stdout contents of output buffer does not contain the
153 * string
154 *
155 * @param expectedString
156 * String that the buffer should not contain
157 * @throws RuntimeException
158 * If the string was found
159 */
160 public void stdoutShouldNotContain(String notExpectedString) {
161 if (stdout.contains(notExpectedString)) {
162 reportDiagnosticSummary();
163 throw new RuntimeException("'" + notExpectedString
164 + "' found in stdout \n");
165 }
166 }
167
168 /**
169 * Verify that the stderr contents of output buffer does not contain the
170 * string
171 *
172 * @param expectedString
173 * String that the buffer should not contain
174 * @throws RuntimeException
175 * If the string was found
176 */
177 public void stderrShouldNotContain(String notExpectedString) {
178 if (stderr.contains(notExpectedString)) {
179 reportDiagnosticSummary();
180 throw new RuntimeException("'" + notExpectedString
181 + "' found in stderr \n");
182 }
183 }
184
185 /**
186 * Verify that the stdout and stderr contents of output buffer matches the
187 * pattern
188 *
189 * @param pattern
190 * @throws RuntimeException
191 * If the pattern was not found
192 */
193 public void shouldMatch(String pattern) {
194 Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
195 .matcher(stdout);
196 Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
197 .matcher(stderr);
198 if (!stdoutMatcher.find() && !stderrMatcher.find()) {
199 reportDiagnosticSummary();
200 throw new RuntimeException("'" + pattern
201 + "' missing from stdout/stderr \n");
202 }
203 }
204
205 /**
206 * Verify that the stdout contents of output buffer matches the pattern
207 *
208 * @param pattern
209 * @throws RuntimeException
210 * If the pattern was not found
211 */
212 public void stdoutShouldMatch(String pattern) {
213 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
214 stdout);
215 if (!matcher.find()) {
216 reportDiagnosticSummary();
217 throw new RuntimeException("'" + pattern
218 + "' missing from stdout \n");
219 }
220 }
221
222 /**
223 * Verify that the stderr contents of output buffer matches the pattern
224 *
225 * @param pattern
226 * @throws RuntimeException
227 * If the pattern was not found
228 */
229 public void stderrShouldMatch(String pattern) {
230 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
231 stderr);
232 if (!matcher.find()) {
233 reportDiagnosticSummary();
234 throw new RuntimeException("'" + pattern
235 + "' missing from stderr \n");
236 }
237 }
238
239 /**
240 * Verify that the stdout and stderr contents of output buffer does not
241 * match the pattern
242 *
243 * @param pattern
244 * @throws RuntimeException
245 * If the pattern was found
246 */
247 public void shouldNotMatch(String pattern) {
248 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
249 stdout);
250 if (matcher.find()) {
251 reportDiagnosticSummary();
252 throw new RuntimeException("'" + pattern + "' found in stdout: '"
253 + matcher.group() + "' \n");
254 }
255 matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
256 if (matcher.find()) {
257 reportDiagnosticSummary();
258 throw new RuntimeException("'" + pattern + "' found in stderr: '"
259 + matcher.group() + "' \n");
260 }
261 }
262
263 /**
264 * Verify that the stdout contents of output buffer does not match the
265 * pattern
266 *
267 * @param pattern
268 * @throws RuntimeException
269 * If the pattern was found
270 */
271 public void stdoutShouldNotMatch(String pattern) {
272 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
273 stdout);
274 if (matcher.find()) {
275 reportDiagnosticSummary();
276 throw new RuntimeException("'" + pattern + "' found in stdout \n");
277 }
278 }
279
280 /**
281 * Verify that the stderr contents of output buffer does not match the
282 * pattern
283 *
284 * @param pattern
285 * @throws RuntimeException
286 * If the pattern was found
287 */
288 public void stderrShouldNotMatch(String pattern) {
289 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
290 stderr);
291 if (matcher.find()) {
292 reportDiagnosticSummary();
293 throw new RuntimeException("'" + pattern + "' found in stderr \n");
294 }
295 }
296
297 /**
298 * Get the captured group of the first string matching the pattern. stderr
299 * is searched before stdout.
300 *
301 * @param pattern
302 * The multi-line pattern to match
303 * @param group
304 * The group to capture
305 * @return The matched string or null if no match was found
306 */
307 public String firstMatch(String pattern, int group) {
308 Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
309 .matcher(stderr);
310 Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
311 .matcher(stdout);
312 if (stderrMatcher.find()) {
313 return stderrMatcher.group(group);
314 }
322 * Get the first string matching the pattern. stderr is searched before
323 * stdout.
324 *
325 * @param pattern
326 * The multi-line pattern to match
327 * @return The matched string or null if no match was found
328 */
329 public String firstMatch(String pattern) {
330 return firstMatch(pattern, 0);
331 }
332
333 /**
334 * Verify the exit value of the process
335 *
336 * @param expectedExitValue
337 * Expected exit value from process
338 * @throws RuntimeException
339 * If the exit value from the process did not match the expected
340 * value
341 */
342 public void shouldHaveExitValue(int expectedExitValue) {
343 if (getExitValue() != expectedExitValue) {
344 reportDiagnosticSummary();
345 throw new RuntimeException("Expected to get exit value of ["
346 + expectedExitValue + "]\n");
347 }
348 }
349
350 /**
351 * Report summary that will help to diagnose the problem Currently includes:
352 * - standard input produced by the process under test - standard output -
353 * exit code Note: the command line is printed by the ProcessTools
354 */
355 private void reportDiagnosticSummary() {
356 String msg = " stdout: [" + stdout + "];\n" + " stderr: [" + stderr
357 + "]\n" + " exitValue = " + getExitValue() + "\n";
358
359 System.err.println(msg);
360 }
361
362 /**
363 * Get the contents of the output buffer (stdout and stderr)
364 *
365 * @return Content of the output buffer
366 */
367 public String getOutput() {
368 return stdout + stderr;
369 }
370
371 /**
372 * Get the contents of the stdout buffer
373 *
374 * @return Content of the stdout buffer
375 */
376 public String getStdout() {
377 return stdout;
378 }
379
380 /**
381 * Get the contents of the stderr buffer
382 *
383 * @return Content of the stderr buffer
384 */
385 public String getStderr() {
386 return stderr;
387 }
388
389 /**
390 * Get the process exit value
391 *
392 * @return Process exit value
393 */
394 public int getExitValue() {
395 return exitValue;
396 }
397 }
|
1 /*
2 * Copyright (c) 2013, 2015 Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 package jdk.testlibrary;
25
26 import java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.regex.Matcher;
30 import java.util.regex.Pattern;
31
32 /**
33 * Utility class for verifying output and exit value from a {@code Process}.
34 */
35 public final class OutputAnalyzer {
36
37 private final String stdout;
38 private final String stderr;
39 private final int exitValue;
40
41 /**
42 * Create an OutputAnalyzer, a utility class for verifying output and exit
43 * value from a Process
44 *
45 * @param process
46 * Process to analyze
47 * @throws IOException
48 * If an I/O error occurs.
70 * @param stdout
71 * stdout buffer to analyze
72 * @param stderr
73 * stderr buffer to analyze
74 */
75 public OutputAnalyzer(String stdout, String stderr) {
76 this.stdout = stdout;
77 this.stderr = stderr;
78 exitValue = -1;
79 }
80
81 /**
82 * Verify that the stdout and stderr contents of output buffer contains the
83 * string
84 *
85 * @param expectedString
86 * String that buffer should contain
87 * @throws RuntimeException
88 * If the string was not found
89 */
90 public OutputAnalyzer shouldContain(String expectedString) {
91 if (!stdout.contains(expectedString)
92 && !stderr.contains(expectedString)) {
93 reportDiagnosticSummary();
94 throw new RuntimeException("'" + expectedString
95 + "' missing from stdout/stderr \n");
96 }
97 return this;
98 }
99
100 /**
101 * Verify that the stdout contents of output buffer contains the string
102 *
103 * @param expectedString
104 * String that buffer should contain
105 * @throws RuntimeException
106 * If the string was not found
107 */
108 public OutputAnalyzer stdoutShouldContain(String expectedString) {
109 if (!stdout.contains(expectedString)) {
110 reportDiagnosticSummary();
111 throw new RuntimeException("'" + expectedString
112 + "' missing from stdout \n");
113 }
114 return this;
115 }
116
117 /**
118 * Verify that the stderr contents of output buffer contains the string
119 *
120 * @param expectedString
121 * String that buffer should contain
122 * @throws RuntimeException
123 * If the string was not found
124 */
125 public OutputAnalyzer stderrShouldContain(String expectedString) {
126 if (!stderr.contains(expectedString)) {
127 reportDiagnosticSummary();
128 throw new RuntimeException("'" + expectedString
129 + "' missing from stderr \n");
130 }
131 return this;
132 }
133
134 /**
135 * Verify that the stdout and stderr contents of output buffer does not
136 * contain the string
137 *
138 * @param notExpectedString
139 * String that the buffer should not contain
140 * @throws RuntimeException
141 * If the string was found
142 */
143 public OutputAnalyzer shouldNotContain(String notExpectedString) {
144 if (stdout.contains(notExpectedString)) {
145 reportDiagnosticSummary();
146 throw new RuntimeException("'" + notExpectedString
147 + "' found in stdout \n");
148 }
149 if (stderr.contains(notExpectedString)) {
150 reportDiagnosticSummary();
151 throw new RuntimeException("'" + notExpectedString
152 + "' found in stderr \n");
153 }
154 return this;
155 }
156
157 /**
158 * Verify that the stdout contents of output buffer does not contain the
159 * string
160 *
161 * @param notExpectedString
162 * String that the buffer should not contain
163 * @throws RuntimeException
164 * If the string was found
165 */
166 public OutputAnalyzer stdoutShouldNotContain(String notExpectedString) {
167 if (stdout.contains(notExpectedString)) {
168 reportDiagnosticSummary();
169 throw new RuntimeException("'" + notExpectedString
170 + "' found in stdout \n");
171 }
172 return this;
173 }
174
175 /**
176 * Verify that the stderr contents of output buffer does not contain the
177 * string
178 *
179 * @param expectedString
180 * String that the buffer should not contain
181 * @throws RuntimeException
182 * If the string was found
183 */
184 public void stderrShouldNotContain(String notExpectedString) {
185 if (stderr.contains(notExpectedString)) {
186 reportDiagnosticSummary();
187 throw new RuntimeException("'" + notExpectedString
188 + "' found in stderr \n");
189 }
190 }
191
192 /**
193 * Verify that the stdout and stderr contents of output buffer matches the
194 * pattern
195 *
196 * @param pattern
197 * @throws RuntimeException
198 * If the pattern was not found
199 */
200 public OutputAnalyzer shouldMatch(String pattern) {
201 Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
202 .matcher(stdout);
203 Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
204 .matcher(stderr);
205 if (!stdoutMatcher.find() && !stderrMatcher.find()) {
206 reportDiagnosticSummary();
207 throw new RuntimeException("'" + pattern
208 + "' missing from stdout/stderr \n");
209 }
210 return this;
211 }
212
213 /**
214 * Verify that the stdout contents of output buffer matches the pattern
215 *
216 * @param pattern
217 * @throws RuntimeException
218 * If the pattern was not found
219 */
220 public OutputAnalyzer stdoutShouldMatch(String pattern) {
221 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
222 stdout);
223 if (!matcher.find()) {
224 reportDiagnosticSummary();
225 throw new RuntimeException("'" + pattern
226 + "' missing from stdout \n");
227 }
228 return this;
229 }
230
231 /**
232 * Verify that the stderr contents of output buffer matches the pattern
233 *
234 * @param pattern
235 * @throws RuntimeException
236 * If the pattern was not found
237 */
238 public OutputAnalyzer stderrShouldMatch(String pattern) {
239 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
240 stderr);
241 if (!matcher.find()) {
242 reportDiagnosticSummary();
243 throw new RuntimeException("'" + pattern
244 + "' missing from stderr \n");
245 }
246 return this;
247 }
248
249 /**
250 * Verify that the stdout and stderr contents of output buffer does not
251 * match the pattern
252 *
253 * @param pattern
254 * @throws RuntimeException
255 * If the pattern was found
256 */
257 public OutputAnalyzer shouldNotMatch(String pattern) {
258 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
259 stdout);
260 if (matcher.find()) {
261 reportDiagnosticSummary();
262 throw new RuntimeException("'" + pattern + "' found in stdout: '"
263 + matcher.group() + "' \n");
264 }
265 matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
266 if (matcher.find()) {
267 reportDiagnosticSummary();
268 throw new RuntimeException("'" + pattern + "' found in stderr: '"
269 + matcher.group() + "' \n");
270 }
271 return this;
272 }
273
274 /**
275 * Verify that the stdout contents of output buffer does not match the
276 * pattern
277 *
278 * @param pattern
279 * @throws RuntimeException
280 * If the pattern was found
281 */
282 public OutputAnalyzer stdoutShouldNotMatch(String pattern) {
283 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
284 stdout);
285 if (matcher.find()) {
286 reportDiagnosticSummary();
287 throw new RuntimeException("'" + pattern + "' found in stdout \n");
288 }
289 return this;
290 }
291
292 /**
293 * Verify that the stderr contents of output buffer does not match the
294 * pattern
295 *
296 * @param pattern
297 * @throws RuntimeException
298 * If the pattern was found
299 */
300 public OutputAnalyzer stderrShouldNotMatch(String pattern) {
301 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
302 stderr);
303 if (matcher.find()) {
304 reportDiagnosticSummary();
305 throw new RuntimeException("'" + pattern + "' found in stderr \n");
306 }
307 return this;
308 }
309
310 /**
311 * Get the captured group of the first string matching the pattern. stderr
312 * is searched before stdout.
313 *
314 * @param pattern
315 * The multi-line pattern to match
316 * @param group
317 * The group to capture
318 * @return The matched string or null if no match was found
319 */
320 public String firstMatch(String pattern, int group) {
321 Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
322 .matcher(stderr);
323 Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
324 .matcher(stdout);
325 if (stderrMatcher.find()) {
326 return stderrMatcher.group(group);
327 }
335 * Get the first string matching the pattern. stderr is searched before
336 * stdout.
337 *
338 * @param pattern
339 * The multi-line pattern to match
340 * @return The matched string or null if no match was found
341 */
342 public String firstMatch(String pattern) {
343 return firstMatch(pattern, 0);
344 }
345
346 /**
347 * Verify the exit value of the process
348 *
349 * @param expectedExitValue
350 * Expected exit value from process
351 * @throws RuntimeException
352 * If the exit value from the process did not match the expected
353 * value
354 */
355 public OutputAnalyzer shouldHaveExitValue(int expectedExitValue) {
356 if (getExitValue() != expectedExitValue) {
357 reportDiagnosticSummary();
358 throw new RuntimeException("Expected to get exit value of ["
359 + expectedExitValue + "]\n");
360 }
361 return this;
362 }
363
364 /**
365 * Report summary that will help to diagnose the problem Currently includes:
366 * - standard input produced by the process under test - standard output -
367 * exit code Note: the command line is printed by the ProcessTools
368 */
369 private OutputAnalyzer reportDiagnosticSummary() {
370 String msg = " stdout: [" + stdout + "];\n" + " stderr: [" + stderr
371 + "]\n" + " exitValue = " + getExitValue() + "\n";
372
373 System.err.println(msg);
374 return this;
375 }
376
377 /**
378 * Get the contents of the output buffer (stdout and stderr)
379 *
380 * @return Content of the output buffer
381 */
382 public String getOutput() {
383 return stdout + stderr;
384 }
385
386 /**
387 * Get the contents of the stdout buffer
388 *
389 * @return Content of the stdout buffer
390 */
391 public String getStdout() {
392 return stdout;
393 }
394
395 /**
396 * Get the contents of the stderr buffer
397 *
398 * @return Content of the stderr buffer
399 */
400 public String getStderr() {
401 return stderr;
402 }
403
404 /**
405 * Get the process exit value
406 *
407 * @return Process exit value
408 */
409 public int getExitValue() {
410 return exitValue;
411 }
412
413 /**
414 * Get the contents of the output buffer (stdout and stderr) as list of strings.
415 * Output will be split by system property 'line.separator'.
416 *
417 * @return Contents of the output buffer as list of strings
418 */
419 public List<String> asLines() {
420 return asLines(getOutput());
421 }
422
423 private List<String> asLines(String buffer) {
424 List<String> l = new ArrayList<String>();
425 String[] a = buffer.split(Utils.NEW_LINE);
426 for (String string : a) {
427 l.add(string);
428 }
429 return l;
430 }
431 }
|