test/lib/jdk/test/lib/process/OutputAnalyzer.java
branchdatagramsocketimpl-branch
changeset 58678 9cf78a70fa4f
parent 54575 84054d68bf85
child 58679 9c3209ff7550
equal deleted inserted replaced
58677:13588c901957 58678:9cf78a70fa4f
    25 
    25 
    26 import jdk.test.lib.Asserts;
    26 import jdk.test.lib.Asserts;
    27 
    27 
    28 import java.io.IOException;
    28 import java.io.IOException;
    29 import java.io.PrintStream;
    29 import java.io.PrintStream;
       
    30 import java.nio.charset.Charset;
    30 import java.nio.file.Files;
    31 import java.nio.file.Files;
    31 import java.nio.file.Path;
    32 import java.nio.file.Path;
    32 import java.util.Arrays;
    33 import java.util.Arrays;
    33 import java.util.List;
    34 import java.util.List;
    34 import java.util.stream.Collectors;
    35 import java.util.stream.Collectors;
    41     /**
    42     /**
    42      * Create an OutputAnalyzer, a utility class for verifying output and exit
    43      * Create an OutputAnalyzer, a utility class for verifying output and exit
    43      * value from a Process
    44      * value from a Process
    44      *
    45      *
    45      * @param process Process to analyze
    46      * @param process Process to analyze
       
    47      * @param cs The charset used to convert stdout/stderr from bytes to chars
       
    48      *           or null for the default charset.
       
    49      * @throws IOException If an I/O error occurs.
       
    50      */
       
    51     public OutputAnalyzer(Process process, Charset cs) throws IOException {
       
    52         buffer = OutputBuffer.of(process, cs);
       
    53     }
       
    54     /**
       
    55      * Create an OutputAnalyzer, a utility class for verifying output and exit
       
    56      * value from a Process
       
    57      *
       
    58      * @param process Process to analyze
    46      * @throws IOException If an I/O error occurs.
    59      * @throws IOException If an I/O error occurs.
    47      */
    60      */
    48     public OutputAnalyzer(Process process) throws IOException {
    61     public OutputAnalyzer(Process process) throws IOException {
    49         buffer = OutputBuffer.of(process);
    62         buffer = OutputBuffer.of(process);
    50     }
    63     }
    73      * @param stdout stdout buffer to analyze
    86      * @param stdout stdout buffer to analyze
    74      * @param stderr stderr buffer to analyze
    87      * @param stderr stderr buffer to analyze
    75      */
    88      */
    76     public OutputAnalyzer(String stdout, String stderr) {
    89     public OutputAnalyzer(String stdout, String stderr) {
    77         buffer = OutputBuffer.of(stdout, stderr);
    90         buffer = OutputBuffer.of(stdout, stderr);
       
    91     }
       
    92 
       
    93     /**
       
    94      * Create an OutputAnalyzer, a utility class for verifying output
       
    95      *
       
    96      * @param stdout stdout buffer to analyze
       
    97      * @param stderr stderr buffer to analyze
       
    98      * @param stderr exitValue result to analyze
       
    99      */
       
   100     public OutputAnalyzer(String stdout, String stderr, int exitValue)
       
   101     {
       
   102         buffer = OutputBuffer.of(stdout, stderr, exitValue);
    78     }
   103     }
    79 
   104 
    80     /**
   105     /**
    81      * Verify that the stdout contents of output buffer is empty
   106      * Verify that the stdout contents of output buffer is empty
    82      *
   107      *
   621      * Verify that the stdout and stderr contents of output buffer match the
   646      * Verify that the stdout and stderr contents of output buffer match the
   622      * {@code pattern} line by line. The whole output could be matched or
   647      * {@code pattern} line by line. The whole output could be matched or
   623      * just a subset of it.
   648      * just a subset of it.
   624      *
   649      *
   625      * @param from
   650      * @param from
   626      *            The line from where output will be matched.
   651      *            The line (excluded) from where output will be matched.
   627      *            Set {@code from} to null for matching from the first line.
   652      *            Set {@code from} to null for matching from the first line.
   628      * @param to
   653      * @param to
   629      *            The line until where output will be matched.
   654      *            The line (excluded) until where output will be matched.
   630      *            Set {@code to} to null for matching until the last line.
   655      *            Set {@code to} to null for matching until the last line.
   631      * @param pattern
   656      * @param pattern
   632      *            Matching pattern
   657      *            Matching pattern
   633      */
   658      */
   634     public OutputAnalyzer shouldMatchByLine(String from, String to, String pattern) {
   659     public OutputAnalyzer shouldMatchByLine(String from, String to, String pattern) {
   639      * Verify that the stdout contents of output buffer matches the
   664      * Verify that the stdout contents of output buffer matches the
   640      * {@code pattern} line by line. The whole stdout could be matched or
   665      * {@code pattern} line by line. The whole stdout could be matched or
   641      * just a subset of it.
   666      * just a subset of it.
   642      *
   667      *
   643      * @param from
   668      * @param from
   644      *            The line from where stdout will be matched.
   669      *            The line (excluded) from where stdout will be matched.
   645      *            Set {@code from} to null for matching from the first line.
   670      *            Set {@code from} to null for matching from the first line.
   646      * @param to
   671      * @param to
   647      *            The line until where stdout will be matched.
   672      *            The line (excluded) until where stdout will be matched.
   648      *            Set {@code to} to null for matching until the last line.
   673      *            Set {@code to} to null for matching until the last line.
   649      * @param pattern
   674      * @param pattern
   650      *            Matching pattern
   675      *            Matching pattern
   651      */
   676      */
   652     public OutputAnalyzer stdoutShouldMatchByLine(String from, String to, String pattern) {
   677     public OutputAnalyzer stdoutShouldMatchByLine(String from, String to, String pattern) {
   656     private OutputAnalyzer shouldMatchByLine(String buffer, String from, String to, String pattern) {
   681     private OutputAnalyzer shouldMatchByLine(String buffer, String from, String to, String pattern) {
   657         List<String> lines = asLines(buffer);
   682         List<String> lines = asLines(buffer);
   658 
   683 
   659         int fromIndex = 0;
   684         int fromIndex = 0;
   660         if (from != null) {
   685         if (from != null) {
   661             fromIndex = indexOf(lines, from);
   686             fromIndex = indexOf(lines, from, 0) + 1; // + 1 -> apply 'pattern' to lines after 'from' match
   662             Asserts.assertGreaterThan(fromIndex, -1,
   687             Asserts.assertGreaterThan(fromIndex, 0,
   663                     "The line/pattern '" + from + "' from where the output should match can not be found");
   688                     "The line/pattern '" + from + "' from where the output should match can not be found");
   664         }
   689         }
   665 
   690 
   666         int toIndex = lines.size();
   691         int toIndex = lines.size();
   667         if (to != null) {
   692         if (to != null) {
   668             toIndex = indexOf(lines, to);
   693             toIndex = indexOf(lines, to, fromIndex);
   669             Asserts.assertGreaterThan(toIndex, -1,
   694             Asserts.assertGreaterThan(toIndex, fromIndex,
   670                     "The line/pattern '" + to + "' until where the output should match can not be found");
   695                     "The line/pattern '" + to + "' until where the output should match can not be found");
   671         }
   696         }
   672 
   697 
   673         List<String> subList = lines.subList(fromIndex, toIndex);
   698         List<String> subList = lines.subList(fromIndex, toIndex);
   674         Asserts.assertFalse(subList.isEmpty(), "There are no lines to check");
   699         Asserts.assertFalse(subList.isEmpty(), "There are no lines to check:"
       
   700                 + " range " + fromIndex + ".." + toIndex + ", subList = " + subList);
   675 
   701 
   676         subList.stream()
   702         subList.stream()
   677                .filter(Pattern.compile(pattern).asPredicate().negate())
   703                .filter(Pattern.compile(pattern).asPredicate().negate())
   678                .findAny()
   704                .findAny()
   679                .ifPresent(line -> Asserts.assertTrue(false,
   705                .ifPresent(line -> Asserts.fail(
   680                        "The line '" + line + "' does not match pattern '" + pattern + "'"));
   706                        "The line '" + line + "' does not match pattern '" + pattern + "'"));
   681 
   707 
   682         return this;
   708         return this;
   683     }
   709     }
   684 
   710 
   685     /**
   711     /**
   686      * Check if there is a line matching {@code regexp} and return its index
   712      * Check if there is a line matching {@code regexp} and return its index
   687      *
   713      *
   688      * @param regexp Matching pattern
   714      * @param regexp Matching pattern
       
   715      * @param fromIndex Start matching after so many lines skipped
   689      * @return Index of first matching line
   716      * @return Index of first matching line
   690      */
   717      */
   691     private int indexOf(List<String> lines, String regexp) {
   718     private int indexOf(List<String> lines, String regexp, int fromIndex) {
   692         Pattern pattern = Pattern.compile(regexp);
   719         Pattern pattern = Pattern.compile(regexp);
   693         for (int i = 0; i < lines.size(); i++) {
   720         for (int i = fromIndex; i < lines.size(); i++) {
   694             if (pattern.matcher(lines.get(i)).matches()) {
   721             if (pattern.matcher(lines.get(i)).matches()) {
   695                 return i;
   722                 return i;
   696             }
   723             }
   697         }
   724         }
   698         return -1;
   725         return -1;