Search in sources :

Example 26 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class PredicateBuilderConcurrentTest method testPredicateBuilderConcurrent.

public void testPredicateBuilderConcurrent() throws Exception {
    context.start();
    List<Future<Boolean>> futures = new ArrayList<Future<Boolean>>();
    ExecutorService pool = Executors.newFixedThreadPool(10);
    for (int i = 0; i < 1000; i++) {
        final Integer num = i;
        Future<Boolean> future = pool.submit(new Callable<Boolean>() {

            public Boolean call() throws Exception {
                Expression left = ExpressionBuilder.headerExpression("foo");
                Expression right;
                if (num % 2 == 0) {
                    right = ExpressionBuilder.constantExpression("ABC");
                } else {
                    right = ExpressionBuilder.constantExpression("DEF");
                }
                Predicate predicate = PredicateBuilder.isEqualTo(left, right);
                Exchange exchange = new DefaultExchange(context);
                exchange.getIn().setBody("Hello World");
                exchange.getIn().setHeader("foo", "ABC");
                return predicate.matches(exchange);
            }
        });
        futures.add(future);
    }
    for (int i = 0; i < 1000; i++) {
        Boolean result = futures.get(i).get(10, TimeUnit.SECONDS);
        if (i % 2 == 0) {
            assertEquals("Should be true for #" + i, true, result.booleanValue());
        } else {
            assertEquals("Should be false for #" + i, false, result.booleanValue());
        }
    }
    pool.shutdownNow();
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) ArrayList(java.util.ArrayList) Predicate(org.apache.camel.Predicate) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) Expression(org.apache.camel.Expression) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 27 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class PredicateBuilderTest method testCompoundOrPredicates.

public void testCompoundOrPredicates() throws Exception {
    Predicate p1 = header("name").isEqualTo(constant("Hiram"));
    Predicate p2 = header("size").isGreaterThanOrEqualTo(constant(10));
    Predicate or = PredicateBuilder.or(p1, p2);
    assertMatches(or);
}
Also used : Predicate(org.apache.camel.Predicate)

Example 28 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class PredicateBuilderTest method testCompoundAndPredicates.

public void testCompoundAndPredicates() throws Exception {
    Predicate p1 = header("name").isEqualTo(constant("James"));
    Predicate p2 = header("size").isGreaterThanOrEqualTo(constant(10));
    Predicate and = PredicateBuilder.and(p1, p2);
    assertMatches(and);
}
Also used : Predicate(org.apache.camel.Predicate)

Example 29 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class ExpressionBuilderTest method testRegexTokenize.

public void testRegexTokenize() throws Exception {
    Expression expression = regexTokenizeExpression(headerExpression("location"), ",");
    List<String> expected = new ArrayList<String>(Arrays.asList(new String[] { "Islington", "London", "UK" }));
    assertExpression(expression, exchange, expected);
    Predicate predicate = contains(regexTokenizeExpression(headerExpression("location"), ","), constantExpression("London"));
    assertPredicate(predicate, exchange, true);
    predicate = contains(regexTokenizeExpression(headerExpression("location"), ","), constantExpression("Manchester"));
    assertPredicate(predicate, exchange, false);
}
Also used : Expression(org.apache.camel.Expression) ArrayList(java.util.ArrayList) Predicate(org.apache.camel.Predicate)

Example 30 with Predicate

use of org.apache.camel.Predicate in project camel by apache.

the class ExpressionBuilderTest method testTokenize.

public void testTokenize() throws Exception {
    Expression expression = tokenizeExpression(headerExpression("location"), ",");
    List<String> expected = new ArrayList<String>(Arrays.asList(new String[] { "Islington", "London", "UK" }));
    assertExpression(expression, exchange, expected);
    Predicate predicate = contains(tokenizeExpression(headerExpression("location"), ","), constantExpression("London"));
    assertPredicate(predicate, exchange, true);
    predicate = contains(tokenizeExpression(headerExpression("location"), ","), constantExpression("Manchester"));
    assertPredicate(predicate, exchange, false);
}
Also used : Expression(org.apache.camel.Expression) ArrayList(java.util.ArrayList) Predicate(org.apache.camel.Predicate)

Aggregations

Predicate (org.apache.camel.Predicate)124 Exchange (org.apache.camel.Exchange)70 Test (org.junit.Test)40 HashMap (java.util.HashMap)25 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)23 Expression (org.apache.camel.Expression)22 Processor (org.apache.camel.Processor)19 DefaultExchange (org.apache.camel.impl.DefaultExchange)10 RouteBuilder (org.apache.camel.builder.RouteBuilder)9 File (java.io.File)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Map (java.util.Map)7 Ignore (org.junit.Ignore)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 AggregateProcessor (org.apache.camel.processor.aggregate.AggregateProcessor)6 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)6 PDDocument (org.apache.pdfbox.pdmodel.PDDocument)6 BodyInAggregatingStrategy (org.apache.camel.processor.BodyInAggregatingStrategy)5