This is an automated email from the ASF dual-hosted git repository. klease pushed a commit to branch backport-19766-3.21.x in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2b58ff2e5a79fd95bd08895fb41ff318214295b2 Author: klease <38634989+kle...@users.noreply.github.com> AuthorDate: Sun Aug 20 21:59:12 2023 +0200 CAMEL-19766: Fix issue with not setting routeConfiguration on XML routes (#11152) The CamelContext has to be set on the RoutesDefinition in the builder and not on the one returned from the ModelParser which is discarded. --- .../camel/dsl/xml/io/XmlRoutesBuilderLoader.java | 2 +- .../org/apache/camel/dsl/xml/io/XmlLoadTest.java | 30 ++++++++++++++++++++++ .../org/apache/camel/dsl/xml/io/routeConfig.xml | 27 +++++++++++++++++++ .../camel/dsl/xml/io/routeWithRouteConfig.xml | 25 ++++++++++++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) diff --git a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java index 127c1d8cd8f..bc903734de1 100644 --- a/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java +++ b/dsl/camel-xml-io-dsl/src/main/java/org/apache/camel/dsl/xml/io/XmlRoutesBuilderLoader.java @@ -86,7 +86,7 @@ public class XmlRoutesBuilderLoader extends RouteBuilderLoaderSupport { } private void addRoutes(RoutesDefinition routes) { - CamelContextAware.trySetCamelContext(routes, getCamelContext()); + CamelContextAware.trySetCamelContext(getRouteCollection(), getCamelContext()); // xml routes must be prepared in the same way java-dsl (via RoutesDefinition) // so create a copy and use the fluent builder to add the route diff --git a/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadTest.java b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadTest.java index 8119d73737e..50474c01ac5 100644 --- a/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadTest.java +++ b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadTest.java @@ -17,6 +17,7 @@ package org.apache.camel.dsl.xml.io; import org.apache.camel.ExtendedCamelContext; +import org.apache.camel.Route; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.impl.DefaultCamelContext; @@ -92,4 +93,33 @@ public class XmlLoadTest { bar.assertIsSatisfied(); } } + + @Test + public void testLoadRoutesAndConfig() throws Exception { + try (DefaultCamelContext context = new DefaultCamelContext()) { + context.start(); + // Load routeConfiguration from XML + ExtendedCamelContext ecc = context.adapt(ExtendedCamelContext.class); + Resource configResource = ecc.getResourceLoader().resolveResource( + "/org/apache/camel/dsl/xml/io/routeConfig.xml"); + + ecc.getRoutesLoader().loadRoutes(configResource); + // load route from XML and add them to the existing camel context + Resource resource = ecc.getResourceLoader().resolveResource( + "/org/apache/camel/dsl/xml/io/routeWithRouteConfig.xml"); + + ecc.getRoutesLoader().loadRoutes(resource); + + Route routewithConfig = context.getRoute("routeWithConfig"); + assertNotNull(routewithConfig, "Loaded routeWithConfig route should be there"); + assertEquals(1, routewithConfig.getOnExceptions().size(), "Loaded route should have onException"); + assertEquals(1, context.getRoutes().size()); + + // test that loaded route works + MockEndpoint bar = context.getEndpoint("mock:afterException", MockEndpoint.class); + bar.expectedBodiesReceived("Hi World"); + context.createProducerTemplate().sendBody("direct:throwException", "Hi World"); + bar.assertIsSatisfied(); + } + } } diff --git a/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/routeConfig.xml b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/routeConfig.xml new file mode 100644 index 00000000000..abf163882cc --- /dev/null +++ b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/routeConfig.xml @@ -0,0 +1,27 @@ +<?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. + +--> +<routeConfiguration id="errorConfig"> + <onException> + <exception>java.lang.Exception</exception> + <handled><constant>true</constant></handled> + <log message="XML WARN: ${exception.message}"/> + <to uri="mock:afterException"/> + </onException> +</routeConfiguration> \ No newline at end of file diff --git a/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/routeWithRouteConfig.xml b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/routeWithRouteConfig.xml new file mode 100644 index 00000000000..319e027e806 --- /dev/null +++ b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/routeWithRouteConfig.xml @@ -0,0 +1,25 @@ +<?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. + +--> +<routes> + <route id="routeWithConfig" routeConfigurationId="errorConfig"> + <from uri="direct:throwException"/> + <throwException exceptionType="java.lang.Exception" message="Error should be handled"/> + </route> +</routes> \ No newline at end of file