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 \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 \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 * Verify the exit value of the process 258 * 259 * @param expectedExitValue Expected exit value from process 260 * @throws RuntimeException If the exit value from the process did not match the expected value 261 */ 262 public void shouldHaveExitValue(int expectedExitValue) { 263 if (getExitValue() != expectedExitValue) { 264 reportDiagnosticSummary(); 265 throw new RuntimeException("Expected to get exit value of [" 266 + expectedExitValue + "]\n"); 267 } 268 } 269 270 271 /** 272 * Report summary that will help to diagnose the problem 273 * Currently includes: 274 * - standard input produced by the process under test 275 * - standard output 276 * - exit code 277 * Note: the command line is printed by the ProcessTools 278 */ 279 private void reportDiagnosticSummary() { 280 String msg = 281 " stdout: [" + stdout + "];\n" + 282 " stderr: [" + stderr + "]\n" + 283 " exitValue = " + getExitValue() + "\n"; 284 285 System.err.println(msg); 286 } 287 288 289 /** 290 * Get the contents of the output buffer (stdout and stderr) 291 * 292 * @return Content of the output buffer 293 */ 294 public String getOutput() { 295 return stdout + stderr; 296 } 297 298 /** 299 * Get the contents of the stdout buffer 300 * 301 * @return Content of the stdout buffer 302 */ 303 public String getStdout() { 304 return stdout; 305 } 306 307 /** 308 * Get the contents of the stderr buffer 309 * 310 * @return Content of the stderr buffer 311 */ 312 public String getStderr() { 313 return stderr; 314 } 315 316 /** 317 * Get the process exit value 318 * 319 * @return Process exit value 320 */ 321 public int getExitValue() { 322 return exitValue; 323 } 324 }