This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 7f924aaf1866 CAMEL-23797: Collapse identical
evalBinary/evalOther/evalTernary into one helper
7f924aaf1866 is described below
commit 7f924aaf18666ff74ab3b73c0b88948a07dd0214
Author: Adriano Machado <[email protected]>
AuthorDate: Fri Jun 19 02:54:04 2026 -0400
CAMEL-23797: Collapse identical evalBinary/evalOther/evalTernary into one
helper
Extract the shared space-check logic from three identical private
methods in SimpleTokenizer into a single evalSurroundedBySpace helper.
Pure behavior-preserving refactor — all three methods enforced the
same invariant (operator surrounded by spaces). Adds SimpleTokenizerTest
covering binary, other, and ternary operator classes plus boundary cases.
Co-Authored-By: Claude <[email protected]>
Closes #24119
---
.../camel/language/simple/SimpleTokenizer.java | 27 +++---
.../camel/language/simple/SimpleTokenizerTest.java | 95 ++++++++++++++++++++++
2 files changed, 105 insertions(+), 17 deletions(-)
diff --git
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
index 650bb72b90ec..c8b4e149d698 100644
---
a/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
+++
b/core/camel-core-languages/src/main/java/org/apache/camel/language/simple/SimpleTokenizer.java
@@ -294,30 +294,23 @@ public class SimpleTokenizer {
}
private static boolean evalBinary(SimpleTokenType token, String text,
String expression, int index) {
- int len = token.getValue().length();
- // The binary operator must be used in the format of "exp1 op exp2"
- if (index < 2 || len >= text.length() - 1) {
- return false;
- }
- String previousOne = expression.substring(index - 1, index);
- String afterOne = text.substring(len, len + 1);
- return " ".equals(previousOne) && " ".equals(afterOne) &&
text.substring(0, len).equals(token.getValue());
+ return evalSurroundedBySpace(token, text, expression, index);
}
private static boolean evalOther(SimpleTokenType token, String text,
String expression, int index) {
- int len = token.getValue().length();
- // The other operator must be used in the format of "exp1 op exp2"
- if (index < 2 || len >= text.length() - 1) {
- return false;
- }
- String previousOne = expression.substring(index - 1, index);
- String afterOne = text.substring(len, len + 1);
- return " ".equals(previousOne) && " ".equals(afterOne) &&
text.substring(0, len).equals(token.getValue());
+ return evalSurroundedBySpace(token, text, expression, index);
}
private static boolean evalTernary(SimpleTokenType token, String text,
String expression, int index) {
+ return evalSurroundedBySpace(token, text, expression, index);
+ }
+
+ /**
+ * Checks that the token appears surrounded by spaces: "exp1 op exp2".
Used by all infix operators (binary, other,
+ * ternary).
+ */
+ private static boolean evalSurroundedBySpace(SimpleTokenType token, String
text, String expression, int index) {
int len = token.getValue().length();
- // The ternary operator must be used in the format of "exp1 ? exp2 :
exp3"
if (index < 2 || len >= text.length() - 1) {
return false;
}
diff --git
a/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTokenizerTest.java
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTokenizerTest.java
new file mode 100644
index 000000000000..26dfe4df8cb4
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/language/simple/SimpleTokenizerTest.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.language.simple;
+
+import org.apache.camel.language.simple.types.SimpleToken;
+import org.apache.camel.language.simple.types.TokenType;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+/**
+ * Tests that binary, other, and ternary infix operators are each recognized
when surrounded by spaces, using the shared
+ * evalSurroundedBySpace logic.
+ */
+public class SimpleTokenizerTest {
+
+ private final SimpleTokenizer tokenizer = new SimpleTokenizer();
+
+ @Test
+ public void testBinaryOperatorIsRecognized() {
+ // "a == b": at index 2, " ==" starts and should be recognized as
binary
+ String expression = "a == b";
+ // index 2 is at '=' after the space
+ SimpleToken token = tokenizer.nextToken(expression, 2, false);
+ assertEquals(TokenType.binaryOperator, token.getType().getType());
+ assertEquals("==", token.getType().getValue());
+ }
+
+ @Test
+ public void testBinaryOperatorNotRecognizedAtStartOfExpression() {
+ // "== b": binary op at index 0 should NOT match (no space before it)
+ String expression = "== b";
+ SimpleToken token = tokenizer.nextToken(expression, 0, false);
+ assertFalse(token.getType().isBinary(), "Binary op at index 0 must not
match without preceding space");
+ }
+
+ @Test
+ public void testOtherOperatorIsRecognized() {
+ // "a ?: b": at index 2, "?:" should be recognized as other operator
+ String expression = "a ?: b";
+ SimpleToken token = tokenizer.nextToken(expression, 2, false);
+ assertEquals(TokenType.otherOperator, token.getType().getType());
+ assertEquals("?:", token.getType().getValue());
+ }
+
+ @Test
+ public void testTernaryOperatorQuestionMarkIsRecognized() {
+ // "a ? b : c": at index 2, "?" should be recognized as ternary
operator
+ String expression = "a ? b : c";
+ SimpleToken token = tokenizer.nextToken(expression, 2, false);
+ assertEquals(TokenType.ternaryOperator, token.getType().getType());
+ assertEquals("?", token.getType().getValue());
+ }
+
+ @Test
+ public void testTernaryOperatorColonIsRecognized() {
+ // "b : c": at index 2, ":" should be recognized as ternary operator
+ String expression = "b : c";
+ SimpleToken token = tokenizer.nextToken(expression, 2, false);
+ assertEquals(TokenType.ternaryOperator, token.getType().getType());
+ assertEquals(":", token.getType().getValue());
+ }
+
+ @Test
+ public void testTernaryOperatorNotRecognizedWithoutTrailingSpace() {
+ // "a ?b": "?" at index 2 without trailing space must not match as
ternary
+ String expression = "a ?b";
+ SimpleToken token = tokenizer.nextToken(expression, 2, false);
+ assertFalse(token.getType().isTernary(), "Ternary op without trailing
space must not match");
+ }
+
+ @Test
+ public void testContainsBinaryOperatorIsRecognized() {
+ // "a contains b": at index 2, "contains" is a binary operator
+ String expression = "a contains b";
+ SimpleToken token = tokenizer.nextToken(expression, 2, false);
+ assertEquals(TokenType.binaryOperator, token.getType().getType());
+ assertEquals("contains", token.getType().getValue());
+ }
+}