CAMEL-7016: Fixed updateRouteFromXML on route mbean to handle if the xml does 
not contain the route id, or does not match.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/ae4bb74b
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/ae4bb74b
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/ae4bb74b

Branch: refs/heads/camel-2.12.x
Commit: ae4bb74bcfe0c31b9c5c8fcfbad997f24af53651
Parents: f7ee839
Author: Claus Ibsen <davscl...@apache.org>
Authored: Wed Nov 27 09:51:56 2013 +0100
Committer: Claus Ibsen <davscl...@apache.org>
Committed: Wed Nov 27 09:52:18 2013 +0100

----------------------------------------------------------------------
 .../camel/management/mbean/ManagedRoute.java    | 10 +++
 .../ManagedRouteUpdateRouteFromXmlTest.java     | 76 ++++++++++++++++++++
 2 files changed, 86 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/ae4bb74b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java 
b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
index d6ae085..7f7be28 100644
--- 
a/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
+++ 
b/camel-core/src/main/java/org/apache/camel/management/mbean/ManagedRoute.java
@@ -228,6 +228,16 @@ public class ManagedRoute extends 
ManagedPerformanceCounter implements TimerList
             return;
         }
 
+        // if the xml does not contain the route-id then we fix this by adding 
the actual route id
+        // this may be needed if the route-id was auto-generated, as the 
intend is to update this route
+        // and not add a new route, adding a new route, use the MBean 
operation on ManagedCamelContext instead.
+        if (ObjectHelper.isEmpty(def.getId())) {
+            def.setId(getRouteId());
+        } else if (!def.getId().equals(getRouteId())) {
+            throw new IllegalArgumentException("Cannot update route from XML 
as routeIds does not match. routeId: "
+                    + getRouteId() + ", routeId from XML: " + def.getId());
+        }
+
         // add will remove existing route first
         context.addRouteDefinition(def);
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/ae4bb74b/camel-core/src/test/java/org/apache/camel/management/ManagedRouteUpdateRouteFromXmlTest.java
----------------------------------------------------------------------
diff --git 
a/camel-core/src/test/java/org/apache/camel/management/ManagedRouteUpdateRouteFromXmlTest.java
 
b/camel-core/src/test/java/org/apache/camel/management/ManagedRouteUpdateRouteFromXmlTest.java
index eed6e22..0469e86 100644
--- 
a/camel-core/src/test/java/org/apache/camel/management/ManagedRouteUpdateRouteFromXmlTest.java
+++ 
b/camel-core/src/test/java/org/apache/camel/management/ManagedRouteUpdateRouteFromXmlTest.java
@@ -57,6 +57,46 @@ public class ManagedRouteUpdateRouteFromXmlTest extends 
ManagementTestSupport {
 
         mbeanServer.invoke(on, "updateRouteFromXml", new Object[]{xml}, new 
String[]{"java.lang.String"});
 
+        assertEquals(1, context.getRoutes().size());
+
+        getMockEndpoint("mock:changed").expectedMessageCount(1);
+
+        template.sendBody("direct:start", "Bye World");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testUpdateRouteFromXmlWithoutRouteId() throws Exception {
+        // JMX tests dont work well on AIX CI servers (hangs them)
+        if (isPlatform("aix")) {
+            return;
+        }
+
+        MBeanServer mbeanServer = getMBeanServer();
+        ObjectName on = getRouteObjectName(mbeanServer);
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        // should be started
+        String routeId = (String) mbeanServer.getAttribute(on, "RouteId");
+        assertEquals("myRoute", routeId);
+
+        String xml =
+                  "<route xmlns=\"http://camel.apache.org/schema/spring\";>"
+                + "  <from uri=\"direct:start\"/>"
+                + "  <log message=\"This is a changed route saying ${body}\"/>"
+                + "  <to uri=\"mock:changed\"/>"
+                + "</route>";
+
+        mbeanServer.invoke(on, "updateRouteFromXml", new Object[]{xml}, new 
String[]{"java.lang.String"});
+
+        assertEquals(1, context.getRoutes().size());
+
         getMockEndpoint("mock:changed").expectedMessageCount(1);
 
         template.sendBody("direct:start", "Bye World");
@@ -64,6 +104,42 @@ public class ManagedRouteUpdateRouteFromXmlTest extends 
ManagementTestSupport {
         assertMockEndpointsSatisfied();
     }
 
+    public void testUpdateRouteFromXmlMismatchRouteId() throws Exception {
+        // JMX tests dont work well on AIX CI servers (hangs them)
+        if (isPlatform("aix")) {
+            return;
+        }
+
+        MBeanServer mbeanServer = getMBeanServer();
+        ObjectName on = getRouteObjectName(mbeanServer);
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+
+        template.sendBody("direct:start", "Hello World");
+
+        assertMockEndpointsSatisfied();
+
+        // should be started
+        String routeId = (String) mbeanServer.getAttribute(on, "RouteId");
+        assertEquals("myRoute", routeId);
+
+        String xml =
+                  "<route id=\"foo\" 
xmlns=\"http://camel.apache.org/schema/spring\";>"
+                + "  <from uri=\"direct:start\"/>"
+                + "  <log message=\"This is a changed route saying ${body}\"/>"
+                + "  <to uri=\"mock:changed\"/>"
+                + "</route>";
+
+        try {
+            mbeanServer.invoke(on, "updateRouteFromXml", new Object[]{xml}, 
new String[]{"java.lang.String"});
+            fail("Should have thrown exception");
+        } catch (Exception e) {
+            assertIsInstanceOf(IllegalArgumentException.class, e.getCause());
+            assertEquals("Cannot update route from XML as routeIds does not 
match. routeId: myRoute, routeId from XML: foo", e.getCause().getMessage());
+        }
+    }
+
     static ObjectName getRouteObjectName(MBeanServer mbeanServer) throws 
Exception {
         Set<ObjectName> set = mbeanServer.queryNames(new 
ObjectName("*:type=routes,*"), null);
         assertEquals(1, set.size());

Reply via email to