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

orpiske 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 241ce9add73 CAMEL-21014: Fixed doFinally clause with route template 
(#14913)
241ce9add73 is described below

commit 241ce9add731ffbcf19ad26edeb226716828565a
Author: Giacomo <gcarnev...@imolainformatica.it>
AuthorDate: Thu Jul 25 09:33:56 2024 +0200

    CAMEL-21014: Fixed doFinally clause with route template (#14913)
    
    * TryDefinitionTest: reproduced IllegalArgumentException with only one 
doFinally clause
    * Fix IllegalArgumentException on a single doFinally clause due to object 
copying and the comparison finallyClause != output (always true due to the 
copying)
---
 .../java/org/apache/camel/model/TryDefinition.java | 13 +++---
 .../org/apache/camel/model/TryDefinitionTest.java  | 51 ++++++++++++++++++++++
 2 files changed, 58 insertions(+), 6 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 f39217f695c..05e574553f8 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
@@ -229,22 +229,23 @@ public class TryDefinition extends 
OutputDefinition<TryDefinition> {
             if (catchClauses == null) {
                 catchClauses = new ArrayList<>();
             }
+            int doFinallyCounter = 0;
             for (ProcessorDefinition<?> output : outputs) {
                 if (output instanceof CatchDefinition) {
                     if (!catchClauses.contains(output)) {
                         catchClauses.add((CatchDefinition) output);
                     }
                 } else if (output instanceof FinallyDefinition) {
-                    if (finallyClause != null && output != finallyClause) {
-                        throw new IllegalArgumentException(
-                                "Multiple finally clauses added: " + 
finallyClause + " and " + output);
-                    } else {
-                        finallyClause = (FinallyDefinition) output;
-                    }
+                    ++doFinallyCounter;
+                    finallyClause = (FinallyDefinition) output;
                 } else {
                     outputsWithoutCatches.add(output);
                 }
             }
+            if (doFinallyCounter > 1) {
+                throw new IllegalArgumentException(
+                        "Multiple finally clauses added: " + doFinallyCounter);
+            }
             // initialize parent
             for (CatchDefinition cd : catchClauses) {
                 cd.setParent(this);
diff --git 
a/core/camel-core-model/src/test/java/org/apache/camel/model/TryDefinitionTest.java
 
b/core/camel-core-model/src/test/java/org/apache/camel/model/TryDefinitionTest.java
new file mode 100644
index 00000000000..10fbfd72231
--- /dev/null
+++ 
b/core/camel-core-model/src/test/java/org/apache/camel/model/TryDefinitionTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.model.rest;
+
+import java.util.List;
+
+import org.apache.camel.model.CatchDefinition;
+import org.apache.camel.model.FinallyDefinition;
+import org.apache.camel.model.ToDefinition;
+import org.apache.camel.model.TryDefinition;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TryDefinitionTest {
+
+    @Test
+    public void doFinallyTest() {
+        TryDefinition tryDefinition = new TryDefinition();
+        CatchDefinition catchDefinition = new CatchDefinition();
+        FinallyDefinition finallyDefinition = new FinallyDefinition();
+        tryDefinition.addOutput(new ToDefinition("mock:1"));
+        catchDefinition.setExceptions(List.of("java.lang.Exception"));
+        catchDefinition.addOutput(new ToDefinition("mock:2"));
+        finallyDefinition.addOutput(new ToDefinition("mock:3"));
+        tryDefinition.addOutput(catchDefinition);
+        tryDefinition.addOutput(finallyDefinition);
+        Assertions.assertDoesNotThrow(tryDefinition::preCreateProcessor);
+        TryDefinition tryDefinition1 = tryDefinition.copyDefinition();
+        Assertions.assertDoesNotThrow(tryDefinition1::preCreateProcessor);
+
+        FinallyDefinition finallyDefinition1 = new FinallyDefinition();
+        finallyDefinition.addOutput(new ToDefinition("mock:4"));
+        tryDefinition.addOutput(finallyDefinition1);
+        Assertions.assertThrows(IllegalArgumentException.class, 
tryDefinition::preCreateProcessor);
+    }
+
+}

Reply via email to