This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.18.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.18.x by this push: new 68fa0f3c4fd CAMEL-18443: Fixed doTry .. doCatch EIP with AdviceWith. Thanks to Lars Haugaard Kristensen for unit test. 68fa0f3c4fd is described below commit 68fa0f3c4fdf04485a3a22ad23dc70cfd696852a Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Sep 1 07:31:09 2022 +0200 CAMEL-18443: Fixed doTry .. doCatch EIP with AdviceWith. Thanks to Lars Haugaard Kristensen for unit test. --- .../java/org/apache/camel/model/TryDefinition.java | 8 +-- .../issues/AdviceWithTryCatchFinallyTest.java | 59 ++++++++++++++++++++++ 2 files changed, 64 insertions(+), 3 deletions(-) diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/TryDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/TryDefinition.java index 1908d3c0a10..af080d46060 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/TryDefinition.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/TryDefinition.java @@ -202,7 +202,7 @@ public class TryDefinition extends OutputDefinition<TryDefinition> { @Override public void preCreateProcessor() { - // force re-creating initialization to ensure its up-to-date + // force re-creating initialization to ensure its up-to-date (yaml-dsl creates this EIP specially via @DslProperty) initialized = false; checkInitialized(); } @@ -219,9 +219,11 @@ public class TryDefinition extends OutputDefinition<TryDefinition> { } for (ProcessorDefinition<?> output : outputs) { if (output instanceof CatchDefinition) { - catchClauses.add((CatchDefinition) output); + if (!catchClauses.contains(output)) { + catchClauses.add((CatchDefinition) output); + } } else if (output instanceof FinallyDefinition) { - if (finallyClause != null) { + if (finallyClause != null && output != finallyClause) { throw new IllegalArgumentException( "Multiple finally clauses added: " + finallyClause + " and " + output); } else { diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithTryCatchFinallyTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithTryCatchFinallyTest.java new file mode 100644 index 00000000000..b76b560166b --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithTryCatchFinallyTest.java @@ -0,0 +1,59 @@ +/* + * 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.RouteBuilder; +import org.junit.jupiter.api.Test; + +import static org.apache.camel.builder.AdviceWith.adviceWith; + +public class AdviceWithTryCatchFinallyTest extends ContextTestSupport { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Test + public void testAdviceTryCatchFinally() throws Exception { + context.addRoutes(createRouteBuilder()); + + adviceWith(context, "my-route", a -> a.weaveById("replace-me") + .replace().to("mock:replaced")); + + context.start(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:start").routeId("my-route") + .doTry() + .log("try") + .to("mock:replace-me").id("replace-me") + .doCatch(Exception.class) + .log("catch") + .doFinally() + .log("finally") + .end(); + } + }; + } +}