OutputAnalyzer.java 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package com.oracle.java.testlibrary;

import java.io.IOException;
27 28
import java.util.regex.Matcher;
import java.util.regex.Pattern;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

public final class OutputAnalyzer {

  private final String stdout;
  private final String stderr;
  private final int exitValue;

  /**
   * Create an OutputAnalyzer, a utility class for verifying output and exit
   * value from a Process
   *
   * @param process Process to analyze
   * @throws IOException If an I/O error occurs.
   */
  public OutputAnalyzer(Process process) throws IOException {
    OutputBuffer output = ProcessTools.getOutput(process);
    exitValue = process.exitValue();
    this.stdout = output.getStdout();
    this.stderr = output.getStderr();
  }

  /**
   * Create an OutputAnalyzer, a utility class for verifying output
   *
   * @param buf String buffer to analyze
   */
  public OutputAnalyzer(String buf) {
    this(buf, buf);
  }

  /**
   * Create an OutputAnalyzer, a utility class for verifying output
   *
   * @param stdout stdout buffer to analyze
   * @param stderr stderr buffer to analyze
   */
  public OutputAnalyzer(String stdout, String stderr) {
    this.stdout = stdout;
    this.stderr = stderr;
    exitValue = -1;
  }

  /**
   * Verify that the stdout and stderr contents of output buffer contains the string
   *
   * @param expectedString String that buffer should contain
   * @throws RuntimeException If the string was not found
   */
  public void shouldContain(String expectedString) {
    if (!stdout.contains(expectedString) && !stderr.contains(expectedString)) {
79 80
        reportDiagnosticSummary();
        throw new RuntimeException("'" + expectedString + "' missing from stdout/stderr \n");
81 82 83 84 85 86 87 88 89 90 91
    }
  }

  /**
   * Verify that the stdout contents of output buffer contains the string
   *
   * @param expectedString String that buffer should contain
   * @throws RuntimeException If the string was not found
   */
  public void stdoutShouldContain(String expectedString) {
    if (!stdout.contains(expectedString)) {
92 93
        reportDiagnosticSummary();
        throw new RuntimeException("'" + expectedString + "' missing from stdout \n");
94 95 96 97 98 99 100 101 102 103 104
    }
  }

  /**
   * Verify that the stderr contents of output buffer contains the string
   *
   * @param expectedString String that buffer should contain
   * @throws RuntimeException If the string was not found
   */
  public void stderrShouldContain(String expectedString) {
    if (!stderr.contains(expectedString)) {
105 106
        reportDiagnosticSummary();
        throw new RuntimeException("'" + expectedString + "' missing from stderr \n");
107 108 109 110 111 112 113 114 115 116 117
    }
  }

  /**
   * Verify that the stdout and stderr contents of output buffer does not contain the string
   *
   * @param expectedString String that the buffer should not contain
   * @throws RuntimeException If the string was found
   */
  public void shouldNotContain(String notExpectedString) {
    if (stdout.contains(notExpectedString)) {
118 119
        reportDiagnosticSummary();
        throw new RuntimeException("'" + notExpectedString + "' found in stdout \n");
120 121
    }
    if (stderr.contains(notExpectedString)) {
122 123
        reportDiagnosticSummary();
        throw new RuntimeException("'" + notExpectedString + "' found in stderr \n");
124 125 126 127 128 129 130 131 132 133 134
    }
  }

  /**
   * Verify that the stdout contents of output buffer does not contain the string
   *
   * @param expectedString String that the buffer should not contain
   * @throws RuntimeException If the string was found
   */
  public void stdoutShouldNotContain(String notExpectedString) {
    if (stdout.contains(notExpectedString)) {
135 136
        reportDiagnosticSummary();
        throw new RuntimeException("'" + notExpectedString + "' found in stdout \n");
137 138 139 140 141 142 143 144 145 146 147
    }
  }

  /**
   * Verify that the stderr contents of output buffer does not contain the string
   *
   * @param expectedString String that the buffer should not contain
   * @throws RuntimeException If the string was found
   */
  public void stderrShouldNotContain(String notExpectedString) {
    if (stderr.contains(notExpectedString)) {
148 149
        reportDiagnosticSummary();
        throw new RuntimeException("'" + notExpectedString + "' found in stderr \n");
150 151 152
    }
  }

153 154 155 156 157 158 159 160 161 162 163
  /**
   * Verify that the stdout and stderr contents of output buffer matches
   * the pattern
   *
   * @param pattern
   * @throws RuntimeException If the pattern was not found
   */
  public void shouldMatch(String pattern) {
      Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
      Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
      if (!stdoutMatcher.find() && !stderrMatcher.find()) {
164
          reportDiagnosticSummary();
165
          throw new RuntimeException("'" + pattern
166
                + "' missing from stdout/stderr \n");
167 168 169 170 171 172 173 174 175 176 177 178 179
      }
  }

  /**
   * Verify that the stdout contents of output buffer matches the
   * pattern
   *
   * @param pattern
   * @throws RuntimeException If the pattern was not found
   */
  public void stdoutShouldMatch(String pattern) {
      Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
      if (!matcher.find()) {
180
          reportDiagnosticSummary();
181
          throw new RuntimeException("'" + pattern
182
                + "' missing from stdout \n");
183 184 185 186 187 188 189 190 191 192 193 194 195
      }
  }

  /**
   * Verify that the stderr contents of output buffer matches the
   * pattern
   *
   * @param pattern
   * @throws RuntimeException If the pattern was not found
   */
  public void stderrShouldMatch(String pattern) {
      Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
      if (!matcher.find()) {
196
          reportDiagnosticSummary();
197
          throw new RuntimeException("'" + pattern
198
                + "' missing from stderr \n");
199 200 201 202 203 204 205 206 207 208 209 210 211
      }
  }

  /**
   * Verify that the stdout and stderr contents of output buffer does not
   * match the pattern
   *
   * @param pattern
   * @throws RuntimeException If the pattern was found
   */
  public void shouldNotMatch(String pattern) {
      Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
      if (matcher.find()) {
212
          reportDiagnosticSummary();
213
          throw new RuntimeException("'" + pattern
214
                  + "' found in stdout \n");
215 216 217
      }
      matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
      if (matcher.find()) {
218
          reportDiagnosticSummary();
219
          throw new RuntimeException("'" + pattern
220
                  + "' found in stderr \n");
221 222 223 224 225 226 227 228 229 230 231 232 233
      }
  }

  /**
   * Verify that the stdout contents of output buffer does not match the
   * pattern
   *
   * @param pattern
   * @throws RuntimeException If the pattern was found
   */
  public void stdoutShouldNotMatch(String pattern) {
      Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stdout);
      if (matcher.find()) {
234
          reportDiagnosticSummary();
235
          throw new RuntimeException("'" + pattern
236
                  + "' found in stdout \n");
237 238 239 240 241 242 243 244 245 246 247 248 249
      }
  }

  /**
   * Verify that the stderr contents of output buffer does not match the
   * pattern
   *
   * @param pattern
   * @throws RuntimeException If the pattern was found
   */
  public void stderrShouldNotMatch(String pattern) {
      Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
      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 260 261 262
   *
   * @param expectedExitValue Expected exit value from process
   * @throws RuntimeException If the exit value from the process did not match the expected value
   */
  public void shouldHaveExitValue(int expectedExitValue) {
263
      if (getExitValue() != expectedExitValue) {
264 265 266
          reportDiagnosticSummary();
          throw new RuntimeException("Expected to get exit value of ["
                  + expectedExitValue + "]\n");
267
      }
268 269
  }

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

  /**
   * Report summary that will help to diagnose the problem
   * Currently includes:
   *  - standard input produced by the process under test
   *  - standard output
   *  - exit code
   *  Note: the command line is printed by the ProcessTools
   */
    private void reportDiagnosticSummary() {
        String msg =
            " stdout: [" + stdout + "];\n" +
            " stderr: [" + stderr + "]\n" +
            " exitValue = " + getExitValue() + "\n";

        System.err.println(msg);
    }


289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
  /**
   * Get the contents of the output buffer (stdout and stderr)
   *
   * @return Content of the output buffer
   */
  public String getOutput() {
    return stdout + stderr;
  }

  /**
   * Get the contents of the stdout buffer
   *
   * @return Content of the stdout buffer
   */
  public String getStdout() {
    return stdout;
  }

  /**
   * Get the contents of the stderr buffer
   *
   * @return Content of the stderr buffer
   */
  public String getStderr() {
    return stderr;
  }

  /**
   * Get the process exit value
   *
   * @return Process exit value
   */
  public int getExitValue() {
    return exitValue;
  }
}