8142926: OutputAnalyzer's shouldXXX() calls return this

Reviewed-by: alanb
This commit is contained in:
Weijun Wang 2015-11-14 11:00:40 +08:00
parent 39cb0e2223
commit 0793aa183c

@ -45,7 +45,7 @@ public final class OutputAnalyzer {
* value from a Process. * value from a Process.
* <p> * <p>
* OutputAnalyzer should never be instantiated directly - * OutputAnalyzer should never be instantiated directly -
* use {@linkplain ProcessTools#executeProcess(p)} instead * use {@linkplain ProcessTools#executeProcess(ProcessBuilder)} instead
* *
* @param process * @param process
* Process to analyze * Process to analyze
@ -93,13 +93,14 @@ public final class OutputAnalyzer {
* @throws RuntimeException * @throws RuntimeException
* If the string was not found * If the string was not found
*/ */
public void shouldContain(String expectedString) { public OutputAnalyzer shouldContain(String expectedString) {
if (!getStdout().contains(expectedString) if (!getStdout().contains(expectedString)
&& !getStderr().contains(expectedString)) { && !getStderr().contains(expectedString)) {
reportDiagnosticSummary(); reportDiagnosticSummary();
throw new RuntimeException("'" + expectedString throw new RuntimeException("'" + expectedString
+ "' missing from stdout/stderr \n"); + "' missing from stdout/stderr \n");
} }
return this;
} }
/** /**
@ -110,12 +111,13 @@ public final class OutputAnalyzer {
* @throws RuntimeException * @throws RuntimeException
* If the string was not found * If the string was not found
*/ */
public void stdoutShouldContain(String expectedString) { public OutputAnalyzer stdoutShouldContain(String expectedString) {
if (!getStdout().contains(expectedString)) { if (!getStdout().contains(expectedString)) {
reportDiagnosticSummary(); reportDiagnosticSummary();
throw new RuntimeException("'" + expectedString throw new RuntimeException("'" + expectedString
+ "' missing from stdout \n"); + "' missing from stdout \n");
} }
return this;
} }
/** /**
@ -126,24 +128,25 @@ public final class OutputAnalyzer {
* @throws RuntimeException * @throws RuntimeException
* If the string was not found * If the string was not found
*/ */
public void stderrShouldContain(String expectedString) { public OutputAnalyzer stderrShouldContain(String expectedString) {
if (!getStderr().contains(expectedString)) { if (!getStderr().contains(expectedString)) {
reportDiagnosticSummary(); reportDiagnosticSummary();
throw new RuntimeException("'" + expectedString throw new RuntimeException("'" + expectedString
+ "' missing from stderr \n"); + "' missing from stderr \n");
} }
return this;
} }
/** /**
* Verify that the stdout and stderr contents of output buffer does not * Verify that the stdout and stderr contents of output buffer does not
* contain the string * contain the string
* *
* @param expectedString * @param notExpectedString
* String that the buffer should not contain * String that the buffer should not contain
* @throws RuntimeException * @throws RuntimeException
* If the string was found * If the string was found
*/ */
public void shouldNotContain(String notExpectedString) { public OutputAnalyzer shouldNotContain(String notExpectedString) {
if (getStdout().contains(notExpectedString)) { if (getStdout().contains(notExpectedString)) {
reportDiagnosticSummary(); reportDiagnosticSummary();
throw new RuntimeException("'" + notExpectedString throw new RuntimeException("'" + notExpectedString
@ -154,40 +157,43 @@ public final class OutputAnalyzer {
throw new RuntimeException("'" + notExpectedString throw new RuntimeException("'" + notExpectedString
+ "' found in stderr \n"); + "' found in stderr \n");
} }
return this;
} }
/** /**
* Verify that the stdout contents of output buffer does not contain the * Verify that the stdout contents of output buffer does not contain the
* string * string
* *
* @param expectedString * @param notExpectedString
* String that the buffer should not contain * String that the buffer should not contain
* @throws RuntimeException * @throws RuntimeException
* If the string was found * If the string was found
*/ */
public void stdoutShouldNotContain(String notExpectedString) { public OutputAnalyzer stdoutShouldNotContain(String notExpectedString) {
if (getStdout().contains(notExpectedString)) { if (getStdout().contains(notExpectedString)) {
reportDiagnosticSummary(); reportDiagnosticSummary();
throw new RuntimeException("'" + notExpectedString throw new RuntimeException("'" + notExpectedString
+ "' found in stdout \n"); + "' found in stdout \n");
} }
return this;
} }
/** /**
* Verify that the stderr contents of output buffer does not contain the * Verify that the stderr contents of output buffer does not contain the
* string * string
* *
* @param expectedString * @param notExpectedString
* String that the buffer should not contain * String that the buffer should not contain
* @throws RuntimeException * @throws RuntimeException
* If the string was found * If the string was found
*/ */
public void stderrShouldNotContain(String notExpectedString) { public OutputAnalyzer stderrShouldNotContain(String notExpectedString) {
if (getStderr().contains(notExpectedString)) { if (getStderr().contains(notExpectedString)) {
reportDiagnosticSummary(); reportDiagnosticSummary();
throw new RuntimeException("'" + notExpectedString throw new RuntimeException("'" + notExpectedString
+ "' found in stderr \n"); + "' found in stderr \n");
} }
return this;
} }
/** /**
@ -198,7 +204,7 @@ public final class OutputAnalyzer {
* @throws RuntimeException * @throws RuntimeException
* If the pattern was not found * If the pattern was not found
*/ */
public void shouldMatch(String pattern) { public OutputAnalyzer shouldMatch(String pattern) {
Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE) Matcher stdoutMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
.matcher(getStdout()); .matcher(getStdout());
Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE) Matcher stderrMatcher = Pattern.compile(pattern, Pattern.MULTILINE)
@ -208,6 +214,7 @@ public final class OutputAnalyzer {
throw new RuntimeException("'" + pattern throw new RuntimeException("'" + pattern
+ "' missing from stdout/stderr \n"); + "' missing from stdout/stderr \n");
} }
return this;
} }
/** /**
@ -217,7 +224,7 @@ public final class OutputAnalyzer {
* @throws RuntimeException * @throws RuntimeException
* If the pattern was not found * If the pattern was not found
*/ */
public void stdoutShouldMatch(String pattern) { public OutputAnalyzer stdoutShouldMatch(String pattern) {
Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher( Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
getStdout()); getStdout());
if (!matcher.find()) { if (!matcher.find()) {
@ -225,6 +232,7 @@ public final class OutputAnalyzer {
throw new RuntimeException("'" + pattern throw new RuntimeException("'" + pattern
+ "' missing from stdout \n"); + "' missing from stdout \n");
} }
return this;
} }
/** /**
@ -234,7 +242,7 @@ public final class OutputAnalyzer {
* @throws RuntimeException * @throws RuntimeException
* If the pattern was not found * If the pattern was not found
*/ */
public void stderrShouldMatch(String pattern) { public OutputAnalyzer stderrShouldMatch(String pattern) {
Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher( Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
getStderr()); getStderr());
if (!matcher.find()) { if (!matcher.find()) {
@ -242,6 +250,7 @@ public final class OutputAnalyzer {
throw new RuntimeException("'" + pattern throw new RuntimeException("'" + pattern
+ "' missing from stderr \n"); + "' missing from stderr \n");
} }
return this;
} }
/** /**
@ -252,7 +261,7 @@ public final class OutputAnalyzer {
* @throws RuntimeException * @throws RuntimeException
* If the pattern was found * If the pattern was found
*/ */
public void shouldNotMatch(String pattern) { public OutputAnalyzer shouldNotMatch(String pattern) {
Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher( Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
getStdout()); getStdout());
if (matcher.find()) { if (matcher.find()) {
@ -266,6 +275,7 @@ public final class OutputAnalyzer {
throw new RuntimeException("'" + pattern + "' found in stderr: '" throw new RuntimeException("'" + pattern + "' found in stderr: '"
+ matcher.group() + "' \n"); + matcher.group() + "' \n");
} }
return this;
} }
/** /**
@ -276,13 +286,14 @@ public final class OutputAnalyzer {
* @throws RuntimeException * @throws RuntimeException
* If the pattern was found * If the pattern was found
*/ */
public void stdoutShouldNotMatch(String pattern) { public OutputAnalyzer stdoutShouldNotMatch(String pattern) {
Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher( Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
getStdout()); getStdout());
if (matcher.find()) { if (matcher.find()) {
reportDiagnosticSummary(); reportDiagnosticSummary();
throw new RuntimeException("'" + pattern + "' found in stdout \n"); throw new RuntimeException("'" + pattern + "' found in stdout \n");
} }
return this;
} }
/** /**
@ -293,13 +304,14 @@ public final class OutputAnalyzer {
* @throws RuntimeException * @throws RuntimeException
* If the pattern was found * If the pattern was found
*/ */
public void stderrShouldNotMatch(String pattern) { public OutputAnalyzer stderrShouldNotMatch(String pattern) {
Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher( Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(
getStderr()); getStderr());
if (matcher.find()) { if (matcher.find()) {
reportDiagnosticSummary(); reportDiagnosticSummary();
throw new RuntimeException("'" + pattern + "' found in stderr \n"); throw new RuntimeException("'" + pattern + "' found in stderr \n");
} }
return this;
} }
/** /**
@ -347,12 +359,13 @@ public final class OutputAnalyzer {
* If the exit value from the process did not match the expected * If the exit value from the process did not match the expected
* value * value
*/ */
public void shouldHaveExitValue(int expectedExitValue) { public OutputAnalyzer shouldHaveExitValue(int expectedExitValue) {
if (getExitValue() != expectedExitValue) { if (getExitValue() != expectedExitValue) {
reportDiagnosticSummary(); reportDiagnosticSummary();
throw new RuntimeException("Expected to get exit value of [" throw new RuntimeException("Expected to get exit value of ["
+ expectedExitValue + "]\n"); + expectedExitValue + "]\n");
} }
return this;
} }
/** /**
@ -360,11 +373,12 @@ public final class OutputAnalyzer {
* - standard input produced by the process under test - standard output - * - standard input produced by the process under test - standard output -
* exit code Note: the command line is printed by the ProcessTools * exit code Note: the command line is printed by the ProcessTools
*/ */
private void reportDiagnosticSummary() { private OutputAnalyzer reportDiagnosticSummary() {
String msg = " stdout: [" + getStdout() + "];\n" + " stderr: [" + getStderr() String msg = " stdout: [" + getStdout() + "];\n" + " stderr: [" + getStderr()
+ "]\n" + " exitValue = " + getExitValue() + "\n"; + "]\n" + " exitValue = " + getExitValue() + "\n";
System.err.println(msg); System.err.println(msg);
return this;
} }
/** /**