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 2017/10/28 10:18:15 UTC

[camel] branch master updated: CAMEL-11962: AdviceWith weaveAddFirst using onCompletion issue

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new f3f0c70  CAMEL-11962: AdviceWith weaveAddFirst using onCompletion issue
f3f0c70 is described below

commit f3f0c70e97c35f4734569303535b423403d186e0
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Sat Oct 28 11:59:05 2017 +0200

    CAMEL-11962: AdviceWith weaveAddFirst using onCompletion issue
---
 .../org/apache/camel/builder/AdviceWithTasks.java  | 12 ++++-
 .../camel/issues/AdviceWithOnCompletionTest.java   | 58 +++++++++++++++++++++
 .../test/issues/AdviceWithOnCompletionTest.java    | 60 ++++++++++++++++++++++
 .../test/issues/AdviceWithOnCompletionTest.xml     | 39 ++++++++++++++
 4 files changed, 168 insertions(+), 1 deletion(-)

diff --git a/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java b/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java
index 0a14ae5..a604111 100644
--- a/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java
+++ b/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java
@@ -430,10 +430,20 @@ public final class AdviceWithTasks {
         // first iterator and apply match by
         List<ProcessorDefinition<?>> matched = new ArrayList<ProcessorDefinition<?>>();
 
+        List<ProcessorDefinition<?>> outputs = new ArrayList<>();
+        // skip abstract nodes in the beginning as they are cross cutting functionality such as onException, onCompletion etc
+        for (ProcessorDefinition output : route.getOutputs()) {
+            boolean invalid = outputs.isEmpty() && output.isAbstract();
+            if (!invalid) {
+                outputs.add(output);
+            }
+        }
+
         @SuppressWarnings("rawtypes")
-        Iterator<ProcessorDefinition> itAll = ProcessorDefinitionHelper.filterTypeInOutputs(route.getOutputs(), ProcessorDefinition.class, maxDeep);
+        Iterator<ProcessorDefinition> itAll = ProcessorDefinitionHelper.filterTypeInOutputs(outputs, ProcessorDefinition.class, maxDeep);
         while (itAll.hasNext()) {
             ProcessorDefinition<?> next = itAll.next();
+
             if (matchBy.match(next)) {
                 matched.add(next);
             }
diff --git a/camel-core/src/test/java/org/apache/camel/issues/AdviceWithOnCompletionTest.java b/camel-core/src/test/java/org/apache/camel/issues/AdviceWithOnCompletionTest.java
new file mode 100644
index 0000000..83c4c06
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/issues/AdviceWithOnCompletionTest.java
@@ -0,0 +1,58 @@
+/**
+ * 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.issues;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.AdviceWithRouteBuilder;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ * @version 
+ */
+public class AdviceWithOnCompletionTest extends ContextTestSupport {
+
+    public void testAdviceOnCompletion() throws Exception {
+        getMockEndpoint("mock:done").expectedMessageCount(1);
+        getMockEndpoint("mock:advice").expectedMessageCount(1);
+
+        context.getRouteDefinitions().get(0).adviceWith(context, new AdviceWithRouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                weaveAddFirst().to("mock:advice");
+            }
+        });
+
+        template.sendBody("direct:advice", "Hello World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                onCompletion().to("mock:done");
+
+                from("direct:advice")
+                    .log("Advice ${body}")
+                    .to("mock:result");
+            }
+        };
+    }
+
+}
diff --git a/components/camel-test-spring/src/test/java/org/apache/camel/test/issues/AdviceWithOnCompletionTest.java b/components/camel-test-spring/src/test/java/org/apache/camel/test/issues/AdviceWithOnCompletionTest.java
new file mode 100644
index 0000000..b6b763e
--- /dev/null
+++ b/components/camel-test-spring/src/test/java/org/apache/camel/test/issues/AdviceWithOnCompletionTest.java
@@ -0,0 +1,60 @@
+/**
+ * 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.test.issues;
+
+import org.apache.camel.builder.AdviceWithRouteBuilder;
+import org.apache.camel.model.RouteDefinition;
+import org.apache.camel.test.spring.CamelSpringTestSupport;
+import org.junit.Test;
+import org.springframework.context.support.AbstractApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+public class AdviceWithOnCompletionTest extends CamelSpringTestSupport {
+
+    @Override
+    protected AbstractApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/test/issues/AdviceWithOnCompletionTest.xml");
+    }
+
+    @Override
+    public boolean isUseAdviceWith() {
+        return true;
+    }
+
+    @Test
+    public void testOnCompletion() throws Exception {
+        RouteDefinition route = context.getRouteDefinitions().get(0);
+        route.adviceWith(context, new AdviceWithRouteBuilder() {
+            public void configure() throws Exception {
+                replaceFromWith("direct:start");
+
+                weaveAddFirst().convertBodyTo(String.class);
+                weaveAddLast().to("mock:result");
+            }
+        });
+
+        context.start();
+
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+        getMockEndpoint("mock:result").message(0).body().isInstanceOf(String.class);
+
+        template.sendBody("direct:start", "Hello World".getBytes());
+
+        assertMockEndpointsSatisfied();
+    }
+
+}
\ No newline at end of file
diff --git a/components/camel-test-spring/src/test/resources/org/apache/camel/test/issues/AdviceWithOnCompletionTest.xml b/components/camel-test-spring/src/test/resources/org/apache/camel/test/issues/AdviceWithOnCompletionTest.xml
new file mode 100644
index 0000000..ad80f65
--- /dev/null
+++ b/components/camel-test-spring/src/test/resources/org/apache/camel/test/issues/AdviceWithOnCompletionTest.xml
@@ -0,0 +1,39 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    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.
+
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns:camel="http://camel.apache.org/schema/spring"
+       xsi:schemaLocation="
+        http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">
+
+  <camelContext xmlns="http://camel.apache.org/schema/spring">
+
+    <onCompletion>
+      <to uri="mock:complete"/>
+    </onCompletion>
+
+    <route>
+      <from uri="file:start"/>
+      <to uri="log:end"/>
+    </route>
+
+  </camelContext>
+</beans>
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
['"commits@camel.apache.org" <co...@camel.apache.org>'].