Skip to content

Commit

Permalink
8315415: OutputAnalyzer.shouldMatchByLine() fails in some cases
Browse files Browse the repository at this point in the history
Backport-of: 7b1e2bfe0f805a69b59839b6bf8250b62ea356b8
  • Loading branch information
Andrew Lu committed Feb 6, 2024
1 parent 0dc17ca commit 38c11fd
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions test/lib/jdk/test/lib/process/OutputAnalyzer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2013, 2023, 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
Expand Down Expand Up @@ -607,68 +607,68 @@ public OutputAnalyzer stdoutShouldMatchByLine(String pattern) {
/**
* @see #shouldMatchByLine(String, String, String)
*/
public OutputAnalyzer shouldMatchByLineFrom(String from, String pattern) {
return shouldMatchByLine(from, null, pattern);
public OutputAnalyzer shouldMatchByLineFrom(String fromPattern, String pattern) {
return shouldMatchByLine(fromPattern, null, pattern);
}

/**
* @see #shouldMatchByLine(String, String, String)
*/
public OutputAnalyzer shouldMatchByLineTo(String to, String pattern) {
return shouldMatchByLine(null, to, pattern);
public OutputAnalyzer shouldMatchByLineTo(String toPattern, String pattern) {
return shouldMatchByLine(null, toPattern, pattern);
}

/**
* Verify that the stdout and stderr contents of output buffer match the
* {@code pattern} line by line. The whole output could be matched or
* just a subset of it.
*
* @param from
* The line (excluded) from where output will be matched.
* Set {@code from} to null for matching from the first line.
* @param to
* The line (excluded) until where output will be matched.
* Set {@code to} to null for matching until the last line.
* @param fromPattern
* The pattern of line (excluded) from where output will be matched.
* Set {@code fromPattern} to null for matching from the first line.
* @param toPattern
* The pattern of line (excluded) until where output will be matched.
* Set {@code toPattern} to null for matching until the last line.
* @param pattern
* Matching pattern
*/
public OutputAnalyzer shouldMatchByLine(String from, String to, String pattern) {
return shouldMatchByLine(getOutput(), from, to, pattern);
public OutputAnalyzer shouldMatchByLine(String fromPattern, String toPattern, String pattern) {
return shouldMatchByLine(getOutput(), fromPattern, toPattern, 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 (excluded) from where stdout will be matched.
* Set {@code from} to null for matching from the first line.
* @param to
* The line (excluded) until where stdout will be matched.
* Set {@code to} to null for matching until the last line.
* @param fromPattern
* The pattern of line (excluded) from where stdout will be matched.
* Set {@code fromPattern} to null for matching from the first line.
* @param toPattern
* The pattern of line (excluded) until where stdout will be matched.
* Set {@code toPattern} to null for matching until the last line.
* @param pattern
* Matching pattern
*/
public OutputAnalyzer stdoutShouldMatchByLine(String from, String to, String pattern) {
return shouldMatchByLine(getStdout(), from, to, pattern);
public OutputAnalyzer stdoutShouldMatchByLine(String fromPattern, String toPattern, String pattern) {
return shouldMatchByLine(getStdout(), fromPattern, toPattern, pattern);
}

private OutputAnalyzer shouldMatchByLine(String buffer, String from, String to, String pattern) {
private OutputAnalyzer shouldMatchByLine(String buffer, String fromPattern, String toPattern, String pattern) {
List<String> lines = asLines(buffer);

int fromIndex = 0;
if (from != null) {
fromIndex = indexOf(lines, from, 0) + 1; // + 1 -> apply 'pattern' to lines after 'from' match
if (fromPattern != null) {
fromIndex = indexOf(lines, fromPattern, 0) + 1; // + 1 -> apply 'pattern' to lines after 'from' match
Asserts.assertGreaterThan(fromIndex, 0,
"The line/pattern '" + from + "' from where the output should match can not be found");
"The line matched with pattern '" + fromPattern + "' from where the output should match can not be found");
}

int toIndex = lines.size();
if (to != null) {
toIndex = indexOf(lines, to, fromIndex);
if (toPattern != null) {
toIndex = indexOf(lines, toPattern, fromIndex);
Asserts.assertGreaterThan(toIndex, fromIndex,
"The line/pattern '" + to + "' until where the output should match can not be found");
"The line matched with pattern '" + toPattern + "' until where the output should match can not be found");
}

List<String> subList = lines.subList(fromIndex, toIndex);
Expand Down

1 comment on commit 38c11fd

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.