You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2021/11/23 14:59:00 UTC

[camel] branch main updated: CAMEL-17225: camel-core - Using predicates in Java DSL with ValueBuilder should init as predicate

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 ee6a51b  CAMEL-17225: camel-core - Using predicates in Java DSL with ValueBuilder should init as predicate
ee6a51b is described below

commit ee6a51b9118361ac7a44d6f9eef9de6c842655cc
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Tue Nov 23 15:55:47 2021 +0100

    CAMEL-17225: camel-core - Using predicates in Java DSL with ValueBuilder should init as predicate
---
 .../ChoiceCompoundPredicateSimpleTest.java         | 132 +++++++++++++++++++++
 .../camel/support/builder/PredicateBuilder.java    |  14 +--
 .../apache/camel/support/builder/ValueBuilder.java |  12 ++
 3 files changed, 151 insertions(+), 7 deletions(-)

diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/ChoiceCompoundPredicateSimpleTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/ChoiceCompoundPredicateSimpleTest.java
new file mode 100644
index 0000000..83d4ca2
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/ChoiceCompoundPredicateSimpleTest.java
@@ -0,0 +1,132 @@
+/*
+ * 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.processor;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.junit.jupiter.api.Test;
+
+import static org.apache.camel.builder.PredicateBuilder.or;
+
+public class ChoiceCompoundPredicateSimpleTest extends ContextTestSupport {
+
+    @Test
+    public void testNull() throws Exception {
+        getMockEndpoint("mock:empty").expectedMessageCount(2);
+        getMockEndpoint("mock:data").expectedMessageCount(0);
+
+        template.sendBody("direct:simple", null);
+        template.sendBody("direct:or", null);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testEmpty() throws Exception {
+        getMockEndpoint("mock:empty").expectedMessageCount(2);
+        getMockEndpoint("mock:data").expectedMessageCount(0);
+
+        template.sendBody("direct:simple", new ArrayList<>());
+        template.sendBody("direct:or", new ArrayList<>());
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testEmptyOr() throws Exception {
+        getMockEndpoint("mock:empty").expectedMessageCount(1);
+        getMockEndpoint("mock:data").expectedMessageCount(0);
+
+        template.sendBody("direct:or", new ArrayList<>());
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testEmptySimple() throws Exception {
+        getMockEndpoint("mock:empty").expectedMessageCount(1);
+        getMockEndpoint("mock:data").expectedMessageCount(0);
+
+        template.sendBody("direct:simple", new ArrayList<>());
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testData() throws Exception {
+        getMockEndpoint("mock:empty").expectedMessageCount(0);
+        getMockEndpoint("mock:data").expectedMessageCount(2);
+
+        List<String> list = new ArrayList<>();
+        list.add("Hello Camel");
+        template.sendBody("direct:simple", list);
+        template.sendBody("direct:or", list);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testDataOr() throws Exception {
+        getMockEndpoint("mock:empty").expectedMessageCount(0);
+        getMockEndpoint("mock:data").expectedMessageCount(1);
+
+        List<String> list = new ArrayList<>();
+        list.add("Hello Camel");
+        template.sendBody("direct:or", list);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testDataSimple() throws Exception {
+        getMockEndpoint("mock:empty").expectedMessageCount(0);
+        getMockEndpoint("mock:data").expectedMessageCount(1);
+
+        List<String> list = new ArrayList<>();
+        list.add("Hello Camel");
+        template.sendBody("direct:simple", list);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            public void configure() {
+                from("direct:or")
+                    .choice()
+                        .when(or(body().isNull(), simple("${body.size()} == 0")))
+                        .to("mock:empty")
+                    .otherwise()
+                        .to("mock:data")
+                    .end();
+
+                from("direct:simple")
+                    .choice()
+                        .when(simple("${body} == null || ${body.size()} == 0"))
+                        .to("mock:empty")
+                    .otherwise()
+                        .to("mock:data")
+                    .end();
+            }
+        };
+    }
+
+}
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/builder/PredicateBuilder.java b/core/camel-support/src/main/java/org/apache/camel/support/builder/PredicateBuilder.java
index 4e3c172..f8373c8 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/builder/PredicateBuilder.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/builder/PredicateBuilder.java
@@ -54,7 +54,7 @@ public class PredicateBuilder {
 
             @Override
             public void init(CamelContext camelContext) {
-                predicate.init(camelContext);
+                predicate.initPredicate(camelContext);
             }
 
             @Override
@@ -77,8 +77,8 @@ public class PredicateBuilder {
 
             @Override
             public void init(CamelContext camelContext) {
-                left.init(camelContext);
-                right.init(camelContext);
+                left.initPredicate(camelContext);
+                right.initPredicate(camelContext);
             }
 
             @Override
@@ -102,8 +102,8 @@ public class PredicateBuilder {
 
             @Override
             public void init(CamelContext camelContext) {
-                left.init(camelContext);
-                right.init(camelContext);
+                left.initPredicate(camelContext);
+                right.initPredicate(camelContext);
             }
 
             @Override
@@ -162,7 +162,7 @@ public class PredicateBuilder {
             @Override
             public void init(CamelContext camelContext) {
                 for (Predicate in : predicates) {
-                    in.init(camelContext);
+                    in.initPredicate(camelContext);
                 }
             }
 
@@ -376,7 +376,7 @@ public class PredicateBuilder {
                 if (leftValue == null) {
                     // the left operator is null so its true
                     return true;
-                } 
+                }
 
                 return ObjectHelper.typeCoerceEquals(exchange.getContext().getTypeConverter(), leftValue, rightValue);
             }
diff --git a/core/camel-support/src/main/java/org/apache/camel/support/builder/ValueBuilder.java b/core/camel-support/src/main/java/org/apache/camel/support/builder/ValueBuilder.java
index fa4be6d..23b770d 100644
--- a/core/camel-support/src/main/java/org/apache/camel/support/builder/ValueBuilder.java
+++ b/core/camel-support/src/main/java/org/apache/camel/support/builder/ValueBuilder.java
@@ -42,12 +42,24 @@ public class ValueBuilder implements Expression, Predicate {
     }
 
     @Override
+    public void initPredicate(CamelContext context) {
+        if (expression instanceof Predicate) {
+            ((Predicate) expression).initPredicate(context);
+        } else {
+            expression.init(context);
+        }
+    }
+
+    @Override
     public <T> T evaluate(Exchange exchange, Class<T> type) {
         return expression.evaluate(exchange, type);
     }
 
     @Override
     public boolean matches(Exchange exchange) {
+        if (expression instanceof Predicate) {
+            return ((Predicate) expression).matches(exchange);
+        }
         return PredicateBuilder.toPredicate(getExpression()).matches(exchange);
     }