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