mirror of
https://github.com/JetBrains/JetBrainsRuntime.git
synced 2025-12-06 01:19:28 +01:00
8370975: OutputAnalyzer.matches() should use Matcher with Pattern.MULTILINE
Reviewed-by: stefank
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2013, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@@ -217,6 +217,69 @@ public class OutputAnalyzerTest {
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
// Multi-line output: OutputAnalyzer uses MULTILINE but not DOTALL, so "." doesn't match newline, but
|
||||
// "^" and "$" matches just after or just before, respectively, a newline.
|
||||
stdout = "aaaaaa\nxxxxxx\n";
|
||||
stderr = "bbbbbb\nyyyyyy\n";
|
||||
|
||||
OutputAnalyzer out = new OutputAnalyzer(stdout, stderr);
|
||||
|
||||
out.shouldMatch("aaa");
|
||||
out.shouldMatch("xxx");
|
||||
out.shouldMatch("bbb");
|
||||
out.shouldMatch("yyy");
|
||||
|
||||
out.stdoutShouldMatch("aaaaaa");
|
||||
out.stdoutShouldMatch("xxxxxx");
|
||||
out.stderrShouldMatch("bbbbbb");
|
||||
out.stderrShouldMatch("yyyyyy");
|
||||
|
||||
out.shouldMatch("^aaaaaa$");
|
||||
out.shouldMatch("^xxxxxx$");
|
||||
out.shouldMatch("^bbbbbb$");
|
||||
out.shouldMatch("^yyyyyy$");
|
||||
|
||||
out.stdoutShouldMatch("^aaaaaa$");
|
||||
out.stdoutShouldMatch("^xxxxxx$");
|
||||
out.stderrShouldMatch("^bbbbbb$");
|
||||
out.stderrShouldMatch("^yyyyyy$");
|
||||
|
||||
out.shouldMatch ("a.*");
|
||||
out.shouldNotMatch("a.*x");
|
||||
out.shouldMatch ("b.*");
|
||||
out.shouldNotMatch("b.*y");
|
||||
out.stdoutShouldMatch ("a.*");
|
||||
out.stdoutShouldNotMatch("a.*x");
|
||||
out.stderrShouldMatch ("b.*");
|
||||
out.stderrShouldNotMatch("b.*y");
|
||||
|
||||
check(out.matches("^aaaaaa$"));
|
||||
check(out.matches("^yyyyyy$"));
|
||||
check(out.stdoutMatches("^aaaaaa$"));
|
||||
check(out.stderrMatches("^yyyyyy$"));
|
||||
|
||||
check( out.matches("a.*"));
|
||||
check(!out.matches("a.*x"));
|
||||
|
||||
check( out.stdoutMatches("a.*"));
|
||||
check(!out.stdoutMatches("a.*x"));
|
||||
|
||||
check( out.stderrMatches("b.*"));
|
||||
check(!out.stderrMatches("b.*y"));
|
||||
|
||||
// Test the "contains" methods as well
|
||||
check(out.contains("aaa\nxxx"));
|
||||
check(out.contains("bbb\nyyy"));
|
||||
check(out.stdoutContains("aaa\nxxx"));
|
||||
check(out.stderrContains("bbb\nyyy"));
|
||||
|
||||
check(!out.contains("X"));
|
||||
check(!out.contains("X"));
|
||||
check(!out.stdoutContains("X"));
|
||||
check(!out.stderrContains("X"));
|
||||
}
|
||||
|
||||
{
|
||||
try {
|
||||
// Verify the exception message
|
||||
@@ -244,4 +307,9 @@ public class OutputAnalyzerTest {
|
||||
}
|
||||
}
|
||||
|
||||
private static void check(boolean b) {
|
||||
if (!b) {
|
||||
throw new RuntimeException("Check failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -354,25 +354,44 @@ public final class OutputAnalyzer {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the pattern can be found in the given string(s).
|
||||
*
|
||||
* NOTE: The meaning of "match" in OutputAnalyzer is NOT the same as String.matches().
|
||||
* Rather it means "can the pattern be found in stdout and/or stderr".
|
||||
*
|
||||
* The pattern is comiled with MULTILINE but without DOTALL, so "." doesn't match newline, but
|
||||
* "^" and "$" matches just after or just before, respectively, a newline.
|
||||
*/
|
||||
private boolean findPattern(String regexp, String... strings) {
|
||||
Pattern pattern = Pattern.compile(regexp, Pattern.MULTILINE);
|
||||
for (String s : strings) {
|
||||
if (pattern.matcher(s).find()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if stdout matches the given pattern
|
||||
*/
|
||||
public boolean stdoutMatches(String regexp) {
|
||||
return getStdout().matches(regexp);
|
||||
return findPattern(regexp, getStdout());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if stderr matches the given pattern
|
||||
*/
|
||||
public boolean stderrMatches(String regexp) {
|
||||
return getStderr().matches(regexp);
|
||||
return findPattern(regexp, getStderr());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if either stdout or stderr matches the given pattern
|
||||
*/
|
||||
public boolean matches(String regexp) {
|
||||
return stdoutMatches(regexp) || stderrMatches(regexp);
|
||||
return findPattern(regexp, getStdout(), getStderr());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -383,12 +402,7 @@ public final class OutputAnalyzer {
|
||||
* @throws RuntimeException If the pattern was not found
|
||||
*/
|
||||
public OutputAnalyzer shouldMatch(String regexp) {
|
||||
String stdout = getStdout();
|
||||
String stderr = getStderr();
|
||||
Pattern pattern = Pattern.compile(regexp, Pattern.MULTILINE);
|
||||
Matcher stdoutMatcher = pattern.matcher(stdout);
|
||||
Matcher stderrMatcher = pattern.matcher(stderr);
|
||||
if (!stdoutMatcher.find() && !stderrMatcher.find()) {
|
||||
if (!matches(regexp)) {
|
||||
reportDiagnosticSummary();
|
||||
throw new RuntimeException("'" + regexp
|
||||
+ "' missing from stdout/stderr");
|
||||
@@ -404,9 +418,7 @@ public final class OutputAnalyzer {
|
||||
* @throws RuntimeException If the pattern was not found
|
||||
*/
|
||||
public OutputAnalyzer stdoutShouldMatch(String regexp) {
|
||||
String stdout = getStdout();
|
||||
Matcher matcher = Pattern.compile(regexp, Pattern.MULTILINE).matcher(stdout);
|
||||
if (!matcher.find()) {
|
||||
if (!stdoutMatches(regexp)) {
|
||||
reportDiagnosticSummary();
|
||||
throw new RuntimeException("'" + regexp
|
||||
+ "' missing from stdout");
|
||||
@@ -421,12 +433,10 @@ public final class OutputAnalyzer {
|
||||
* @param pattern
|
||||
* @throws RuntimeException If the pattern was not found
|
||||
*/
|
||||
public OutputAnalyzer stderrShouldMatch(String pattern) {
|
||||
String stderr = getStderr();
|
||||
Matcher matcher = Pattern.compile(pattern, Pattern.MULTILINE).matcher(stderr);
|
||||
if (!matcher.find()) {
|
||||
public OutputAnalyzer stderrShouldMatch(String regexp) {
|
||||
if (!stderrMatches(regexp)) {
|
||||
reportDiagnosticSummary();
|
||||
throw new RuntimeException("'" + pattern
|
||||
throw new RuntimeException("'" + regexp
|
||||
+ "' missing from stderr");
|
||||
}
|
||||
return this;
|
||||
@@ -468,9 +478,7 @@ public final class OutputAnalyzer {
|
||||
* @throws RuntimeException If the pattern was found
|
||||
*/
|
||||
public OutputAnalyzer stdoutShouldNotMatch(String regexp) {
|
||||
String stdout = getStdout();
|
||||
Matcher matcher = Pattern.compile(regexp, Pattern.MULTILINE).matcher(stdout);
|
||||
if (matcher.find()) {
|
||||
if (stdoutMatches(regexp)) {
|
||||
reportDiagnosticSummary();
|
||||
throw new RuntimeException("'" + regexp
|
||||
+ "' found in stdout");
|
||||
@@ -486,9 +494,7 @@ public final class OutputAnalyzer {
|
||||
* @throws RuntimeException If the pattern was found
|
||||
*/
|
||||
public OutputAnalyzer stderrShouldNotMatch(String regexp) {
|
||||
String stderr = getStderr();
|
||||
Matcher matcher = Pattern.compile(regexp, Pattern.MULTILINE).matcher(stderr);
|
||||
if (matcher.find()) {
|
||||
if (stderrMatches(regexp)) {
|
||||
reportDiagnosticSummary();
|
||||
throw new RuntimeException("'" + regexp
|
||||
+ "' found in stderr");
|
||||
|
||||
Reference in New Issue
Block a user