This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch when2 in repository https://gitbox.apache.org/repos/asf/camel.git
commit bcf4408de797793fae91f2208a1ac0b99b05d7a7 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Jan 16 19:12:15 2025 +0100 CAMEL-21620: camel-core - Choice EIP fix to make when/otherwise as exclusive for this EIP and not generic EIPs --- .../org/apache/camel/model/ChoiceDefinition.java | 14 +- .../camel/model/OptionalIdentifiedDefinition.java | 4 +- .../apache/camel/model/ProcessorDefinition.java | 21 +- .../camel/model/ProcessorDefinitionHelper.java | 6 +- .../apache/camel/model/ChoiceDefinitionTest.java | 232 --------------------- 5 files changed, 33 insertions(+), 244 deletions(-) diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/ChoiceDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/ChoiceDefinition.java index d21a950f76e..155fef7427d 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/ChoiceDefinition.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/ChoiceDefinition.java @@ -18,7 +18,6 @@ package org.apache.camel.model; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; import jakarta.xml.bind.annotation.XmlAccessType; import jakarta.xml.bind.annotation.XmlAccessorType; @@ -193,7 +192,6 @@ public class ChoiceDefinition extends NoOutputDefinition<ChoiceDefinition> { } } - // TODO: Remove me as we should avoid having this @Override public List<ProcessorDefinition<?>> getOutputs() { // backwards compatible where choice would fake outputs to include when/otherwise as a single list @@ -218,8 +216,16 @@ public class ChoiceDefinition extends NoOutputDefinition<ChoiceDefinition> { @Override public String getLabel() { - return getOutputs().stream().map(ProcessorDefinition::getLabel) - .collect(Collectors.joining(",", getShortName() + "[", "]")); + StringBuilder sb = new StringBuilder(); + sb.append("choice["); + for (WhenDefinition when : whenClauses) { + sb.append(when.getLabel()); + } + if (otherwise != null) { + sb.append(otherwise.getLabel()); + } + sb.append("]"); + return sb.toString(); } public List<WhenDefinition> getWhenClauses() { diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java index 552cdb19395..f7cf9b2147e 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/OptionalIdentifiedDefinition.java @@ -78,7 +78,7 @@ public abstract class OptionalIdentifiedDefinition<T extends OptionalIdentifiedD // prefix is only for nodes in the route (not the route id) String prefix = null; boolean iAmRoute = this instanceof RouteDefinition; - boolean allowPrefix = !iAmRoute && this instanceof ProcessorDefinition; + boolean allowPrefix = !iAmRoute; if (allowPrefix) { RouteDefinition route = ProcessorDefinitionHelper.getRoute(this); if (route != null) { @@ -186,7 +186,7 @@ public abstract class OptionalIdentifiedDefinition<T extends OptionalIdentifiedD // return with prefix if configured boolean iAmRoute = this instanceof RouteDefinition; - boolean allowPrefix = !iAmRoute && this instanceof ProcessorDefinition; + boolean allowPrefix = !iAmRoute; if (allowPrefix) { String prefix = getNodePrefixId(); if (prefix != null) { diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinition.java b/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinition.java index fd42711b833..86568cdaa0b 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinition.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinition.java @@ -664,10 +664,25 @@ public abstract class ProcessorDefinition<Type extends ProcessorDefinition<Type> public Type id(String id) { // special for choice otherwise if (this instanceof ChoiceDefinition cbr) { - if (cbr.getOtherwise() != null && cbr.getOtherwise().getOutputs().isEmpty()) { - cbr.getOtherwise().id(id); - return asType(); + if (cbr.getOtherwise() != null) { + if (cbr.getOtherwise().getOutputs().isEmpty()) { + cbr.getOtherwise().id(id); + } else { + var last = cbr.getOtherwise().getOutputs().get(cbr.getOtherwise().getOutputs().size() - 1); + last.id(id); + } + } else if (!cbr.getWhenClauses().isEmpty()) { + var last = cbr.getWhenClauses().get(cbr.getWhenClauses().size() - 1); + if (last.getOutputs().isEmpty()) { + last.setId(id); + } else { + var p = last.getOutputs().get(last.getOutputs().size() - 1); + p.id(id); + } + } else { + cbr.setId(id); } + return asType(); } if (this instanceof OutputNode && getOutputs().isEmpty()) { diff --git a/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java b/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java index ff5494d7c86..8d7a17f981c 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java @@ -149,14 +149,14 @@ public final class ProcessorDefinitionHelper { return null; } - ProcessorDefinition<?> def = (ProcessorDefinition) node; + NamedNode def = node; // drill to the top while (def != null && def.getParent() != null) { def = def.getParent(); } - if (def instanceof RouteDefinition routeDefinition) { - return routeDefinition; + if (def instanceof RouteDefinition rd) { + return rd; } else { // not found return null; diff --git a/core/camel-core/src/test/java/org/apache/camel/model/ChoiceDefinitionTest.java b/core/camel-core/src/test/java/org/apache/camel/model/ChoiceDefinitionTest.java deleted file mode 100644 index 8e443e9a35a..00000000000 --- a/core/camel-core/src/test/java/org/apache/camel/model/ChoiceDefinitionTest.java +++ /dev/null @@ -1,232 +0,0 @@ -/* - * 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; - -import org.apache.camel.TestSupport; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * - */ -@Disabled -public class ChoiceDefinitionTest extends TestSupport { - - @Test - public void testChoiceOutputOrder() { - ChoiceDefinition choice = new ChoiceDefinition(); - WhenDefinition when1 = new WhenDefinition(body().contains("Camel")); - WhenDefinition when2 = new WhenDefinition(body().contains("Donkey")); - OtherwiseDefinition other = new OtherwiseDefinition(); - - choice.addOutput(when1); - choice.addOutput(when2); - choice.addOutput(other); - - // assertEquals(3, choice.getOutputs().size()); - // assertEquals(when1, choice.getOutputs().get(0)); - // assertEquals(when2, choice.getOutputs().get(1)); - // assertEquals(other, choice.getOutputs().get(2)); - assertEquals("choice[when[{body contains Camel}],when[{body contains Donkey}],otherwise]", choice.getLabel()); - } - - @Test - public void testChoiceOutputOrderIterate() { - ChoiceDefinition choice = new ChoiceDefinition(); - WhenDefinition when1 = new WhenDefinition(body().contains("Camel")); - WhenDefinition when2 = new WhenDefinition(body().contains("Donkey")); - OtherwiseDefinition other = new OtherwiseDefinition(); - - choice.addOutput(when1); - choice.addOutput(when2); - choice.addOutput(other); - - assertEquals(3, choice.getOutputs().size()); - int i = 0; - for (ProcessorDefinition<?> def : choice.getOutputs()) { - if (i == 0) { - assertEquals(when1, def); - } else if (i == 1) { - assertEquals(when2, def); - } else { - assertEquals(other, def); - } - i++; - } - } - - @Test - public void testChoiceOutputOrderNoOtherwise() { - ChoiceDefinition choice = new ChoiceDefinition(); - WhenDefinition when1 = new WhenDefinition(body().contains("Camel")); - WhenDefinition when2 = new WhenDefinition(body().contains("Donkey")); - - choice.addOutput(when1); - choice.addOutput(when2); - - assertEquals(2, choice.getOutputs().size()); - assertEquals(when1, choice.getOutputs().get(0)); - assertEquals(when2, choice.getOutputs().get(1)); - } - - @Test - public void testChoiceOutputOrderNoOtherwiseIterate() { - ChoiceDefinition choice = new ChoiceDefinition(); - WhenDefinition when1 = new WhenDefinition(body().contains("Camel")); - WhenDefinition when2 = new WhenDefinition(body().contains("Donkey")); - - choice.addOutput(when1); - choice.addOutput(when2); - - assertEquals(2, choice.getOutputs().size()); - assertEquals(when1, choice.getOutputs().get(0)); - assertEquals(when2, choice.getOutputs().get(1)); - - assertEquals(2, choice.getOutputs().size()); - int i = 0; - for (ProcessorDefinition<?> def : choice.getOutputs()) { - if (i == 0) { - assertEquals(when1, def); - } else if (i == 1) { - assertEquals(when2, def); - } - i++; - } - } - - @Test - public void testChoiceOtherwiseAlwaysLast() { - ChoiceDefinition choice = new ChoiceDefinition(); - WhenDefinition when1 = new WhenDefinition(body().contains("Camel")); - WhenDefinition when2 = new WhenDefinition(body().contains("Donkey")); - OtherwiseDefinition other = new OtherwiseDefinition(); - - // add otherwise in between - choice.addOutput(when1); - choice.addOutput(other); - choice.addOutput(when2); - - // should ensure otherwise is last - assertEquals(3, choice.getOutputs().size()); - assertEquals(when1, choice.getOutputs().get(0)); - assertEquals(when2, choice.getOutputs().get(1)); - assertEquals(other, choice.getOutputs().get(2)); - } - - @Test - public void testChoiceOtherwiseAlwaysLastIterate() { - ChoiceDefinition choice = new ChoiceDefinition(); - WhenDefinition when1 = new WhenDefinition(body().contains("Camel")); - WhenDefinition when2 = new WhenDefinition(body().contains("Donkey")); - OtherwiseDefinition other = new OtherwiseDefinition(); - - // add otherwise in between - choice.addOutput(when1); - choice.addOutput(other); - choice.addOutput(when2); - - // should ensure otherwise is last - assertEquals(3, choice.getOutputs().size()); - assertEquals(when1, choice.getOutputs().get(0)); - assertEquals(when2, choice.getOutputs().get(1)); - assertEquals(other, choice.getOutputs().get(2)); - - assertEquals(3, choice.getOutputs().size()); - int i = 0; - for (ProcessorDefinition<?> def : choice.getOutputs()) { - if (i == 0) { - assertEquals(when1, def); - } else if (i == 1) { - assertEquals(when2, def); - } else { - assertEquals(other, def); - } - i++; - } - } - - @Test - public void testChoiceOutputRemoveFirst() { - ChoiceDefinition choice = new ChoiceDefinition(); - WhenDefinition when1 = new WhenDefinition(body().contains("Camel")); - WhenDefinition when2 = new WhenDefinition(body().contains("Donkey")); - OtherwiseDefinition other = new OtherwiseDefinition(); - - choice.addOutput(when1); - choice.addOutput(when2); - choice.addOutput(other); - - assertEquals(3, choice.getOutputs().size()); - choice.getOutputs().remove(0); - assertEquals(2, choice.getOutputs().size()); - assertEquals(when2, choice.getOutputs().get(0)); - assertEquals(other, choice.getOutputs().get(1)); - } - - @Test - public void testChoiceOutputRemoveLast() { - ChoiceDefinition choice = new ChoiceDefinition(); - WhenDefinition when1 = new WhenDefinition(body().contains("Camel")); - WhenDefinition when2 = new WhenDefinition(body().contains("Donkey")); - OtherwiseDefinition other = new OtherwiseDefinition(); - - choice.addOutput(when1); - choice.addOutput(when2); - choice.addOutput(other); - - assertEquals(2, choice.getOutputs().size()); - assertEquals(when1, choice.getOutputs().get(0)); - assertEquals(when2, choice.getOutputs().get(1)); - } - - @Test - public void testChoiceOutputSetFirst() { - ChoiceDefinition choice = new ChoiceDefinition(); - WhenDefinition when1 = new WhenDefinition(body().contains("Camel")); - WhenDefinition when2 = new WhenDefinition(body().contains("Donkey")); - WhenDefinition when3 = new WhenDefinition(body().contains("Beer")); - OtherwiseDefinition other = new OtherwiseDefinition(); - - choice.addOutput(when1); - choice.addOutput(when2); - choice.addOutput(other); - - assertEquals(3, choice.getOutputs().size()); - choice.getOutputs().set(0, when3); - assertEquals(3, choice.getOutputs().size()); - assertEquals(when3, choice.getOutputs().get(0)); - assertEquals(when2, choice.getOutputs().get(1)); - assertEquals(other, choice.getOutputs().get(2)); - } - - @Test - public void testChoiceOutputClear() { - ChoiceDefinition choice = new ChoiceDefinition(); - WhenDefinition when1 = new WhenDefinition(body().contains("Camel")); - WhenDefinition when2 = new WhenDefinition(body().contains("Donkey")); - - choice.addOutput(when1); - choice.addOutput(when2); - - assertEquals(2, choice.getOutputs().size()); - choice.getOutputs().clear(); - assertEquals(0, choice.getOutputs().size()); - } - -}