Author: davsclaus Date: Thu Oct 8 11:42:44 2009 New Revision: 823135 URL: http://svn.apache.org/viewvc?rev=823135&view=rev Log: MR-187: Added more unit tests.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanEndpointTest.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodBeanTest.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInfoTest.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyFooBean.java (with props) camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedUnregisterProcessorTest.java - copied, changed from r823018, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedUnregisterProducerTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanComponent.java camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/ParameterMappingStrategy.java camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInvocationSerializeTest.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanComponent.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanComponent.java?rev=823135&r1=823134&r2=823135&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanComponent.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanComponent.java Thu Oct 8 11:42:44 2009 @@ -21,7 +21,6 @@ import org.apache.camel.Endpoint; import org.apache.camel.Processor; import org.apache.camel.impl.DefaultComponent; -import org.apache.camel.impl.ProcessorEndpoint; /** * The <a href="http://activemq.apache.org/bean.html">Bean Component</a> @@ -30,38 +29,10 @@ * @version $Revision$ */ public class BeanComponent extends DefaultComponent { - private ParameterMappingStrategy parameterMappingStrategy; public BeanComponent() { } - /** - * A helper method to create a new endpoint from a bean with a generated URI - */ - public ProcessorEndpoint createEndpoint(Object bean) { - String uri = "bean:generated:" + bean; - return createEndpoint(bean, uri); - } - - /** - * A helper method to create a new endpoint from a bean with a given URI - */ - public ProcessorEndpoint createEndpoint(Object bean, String uri) { - BeanProcessor processor = new BeanProcessor(bean, getCamelContext(), getParameterMappingStrategy()); - return createEndpoint(uri, processor); - } - - public ParameterMappingStrategy getParameterMappingStrategy() { - if (parameterMappingStrategy == null) { - parameterMappingStrategy = createParameterMappingStrategy(); - } - return parameterMappingStrategy; - } - - public void setParameterMappingStrategy(ParameterMappingStrategy parameterMappingStrategy) { - this.parameterMappingStrategy = parameterMappingStrategy; - } - // Implementation methods //----------------------------------------------------------------------- protected Endpoint createEndpoint(String uri, String remaining, Map parameters) throws Exception { @@ -74,11 +45,4 @@ return endpoint; } - protected BeanEndpoint createEndpoint(String uri, BeanProcessor processor) { - return new BeanEndpoint(uri, this, processor); - } - - protected ParameterMappingStrategy createParameterMappingStrategy() { - return BeanInfo.createParameterMappingStrategy(getCamelContext()); - } } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java?rev=823135&r1=823134&r2=823135&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/BeanInfo.java Thu Oct 8 11:42:44 2009 @@ -283,6 +283,9 @@ hasCustomAnnotation |= expression != null; ParameterInfo parameterInfo = new ParameterInfo(i, parameterType, parameterAnnotations, expression); + if (LOG.isTraceEnabled()) { + LOG.trace("Parameter #" + i + ": " + parameterInfo); + } parameters.add(parameterInfo); if (expression == null) { boolean bodyAnnotation = ObjectHelper.hasAnnotation(parameterAnnotations, Body.class); Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/ParameterMappingStrategy.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/ParameterMappingStrategy.java?rev=823135&r1=823134&r2=823135&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/ParameterMappingStrategy.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/bean/ParameterMappingStrategy.java Thu Oct 8 11:42:44 2009 @@ -30,7 +30,7 @@ * is used as parameter value for the given type * * @param parameterType the parameter type - * @return the expression to evalute as value + * @return the expression to evaluate as value */ Expression getDefaultParameterTypeExpression(Class parameterType); } Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanEndpointTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanEndpointTest.java?rev=823135&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanEndpointTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanEndpointTest.java Thu Oct 8 11:42:44 2009 @@ -0,0 +1,146 @@ +/** + * 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.component.bean; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.JndiRegistry; + +/** + * @version $Revision$ + */ +public class BeanEndpointTest extends ContextTestSupport { + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Override + protected JndiRegistry createRegistry() throws Exception { + JndiRegistry jndi = super.createRegistry(); + jndi.bind("foo", new FooBean()); + return jndi; + } + + public void testBeanEndpointCtr() throws Exception { + final BeanEndpoint endpoint = new BeanEndpoint(); + endpoint.setCamelContext(context); + + endpoint.setBeanName("foo"); + assertEquals("foo", endpoint.getBeanName()); + + assertEquals(false, endpoint.isCache()); + assertNull(endpoint.getBeanHolder()); + assertNull(endpoint.getMethod()); + assertEquals("bean:foo", endpoint.getEndpointUri()); + + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").to(endpoint); + } + }); + context.start(); + + String out = template.requestBody("direct:start", "World", String.class); + assertEquals("Hello World", out); + } + + public void testBeanEndpointCtrWithMethod() throws Exception { + final BeanEndpoint endpoint = new BeanEndpoint(); + endpoint.setCamelContext(context); + + endpoint.setBeanName("foo"); + endpoint.setMethod("hello"); + assertEquals("foo", endpoint.getBeanName()); + + assertEquals(false, endpoint.isCache()); + assertNull(endpoint.getBeanHolder()); + assertEquals("hello", endpoint.getMethod()); + assertEquals("bean:foo?method=hello", endpoint.getEndpointUri()); + + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").to(endpoint); + } + }); + context.start(); + + String out = template.requestBody("direct:start", "World", String.class); + assertEquals("Hello World", out); + } + + public void testBeanEndpointCtrWithMethodAndCache() throws Exception { + final BeanEndpoint endpoint = new BeanEndpoint(); + endpoint.setCamelContext(context); + endpoint.setCache(true); + + endpoint.setBeanName("foo"); + endpoint.setMethod("hello"); + assertEquals("foo", endpoint.getBeanName()); + + assertEquals(true, endpoint.isCache()); + assertNull(endpoint.getBeanHolder()); + assertEquals("hello", endpoint.getMethod()); + assertEquals("bean:foo?method=hello", endpoint.getEndpointUri()); + + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").to(endpoint); + } + }); + context.start(); + + String out = template.requestBody("direct:start", "World", String.class); + assertEquals("Hello World", out); + + out = template.requestBody("direct:start", "Moon", String.class); + assertEquals("Hello Moon", out); + } + + public void testBeanEndpointCtrWithBeanHolder() throws Exception { + final BeanEndpoint endpoint = new BeanEndpoint(); + endpoint.setCamelContext(context); + + BeanHolder holder = new RegistryBean(context, "foo"); + endpoint.setBeanHolder(holder); + + assertEquals(false, endpoint.isCache()); + assertEquals(holder, endpoint.getBeanHolder()); + + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").to(endpoint); + } + }); + context.start(); + + String out = template.requestBody("direct:start", "World", String.class); + assertEquals("Hello World", out); + } + + public class FooBean { + + public String hello(String hello) { + return "Hello " + hello; + } + } +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanEndpointTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanEndpointTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInvocationSerializeTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInvocationSerializeTest.java?rev=823135&r1=823134&r2=823135&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInvocationSerializeTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanInvocationSerializeTest.java Thu Oct 8 11:42:44 2009 @@ -28,6 +28,7 @@ * @version $Revision$ */ public class BeanInvocationSerializeTest extends TestSupport { + public void testSerialize() throws Exception { Method method = getClass().getMethod("cheese", String.class, String.class); BeanInvocation invocation = new BeanInvocation(method, new Object[] {"a", "b"}); @@ -42,8 +43,24 @@ log.debug("Received " + actual); } + public void testSerializeCtr() throws Exception { + Method method = getClass().getMethod("cheese", String.class, String.class); + BeanInvocation invocation = new BeanInvocation(); + invocation.setArgs(new Object[] {"a", "b"}); + invocation.setMethod(method); + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + ObjectOutputStream out = new ObjectOutputStream(buffer); + out.writeObject(invocation); + out.close(); + + ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buffer.toByteArray())); + Object object = in.readObject(); + BeanInvocation actual = assertIsInstanceOf(BeanInvocation.class, object); + log.debug("Received " + actual); + } + public void cheese(String a, String b) { log.debug("Called with a: " + a + " b: " + b); - } + } Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodBeanTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodBeanTest.java?rev=823135&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodBeanTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodBeanTest.java Thu Oct 8 11:42:44 2009 @@ -0,0 +1,41 @@ +/** + * 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.component.bean; + +import java.lang.reflect.Method; + +import org.apache.camel.TestSupport; + +/** + * @version $Revision$ + */ +public class BeanMethodBeanTest extends TestSupport { + + public void testBeanMethod() throws Exception { + Method method = MyFooBean.class.getMethod("hello", String.class); + + MethodBean mb = new MethodBean(); + mb.setName("hello"); + mb.setType(MyFooBean.class); + mb.setParameterTypes(method.getParameterTypes()); + + assertEquals("hello", mb.getName()); + assertEquals(method, mb.getMethod()); + assertNotNull(mb.getParameterTypes()); + } + +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodBeanTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanMethodBeanTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInfoTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInfoTest.java?rev=823135&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInfoTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInfoTest.java Thu Oct 8 11:42:44 2009 @@ -0,0 +1,51 @@ +/** + * 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.component.bean; + +import org.apache.camel.Body; +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.impl.DefaultCamelContext; + +/** + * @version $Revision$ + */ +public class BeanParameterInfoTest extends ContextTestSupport { + + protected CamelContext camelContext = new DefaultCamelContext(); + + public void testMethodPatternUsingMethodAnnotations() throws Exception { + Class foo = Foo.class.getClass(); + ParameterInfo info = new ParameterInfo(1, foo.getClass(), foo.getAnnotations(), null); + + assertNotNull(info); + assertNotNull(info.toString()); + + assertEquals(1, info.getIndex()); + assertEquals(foo, info.getType()); + assertNull(info.getExpression()); + assertNotNull(info.getAnnotations()); + } + + private class Foo { + + public String hello(@Body String body) { + return "Hello " + body; + } + } + +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInfoTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/BeanParameterInfoTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyFooBean.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyFooBean.java?rev=823135&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyFooBean.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyFooBean.java Thu Oct 8 11:42:44 2009 @@ -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.component.bean; + +/** + * @version $Revision$ + */ +public class MyFooBean { + + public String hello(String s) { + return "Hello " + s; + } + +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyFooBean.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/component/bean/MyFooBean.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedUnregisterProcessorTest.java (from r823018, camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedUnregisterProducerTest.java) URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedUnregisterProcessorTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedUnregisterProcessorTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedUnregisterProducerTest.java&r1=823018&r2=823135&rev=823135&view=diff ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedUnregisterProducerTest.java (original) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/management/ManagedUnregisterProcessorTest.java Thu Oct 8 11:42:44 2009 @@ -22,16 +22,13 @@ import org.apache.camel.CamelContext; import org.apache.camel.ContextTestSupport; -import org.apache.camel.Endpoint; -import org.apache.camel.Exchange; -import org.apache.camel.Producer; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.DefaultCamelContext; /** * @version $Revision$ */ -public class ManagedUnregisterProducerTest extends ContextTestSupport { +public class ManagedUnregisterProcessorTest extends ContextTestSupport { protected CamelContext createCamelContext() throws Exception { CamelContext context = new DefaultCamelContext(); @@ -42,40 +39,20 @@ } @SuppressWarnings("unchecked") - public void testUnregisterProducer() throws Exception { - // send a message so the managed producer is started - // do this "manually" to avoid camel manageging the direct:start producer as well - // this makes the unit test easier as we only have 1 managed producer = mock:result - Endpoint endpoint = context.getEndpoint("direct:start"); - Producer producer = endpoint.createProducer(); - Exchange exchange = endpoint.createExchange(); - exchange.getIn().setBody("Hello World"); - - producer.start(); - producer.process(exchange); - producer.stop(); - + public void testUnregisterProcessor() throws Exception { MBeanServer mbeanServer = context.getManagementStrategy().getManagementAgent().getMBeanServer(); - Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=producers,*"), null); + Set<ObjectName> set = mbeanServer.queryNames(new ObjectName("*:type=processors,*"), null); assertEquals(1, set.size()); ObjectName on = set.iterator().next(); assertTrue("Should be registered", mbeanServer.isRegistered(on)); - String uri = (String) mbeanServer.getAttribute(on, "EndpointUri"); - assertEquals("mock://result", uri); - - // TODO: populating route id on producers is not implemented yet -// String routeId = (String) mbeanServer.getAttribute(on, "RouteId"); -// assertEquals("route1", routeId); - - Boolean singleton = (Boolean) mbeanServer.getAttribute(on, "Singleton"); - assertEquals(Boolean.TRUE, singleton); - - context.stop(); + String id = (String) mbeanServer.getAttribute(on, "CamelId"); + assertEquals("camel-1", id); - assertFalse("Should no longer be registered", mbeanServer.isRegistered(on)); + String routeId = (String) mbeanServer.getAttribute(on, "RouteId"); + assertEquals("route1", routeId); } @Override @@ -83,7 +60,7 @@ return new RouteBuilder() { @Override public void configure() throws Exception { - from("direct:start").to("mock:result"); + from("direct:start").setHeader("foo", constant("bar")).to("mock:result"); } }; }