57 }
58
59 /**
60 * Create an OutputAnalyzer, a utility class for verifying output
61 *
62 * @param stdout stdout buffer to analyze
63 * @param stderr stderr buffer to analyze
64 */
65 public OutputAnalyzer(String stdout, String stderr) {
66 this.stdout = stdout;
67 this.stderr = stderr;
68 exitValue = -1;
69 }
70
71 /**
72 * Verify that the stdout and stderr contents of output buffer contains the string
73 *
74 * @param expectedString String that buffer should contain
75 * @throws RuntimeException If the string was not found
76 */
77 public void shouldContain(String expectedString) {
78 if (!stdout.contains(expectedString) && !stderr.contains(expectedString)) {
79 reportDiagnosticSummary();
80 throw new RuntimeException("'" + expectedString + "' missing from stdout/stderr \n");
81 }
82 }
83
84 /**
85 * Verify that the stdout contents of output buffer contains the string
86 *
87 * @param expectedString String that buffer should contain
88 * @throws RuntimeException If the string was not found
89 */
90 public void stdoutShouldContain(String expectedString) {
91 if (!stdout.contains(expectedString)) {
92 reportDiagnosticSummary();
93 throw new RuntimeException("'" + expectedString + "' missing from stdout \n");
94 }
95 }
96
97 /**
98 * Verify that the stderr contents of output buffer contains the string
99 *
100 * @param expectedString String that buffer should contain
101 * @throws RuntimeException If the string was not found
102 */
103 public void stderrShouldContain(String expectedString) {
104 if (!stderr.contains(expectedString)) {
105 reportDiagnosticSummary();
106 throw new RuntimeException("'" + expectedString + "' missing from stderr \n");
107 }
108 }
109
110 /**
111 * Verify that the stdout and stderr contents of output buffer does not contain the string
112 *
113 * @param expectedString String that the buffer should not contain
114 * @throws RuntimeException If the string was found
115 */
116 public void shouldNotContain(String notExpectedString) {
117 if (stdout.contains(notExpectedString)) {
118 reportDiagnosticSummary();
119 throw new RuntimeException("'" + notExpectedString + "' found in stdout \n");
120 }
121 if (stderr.contains(notExpectedString)) {
122 reportDiagnosticSummary();
123 throw new RuntimeException("'" + notExpectedString + "' found in stderr \n");
124 }
125 }
126
127 /**
128 * Verify that the stdout contents of output buffer does not contain the string
129 *
130 * @param expectedString String that the buffer should not contain
131 * @throws RuntimeException If the string was found
132 */
133 public void stdoutShouldNotContain(String notExpectedString) {
134 if (stdout.contains(notExpectedString)) {
135 reportDiagnosticSummary();
136 throw new RuntimeException("'" + notExpectedString + "' found in stdout \n");
137 }
138 }
139
140 /**
141 * Verify that the stderr contents of output buffer does not contain the string
142 *
143 * @param expectedString String that the buffer should not contain
144 * @throws RuntimeException If the string was found
145 */
146 public void stderrShouldNotContain(String notExpectedString) {
147 if (stderr.contains(notExpectedString)) {
148 reportDiagnosticSummary();
149 throw new RuntimeException("'" + notExpectedString + "' found in stderr \n");
150 }
151 }
152
153 /**
154 * Verify that the stdout and stderr contents of output buffer matches
155 * the pattern
156 *
157 * @param pattern
158 * @throws RuntimeException If the pattern was not found
159 */
160 public void shouldMatch(String pattern) {
161 Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
162 Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
163 if (!stdoutMatcher.find() && !stderrMatcher.find()) {
164 reportDiagnosticSummary();
165 throw new RuntimeException("'" + pattern
166 + "' missing from stdout/stderr \n");
167 }
168 }
169
170 /**
171 * Verify that the stdout contents of output buffer matches the
172 * pattern
173 *
174 * @param pattern
175 * @throws RuntimeException If the pattern was not found
176 */
177 public void stdoutShouldMatch(String pattern) {
178 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
179 if (!matcher.find()) {
180 reportDiagnosticSummary();
181 throw new RuntimeException("'" + pattern
182 + "' missing from stdout \n");
183 }
184 }
185
186 /**
187 * Verify that the stderr contents of output buffer matches the
188 * pattern
189 *
190 * @param pattern
191 * @throws RuntimeException If the pattern was not found
192 */
193 public void stderrShouldMatch(String pattern) {
194 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
195 if (!matcher.find()) {
196 reportDiagnosticSummary();
197 throw new RuntimeException("'" + pattern
198 + "' missing from stderr \n");
199 }
200 }
201
202 /**
203 * Verify that the stdout and stderr contents of output buffer does not
204 * match the pattern
205 *
206 * @param pattern
207 * @throws RuntimeException If the pattern was found
208 */
209 public void shouldNotMatch(String pattern) {
210 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
211 if (matcher.find()) {
212 reportDiagnosticSummary();
213 throw new RuntimeException("'" + pattern
214 + "' found in stdout: '" + matcher.group() + "' \n");
215 }
216 matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
217 if (matcher.find()) {
218 reportDiagnosticSummary();
219 throw new RuntimeException("'" + pattern
220 + "' found in stderr: '" + matcher.group() + "' \n");
221 }
222 }
223
224 /**
225 * Verify that the stdout contents of output buffer does not match the
226 * pattern
227 *
228 * @param pattern
229 * @throws RuntimeException If the pattern was found
230 */
231 public void stdoutShouldNotMatch(String pattern) {
232 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
233 if (matcher.find()) {
234 reportDiagnosticSummary();
235 throw new RuntimeException("'" + pattern
236 + "' found in stdout \n");
237 }
238 }
239
240 /**
241 * Verify that the stderr contents of output buffer does not match the
242 * pattern
243 *
244 * @param pattern
245 * @throws RuntimeException If the pattern was found
246 */
247 public void stderrShouldNotMatch(String pattern) {
248 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
249 if (matcher.find()) {
250 reportDiagnosticSummary();
251 throw new RuntimeException("'" + pattern
252 + "' found in stderr \n");
253 }
254 }
255
256 /**
257 * Get the captured group of the first string matching the pattern.
258 * stderr is searched before stdout.
259 *
260 * @param pattern The multi-line pattern to match
261 * @param group The group to capture
262 * @return The matched string or null if no match was found
263 */
264 public String firstMatch(String pattern, int group) {
265 Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
266 Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
267 if (stderrMatcher.find()) {
268 return stderrMatcher.group(group);
269 }
270 if (stdoutMatcher.find()) {
271 return stdoutMatcher.group(group);
272 }
273 return null;
274 }
275
276 /**
277 * Get the first string matching the pattern.
278 * stderr is searched before stdout.
279 *
280 * @param pattern The multi-line pattern to match
281 * @return The matched string or null if no match was found
282 */
283 public String firstMatch(String pattern) {
284 return firstMatch(pattern, 0);
285 }
286
287 /**
288 * Verify the exit value of the process
289 *
290 * @param expectedExitValue Expected exit value from process
291 * @throws RuntimeException If the exit value from the process did not match the expected value
292 */
293 public void shouldHaveExitValue(int expectedExitValue) {
294 if (getExitValue() != expectedExitValue) {
295 reportDiagnosticSummary();
296 throw new RuntimeException("Expected to get exit value of ["
297 + expectedExitValue + "]\n");
298 }
299 }
300
301
302 /**
303 * Report summary that will help to diagnose the problem
304 * Currently includes:
305 * - standard input produced by the process under test
306 * - standard output
307 * - exit code
308 * Note: the command line is printed by the ProcessTools
309 */
310 private void reportDiagnosticSummary() {
311 String msg =
312 " stdout: [" + stdout + "];\n" +
313 " stderr: [" + stderr + "]\n" +
314 " exitValue = " + getExitValue() + "\n";
315
316 System.err.println(msg);
317 }
318
|
57 }
58
59 /**
60 * Create an OutputAnalyzer, a utility class for verifying output
61 *
62 * @param stdout stdout buffer to analyze
63 * @param stderr stderr buffer to analyze
64 */
65 public OutputAnalyzer(String stdout, String stderr) {
66 this.stdout = stdout;
67 this.stderr = stderr;
68 exitValue = -1;
69 }
70
71 /**
72 * Verify that the stdout and stderr contents of output buffer contains the string
73 *
74 * @param expectedString String that buffer should contain
75 * @throws RuntimeException If the string was not found
76 */
77 public OutputAnalyzer shouldContain(String expectedString) {
78 if (!stdout.contains(expectedString) && !stderr.contains(expectedString)) {
79 reportDiagnosticSummary();
80 throw new RuntimeException("'" + expectedString + "' missing from stdout/stderr \n");
81 }
82 return this;
83 }
84
85 /**
86 * Verify that the stdout contents of output buffer contains the string
87 *
88 * @param expectedString String that buffer should contain
89 * @throws RuntimeException If the string was not found
90 */
91 public OutputAnalyzer stdoutShouldContain(String expectedString) {
92 if (!stdout.contains(expectedString)) {
93 reportDiagnosticSummary();
94 throw new RuntimeException("'" + expectedString + "' missing from stdout \n");
95 }
96 return this;
97 }
98
99 /**
100 * Verify that the stderr contents of output buffer contains the string
101 *
102 * @param expectedString String that buffer should contain
103 * @throws RuntimeException If the string was not found
104 */
105 public OutputAnalyzer stderrShouldContain(String expectedString) {
106 if (!stderr.contains(expectedString)) {
107 reportDiagnosticSummary();
108 throw new RuntimeException("'" + expectedString + "' missing from stderr \n");
109 }
110 return this;
111 }
112
113 /**
114 * Verify that the stdout and stderr contents of output buffer does not contain the string
115 *
116 * @param expectedString String that the buffer should not contain
117 * @throws RuntimeException If the string was found
118 */
119 public OutputAnalyzer shouldNotContain(String notExpectedString) {
120 if (stdout.contains(notExpectedString)) {
121 reportDiagnosticSummary();
122 throw new RuntimeException("'" + notExpectedString + "' found in stdout \n");
123 }
124 if (stderr.contains(notExpectedString)) {
125 reportDiagnosticSummary();
126 throw new RuntimeException("'" + notExpectedString + "' found in stderr \n");
127 }
128 return this;
129 }
130
131 /**
132 * Verify that the stdout contents of output buffer does not contain the string
133 *
134 * @param expectedString String that the buffer should not contain
135 * @throws RuntimeException If the string was found
136 */
137 public OutputAnalyzer stdoutShouldNotContain(String notExpectedString) {
138 if (stdout.contains(notExpectedString)) {
139 reportDiagnosticSummary();
140 throw new RuntimeException("'" + notExpectedString + "' found in stdout \n");
141 }
142 return this;
143 }
144
145 /**
146 * Verify that the stderr contents of output buffer does not contain the string
147 *
148 * @param expectedString String that the buffer should not contain
149 * @throws RuntimeException If the string was found
150 */
151 public OutputAnalyzer stderrShouldNotContain(String notExpectedString) {
152 if (stderr.contains(notExpectedString)) {
153 reportDiagnosticSummary();
154 throw new RuntimeException("'" + notExpectedString + "' found in stderr \n");
155 }
156 return this;
157 }
158
159 /**
160 * Verify that the stdout and stderr contents of output buffer matches
161 * the pattern
162 *
163 * @param pattern
164 * @throws RuntimeException If the pattern was not found
165 */
166 public OutputAnalyzer shouldMatch(String pattern) {
167 Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
168 Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
169 if (!stdoutMatcher.find() && !stderrMatcher.find()) {
170 reportDiagnosticSummary();
171 throw new RuntimeException("'" + pattern
172 + "' missing from stdout/stderr \n");
173 }
174 return this;
175 }
176
177 /**
178 * Verify that the stdout contents of output buffer matches the
179 * pattern
180 *
181 * @param pattern
182 * @throws RuntimeException If the pattern was not found
183 */
184 public OutputAnalyzer stdoutShouldMatch(String pattern) {
185 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
186 if (!matcher.find()) {
187 reportDiagnosticSummary();
188 throw new RuntimeException("'" + pattern
189 + "' missing from stdout \n");
190 }
191 return this;
192 }
193
194 /**
195 * Verify that the stderr contents of output buffer matches the
196 * pattern
197 *
198 * @param pattern
199 * @throws RuntimeException If the pattern was not found
200 */
201 public OutputAnalyzer stderrShouldMatch(String pattern) {
202 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
203 if (!matcher.find()) {
204 reportDiagnosticSummary();
205 throw new RuntimeException("'" + pattern
206 + "' missing from stderr \n");
207 }
208 return this;
209 }
210
211 /**
212 * Verify that the stdout and stderr contents of output buffer does not
213 * match the pattern
214 *
215 * @param pattern
216 * @throws RuntimeException If the pattern was found
217 */
218 public OutputAnalyzer shouldNotMatch(String pattern) {
219 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
220 if (matcher.find()) {
221 reportDiagnosticSummary();
222 throw new RuntimeException("'" + pattern
223 + "' found in stdout: '" + matcher.group() + "' \n");
224 }
225 matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
226 if (matcher.find()) {
227 reportDiagnosticSummary();
228 throw new RuntimeException("'" + pattern
229 + "' found in stderr: '" + matcher.group() + "' \n");
230 }
231 return this;
232 }
233
234 /**
235 * Verify that the stdout contents of output buffer does not match the
236 * pattern
237 *
238 * @param pattern
239 * @throws RuntimeException If the pattern was found
240 */
241 public OutputAnalyzer stdoutShouldNotMatch(String pattern) {
242 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
243 if (matcher.find()) {
244 reportDiagnosticSummary();
245 throw new RuntimeException("'" + pattern
246 + "' found in stdout \n");
247 }
248 return this;
249 }
250
251 /**
252 * Verify that the stderr contents of output buffer does not match the
253 * pattern
254 *
255 * @param pattern
256 * @throws RuntimeException If the pattern was found
257 */
258 public OutputAnalyzer stderrShouldNotMatch(String pattern) {
259 Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
260 if (matcher.find()) {
261 reportDiagnosticSummary();
262 throw new RuntimeException("'" + pattern
263 + "' found in stderr \n");
264 }
265 return this;
266 }
267
268 /**
269 * Get the captured group of the first string matching the pattern.
270 * stderr is searched before stdout.
271 *
272 * @param pattern The multi-line pattern to match
273 * @param group The group to capture
274 * @return The matched string or null if no match was found
275 */
276 public String firstMatch(String pattern, int group) {
277 Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
278 Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
279 if (stderrMatcher.find()) {
280 return stderrMatcher.group(group);
281 }
282 if (stdoutMatcher.find()) {
283 return stdoutMatcher.group(group);
284 }
285 return null;
286 }
287
288 /**
289 * Get the first string matching the pattern.
290 * stderr is searched before stdout.
291 *
292 * @param pattern The multi-line pattern to match
293 * @return The matched string or null if no match was found
294 */
295 public String firstMatch(String pattern) {
296 return firstMatch(pattern, 0);
297 }
298
299 /**
300 * Verify the exit value of the process
301 *
302 * @param expectedExitValue Expected exit value from process
303 * @throws RuntimeException If the exit value from the process did not match the expected value
304 */
305 public OutputAnalyzer shouldHaveExitValue(int expectedExitValue) {
306 if (getExitValue() != expectedExitValue) {
307 reportDiagnosticSummary();
308 throw new RuntimeException("Expected to get exit value of ["
309 + expectedExitValue + "]\n");
310 }
311 return this;
312 }
313
314
315 /**
316 * Report summary that will help to diagnose the problem
317 * Currently includes:
318 * - standard input produced by the process under test
319 * - standard output
320 * - exit code
321 * Note: the command line is printed by the ProcessTools
322 */
323 private void reportDiagnosticSummary() {
324 String msg =
325 " stdout: [" + stdout + "];\n" +
326 " stderr: [" + stderr + "]\n" +
327 " exitValue = " + getExitValue() + "\n";
328
329 System.err.println(msg);
330 }
331
|