You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by kl...@apache.org on 2023/08/20 19:59:18 UTC

[camel] branch main updated: CAMEL-19766: Fix issue with not setting routeConfiguration on XML routes (#11152)

This is an automated email from the ASF dual-hosted git repository.

klease pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new deea6d602c4 CAMEL-19766: Fix issue with not setting routeConfiguration on XML routes (#11152)
deea6d602c4 is described below

commit deea6d602c43e35b1a8cb7e479d18ad9867b4b41
Author: klease <38...@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   | 29 ++++++++++++++++++++++
 .../org/apache/camel/dsl/xml/io/routeConfig.xml    | 27 ++++++++++++++++++++
 .../camel/dsl/xml/io/routeWithRouteConfig.xml      | 25 +++++++++++++++++++
 4 files changed, 82 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 4a40f99ec03..f56177cd12e 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
@@ -223,7 +223,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 0fec6272c11..abb761bed54 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
@@ -16,6 +16,7 @@
  */
 package org.apache.camel.dsl.xml.io;
 
+import org.apache.camel.Route;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.impl.DefaultCamelContext;
@@ -90,4 +91,32 @@ public class XmlLoadTest {
             bar.assertIsSatisfied();
         }
     }
+
+    @Test
+    public void testLoadRoutesAndConfig() throws Exception {
+        try (DefaultCamelContext context = new DefaultCamelContext()) {
+            context.start();
+            // Load routeConfiguration from XML
+            Resource configResource = PluginHelper.getResourceLoader(context).resolveResource(
+                    "/org/apache/camel/dsl/xml/io/routeConfig.xml");
+
+            PluginHelper.getRoutesLoader(context).loadRoutes(configResource);
+            // load route from XML and add them to the existing camel context
+            Resource resource = PluginHelper.getResourceLoader(context).resolveResource(
+                    "/org/apache/camel/dsl/xml/io/routeWithRouteConfig.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/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