@@ -354,25 +354,44 @@ public OutputAnalyzer stderrShouldNotContain(String notExpectedString) {
354354 return this ;
355355 }
356356
357+ /**
358+ * Returns true if the pattern can be found in the given string(s).
359+ *
360+ * NOTE: The meaning of "match" in OutputAnalyzer is NOT the same as String.matches().
361+ * Rather it means "can the pattern be found in stdout and/or stderr".
362+ *
363+ * The pattern is comiled with MULTILINE but without DOTALL, so "." doesn't match newline, but
364+ * "^" and "$" matches just after or just before, respectively, a newline.
365+ */
366+ private boolean findPattern (String regexp , String ... strings ) {
367+ Pattern pattern = Pattern .compile (regexp , Pattern .MULTILINE );
368+ for (String s : strings ) {
369+ if (pattern .matcher (s ).find ()) {
370+ return true ;
371+ }
372+ }
373+ return false ;
374+ }
375+
357376 /**
358377 * Returns true if stdout matches the given pattern
359378 */
360379 public boolean stdoutMatches (String regexp ) {
361- return getStdout (). matches ( regexp );
380+ return findPattern ( regexp , getStdout ());
362381 }
363382
364383 /**
365384 * Returns true if stderr matches the given pattern
366385 */
367386 public boolean stderrMatches (String regexp ) {
368- return getStderr (). matches ( regexp );
387+ return findPattern ( regexp , getStderr ());
369388 }
370389
371390 /**
372391 * Returns true if either stdout or stderr matches the given pattern
373392 */
374393 public boolean matches (String regexp ) {
375- return stdoutMatches (regexp ) || stderrMatches ( regexp );
394+ return findPattern (regexp , getStdout (), getStderr () );
376395 }
377396
378397 /**
@@ -383,12 +402,7 @@ public boolean matches(String regexp) {
383402 * @throws RuntimeException If the pattern was not found
384403 */
385404 public OutputAnalyzer shouldMatch (String regexp ) {
386- String stdout = getStdout ();
387- String stderr = getStderr ();
388- Pattern pattern = Pattern .compile (regexp , Pattern .MULTILINE );
389- Matcher stdoutMatcher = pattern .matcher (stdout );
390- Matcher stderrMatcher = pattern .matcher (stderr );
391- if (!stdoutMatcher .find () && !stderrMatcher .find ()) {
405+ if (!matches (regexp )) {
392406 reportDiagnosticSummary ();
393407 throw new RuntimeException ("'" + regexp
394408 + "' missing from stdout/stderr" );
@@ -404,9 +418,7 @@ public OutputAnalyzer shouldMatch(String regexp) {
404418 * @throws RuntimeException If the pattern was not found
405419 */
406420 public OutputAnalyzer stdoutShouldMatch (String regexp ) {
407- String stdout = getStdout ();
408- Matcher matcher = Pattern .compile (regexp , Pattern .MULTILINE ).matcher (stdout );
409- if (!matcher .find ()) {
421+ if (!stdoutMatches (regexp )) {
410422 reportDiagnosticSummary ();
411423 throw new RuntimeException ("'" + regexp
412424 + "' missing from stdout" );
@@ -421,12 +433,10 @@ public OutputAnalyzer stdoutShouldMatch(String regexp) {
421433 * @param pattern
422434 * @throws RuntimeException If the pattern was not found
423435 */
424- public OutputAnalyzer stderrShouldMatch (String pattern ) {
425- String stderr = getStderr ();
426- Matcher matcher = Pattern .compile (pattern , Pattern .MULTILINE ).matcher (stderr );
427- if (!matcher .find ()) {
436+ public OutputAnalyzer stderrShouldMatch (String regexp ) {
437+ if (!stderrMatches (regexp )) {
428438 reportDiagnosticSummary ();
429- throw new RuntimeException ("'" + pattern
439+ throw new RuntimeException ("'" + regexp
430440 + "' missing from stderr" );
431441 }
432442 return this ;
@@ -468,9 +478,7 @@ public OutputAnalyzer shouldNotMatch(String regexp) {
468478 * @throws RuntimeException If the pattern was found
469479 */
470480 public OutputAnalyzer stdoutShouldNotMatch (String regexp ) {
471- String stdout = getStdout ();
472- Matcher matcher = Pattern .compile (regexp , Pattern .MULTILINE ).matcher (stdout );
473- if (matcher .find ()) {
481+ if (stdoutMatches (regexp )) {
474482 reportDiagnosticSummary ();
475483 throw new RuntimeException ("'" + regexp
476484 + "' found in stdout" );
@@ -486,9 +494,7 @@ public OutputAnalyzer stdoutShouldNotMatch(String regexp) {
486494 * @throws RuntimeException If the pattern was found
487495 */
488496 public OutputAnalyzer stderrShouldNotMatch (String regexp ) {
489- String stderr = getStderr ();
490- Matcher matcher = Pattern .compile (regexp , Pattern .MULTILINE ).matcher (stderr );
491- if (matcher .find ()) {
497+ if (stderrMatches (regexp )) {
492498 reportDiagnosticSummary ();
493499 throw new RuntimeException ("'" + regexp
494500 + "' found in stderr" );
0 commit comments