Skip to content

Commit

Permalink
CAMEL-9838 - Add ends with operator to simple language
Browse files Browse the repository at this point in the history
  • Loading branch information
janstey committed Apr 7, 2016
1 parent f465ab5 commit 8bdbe39
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
Expand Up @@ -66,6 +66,7 @@ public final class SimpleTokenizer {
KNOWN_TOKENS.add(new SimpleTokenType(TokenType.binaryOperator, "in"));
KNOWN_TOKENS.add(new SimpleTokenType(TokenType.binaryOperator, "range"));
KNOWN_TOKENS.add(new SimpleTokenType(TokenType.binaryOperator, "not range"));
KNOWN_TOKENS.add(new SimpleTokenType(TokenType.binaryOperator, "ends with"));

// unary operators
KNOWN_TOKENS.add(new SimpleTokenType(TokenType.unaryOperator, "++"));
Expand Down
Expand Up @@ -104,6 +104,8 @@ public Expression createExpression(String expression) {
return createInExpression(leftExp, rightExp);
} else if (operator == BinaryOperatorType.RANGE || operator == BinaryOperatorType.NOT_RANGE) {
return createRangeExpression(expression, leftExp, rightExp);
} else if (operator == BinaryOperatorType.ENDS_WITH) {
return createExpression(leftExp, rightExp, PredicateBuilder.endsWith(leftExp, rightExp));
}

throw new SimpleParserException("Unknown binary operator " + operator, token.getIndex());
Expand Down
Expand Up @@ -22,7 +22,7 @@
public enum BinaryOperatorType {

EQ, EQ_IGNORE, GT, GTE, LT, LTE, NOT_EQ, CONTAINS, NOT_CONTAINS, REGEX, NOT_REGEX,
IN, NOT_IN, IS, NOT_IS, RANGE, NOT_RANGE;
IN, NOT_IN, IS, NOT_IS, RANGE, NOT_RANGE, ENDS_WITH;

public static BinaryOperatorType asOperator(String text) {
if ("==".equals(text)) {
Expand Down Expand Up @@ -59,6 +59,8 @@ public static BinaryOperatorType asOperator(String text) {
return RANGE;
} else if ("not range".equals(text)) {
return NOT_RANGE;
} else if ("ends with".equals(text)) {
return ENDS_WITH;
}
throw new IllegalArgumentException("Operator not supported: " + text);
}
Expand Down Expand Up @@ -98,6 +100,8 @@ public static String getOperatorText(BinaryOperatorType operator) {
return "range";
} else if (operator == NOT_RANGE) {
return "not range";
} else if (operator == ENDS_WITH) {
return "ends with";
}
return "";
}
Expand Down Expand Up @@ -182,6 +186,8 @@ public static ParameterType[] supportedParameterTypes(BinaryOperatorType operato
return new ParameterType[]{ParameterType.LiteralWithFunction, ParameterType.Function};
} else if (operator == NOT_RANGE) {
return new ParameterType[]{ParameterType.LiteralWithFunction, ParameterType.Function};
} else if (operator == ENDS_WITH) {
return null;
}
return null;
}
Expand Down
Expand Up @@ -321,7 +321,7 @@ public void testContains() throws Exception {
assertPredicate("${in.header.foo} contains 'abc'", true);
assertPredicate("${in.header.foo} contains 'def'", false);
}

public void testNotContains() throws Exception {
assertPredicate("${in.header.foo} not contains 'a'", false);
assertPredicate("${in.header.foo} not contains 'ab'", false);
Expand Down Expand Up @@ -500,6 +500,16 @@ public void testUnaryDec() throws Exception {
assertPredicate("${in.header.bar}-- == 124", false);
}

public void testEndsWith() throws Exception {
exchange.getIn().setBody("Hello there");
assertPredicate("${in.body} ends with 'there'", true);
assertPredicate("${in.body} ends with 're'", true);
assertPredicate("${in.body} ends with ' there'", true);
assertPredicate("${in.body} ends with 'Hello there'", true);
assertPredicate("${in.body} ends with 'Hello ther'", false);
assertPredicate("${in.body} ends with 'Hi'", false);
}

protected String getLanguageName() {
return "simple";
}
Expand Down

0 comments on commit 8bdbe39

Please sign in to comment.