Skip to content

Commit c0bfa10

Browse files
ppalagadavsclaus
andauthored
[4.10.x] CAMEL-21958: camel-core: Java DSL. Fix endChoice() to reuse end() and scope to current/nearest choice (#17778)
* CAMEL-21958: camel-core: Java DSL. Fix endChoice() to reuse end() and scope to current/nearest choice. (#17761) * CAMEL-21958: camel-core: Java DSL. Fix endChoice() to reuse end() and scope to current/nearest choice. * CAMEL-21958: camel-core: Java DSL. Fix endChoice() to reuse end() and scope to current/nearest choice. --------- Co-authored-by: Claus Ibsen <[email protected]>
1 parent 3b05ccb commit c0bfa10

File tree

7 files changed

+119
-18
lines changed

7 files changed

+119
-18
lines changed

components/camel-spring-xml/src/test/resources/org/apache/camel/spring/processor/SpringNestedChoiceIssueTest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
<camelContext xmlns="http://camel.apache.org/schema/spring">
2828
<jmxAgent id="jmx" disabled="true"/>
29-
<route>
29+
<route id="route1">
3030
<from uri="direct:start"/>
3131
<choice>
3232
<when>

core/camel-core-model/src/main/java/org/apache/camel/model/ChoiceDefinition.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ public ExpressionClause<ChoiceDefinition> when() {
186186
* @return the builder
187187
*/
188188
public ChoiceDefinition otherwise() {
189+
if (this.otherwise != null) {
190+
throw new IllegalArgumentException(
191+
"Cannot add a 2nd otherwise to this choice: " + this
192+
+ ". If you have nested choice then you may need to end().endChoice() to go back to parent choice.");
193+
}
189194
OtherwiseDefinition answer = new OtherwiseDefinition();
190195
addClause(answer);
191196
return this;

core/camel-core-model/src/main/java/org/apache/camel/model/ProcessorDefinition.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
import org.apache.camel.ExchangePattern;
4242
import org.apache.camel.Expression;
4343
import org.apache.camel.LoggingLevel;
44-
import org.apache.camel.NamedNode;
4544
import org.apache.camel.Predicate;
4645
import org.apache.camel.Processor;
4746
import org.apache.camel.builder.DataFormatClause;
@@ -1089,25 +1088,24 @@ public ProcessorDefinition<?> endParent() {
10891088
public ChoiceDefinition endChoice() {
10901089
ProcessorDefinition<?> def = this;
10911090

1092-
// are we nested choice?
1093-
if (def.getParent() instanceof ChoiceDefinition cho) {
1091+
// are we already a choice
1092+
if (def instanceof ChoiceDefinition cho) {
10941093
return cho;
10951094
}
10961095

1096+
// end and find the choice
1097+
def = end();
1098+
if (def instanceof RouteDefinition) {
1099+
// okay that was too far down so go back up
1100+
def = this;
1101+
}
1102+
10971103
// are we already a choice?
10981104
if (def instanceof ChoiceDefinition choice) {
10991105
return choice;
1100-
}
1101-
1102-
// okay end this and get back to the choice
1103-
def = end();
1104-
NamedNode p = def.getParent();
1105-
if ("when".equals(p.getShortName())) {
1106-
return (ChoiceDefinition) p;
1107-
} else if ("otherwise".equals(p.getShortName())) {
1108-
return (ChoiceDefinition) p;
11091106
} else {
1110-
return (ChoiceDefinition) def;
1107+
throw new IllegalArgumentException(
1108+
"Cannot endChoice() to find current/parent choice DSL. If you have nested choice then you may need to end().endChoice() to go back to parent choice.");
11111109
}
11121110
}
11131111

core/camel-core/src/test/java/org/apache/camel/processor/NestedChoiceIssueTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
import org.apache.camel.ContextTestSupport;
2020
import org.apache.camel.builder.RouteBuilder;
21+
import org.apache.camel.support.PluginHelper;
2122
import org.junit.jupiter.api.Test;
2223

24+
import static org.junit.jupiter.api.Assertions.assertNotNull;
25+
2326
public class NestedChoiceIssueTest extends ContextTestSupport {
2427

2528
@Test
@@ -46,6 +49,11 @@ public void testNestedChoiceMed() throws Exception {
4649

4750
@Test
4851
public void testNestedChoiceLow() throws Exception {
52+
String xml = PluginHelper.getModelToXMLDumper(context).dumpModelAsXml(context,
53+
context.getRouteDefinition("route1"));
54+
assertNotNull(xml);
55+
log.info(xml);
56+
4957
getMockEndpoint("mock:low").expectedMessageCount(1);
5058
getMockEndpoint("mock:med").expectedMessageCount(0);
5159
getMockEndpoint("mock:big").expectedMessageCount(0);
@@ -62,7 +70,7 @@ protected RouteBuilder createRouteBuilder() {
6270
public void configure() {
6371
from("direct:start").choice().when(header("foo").isGreaterThan(1)).choice().when(header("foo").isGreaterThan(5))
6472
.to("mock:big").otherwise().to("mock:med")
65-
.endChoice().otherwise().to("mock:low").end();
73+
.end().endChoice().otherwise().to("mock:low").end();
6674
}
6775
};
6876
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.camel.processor;
18+
19+
import org.apache.camel.ContextTestSupport;
20+
import org.apache.camel.builder.RouteBuilder;
21+
import org.apache.camel.support.PluginHelper;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.junit.jupiter.api.Assertions.assertNotNull;
25+
26+
public class NestedChoiceOtherwiseIssueTest extends ContextTestSupport {
27+
28+
@Test
29+
public void testNestedChoiceOtherwise() throws Exception {
30+
String xml = PluginHelper.getModelToXMLDumper(context).dumpModelAsXml(context,
31+
context.getRouteDefinition("myRoute"));
32+
assertNotNull(xml);
33+
log.info(xml);
34+
35+
getMockEndpoint("mock:foo").expectedMessageCount(1);
36+
getMockEndpoint("mock:bar").expectedMessageCount(0);
37+
getMockEndpoint("mock:other").expectedMessageCount(0);
38+
getMockEndpoint("mock:other2").expectedMessageCount(0);
39+
template.sendBodyAndHeader("direct:start", "Hello World", "foo", 10);
40+
assertMockEndpointsSatisfied();
41+
42+
resetMocks();
43+
getMockEndpoint("mock:foo").expectedMessageCount(0);
44+
getMockEndpoint("mock:bar").expectedMessageCount(1);
45+
getMockEndpoint("mock:other").expectedMessageCount(1);
46+
getMockEndpoint("mock:other2").expectedMessageCount(0);
47+
template.sendBodyAndHeader("direct:start", "Hello World", "bar", 11);
48+
assertMockEndpointsSatisfied();
49+
50+
resetMocks();
51+
getMockEndpoint("mock:foo").expectedMessageCount(0);
52+
getMockEndpoint("mock:bar").expectedMessageCount(0);
53+
getMockEndpoint("mock:other").expectedMessageCount(1);
54+
getMockEndpoint("mock:other2").expectedMessageCount(1);
55+
template.sendBodyAndHeader("direct:start", "Hello World", "cheese", 12);
56+
assertMockEndpointsSatisfied();
57+
}
58+
59+
@Override
60+
protected RouteBuilder createRouteBuilder() {
61+
return new RouteBuilder() {
62+
@Override
63+
public void configure() {
64+
from("direct:start").routeId("myRoute")
65+
.errorHandler(noErrorHandler())
66+
.choice()
67+
.when(header("foo"))
68+
.to("mock:foo")
69+
.otherwise()
70+
.to("mock:other")
71+
.choice()
72+
.when(header("bar"))
73+
.to("mock:bar")
74+
.endChoice()
75+
.otherwise()
76+
.to("mock:other2")
77+
.end()
78+
.end();
79+
}
80+
};
81+
}
82+
}

core/camel-core/src/test/java/org/apache/camel/processor/TripleNestedChoiceIssueTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,11 @@
1818

1919
import org.apache.camel.ContextTestSupport;
2020
import org.apache.camel.builder.RouteBuilder;
21+
import org.apache.camel.support.PluginHelper;
2122
import org.junit.jupiter.api.Test;
2223

24+
import static org.junit.jupiter.api.Assertions.assertNotNull;
25+
2326
public class TripleNestedChoiceIssueTest extends ContextTestSupport {
2427

2528
@Test
@@ -60,6 +63,11 @@ public void testNestedChoiceMed() throws Exception {
6063

6164
@Test
6265
public void testNestedChoiceLow() throws Exception {
66+
String xml = PluginHelper.getModelToXMLDumper(context).dumpModelAsXml(context,
67+
context.getRouteDefinition("route1"));
68+
assertNotNull(xml);
69+
log.info(xml);
70+
6371
getMockEndpoint("mock:low").expectedMessageCount(1);
6472
getMockEndpoint("mock:med").expectedMessageCount(0);
6573
getMockEndpoint("mock:big").expectedMessageCount(0);
@@ -77,7 +85,7 @@ protected RouteBuilder createRouteBuilder() {
7785
public void configure() {
7886
from("direct:start").choice().when(header("foo").isGreaterThan(1)).choice().when(header("foo").isGreaterThan(5))
7987
.choice().when(header("foo").isGreaterThan(10))
80-
.to("mock:verybig").otherwise().to("mock:big").endChoice().otherwise().to("mock:med").endChoice()
88+
.to("mock:verybig").otherwise().to("mock:big").end().endChoice().otherwise().to("mock:med").end().endChoice()
8189
.otherwise().to("mock:low").end();
8290
}
8391
};

core/camel-core/src/test/java/org/apache/camel/processor/async/AsyncNestedTripleChoiceIssueTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ public void configure() {
7979

8080
from("direct:start").choice().when(header("foo").isGreaterThan(1)).to("async:bye:camel").choice()
8181
.when(header("foo").isGreaterThan(5)).to("async:bye:camel2")
82-
.choice().when(header("foo").isGreaterThan(7)).to("mock:verybig").otherwise().to("mock:big").endChoice()
83-
.otherwise().to("mock:med").endChoice().otherwise()
82+
.choice().when(header("foo").isGreaterThan(7)).to("mock:verybig").otherwise().to("mock:big").end().endChoice()
83+
.otherwise().to("mock:med").end().endChoice().otherwise()
8484
.to("mock:low").end();
8585
}
8686
};

0 commit comments

Comments
 (0)