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 com.oracle.java.testlibrary; 25 26 import java.io.IOException; 27 import java.util.regex.Matcher; 28 import java.util.regex.Pattern; 29 30 public final class OutputAnalyzer { 31 32 private final String stdout; 33 private final String stderr; 34 private final int exitValue; 35 36 /** 37 * Create an OutputAnalyzer, a utility class for verifying output and exit 38 * value from a Process 39 * 40 * @param process Process to analyze 41 * @throws IOException If an I/O error occurs. 42 */ 43 public OutputAnalyzer(Process process) throws IOException { 44 OutputBuffer output = ProcessTools.getOutput(process); 45 exitValue = process.exitValue(); 46 this.stdout = output.getStdout(); 47 this.stderr = output.getStderr(); 48 } 49 50 /** 51 * Create an OutputAnalyzer, a utility class for verifying output 52 * 53 * @param buf String buffer to analyze 54 */ 55 public OutputAnalyzer(String buf) { 56 this(buf, buf); 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 319 320 /** 321 * Get the contents of the output buffer (stdout and stderr) 322 * 323 * @return Content of the output buffer 324 */ 325 public String getOutput() { 326 return stdout + stderr; 327 } 328 329 /** 330 * Get the contents of the stdout buffer 331 * 332 * @return Content of the stdout buffer 333 */ 334 public String getStdout() { 335 return stdout; 336 } 337 338 /** 339 * Get the contents of the stderr buffer 340 * 341 * @return Content of the stderr buffer 342 */ 343 public String getStderr() { 344 return stderr; 345 } 346 347 /** 348 * Get the process exit value 349 * 350 * @return Process exit value 351 */ 352 public int getExitValue() { 353 return exitValue; 354 } 355 }