test/testlibrary/com/oracle/java/testlibrary/OutputAnalyzer.java

Print this page




  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     }


 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    *


  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     }


 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    *