Updated Branches: refs/heads/camel-2.11.x c489449af -> 4af0d3ee3
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/4af0d3ee Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/4af0d3ee Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/4af0d3ee Branch: refs/heads/camel-2.11.x Commit: 4af0d3ee3b85b351068167961084e9b1193b7255 Parents: c489449 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:53:12 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/4af0d3ee/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 3c612d1..1da2af5 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 @@ -220,6 +220,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/4af0d3ee/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 d119687..3eb49df 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 @@ -52,6 +52,8 @@ 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"); @@ -59,6 +61,80 @@ public class ManagedRouteUpdateRouteFromXmlTest extends ManagementTestSupport { 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"); + + 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());