Search in sources :

Example 61 with Expression

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));
}
Also used : Expression(org.apache.camel.Expression)

Example 62 with Expression

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));
}
Also used : Expression(org.apache.camel.Expression)

Example 63 with Expression

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);
}
Also used : Expression(org.apache.camel.Expression)

Example 64 with Expression

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);
}
Also used : Language(org.apache.camel.spi.Language) Expression(org.apache.camel.Expression)

Example 65 with Expression

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);
}
Also used : Expression(org.apache.camel.Expression) CamelAuthorizationException(org.apache.camel.CamelAuthorizationException) CamelExecutionException(org.apache.camel.CamelExecutionException) RuntimeBeanExpressionException(org.apache.camel.language.bean.RuntimeBeanExpressionException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) SimpleIllegalSyntaxException(org.apache.camel.language.simple.types.SimpleIllegalSyntaxException) ExpressionIllegalSyntaxException(org.apache.camel.ExpressionIllegalSyntaxException) MethodNotFoundException(org.apache.camel.component.bean.MethodNotFoundException)

Aggregations

Expression (org.apache.camel.Expression)184 Exchange (org.apache.camel.Exchange)44 Processor (org.apache.camel.Processor)24 Predicate (org.apache.camel.Predicate)22 DefaultExchange (org.apache.camel.impl.DefaultExchange)21 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)18 Language (org.apache.camel.spi.Language)16 ArrayList (java.util.ArrayList)15 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)15 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)15 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)14 SendProcessor (org.apache.camel.processor.SendProcessor)14 Test (org.junit.Test)8 SimpleParserException (org.apache.camel.language.simple.types.SimpleParserException)7 File (java.io.File)4 ExecutorService (java.util.concurrent.ExecutorService)4 SimpleIllegalSyntaxException (org.apache.camel.language.simple.types.SimpleIllegalSyntaxException)4 HashMap (java.util.HashMap)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 CamelExchangeException (org.apache.camel.CamelExchangeException)3