test/lib/jdk/test/lib/process/OutputAnalyzer.java
changeset 57488 94691d8e746f
parent 55147 d22206f24d59
child 57564 0a8436eda2fa
equal deleted inserted replaced
57487:643978a35f6e 57488:94691d8e746f
   633      * Verify that the stdout and stderr contents of output buffer match the
   633      * Verify that the stdout and stderr contents of output buffer match the
   634      * {@code pattern} line by line. The whole output could be matched or
   634      * {@code pattern} line by line. The whole output could be matched or
   635      * just a subset of it.
   635      * just a subset of it.
   636      *
   636      *
   637      * @param from
   637      * @param from
   638      *            The line from where output will be matched.
   638      *            The line (excluded) from where output will be matched.
   639      *            Set {@code from} to null for matching from the first line.
   639      *            Set {@code from} to null for matching from the first line.
   640      * @param to
   640      * @param to
   641      *            The line until where output will be matched.
   641      *            The line (excluded) until where output will be matched.
   642      *            Set {@code to} to null for matching until the last line.
   642      *            Set {@code to} to null for matching until the last line.
   643      * @param pattern
   643      * @param pattern
   644      *            Matching pattern
   644      *            Matching pattern
   645      */
   645      */
   646     public OutputAnalyzer shouldMatchByLine(String from, String to, String pattern) {
   646     public OutputAnalyzer shouldMatchByLine(String from, String to, String pattern) {
   651      * Verify that the stdout contents of output buffer matches the
   651      * Verify that the stdout contents of output buffer matches the
   652      * {@code pattern} line by line. The whole stdout could be matched or
   652      * {@code pattern} line by line. The whole stdout could be matched or
   653      * just a subset of it.
   653      * just a subset of it.
   654      *
   654      *
   655      * @param from
   655      * @param from
   656      *            The line from where stdout will be matched.
   656      *            The line (excluded) from where stdout will be matched.
   657      *            Set {@code from} to null for matching from the first line.
   657      *            Set {@code from} to null for matching from the first line.
   658      * @param to
   658      * @param to
   659      *            The line until where stdout will be matched.
   659      *            The line (excluded) until where stdout will be matched.
   660      *            Set {@code to} to null for matching until the last line.
   660      *            Set {@code to} to null for matching until the last line.
   661      * @param pattern
   661      * @param pattern
   662      *            Matching pattern
   662      *            Matching pattern
   663      */
   663      */
   664     public OutputAnalyzer stdoutShouldMatchByLine(String from, String to, String pattern) {
   664     public OutputAnalyzer stdoutShouldMatchByLine(String from, String to, String pattern) {
   668     private OutputAnalyzer shouldMatchByLine(String buffer, String from, String to, String pattern) {
   668     private OutputAnalyzer shouldMatchByLine(String buffer, String from, String to, String pattern) {
   669         List<String> lines = asLines(buffer);
   669         List<String> lines = asLines(buffer);
   670 
   670 
   671         int fromIndex = 0;
   671         int fromIndex = 0;
   672         if (from != null) {
   672         if (from != null) {
   673             fromIndex = indexOf(lines, from);
   673             fromIndex = indexOf(lines, from, 0) + 1; // + 1 -> apply 'pattern' to lines after 'from' match
   674             Asserts.assertGreaterThan(fromIndex, -1,
   674             Asserts.assertGreaterThan(fromIndex, 0,
   675                     "The line/pattern '" + from + "' from where the output should match can not be found");
   675                     "The line/pattern '" + from + "' from where the output should match can not be found");
   676         }
   676         }
   677 
   677 
   678         int toIndex = lines.size();
   678         int toIndex = lines.size();
   679         if (to != null) {
   679         if (to != null) {
   680             toIndex = indexOf(lines, to);
   680             toIndex = indexOf(lines, to, fromIndex);
   681             Asserts.assertGreaterThan(toIndex, -1,
   681             Asserts.assertGreaterThan(toIndex, fromIndex,
   682                     "The line/pattern '" + to + "' until where the output should match can not be found");
   682                     "The line/pattern '" + to + "' until where the output should match can not be found");
   683         }
   683         }
   684 
   684 
   685         List<String> subList = lines.subList(fromIndex, toIndex);
   685         List<String> subList = lines.subList(fromIndex, toIndex);
   686         Asserts.assertFalse(subList.isEmpty(), "There are no lines to check");
   686         Asserts.assertFalse(subList.isEmpty(), "There are no lines to check:"
       
   687                 + " range " + fromIndex + ".." + toIndex + ", subList = " + subList);
   687 
   688 
   688         subList.stream()
   689         subList.stream()
   689                .filter(Pattern.compile(pattern).asPredicate().negate())
   690                .filter(Pattern.compile(pattern).asPredicate().negate())
   690                .findAny()
   691                .findAny()
   691                .ifPresent(line -> Asserts.assertTrue(false,
   692                .ifPresent(line -> Asserts.fail(
   692                        "The line '" + line + "' does not match pattern '" + pattern + "'"));
   693                        "The line '" + line + "' does not match pattern '" + pattern + "'"));
   693 
   694 
   694         return this;
   695         return this;
   695     }
   696     }
   696 
   697 
   697     /**
   698     /**
   698      * Check if there is a line matching {@code regexp} and return its index
   699      * Check if there is a line matching {@code regexp} and return its index
   699      *
   700      *
   700      * @param regexp Matching pattern
   701      * @param regexp Matching pattern
       
   702      * @param fromIndex Start matching after so many lines skipped
   701      * @return Index of first matching line
   703      * @return Index of first matching line
   702      */
   704      */
   703     private int indexOf(List<String> lines, String regexp) {
   705     private int indexOf(List<String> lines, String regexp, int fromIndex) {
   704         Pattern pattern = Pattern.compile(regexp);
   706         Pattern pattern = Pattern.compile(regexp);
   705         for (int i = 0; i < lines.size(); i++) {
   707         for (int i = fromIndex; i < lines.size(); i++) {
   706             if (pattern.matcher(lines.get(i)).matches()) {
   708             if (pattern.matcher(lines.get(i)).matches()) {
   707                 return i;
   709                 return i;
   708             }
   710             }
   709         }
   711         }
   710         return -1;
   712         return -1;