Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -129,18 +129,21 @@ private List<String> tokenize(String expression) {
}
quoteType = c;
sb.append(c);
} else if (c == ' ' || c == '(' || c == ')' || c == ',' || c == '+' || c == '>' || c == '<' || c == '='
|| c == '!') {
} else if (Character.isWhitespace(c) || c == '(' || c == ')' || c == ',' || c == '+'
|| c == '>' || c == '<' || c == '=' || c == '!' || c == '&' || c == '|') {
if (!sb.isEmpty()) {
tokens.add(sb.toString());
sb.setLength(0);
}
if (c != ' ') {
if ((c == '>' || c == '<' || c == '=' || c == '!')
if (!Character.isWhitespace(c)) {
if ((c == '>' || c == '<' || c == '=' || c == '!' || c == '&')
&& i + 1 < expression.length()
&& expression.charAt(i + 1) == '=') {
tokens.add(c + "=");
i++; // Skip the next character
} else if (c == '&' && i + 1 < expression.length() && expression.charAt(i + 1) == '&') {
tokens.add("&&");
i++; // Skip the next character
Comment on lines +139 to +146
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Two issues:

  1. & in the X= group: & was added to the >=/<=/==/!= lookahead, which means &= would be tokenized as a valid two-char operator. But &= is not valid in this language — remove || c == '&' from this condition.

  2. || not handled: The || operator has the exact same tokenization gap that && had. The | character is not in the operator list, so || only works when surrounded by spaces. Handle it symmetrically with &&.

Suggested change
if ((c == '>' || c == '<' || c == '=' || c == '!' || c == '&')
&& i + 1 < expression.length()
&& expression.charAt(i + 1) == '=') {
tokens.add(c + "=");
i++; // Skip the next character
} else if (c == '&' && i + 1 < expression.length() && expression.charAt(i + 1) == '&') {
tokens.add("&&");
i++; // Skip the next character
if ((c == '>' || c == '<' || c == '=' || c == '!')
&& i + 1 < expression.length()
&& expression.charAt(i + 1) == '=') {
tokens.add(c + "=");
i++; // Skip the next character
} else if (c == '&' && i + 1 < expression.length() && expression.charAt(i + 1) == '&') {
tokens.add("&&");
i++; // Skip the next character
} else if (c == '|' && i + 1 < expression.length() && expression.charAt(i + 1) == '|') {
tokens.add("||");
i++; // Skip the next character

Please also add corresponding test cases for || with newlines, similar to the && tests.

} else {
tokens.add(String.valueOf(c));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,31 @@ void testPropertyAlias() {
assertThrows(RuntimeException.class, () -> parser.parse("${unclosed"));
}

@Test
void testAmpersandAmpersandTokenizerMultiline() {
// Regression test for https://github.com/apache/maven/issues/11882
// The && operator was not being tokenized correctly when a line break appeared before it.
// Uses ${os.name} and ${os.arch} which are set to 'windows' and 'amd64' in the mock context.

// Case 1: Basic && without line breaks (baseline - always worked)
assertTrue((Boolean) parser.parse("${os.arch} == 'amd64' && ${os.name} == 'windows'"));

// Case 2: Line break BEFORE && - this was the bug from issue #11882
// In the issue, CDATA content had a line break before &&:
// <condition><![CDATA[exists( '.profile-2' )\n&& missing( '.profile-1' )]]></condition>
assertTrue((Boolean) parser.parse("${os.arch} == 'amd64'\n&& ${os.name} == 'windows'"));

// Case 3: Line break AFTER &&
assertTrue((Boolean) parser.parse("${os.arch} == 'amd64' &&\n${os.name} == 'windows'"));

// Case 4: Line breaks on both sides
assertTrue((Boolean) parser.parse("${os.arch} == 'amd64'\n&&\n${os.name} == 'windows'"));

// Case 5: Multiple && with line break before first && (like bad-profile-2d in issue)
assertTrue(
(Boolean) parser.parse("${os.arch} == 'amd64'\n&& ${os.name} == 'windows' && ${os.name} == 'windows'"));
}

@Test
void testNestedPropertyAlias() {
functions.put("property", args -> {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please also add a test for || with newlines, similar to testAmpersandAmpersandTokenizerMultiline. For example:

@Test
void testPipePipeTokenizerMultiline() {
    // || with newlines should work the same as &&
    assertTrue((Boolean) parser.parse("${os.arch} == 'amd64' || ${os.name} == 'linux'"));
    assertTrue((Boolean) parser.parse("${os.arch} == 'amd64'\n|| ${os.name} == 'linux'"));
    assertTrue((Boolean) parser.parse("${os.arch} == 'amd64' ||\n${os.name} == 'linux'"));
    assertTrue((Boolean) parser.parse("${os.arch} == 'amd64'\n||\n${os.name} == 'linux'"));
}

Expand Down