8066106: sun/tools/jps/TestJpsClass.java failed to remove stale attach pid file

Reviewed-by: jbachorik
This commit is contained in:
Katja Kantserova 2014-12-01 09:49:44 +01:00
parent f12ce83a25
commit e325ce88cb
3 changed files with 57 additions and 11 deletions

View File

@ -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);

View File

@ -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<String> asLines() {
return asLines(getOutput());
}
private List<String> asLines(String buffer) {
List<String> 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<String> 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<String> lines = asLines(buffer);
int fromIndex = 0;
if (from != null) {
@ -500,4 +530,5 @@ public final class OutputAnalyzer {
return matchedCount;
}
}

View File

@ -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");
}
/**