use of org.apache.camel.Expression in project camel by apache.
the class PredicateBinaryCoerceTypeTest method testEqualWithNull.
public void testEqualWithNull() throws Exception {
Expression a = ExpressionBuilder.constantExpression("123");
Expression b = ExpressionBuilder.constantExpression(null);
assertDoesNotMatch(PredicateBuilder.isEqualTo(a, b));
// reverse the type and try again
a = ExpressionBuilder.constantExpression(null);
b = ExpressionBuilder.constantExpression("123");
assertDoesNotMatch(PredicateBuilder.isEqualTo(a, b));
// try two null values
a = ExpressionBuilder.constantExpression(null);
b = ExpressionBuilder.constantExpression(null);
assertMatches(PredicateBuilder.isEqualTo(a, b));
}
use of org.apache.camel.Expression in project camel by apache.
the class PredicateBinaryCoerceTypeTest method testLessThanOrEqual.
public void testLessThanOrEqual() throws Exception {
// less than
Expression a = ExpressionBuilder.constantExpression("100");
Expression b = ExpressionBuilder.constantExpression(Integer.valueOf("200"));
assertMatches(PredicateBuilder.isLessThanOrEqualTo(a, b));
assertDoesNotMatch(PredicateBuilder.isLessThanOrEqualTo(b, a));
// reverse the types and try again
a = ExpressionBuilder.constantExpression(Integer.valueOf("200"));
b = ExpressionBuilder.constantExpression("100");
assertDoesNotMatch(PredicateBuilder.isLessThanOrEqualTo(a, b));
assertMatches(PredicateBuilder.isLessThanOrEqualTo(b, a));
// equal
a = ExpressionBuilder.constantExpression("100");
b = ExpressionBuilder.constantExpression(Integer.valueOf("100"));
assertMatches(PredicateBuilder.isLessThanOrEqualTo(a, b));
assertMatches(PredicateBuilder.isLessThanOrEqualTo(b, a));
// reverse the types and try again
a = ExpressionBuilder.constantExpression(Integer.valueOf("100"));
b = ExpressionBuilder.constantExpression("100");
assertMatches(PredicateBuilder.isGreaterThanOrEqualTo(a, b));
assertMatches(PredicateBuilder.isGreaterThanOrEqualTo(b, a));
}
use of org.apache.camel.Expression in project camel by apache.
the class LanguageNoCacheScriptTest method testNoCache.
public void testNoCache() throws Exception {
getMockEndpoint("mock:result").expectedBodiesReceived("World", "Camel");
template.sendBody("direct:start", "World");
Expression first = endpoint.getExpression();
template.sendBody("direct:start", "Camel");
Expression second = endpoint.getExpression();
assertMockEndpointsSatisfied();
assertSame(first, second);
assertNull(first);
assertNull(second);
}
use of org.apache.camel.Expression in project camel by apache.
the class SimpleTest method assertExpressionResultInstanceOf.
protected void assertExpressionResultInstanceOf(String expressionText, Class<?> expectedType) {
Language language = assertResolveLanguage(getLanguageName());
Expression expression = language.createExpression(expressionText);
assertNotNull("Cannot assert type when no type is provided", expectedType);
assertNotNull("No Expression could be created for text: " + expressionText + " language: " + language, expression);
Object answer = expression.evaluate(exchange, Object.class);
assertIsInstanceOf(expectedType, answer);
}
use of org.apache.camel.Expression in project camel by apache.
the class SimpleTest method testRandomExpression.
public void testRandomExpression() throws Exception {
int min = 1;
int max = 10;
int iterations = 30;
int i = 0;
for (i = 0; i < iterations; i++) {
Expression expression = SimpleLanguage.simple("${random(1,10)}", Integer.class);
assertTrue(min <= expression.evaluate(exchange, Integer.class) && expression.evaluate(exchange, Integer.class) < max);
}
for (i = 0; i < iterations; i++) {
Expression expression = SimpleLanguage.simple("${random(10)}", Integer.class);
assertTrue(0 <= expression.evaluate(exchange, Integer.class) && expression.evaluate(exchange, Integer.class) < max);
}
Expression expression = SimpleLanguage.simple("${random(1, 10)}", Integer.class);
assertTrue(min <= expression.evaluate(exchange, Integer.class) && expression.evaluate(exchange, Integer.class) < max);
Expression expression1 = SimpleLanguage.simple("${random( 10)}", Integer.class);
assertTrue(0 <= expression1.evaluate(exchange, Integer.class) && expression1.evaluate(exchange, Integer.class) < max);
try {
assertExpression("${random(10,21,30)}", null);
fail("Should have thrown exception");
} catch (Exception e) {
assertEquals("Valid syntax: ${random(min,max)} or ${random(max)} was: random(10,21,30)", e.getCause().getMessage());
}
try {
assertExpression("${random()}", null);
fail("Should have thrown exception");
} catch (Exception e) {
assertEquals("Valid syntax: ${random(min,max)} or ${random(max)} was: random()", e.getCause().getMessage());
}
exchange.getIn().setHeader("max", 20);
Expression expression3 = SimpleLanguage.simple("${random(10,${header.max})}", Integer.class);
int num = expression3.evaluate(exchange, Integer.class);
assertTrue("Should be 10..20", num >= 0 && num < 20);
}
Aggregations