From e325ce88cbf869875377e19780ee28f59a6e321b Mon Sep 17 00:00:00 2001 From: Katja Kantserova Date: Mon, 1 Dec 2014 09:49:44 +0100 Subject: [PATCH] 8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file Reviewed-by: jbachorik --- .../lib/testlibrary/OutputAnalyzerTest.java | 15 +++++++ .../jdk/testlibrary/OutputAnalyzer.java | 43 ++++++++++++++++--- jdk/test/sun/tools/jps/JpsHelper.java | 10 ++--- 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/jdk/test/lib/testlibrary/OutputAnalyzerTest.java b/jdk/test/lib/testlibrary/OutputAnalyzerTest.java index af2b8f6b14a..cb251b37964 100644 --- a/jdk/test/lib/testlibrary/OutputAnalyzerTest.java +++ b/jdk/test/lib/testlibrary/OutputAnalyzerTest.java @@ -112,8 +112,10 @@ public class OutputAnalyzerTest { } String stdoutPattern = "[a]"; + String stdoutByLinePattern = "a*"; String stderrPattern = "[b]"; String nonExistingPattern = "[c]"; + String byLinePattern = "[ab]*"; // Should match try { @@ -148,6 +150,19 @@ public class OutputAnalyzerTest { // expected } + if (output.shouldMatchByLine(byLinePattern) != 1) { + throw new Exception("shouldMatchByLine() should find one line"); + } + try { + output.shouldMatchByLine(nonExistingPattern); + throw new Exception("shouldMatchByLine() failed to throw exception"); + } catch (RuntimeException e) { + // expected + } + if (output.stdoutShouldMatchByLine(stdoutByLinePattern) != 1) { + throw new Exception("stdoutShouldMatchByLine() should find one line"); + } + // Should not match try { output.shouldNotMatch(nonExistingPattern); diff --git a/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java b/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java index ad1d2ab8ea3..88819e4d594 100644 --- a/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java +++ b/jdk/test/lib/testlibrary/jdk/testlibrary/OutputAnalyzer.java @@ -25,13 +25,9 @@ package jdk.testlibrary; import static jdk.testlibrary.Asserts.*; -import java.io.ByteArrayOutputStream; - import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.Future; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -414,8 +410,12 @@ public final class OutputAnalyzer { * @return Contents of the output buffer as list of strings */ public List asLines() { + return asLines(getOutput()); + } + + private List asLines(String buffer) { List l = new ArrayList<>(); - String[] a = getOutput().split(Utils.NEW_LINE); + String[] a = buffer.split(Utils.NEW_LINE); for (String string : a) { l.add(string); } @@ -444,6 +444,13 @@ public final class OutputAnalyzer { return shouldMatchByLine(null, null, pattern); } + /** + * @see #stdoutShouldMatchByLine(String, String, String) + */ + public int stdoutShouldMatchByLine(String pattern) { + return stdoutShouldMatchByLine(null, null, pattern); + } + /** * @see #shouldMatchByLine(String, String, String) */ @@ -474,7 +481,30 @@ public final class OutputAnalyzer { * @return Count of lines which match the {@code pattern} */ public int shouldMatchByLine(String from, String to, String pattern) { - List lines = asLines(); + return shouldMatchByLine(getOutput(), from, to, pattern); + } + + /** + * Verify that the stdout contents of output buffer matches the + * {@code pattern} line by line. The whole stdout could be matched or + * just a subset of it. + * + * @param from + * The line from where stdout will be matched. + * Set {@code from} to null for matching from the first line. + * @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) { + return shouldMatchByLine(getStdout(), from, to, pattern); + } + + private int shouldMatchByLine(String buffer, String from, String to, String pattern) { + List lines = asLines(buffer); int fromIndex = 0; if (from != null) { @@ -500,4 +530,5 @@ public final class OutputAnalyzer { return matchedCount; } + } diff --git a/jdk/test/sun/tools/jps/JpsHelper.java b/jdk/test/sun/tools/jps/JpsHelper.java index 4e00cc95de4..655b5eb465e 100644 --- a/jdk/test/sun/tools/jps/JpsHelper.java +++ b/jdk/test/sun/tools/jps/JpsHelper.java @@ -168,10 +168,8 @@ public final class JpsHelper { } /** - * Verify jps output contains pids and programs' name information. - * The function will discard any lines that come before the first line with pid. - * This can happen if the JVM outputs a warning message for some reason - * before running jps. + * Verify jps stdout contains only pids and programs' name information. + * jps stderr may contain VM warning messages which will be ignored. * * The output can look like: * 35536 Jps @@ -180,8 +178,10 @@ public final class JpsHelper { */ public static void verifyJpsOutput(OutputAnalyzer output, String regex) throws Exception { output.shouldHaveExitValue(0); - int matchedCount = output.shouldMatchByLineFrom(regex, regex); + int matchedCount = output.stdoutShouldMatchByLine(regex); assertGreaterThan(matchedCount , 0, "Found no lines matching pattern: " + regex); + output.stderrShouldNotMatch("[E|e]xception"); + output.stderrShouldNotMatch("[E|e]rror"); } /**