Updated Branches: refs/heads/camel-2.12.x ecbb9498d -> 5bf740aea
CAMEL-6693: Added unit tests. Thanks to Scott Cranton for the patch. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5bf740ae Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5bf740ae Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5bf740ae Branch: refs/heads/camel-2.12.x Commit: 5bf740aeabdee94e0e4ade6770a84a85549b7e6b Parents: ecbb949 Author: Claus Ibsen <davscl...@apache.org> Authored: Thu Sep 5 16:21:50 2013 +0200 Committer: Claus Ibsen <davscl...@apache.org> Committed: Thu Sep 5 16:22:47 2013 +0200 ---------------------------------------------------------------------- .../camel/management/ManagedResourceTest.java | 83 ++++++++++++++++++++ .../apache/camel/management/MyManagedBean.java | 44 +++++++++++ .../management/SpringManagedResourceTest.java | 28 +++++++ .../management/SpringManagedResourceTest.xml | 44 +++++++++++ 4 files changed, 199 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/5bf740ae/camel-core/src/test/java/org/apache/camel/management/ManagedResourceTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/management/ManagedResourceTest.java b/camel-core/src/test/java/org/apache/camel/management/ManagedResourceTest.java new file mode 100644 index 0000000..89116b5 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/management/ManagedResourceTest.java @@ -0,0 +1,83 @@ +/** + * 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. + */ +package org.apache.camel.management; + +import javax.management.MBeanServer; +import javax.management.ObjectName; + +import junit.framework.TestCase; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.spi.ManagementAgent; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ManagedResourceTest extends ManagementTestSupport { + private static final Logger LOG = LoggerFactory.getLogger(ManagedResourceTest.class); + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .bean(MyManagedBean.class).id("myManagedBean") + .log("${body}") + .to("mock:result"); + } + }; + } + + @Test + public void testManagedResource() throws Exception { + final ManagementAgent managementAgent = context.getManagementStrategy().getManagementAgent(); + TestCase.assertNotNull(managementAgent); + + final MBeanServer mBeanServer = managementAgent.getMBeanServer(); + TestCase.assertNotNull(mBeanServer); + + final String mBeanServerDefaultDomain = managementAgent.getMBeanServerDefaultDomain(); + TestCase.assertEquals("org.apache.camel", mBeanServerDefaultDomain); + + final String managementName = context.getManagementName(); + TestCase.assertNotNull("CamelContext should have a management name if JMX is enabled", managementName); + LOG.info("managementName = {}", managementName); + + // Get the Camel Context MBean + ObjectName onContext = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=context,name=\"" + context.getName() + "\""); + TestCase.assertTrue("Should be registered", mBeanServer.isRegistered(onContext)); + + // Get myManagedBean + ObjectName onManagedBean = ObjectName.getInstance(mBeanServerDefaultDomain + ":context=localhost/" + managementName + ",type=processors,name=\"myManagedBean\""); + LOG.info("Canonical Name = {}", onManagedBean.getCanonicalName()); + TestCase.assertTrue("Should be registered", mBeanServer.isRegistered(onManagedBean)); + + // Send a couple of messages to get some route statistics + template.sendBody("direct:start", "Hello Camel"); + template.sendBody("direct:start", "Camel Rocks!"); + + // Get MBean attribute + int camelsSeenCount = (Integer) mBeanServer.getAttribute(onManagedBean, "CamelsSeenCount"); + TestCase.assertEquals(2, camelsSeenCount); + + // Stop the route via JMX + mBeanServer.invoke(onManagedBean, "resetCamelsSeenCount", null, null); + + camelsSeenCount = (Integer) mBeanServer.getAttribute(onManagedBean, "CamelsSeenCount"); + TestCase.assertEquals(0, camelsSeenCount); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/5bf740ae/camel-core/src/test/java/org/apache/camel/management/MyManagedBean.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/management/MyManagedBean.java b/camel-core/src/test/java/org/apache/camel/management/MyManagedBean.java new file mode 100644 index 0000000..8137cf7 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/management/MyManagedBean.java @@ -0,0 +1,44 @@ +/** + * 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. + */ +package org.apache.camel.management; + +import org.apache.camel.api.management.ManagedAttribute; +import org.apache.camel.api.management.ManagedOperation; +import org.apache.camel.api.management.ManagedResource; + +@ManagedResource(description = "My Managed Bean within Camel") +public class MyManagedBean { + private int camelsSeenCount; + + public String doSomething(String body) { + if (body.contains("Camel")) { + camelsSeenCount++; + } + + return "Managed " + body; + } + + @ManagedAttribute(description = "How many Camels Have been Seen") + public int getCamelsSeenCount() { + return camelsSeenCount; + } + + @ManagedOperation(description = "Set Camels Seen Count to Zero") + public void resetCamelsSeenCount() { + camelsSeenCount = 0; + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/5bf740ae/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringManagedResourceTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringManagedResourceTest.java b/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringManagedResourceTest.java new file mode 100644 index 0000000..57b7f14 --- /dev/null +++ b/components/camel-spring/src/test/java/org/apache/camel/spring/management/SpringManagedResourceTest.java @@ -0,0 +1,28 @@ +/** + * 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. + */ +package org.apache.camel.spring.management; + +import org.apache.camel.CamelContext; +import org.apache.camel.management.ManagedResourceTest; + +import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext; + +public class SpringManagedResourceTest extends ManagedResourceTest { + protected CamelContext createCamelContext() throws Exception { + return createSpringCamelContext(this, "org/apache/camel/spring/management/SpringManagedResourceTest.xml"); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/5bf740ae/components/camel-spring/src/test/resources/org/apache/camel/spring/management/SpringManagedResourceTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/test/resources/org/apache/camel/spring/management/SpringManagedResourceTest.xml b/components/camel-spring/src/test/resources/org/apache/camel/spring/management/SpringManagedResourceTest.xml new file mode 100644 index 0000000..3b4ca52 --- /dev/null +++ b/components/camel-spring/src/test/resources/org/apache/camel/spring/management/SpringManagedResourceTest.xml @@ -0,0 +1,44 @@ +<?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. +--> +<beans xmlns="http://www.springframework.org/schema/beans" + xmlns:camel="http://camel.apache.org/schema/spring" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd + http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd + "> + + <bean id="naming" class="org.apache.camel.management.DefaultManagementNamingStrategy"> + <property name="hostName" value="localhost"/> + <property name="domainName" value="org.apache.camel"/> + </bean> + + <bean id="myBean" class="org.apache.camel.management.MyManagedBean"/> + + <camelContext xmlns="http://camel.apache.org/schema/spring"> + <jmxAgent id="agent"/> + + <route> + <from uri="direct:start"/> + <bean id="myManagedBean" ref="myBean"/> + <log message="${body}"/> + <to uri="mock:result"/> + </route> + </camelContext> + +</beans>