CAMEL-10174: Fixed advice with using weaveBy to work with CBR to work on when/ohterwise outputs.
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/12ddce8e Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/12ddce8e Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/12ddce8e Branch: refs/heads/camel-2.17.x Commit: 12ddce8e8be5e5321294c78d30a1663a0973d72d Parents: 9c529be Author: Claus Ibsen <davscl...@apache.org> Authored: Mon Jul 25 11:59:42 2016 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Mon Jul 25 13:58:07 2016 +0200 ---------------------------------------------------------------------- .../apache/camel/builder/AdviceWithTasks.java | 26 ++++++--- .../apache/camel/model/ChoiceDefinition.java | 3 +- .../AdviceWithWeaveByToStringCBRTest.java | 60 ++++++++++++++++++++ 3 files changed, 80 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/12ddce8e/camel-core/src/main/java/org/apache/camel/builder/AdviceWithTasks.java ---------------------------------------------------------------------- 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 23690a1..bc1d066 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 @@ -21,6 +21,7 @@ import java.util.Iterator; import java.util.List; import org.apache.camel.Endpoint; +import org.apache.camel.model.ChoiceDefinition; import org.apache.camel.model.FromDefinition; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.model.ProcessorDefinitionHelper; @@ -143,7 +144,7 @@ public final class AdviceWithTasks { while (it.hasNext()) { ProcessorDefinition<?> output = it.next(); if (matchBy.match(output)) { - List<ProcessorDefinition<?>> outputs = getParentOutputs(output.getParent()); + List<ProcessorDefinition<?>> outputs = getOutputs(output); if (outputs != null) { int index = outputs.indexOf(output); if (index != -1) { @@ -192,7 +193,7 @@ public final class AdviceWithTasks { while (it.hasNext()) { ProcessorDefinition<?> output = it.next(); if (matchBy.match(output)) { - List<ProcessorDefinition<?>> outputs = getParentOutputs(output.getParent()); + List<ProcessorDefinition<?>> outputs = getOutputs(output); if (outputs != null) { int index = outputs.indexOf(output); if (index != -1) { @@ -240,7 +241,7 @@ public final class AdviceWithTasks { while (it.hasNext()) { ProcessorDefinition<?> output = it.next(); if (matchBy.match(output)) { - List<ProcessorDefinition<?>> outputs = getParentOutputs(output.getParent()); + List<ProcessorDefinition<?>> outputs = getOutputs(output); if (outputs != null) { int index = outputs.indexOf(output); if (index != -1) { @@ -289,7 +290,7 @@ public final class AdviceWithTasks { while (it.hasNext()) { ProcessorDefinition<?> output = it.next(); if (matchBy.match(output)) { - List<ProcessorDefinition<?>> outputs = getParentOutputs(output.getParent()); + List<ProcessorDefinition<?>> outputs = getOutputs(output); if (outputs != null) { int index = outputs.indexOf(output); if (index != -1) { @@ -310,17 +311,26 @@ public final class AdviceWithTasks { } /** - * Gets the outputs from the given parent. + * Gets the outputs to use with advice with from the given child/parent * <p/> * This implementation deals with that outputs can be abstract and retrieves the <i>correct</i> parent output. * - * @param parent the parent - * @return <tt>null</tt> if no parent + * @param node the node + * @return <tt>null</tt> if not outputs to be used */ - private static List<ProcessorDefinition<?>> getParentOutputs(ProcessorDefinition<?> parent) { + private static List<ProcessorDefinition<?>> getOutputs(ProcessorDefinition<?> node) { + if (node == null) { + return null; + } + ProcessorDefinition<?> parent = node.getParent(); if (parent == null) { return null; } + // for CBR then use the outputs from the node itself + // so we work on the right branch in the CBR (when/otherwise) + if (parent instanceof ChoiceDefinition) { + return node.getOutputs(); + } List<ProcessorDefinition<?>> outputs = parent.getOutputs(); if (outputs.size() == 1 && outputs.get(0).isAbstract()) { // if the output is abstract then get its output, as http://git-wip-us.apache.org/repos/asf/camel/blob/12ddce8e/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java b/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java index 60370cc..6ec26cd 100644 --- a/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/ChoiceDefinition.java @@ -125,7 +125,8 @@ public class ChoiceDefinition extends ProcessorDefinition<ChoiceDefinition> { @Override public String toString() { - return "Choice[" + getWhenClauses() + (getOtherwise() != null ? " " + getOtherwise() : "") + "]"; + // when and otherwise has detailed output in their toString so keep this toString short + return "Choice"; } @Override http://git-wip-us.apache.org/repos/asf/camel/blob/12ddce8e/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToStringCBRTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToStringCBRTest.java b/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToStringCBRTest.java new file mode 100644 index 0000000..124b1fc --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/issues/AdviceWithWeaveByToStringCBRTest.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.issues; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.AdviceWithRouteBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.model.RouteDefinition; + +/** + * @version + */ +public class AdviceWithWeaveByToStringCBRTest extends ContextTestSupport { + + public void testAdviceCBR() throws Exception { + RouteDefinition route = context.getRouteDefinitions().get(0); + route.adviceWith(context, new AdviceWithRouteBuilder() { + @Override + public void configure() throws Exception { + weaveByToString("direct:branch.*").replace().to("mock:foo"); + mockEndpointsAndSkip("direct:branch*"); + } + }); + + getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .choice() + .when(header("foo")).to("direct:branch-1") + .otherwise() + .to("direct:branch-2"); + } + }; + } + +}