This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch xml-rc in repository https://gitbox.apache.org/repos/asf/camel.git
commit 3308c71f63e3739a1329823fa2075f86426f903d Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Wed Jun 26 07:00:49 2024 +0200 CAMEL-20921: camel-xml-io - Fix loading route configuration if using camel root tag. Thanks to Adriano Machado for unit test. --- .../camel/dsl/xml/io/XmlRoutesBuilderLoader.java | 18 ++++++++++-- .../apache/camel/dsl/xml/io/XmlLoadAppTest.java | 25 ++++++++++++++++ .../org/apache/camel/dsl/xml/io/camel-app13.xml | 34 ++++++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) 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 49028290c6c..56bfbd8577c 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 @@ -160,11 +160,25 @@ public class XmlRoutesBuilderLoader extends RouteBuilderLoaderSupport { @Override public void configuration() throws Exception { switch (xmlInfo.getRootElementName()) { - case "routeConfigurations", "routeConfiguration" -> + // load any route configuration before that may be nested under camel/spring/blueprint root tag + case "beans", "blueprint", "camel", "routeConfigurations", "routeConfiguration": { + BeansDefinition bp = camelAppCache.get(input.getLocation()); + if (bp != null) { + bp.getRouteConfigurations().forEach(rc -> { + rc.setResource(getResource()); + List<RouteConfigurationDefinition> list = new ArrayList<>(); + list.add(rc); + RouteConfigurationsDefinition def = new RouteConfigurationsDefinition(); + def.setResource(getResource()); + def.setRouteConfigurations(list); + addConfigurations(def); + }); + // remove the configurations we have added + bp.getRouteConfigurations().clear(); + } new XmlModelParser(resource, xmlInfo.getRootElementNamespace()) .parseRouteConfigurationsDefinition() .ifPresent(this::addConfigurations); - default -> { } } } diff --git a/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java index c93a55aa48d..114f538344b 100644 --- a/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java +++ b/dsl/camel-xml-io-dsl/src/test/java/org/apache/camel/dsl/xml/io/XmlLoadAppTest.java @@ -18,6 +18,7 @@ package org.apache.camel.dsl.xml.io; import java.util.Map; +import org.apache.camel.Route; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.component.rest.DummyRestConsumerFactory; import org.apache.camel.component.rest.DummyRestProcessorFactory; @@ -336,4 +337,28 @@ public class XmlLoadAppTest { context.stop(); } } + + @Test + public void testLoadAppWithRouteConfigAndRoutes() throws Exception { + try (DefaultCamelContext context = new DefaultCamelContext()) { + context.start(); + + // camel-app13 has a route configuration and a route using the configuration + Resource resource = PluginHelper.getResourceLoader(context).resolveResource( + "/org/apache/camel/dsl/xml/io/camel-app13.xml"); + + PluginHelper.getRoutesLoader(context).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/camel-app13.xml b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/camel-app13.xml new file mode 100644 index 00000000000..b6e0c6cd40e --- /dev/null +++ b/dsl/camel-xml-io-dsl/src/test/resources/org/apache/camel/dsl/xml/io/camel-app13.xml @@ -0,0 +1,34 @@ +<?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. + +--> +<camel xmlns:s="http://www.springframework.org/schema/beans"> + <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> + + <route id="routeWithConfig" routeConfigurationId="errorConfig"> + <from uri="direct:throwException"/> + <throwException exceptionType="java.lang.Exception" message="Error should be handled"/> + </route> +</camel> \ No newline at end of file