< prev index next >

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

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

@@ -311,24 +311,26 @@
      * @param regexp
      * @throws RuntimeException If the pattern was found
      */
     public OutputAnalyzer shouldNotMatch(String regexp) {
         String stdout = getStdout();
-        String stderr = getStderr();
         Pattern pattern = Pattern.compile(regexp, Pattern.MULTILINE);
         Matcher matcher = pattern.matcher(stdout);
         if (matcher.find()) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + regexp
                     + "' found in stdout: '" + matcher.group() + "' \n");
         }
+
+        String stderr = getStderr();
         matcher = pattern.matcher(stderr);
         if (matcher.find()) {
             reportDiagnosticSummary();
             throw new RuntimeException("'" + regexp
                     + "' found in stderr: '" + matcher.group() + "' \n");
         }
+
         return this;
     }
 
     /**
      * Verify that the stdout contents of output buffer does not match the

@@ -373,18 +375,18 @@
      * @param regexp The multi-line pattern to match
      * @param group The group to capture
      * @return The matched string or null if no match was found
      */
     public String firstMatch(String regexp, int group) {
-        String stdout = getStdout();
-        String stderr = getStderr();
         Pattern pattern = Pattern.compile(regexp, Pattern.MULTILINE);
+        String stderr = getStderr();
         Matcher stderrMatcher = pattern.matcher(stderr);
-        Matcher stdoutMatcher = pattern.matcher(stdout);
         if (stderrMatcher.find()) {
             return stderrMatcher.group(group);
         }
+        String stdout = getStdout();
+        Matcher stdoutMatcher = pattern.matcher(stdout);
         if (stdoutMatcher.find()) {
             return stdoutMatcher.group(group);
         }
         return null;
     }

@@ -513,11 +515,11 @@
     public List<String> asLines() {
         return asLines(getOutput());
     }
 
     private List<String> asLines(String buffer) {
-        return Arrays.asList(buffer.split("(\\r\\n|\\n|\\r)"));
+        return Arrays.asList(buffer.split("\\R"));
     }
 
 
     private static final String jvmwarningmsg = ".* VM warning:.*";
 

@@ -569,36 +571,35 @@
         return Arrays.stream(getOutput().split("\\R"))
                      .filter(Pattern.compile(jvmwarningmsg).asPredicate().negate())
                      .collect(Collectors.toList());
     }
 
-
     /**
      * @see #shouldMatchByLine(String, String, String)
      */
-    public int shouldMatchByLine(String pattern) {
+    public OutputAnalyzer shouldMatchByLine(String pattern) {
         return shouldMatchByLine(null, null, pattern);
     }
 
     /**
      * @see #stdoutShouldMatchByLine(String, String, String)
      */
-    public int stdoutShouldMatchByLine(String pattern) {
+    public OutputAnalyzer stdoutShouldMatchByLine(String pattern) {
         return stdoutShouldMatchByLine(null, null, pattern);
     }
 
     /**
      * @see #shouldMatchByLine(String, String, String)
      */
-    public int shouldMatchByLineFrom(String from, String pattern) {
+    public OutputAnalyzer shouldMatchByLineFrom(String from, String pattern) {
         return shouldMatchByLine(from, null, pattern);
     }
 
     /**
      * @see #shouldMatchByLine(String, String, String)
      */
-    public int shouldMatchByLineTo(String to, String pattern) {
+    public OutputAnalyzer shouldMatchByLineTo(String to, String pattern) {
         return shouldMatchByLine(null, to, pattern);
     }
 
     /**
      * Verify that the stdout and stderr contents of output buffer match the

@@ -611,13 +612,12 @@
      * @param to
      *            The line until where output will be matched.
      *            Set {@code to} to null for matching until the last line.
      * @param pattern
      *            Matching pattern
-     * @return Count of lines which match the {@code pattern}
      */
-    public int shouldMatchByLine(String from, String to, String pattern) {
+    public OutputAnalyzer shouldMatchByLine(String from, String to, String pattern) {
         return shouldMatchByLine(getOutput(), from, to, pattern);
     }
 
     /**
      * Verify that the stdout contents of output buffer matches the

@@ -630,17 +630,16 @@
      * @param to
      *            The line until where stdout will be matched.
      *            Set {@code to} to null for matching until the last line.
      * @param pattern
      *            Matching pattern
-     * @return Count of lines which match the {@code pattern}
      */
-    public int stdoutShouldMatchByLine(String from, String to, String pattern) {
+    public OutputAnalyzer stdoutShouldMatchByLine(String from, String to, String pattern) {
         return shouldMatchByLine(getStdout(), from, to, pattern);
     }
 
-    private int shouldMatchByLine(String buffer, String from, String to, String pattern) {
+    private OutputAnalyzer shouldMatchByLine(String buffer, String from, String to, String pattern) {
         List<String> lines = asLines(buffer);
 
         int fromIndex = 0;
         if (from != null) {
             fromIndex = indexOf(lines, from);

@@ -654,29 +653,31 @@
             Asserts.assertGreaterThan(toIndex, -1,
                     "The line/pattern '" + to + "' until where the output should match can not be found");
         }
 
         List<String> subList = lines.subList(fromIndex, toIndex);
-        int matchedCount = 0;
-        for (String line : subList) {
-            Asserts.assertTrue(line.matches(pattern),
-                    "The line '" + line + "' does not match pattern '" + pattern + "'");
-            matchedCount++;
-        }
+        Asserts.assertFalse(subList.isEmpty(), "There is no lines to check");
+
+        subList.stream()
+               .filter(Pattern.compile(pattern).asPredicate().negate())
+               .findAny()
+               .ifPresent(line -> Asserts.assertTrue(false,
+                       "The line '" + line + "' does not match pattern '" + pattern + "'"));
 
-        return matchedCount;
+        return this;
     }
 
     /**
-     * Check if there is a line matching {@code pattern} and return its index
+     * Check if there is a line matching {@code regexp} and return its index
      *
-     * @param pattern Matching pattern
+     * @param regexp Matching pattern
      * @return Index of first matching line
      */
-    private int indexOf(List<String> lines, String pattern) {
+    private int indexOf(List<String> lines, String regexp) {
+        Pattern pattern = Pattern.compile(regexp);
         for (int i = 0; i < lines.size(); i++) {
-            if (lines.get(i).matches(pattern)) {
+            if (pattern.matcher(lines.get(i)).matches()) {
                 return i;
             }
         }
         return -1;
     }
< prev index next >