This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-4.10.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.10.x by this push: new e9305b167c2 CAMEL-21958: camel-core: Java DSL. Fix endChoice() to reuse end() and scope to current/nearest choice. e9305b167c2 is described below commit e9305b167c20baad9653151fa3e86fd178e47205 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Apr 16 16:31:10 2025 +0200 CAMEL-21958: camel-core: Java DSL. Fix endChoice() to reuse end() and scope to current/nearest choice. --- .../NestedChoiceWithEndChoiceIssueTest.java | 121 +++++++++++++++++++++ .../NestedChoiceWithEndChoiceIssueTest.xml | 84 ++++++++++++++ 2 files changed, 205 insertions(+) diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/NestedChoiceWithEndChoiceIssueTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/NestedChoiceWithEndChoiceIssueTest.java new file mode 100644 index 00000000000..b7af477e1b6 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/processor/NestedChoiceWithEndChoiceIssueTest.java @@ -0,0 +1,121 @@ +/* + * 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.processor; + +import java.nio.file.Paths; + +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.NamedNode; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.spi.NodeIdFactory; +import org.apache.camel.support.PluginHelper; +import org.apache.camel.util.IOHelper; +import org.apache.camel.util.StringHelper; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class NestedChoiceWithEndChoiceIssueTest extends ContextTestSupport { + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext ctx = super.createCamelContext(); + ctx.getCamelContextExtension().addContextPlugin(NodeIdFactory.class, buildNodeIdFactory()); + return ctx; + } + + private static NodeIdFactory buildNodeIdFactory() { + return new NodeIdFactory() { + @Override + public String createId(NamedNode definition) { + return definition.getShortName(); // do not use counter + } + }; + } + + @Test + public void testNestedChoiceOtherwise() throws Exception { + String xml = PluginHelper.getModelToXMLDumper(context).dumpModelAsXml(context, + context.getRouteDefinition("myRoute")); + assertNotNull(xml); + log.info(xml); + + String expected + = IOHelper.stripLineComments( + Paths.get("src/test/resources/org/apache/camel/processor/NestedChoiceWithEndChoiceIssueTest.xml"), "#", + true); + expected = StringHelper.after(expected, "-->"); + Assertions.assertEquals(expected, "\n" + xml + "\n"); + + getMockEndpoint("mock:result").expectedMessageCount(1); + getMockEndpoint("mock:result").expectedBodiesReceived("2"); + getMockEndpoint("mock:result").expectedHeaderReceived("count", "1000"); + + template.sendBodyAndHeader("direct:start", 1, "count", 1); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("direct:test").routeId("myRoute") + .choice() + .when(simple("${header.count} < 1000 && ${body} == 0")) + .setHeader("count", simple("${header.count}++")) + .setBody(constant(1)) + .log("First when. Header is:${header.count} Body is:${body}") + .to("direct:test") + .when(simple("${header.count} < 1000 && ${body} == 1")) + .setHeader("count", simple("${header.count}++")) + .setBody(constant(2)) + .log("Second when. Header is:${header.count} Body is:${body}") + .to("direct:test") + .when(simple("${header.count} < 1000 && ${body} == 2")) + .setHeader("count", simple("${header.count}++")) + .setBody(constant(0)) + .choice() + .when(simple("${header.count} < 500")) + .log("Third when and small header. Header is:${header.count} Body is:${body}") + .when(simple("${header.count} < 900")) + .log("Third when and big header. Header is:${header.count} Body is:${body}") + .otherwise() + .log("Third when and header over 900. Header is:${header.count} Body is:${body}") + .choice() + .when(simple("${header.count} == 996")) + .log("Deep choice log. Header is:${header.count}") + .setHeader("count", constant(998)) + .end().endChoice() + .end() + .to("direct:test") + .endChoice() + .otherwise() + .log("Header is:${header.count}") + .log("Final Body is:${body}") + .end(); + + from("direct:start").routeId("start") + .to("direct:test") + .to("mock:result"); + } + }; + } +} diff --git a/core/camel-core/src/test/resources/org/apache/camel/processor/NestedChoiceWithEndChoiceIssueTest.xml b/core/camel-core/src/test/resources/org/apache/camel/processor/NestedChoiceWithEndChoiceIssueTest.xml new file mode 100644 index 00000000000..ba9efbd6416 --- /dev/null +++ b/core/camel-core/src/test/resources/org/apache/camel/processor/NestedChoiceWithEndChoiceIssueTest.xml @@ -0,0 +1,84 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!-- + + 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. + +--> + + +<route xmlns="http://camel.apache.org/schema/spring" id="myRoute"> + <from id="from" uri="direct:test"/> + <choice id="choice"> + <when id="when"> + <simple>${header.count} < 1000 && ${body} == 0</simple> + <setHeader id="setHeader" name="count"> + <simple>${header.count}++</simple> + </setHeader> + <setBody id="setBody"> + <expressionDefinition>1</expressionDefinition> + </setBody> + <log id="log" message="First when. Header is:${header.count} Body is:${body}"/> + <to id="to" uri="direct:test"/> + </when> + <when id="when"> + <simple>${header.count} < 1000 && ${body} == 1</simple> + <setHeader id="setHeader" name="count"> + <simple>${header.count}++</simple> + </setHeader> + <setBody id="setBody"> + <expressionDefinition>2</expressionDefinition> + </setBody> + <log id="log" message="Second when. Header is:${header.count} Body is:${body}"/> + <to id="to" uri="direct:test"/> + </when> + <when id="when"> + <simple>${header.count} < 1000 && ${body} == 2</simple> + <setHeader id="setHeader" name="count"> + <simple>${header.count}++</simple> + </setHeader> + <setBody id="setBody"> + <expressionDefinition>0</expressionDefinition> + </setBody> + <choice id="choice"> + <when id="when"> + <simple>${header.count} < 500</simple> + <log id="log" message="Third when and small header. Header is:${header.count} Body is:${body}"/> + </when> + <when id="when"> + <simple>${header.count} < 900</simple> + <log id="log" message="Third when and big header. Header is:${header.count} Body is:${body}"/> + </when> + <otherwise id="otherwise"> + <log id="log" message="Third when and header over 900. Header is:${header.count} Body is:${body}"/> + <choice id="choice"> + <when id="when"> + <simple>${header.count} == 996</simple> + <log id="log" message="Deep choice log. Header is:${header.count}"/> + <setHeader id="setHeader" name="count"> + <expressionDefinition>998</expressionDefinition> + </setHeader> + </when> + </choice> + </otherwise> + </choice> + <to id="to" uri="direct:test"/> + </when> + <otherwise id="otherwise"> + <log id="log" message="Header is:${header.count}"/> + <log id="log" message="Final Body is:${body}"/> + </otherwise> + </choice> +</route>