< prev index next >

test/lib/jdk/test/lib/process/OutputAnalyzer.java

Print this page
rev 51638 : [mq]: 8210112
rev 51639 : [mq]: 8210112-1


 296     public OutputAnalyzer stderrShouldMatch(String pattern) {
 297         String stderr = getStderr();
 298         Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
 299         if (!matcher.find()) {
 300             reportDiagnosticSummary();
 301             throw new RuntimeException("'" + pattern
 302                   + "' missing from stderr \n");
 303         }
 304         return this;
 305     }
 306 
 307     /**
 308      * Verify that the stdout and stderr contents of output buffer does not
 309      * match the pattern
 310      *
 311      * @param regexp
 312      * @throws RuntimeException If the pattern was found
 313      */
 314     public OutputAnalyzer shouldNotMatch(String regexp) {
 315         String stdout = getStdout();
 316         String stderr = getStderr();
 317         Pattern pattern = Pattern.compile(regexp, Pattern.MULTILINE);
 318         Matcher matcher = pattern.matcher(stdout);
 319         if (matcher.find()) {
 320             reportDiagnosticSummary();
 321             throw new RuntimeException("'" + regexp
 322                     + "' found in stdout: '" + matcher.group() + "' \n");
 323         }


 324         matcher = pattern.matcher(stderr);
 325         if (matcher.find()) {
 326             reportDiagnosticSummary();
 327             throw new RuntimeException("'" + regexp
 328                     + "' found in stderr: '" + matcher.group() + "' \n");
 329         }

 330         return this;
 331     }
 332 
 333     /**
 334      * Verify that the stdout contents of output buffer does not match the
 335      * pattern
 336      *
 337      * @param regexp
 338      * @throws RuntimeException If the pattern was found
 339      */
 340     public OutputAnalyzer stdoutShouldNotMatch(String regexp) {
 341         String stdout = getStdout();
 342         Matcher matcher = Pattern.compile(regexp, Pattern.MULTILINE).matcher(stdout);
 343         if (matcher.find()) {
 344             reportDiagnosticSummary();
 345             throw new RuntimeException("'" + regexp
 346                     + "' found in stdout \n");
 347         }
 348         return this;
 349     }


 358     public OutputAnalyzer stderrShouldNotMatch(String regexp) {
 359         String stderr = getStderr();
 360         Matcher matcher = Pattern.compile(regexp, Pattern.MULTILINE).matcher(stderr);
 361         if (matcher.find()) {
 362             reportDiagnosticSummary();
 363             throw new RuntimeException("'" + regexp
 364                     + "' found in stderr \n");
 365         }
 366         return this;
 367     }
 368 
 369     /**
 370      * Get the captured group of the first string matching the pattern.
 371      * stderr is searched before stdout.
 372      *
 373      * @param regexp The multi-line pattern to match
 374      * @param group The group to capture
 375      * @return The matched string or null if no match was found
 376      */
 377     public String firstMatch(String regexp, int group) {
 378         String stdout = getStdout();
 379         String stderr = getStderr();
 380         Pattern pattern = Pattern.compile(regexp, Pattern.MULTILINE);

 381         Matcher stderrMatcher = pattern.matcher(stderr);
 382         Matcher stdoutMatcher = pattern.matcher(stdout);
 383         if (stderrMatcher.find()) {
 384             return stderrMatcher.group(group);
 385         }


 386         if (stdoutMatcher.find()) {
 387             return stdoutMatcher.group(group);
 388         }
 389         return null;
 390     }
 391 
 392     /**
 393      * Get the first string matching the pattern.
 394      * stderr is searched before stdout.
 395      *
 396      * @param pattern The multi-line pattern to match
 397      * @return The matched string or null if no match was found
 398      */
 399     public String firstMatch(String pattern) {
 400         return firstMatch(pattern, 0);
 401     }
 402 
 403     /**
 404      * Verify the exit value of the process
 405      *


 498     /**
 499      * Get the process exit value
 500      *
 501      * @return Process exit value
 502      */
 503     public int getExitValue() {
 504         return buffer.getExitValue();
 505     }
 506 
 507     /**
 508      * Get the contents of the output buffer (stdout and stderr) as list of strings.
 509      * Output will be split by newlines.
 510      *
 511      * @return Contents of the output buffer as list of strings
 512      */
 513     public List<String> asLines() {
 514         return asLines(getOutput());
 515     }
 516 
 517     private List<String> asLines(String buffer) {
 518         return Arrays.asList(buffer.split("(\\r\\n|\\n|\\r)"));
 519     }
 520 
 521 
 522     private static final String jvmwarningmsg = ".* VM warning:.*";
 523 
 524     /**
 525      * Verifies that the stdout and stderr contents of output buffer are empty, after
 526      * filtering out the HotSpot warning messages.
 527      *
 528      * @throws RuntimeException If the stdout and stderr are not empty
 529      */
 530     public OutputAnalyzer shouldBeEmptyIgnoreVMWarnings() {
 531         String stdout = getStdout();
 532         String stderr = getStderr();
 533         if (!stdout.isEmpty()) {
 534             reportDiagnosticSummary();
 535             throw new RuntimeException("stdout was not empty");
 536         }
 537         if (!stderr.replaceAll(jvmwarningmsg + "\\R", "").isEmpty()) {
 538             reportDiagnosticSummary();


 554         if (!matcher.find()) {
 555             reportDiagnosticSummary();
 556             throw new RuntimeException("'" + pattern
 557                   + "' missing from stderr \n");
 558         }
 559         return this;
 560     }
 561 
 562     /**
 563      * Returns the contents of the output buffer (stdout and stderr), without those
 564      * JVM warning msgs, as list of strings. Output is split by newlines.
 565      *
 566      * @return Contents of the output buffer as list of strings
 567      */
 568     public List<String> asLinesWithoutVMWarnings() {
 569         return Arrays.stream(getOutput().split("\\R"))
 570                      .filter(Pattern.compile(jvmwarningmsg).asPredicate().negate())
 571                      .collect(Collectors.toList());
 572     }
 573 
 574 
 575     /**
 576      * @see #shouldMatchByLine(String, String, String)
 577      */
 578     public int shouldMatchByLine(String pattern) {
 579         return shouldMatchByLine(null, null, pattern);
 580     }
 581 
 582     /**
 583      * @see #stdoutShouldMatchByLine(String, String, String)
 584      */
 585     public int stdoutShouldMatchByLine(String pattern) {
 586         return stdoutShouldMatchByLine(null, null, pattern);
 587     }
 588 
 589     /**
 590      * @see #shouldMatchByLine(String, String, String)
 591      */
 592     public int shouldMatchByLineFrom(String from, String pattern) {
 593         return shouldMatchByLine(from, null, pattern);
 594     }
 595 
 596     /**
 597      * @see #shouldMatchByLine(String, String, String)
 598      */
 599     public int shouldMatchByLineTo(String to, String pattern) {
 600         return shouldMatchByLine(null, to, pattern);
 601     }
 602 
 603     /**
 604      * Verify that the stdout and stderr contents of output buffer match the
 605      * {@code pattern} line by line. The whole output could be matched or
 606      * just a subset of it.
 607      *
 608      * @param from
 609      *            The line from where output will be matched.
 610      *            Set {@code from} to null for matching from the first line.
 611      * @param to
 612      *            The line until where output will be matched.
 613      *            Set {@code to} to null for matching until the last line.
 614      * @param pattern
 615      *            Matching pattern
 616      * @return Count of lines which match the {@code pattern}
 617      */
 618     public int shouldMatchByLine(String from, String to, String pattern) {
 619         return shouldMatchByLine(getOutput(), from, to, pattern);
 620     }
 621 
 622     /**
 623      * Verify that the stdout contents of output buffer matches the
 624      * {@code pattern} line by line. The whole stdout could be matched or
 625      * just a subset of it.
 626      *
 627      * @param from
 628      *            The line from where stdout will be matched.
 629      *            Set {@code from} to null for matching from the first line.
 630      * @param to
 631      *            The line until where stdout will be matched.
 632      *            Set {@code to} to null for matching until the last line.
 633      * @param pattern
 634      *            Matching pattern
 635      * @return Count of lines which match the {@code pattern}
 636      */
 637     public int stdoutShouldMatchByLine(String from, String to, String pattern) {
 638         return shouldMatchByLine(getStdout(), from, to, pattern);
 639     }
 640 
 641     private int shouldMatchByLine(String buffer, String from, String to, String pattern) {
 642         List<String> lines = asLines(buffer);
 643 
 644         int fromIndex = 0;
 645         if (from != null) {
 646             fromIndex = indexOf(lines, from);
 647             Asserts.assertGreaterThan(fromIndex, -1,
 648                     "The line/pattern '" + from + "' from where the output should match can not be found");
 649         }
 650 
 651         int toIndex = lines.size();
 652         if (to != null) {
 653             toIndex = indexOf(lines, to);
 654             Asserts.assertGreaterThan(toIndex, -1,
 655                     "The line/pattern '" + to + "' until where the output should match can not be found");
 656         }
 657 
 658         List<String> subList = lines.subList(fromIndex, toIndex);
 659         int matchedCount = 0;
 660         for (String line : subList) {
 661             Asserts.assertTrue(line.matches(pattern),
 662                     "The line '" + line + "' does not match pattern '" + pattern + "'");
 663             matchedCount++;
 664         }

 665 
 666         return matchedCount;
 667     }
 668 
 669     /**
 670      * Check if there is a line matching {@code pattern} and return its index
 671      *
 672      * @param pattern Matching pattern
 673      * @return Index of first matching line
 674      */
 675     private int indexOf(List<String> lines, String pattern) {

 676         for (int i = 0; i < lines.size(); i++) {
 677             if (lines.get(i).matches(pattern)) {
 678                 return i;
 679             }
 680         }
 681         return -1;
 682     }
 683 
 684 }


 296     public OutputAnalyzer stderrShouldMatch(String pattern) {
 297         String stderr = getStderr();
 298         Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
 299         if (!matcher.find()) {
 300             reportDiagnosticSummary();
 301             throw new RuntimeException("'" + pattern
 302                   + "' missing from stderr \n");
 303         }
 304         return this;
 305     }
 306 
 307     /**
 308      * Verify that the stdout and stderr contents of output buffer does not
 309      * match the pattern
 310      *
 311      * @param regexp
 312      * @throws RuntimeException If the pattern was found
 313      */
 314     public OutputAnalyzer shouldNotMatch(String regexp) {
 315         String stdout = getStdout();

 316         Pattern pattern = Pattern.compile(regexp, Pattern.MULTILINE);
 317         Matcher matcher = pattern.matcher(stdout);
 318         if (matcher.find()) {
 319             reportDiagnosticSummary();
 320             throw new RuntimeException("'" + regexp
 321                     + "' found in stdout: '" + matcher.group() + "' \n");
 322         }
 323 
 324         String stderr = getStderr();
 325         matcher = pattern.matcher(stderr);
 326         if (matcher.find()) {
 327             reportDiagnosticSummary();
 328             throw new RuntimeException("'" + regexp
 329                     + "' found in stderr: '" + matcher.group() + "' \n");
 330         }
 331 
 332         return this;
 333     }
 334 
 335     /**
 336      * Verify that the stdout contents of output buffer does not match the
 337      * pattern
 338      *
 339      * @param regexp
 340      * @throws RuntimeException If the pattern was found
 341      */
 342     public OutputAnalyzer stdoutShouldNotMatch(String regexp) {
 343         String stdout = getStdout();
 344         Matcher matcher = Pattern.compile(regexp, Pattern.MULTILINE).matcher(stdout);
 345         if (matcher.find()) {
 346             reportDiagnosticSummary();
 347             throw new RuntimeException("'" + regexp
 348                     + "' found in stdout \n");
 349         }
 350         return this;
 351     }


 360     public OutputAnalyzer stderrShouldNotMatch(String regexp) {
 361         String stderr = getStderr();
 362         Matcher matcher = Pattern.compile(regexp, Pattern.MULTILINE).matcher(stderr);
 363         if (matcher.find()) {
 364             reportDiagnosticSummary();
 365             throw new RuntimeException("'" + regexp
 366                     + "' found in stderr \n");
 367         }
 368         return this;
 369     }
 370 
 371     /**
 372      * Get the captured group of the first string matching the pattern.
 373      * stderr is searched before stdout.
 374      *
 375      * @param regexp The multi-line pattern to match
 376      * @param group The group to capture
 377      * @return The matched string or null if no match was found
 378      */
 379     public String firstMatch(String regexp, int group) {


 380         Pattern pattern = Pattern.compile(regexp, Pattern.MULTILINE);
 381         String stderr = getStderr();
 382         Matcher stderrMatcher = pattern.matcher(stderr);

 383         if (stderrMatcher.find()) {
 384             return stderrMatcher.group(group);
 385         }
 386         String stdout = getStdout();
 387         Matcher stdoutMatcher = pattern.matcher(stdout);
 388         if (stdoutMatcher.find()) {
 389             return stdoutMatcher.group(group);
 390         }
 391         return null;
 392     }
 393 
 394     /**
 395      * Get the first string matching the pattern.
 396      * stderr is searched before stdout.
 397      *
 398      * @param pattern The multi-line pattern to match
 399      * @return The matched string or null if no match was found
 400      */
 401     public String firstMatch(String pattern) {
 402         return firstMatch(pattern, 0);
 403     }
 404 
 405     /**
 406      * Verify the exit value of the process
 407      *


 500     /**
 501      * Get the process exit value
 502      *
 503      * @return Process exit value
 504      */
 505     public int getExitValue() {
 506         return buffer.getExitValue();
 507     }
 508 
 509     /**
 510      * Get the contents of the output buffer (stdout and stderr) as list of strings.
 511      * Output will be split by newlines.
 512      *
 513      * @return Contents of the output buffer as list of strings
 514      */
 515     public List<String> asLines() {
 516         return asLines(getOutput());
 517     }
 518 
 519     private List<String> asLines(String buffer) {
 520         return Arrays.asList(buffer.split("\\R"));
 521     }
 522 
 523 
 524     private static final String jvmwarningmsg = ".* VM warning:.*";
 525 
 526     /**
 527      * Verifies that the stdout and stderr contents of output buffer are empty, after
 528      * filtering out the HotSpot warning messages.
 529      *
 530      * @throws RuntimeException If the stdout and stderr are not empty
 531      */
 532     public OutputAnalyzer shouldBeEmptyIgnoreVMWarnings() {
 533         String stdout = getStdout();
 534         String stderr = getStderr();
 535         if (!stdout.isEmpty()) {
 536             reportDiagnosticSummary();
 537             throw new RuntimeException("stdout was not empty");
 538         }
 539         if (!stderr.replaceAll(jvmwarningmsg + "\\R", "").isEmpty()) {
 540             reportDiagnosticSummary();


 556         if (!matcher.find()) {
 557             reportDiagnosticSummary();
 558             throw new RuntimeException("'" + pattern
 559                   + "' missing from stderr \n");
 560         }
 561         return this;
 562     }
 563 
 564     /**
 565      * Returns the contents of the output buffer (stdout and stderr), without those
 566      * JVM warning msgs, as list of strings. Output is split by newlines.
 567      *
 568      * @return Contents of the output buffer as list of strings
 569      */
 570     public List<String> asLinesWithoutVMWarnings() {
 571         return Arrays.stream(getOutput().split("\\R"))
 572                      .filter(Pattern.compile(jvmwarningmsg).asPredicate().negate())
 573                      .collect(Collectors.toList());
 574     }
 575 

 576     /**
 577      * @see #shouldMatchByLine(String, String, String)
 578      */
 579     public OutputAnalyzer shouldMatchByLine(String pattern) {
 580         return shouldMatchByLine(null, null, pattern);
 581     }
 582 
 583     /**
 584      * @see #stdoutShouldMatchByLine(String, String, String)
 585      */
 586     public OutputAnalyzer stdoutShouldMatchByLine(String pattern) {
 587         return stdoutShouldMatchByLine(null, null, pattern);
 588     }
 589 
 590     /**
 591      * @see #shouldMatchByLine(String, String, String)
 592      */
 593     public OutputAnalyzer shouldMatchByLineFrom(String from, String pattern) {
 594         return shouldMatchByLine(from, null, pattern);
 595     }
 596 
 597     /**
 598      * @see #shouldMatchByLine(String, String, String)
 599      */
 600     public OutputAnalyzer shouldMatchByLineTo(String to, String pattern) {
 601         return shouldMatchByLine(null, to, pattern);
 602     }
 603 
 604     /**
 605      * Verify that the stdout and stderr contents of output buffer match the
 606      * {@code pattern} line by line. The whole output could be matched or
 607      * just a subset of it.
 608      *
 609      * @param from
 610      *            The line from where output will be matched.
 611      *            Set {@code from} to null for matching from the first line.
 612      * @param to
 613      *            The line until where output will be matched.
 614      *            Set {@code to} to null for matching until the last line.
 615      * @param pattern
 616      *            Matching pattern

 617      */
 618     public OutputAnalyzer shouldMatchByLine(String from, String to, String pattern) {
 619         return shouldMatchByLine(getOutput(), from, to, pattern);
 620     }
 621 
 622     /**
 623      * Verify that the stdout contents of output buffer matches the
 624      * {@code pattern} line by line. The whole stdout could be matched or
 625      * just a subset of it.
 626      *
 627      * @param from
 628      *            The line from where stdout will be matched.
 629      *            Set {@code from} to null for matching from the first line.
 630      * @param to
 631      *            The line until where stdout will be matched.
 632      *            Set {@code to} to null for matching until the last line.
 633      * @param pattern
 634      *            Matching pattern

 635      */
 636     public OutputAnalyzer stdoutShouldMatchByLine(String from, String to, String pattern) {
 637         return shouldMatchByLine(getStdout(), from, to, pattern);
 638     }
 639 
 640     private OutputAnalyzer shouldMatchByLine(String buffer, String from, String to, String pattern) {
 641         List<String> lines = asLines(buffer);
 642 
 643         int fromIndex = 0;
 644         if (from != null) {
 645             fromIndex = indexOf(lines, from);
 646             Asserts.assertGreaterThan(fromIndex, -1,
 647                     "The line/pattern '" + from + "' from where the output should match can not be found");
 648         }
 649 
 650         int toIndex = lines.size();
 651         if (to != null) {
 652             toIndex = indexOf(lines, to);
 653             Asserts.assertGreaterThan(toIndex, -1,
 654                     "The line/pattern '" + to + "' until where the output should match can not be found");
 655         }
 656 
 657         List<String> subList = lines.subList(fromIndex, toIndex);
 658         Asserts.assertFalse(subList.isEmpty(), "There is no lines to check");
 659 
 660         subList.stream()
 661                .filter(Pattern.compile(pattern).asPredicate().negate())
 662                .findAny()
 663                .ifPresent(line -> Asserts.assertTrue(false,
 664                        "The line '" + line + "' does not match pattern '" + pattern + "'"));
 665 
 666         return this;
 667     }
 668 
 669     /**
 670      * Check if there is a line matching {@code regexp} and return its index
 671      *
 672      * @param regexp Matching pattern
 673      * @return Index of first matching line
 674      */
 675     private int indexOf(List<String> lines, String regexp) {
 676         Pattern pattern = Pattern.compile(regexp);
 677         for (int i = 0; i < lines.size(); i++) {
 678             if (pattern.matcher(lines.get(i)).matches()) {
 679                 return i;
 680             }
 681         }
 682         return -1;
 683     }
 684 
 685 }
< prev index next >