Author: davsclaus Date: Fri Jan 28 15:40:17 2011 New Revision: 1064736 URL: http://svn.apache.org/viewvc?rev=1064736&view=rev Log: CAMEL-3578: Any endpoints can now easily be mocked during test. CAMEL-2630: Mock endpoint now has DSL for arrived to set expectations about timespan between messages arrived on the mock.
Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.java camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.xml - copied, changed from r1062109, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/spring.xml camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/camel-route.xml Modified: camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestEndpoint.java Modified: camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java?rev=1064736&r1=1064735&r2=1064736&view=diff ============================================================================== --- camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java (original) +++ camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java Fri Jan 28 15:40:17 2011 @@ -65,6 +65,7 @@ import org.apache.camel.processor.interc import org.apache.camel.processor.interceptor.Tracer; import org.apache.camel.spi.ClassResolver; import org.apache.camel.spi.Debugger; +import org.apache.camel.spi.EndpointStrategy; import org.apache.camel.spi.EventFactory; import org.apache.camel.spi.EventNotifier; import org.apache.camel.spi.ExecutorServiceStrategy; @@ -234,6 +235,16 @@ public abstract class AbstractCamelConte } } } + // set endpoint strategies if defined + Map<String, EndpointStrategy> endpointStrategies = getContext().getRegistry().lookupByType(EndpointStrategy.class); + if (endpointStrategies != null && !endpointStrategies.isEmpty()) { + for (String id : endpointStrategies.keySet()) { + EndpointStrategy strategy = endpointStrategies.get(id); + LOG.info("Using custom EndpointStrategy with id: " + id + " and implementation: " + strategy); + getContext().addRegisterEndpointCallback(strategy); + } + } + // shutdown ShutdownStrategy shutdownStrategy = getBeanForType(ShutdownStrategy.class); if (shutdownStrategy != null) { LOG.info("Using custom ShutdownStrategy: " + shutdownStrategy); Modified: camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestEndpoint.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestEndpoint.java?rev=1064736&r1=1064735&r2=1064736&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestEndpoint.java (original) +++ camel/trunk/components/camel-spring/src/main/java/org/apache/camel/component/test/TestEndpoint.java Fri Jan 28 15:40:17 2011 @@ -23,7 +23,6 @@ import org.apache.camel.Component; import org.apache.camel.Endpoint; import org.apache.camel.Exchange; import org.apache.camel.Processor; -import org.apache.camel.Service; import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.util.EndpointHelper; import org.apache.commons.logging.Log; @@ -36,7 +35,7 @@ import org.apache.commons.logging.LogFac * * @version $Revision$ */ -public class TestEndpoint extends MockEndpoint implements Service { +public class TestEndpoint extends MockEndpoint { private static final transient Log LOG = LogFactory.getLog(TestEndpoint.class); private final Endpoint expectedMessageEndpoint; private long timeout = 2000L; @@ -46,12 +45,12 @@ public class TestEndpoint extends MockEn this.expectedMessageEndpoint = expectedMessageEndpoint; } - @SuppressWarnings("unchecked") - public void start() throws Exception { + @Override + protected void doStart() throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("Consuming expected messages from: " + expectedMessageEndpoint); } - final List expectedBodies = new ArrayList(); + final List<Object> expectedBodies = new ArrayList<Object>(); EndpointHelper.pollEndpoint(expectedMessageEndpoint, new Processor() { public void process(Exchange exchange) throws Exception { Object body = getInBody(exchange); @@ -65,9 +64,6 @@ public class TestEndpoint extends MockEn expectedBodiesReceived(expectedBodies); } - public void stop() throws Exception { - } - /** * This method allows us to convert or coerce the expected message body into some other type */ Added: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.java?rev=1064736&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.java (added) +++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.java Fri Jan 28 15:40:17 2011 @@ -0,0 +1,54 @@ +/** + * 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.mock; + +import org.apache.camel.spring.SpringTestSupport; +import org.springframework.context.support.AbstractXmlApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * @version $Revision$ + */ +public class InterceptSendToMockEndpointStrategyTest extends SpringTestSupport { + + @Override + protected AbstractXmlApplicationContext createApplicationContext() { + return new ClassPathXmlApplicationContext("org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.xml"); + } + + public void testAdvisedMockEndpoints() throws Exception { + getMockEndpoint("mock:direct:start").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:direct:foo").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:log:foo").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:result").expectedBodiesReceived("Bye World"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + + // additional test to ensure correct endpoints in registry + assertNotNull(context.hasEndpoint("direct:start")); + assertNotNull(context.hasEndpoint("direct:foo")); + assertNotNull(context.hasEndpoint("log:foo")); + assertNotNull(context.hasEndpoint("mock:result")); + // all the endpoints was mocked + assertNotNull(context.hasEndpoint("mock:direct:start")); + assertNotNull(context.hasEndpoint("mock:direct:foo")); + assertNotNull(context.hasEndpoint("mock:log:foo")); + } + +} Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.xml (from r1062109, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/spring.xml) URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/spring.xml&r1=1062109&r2=1064736&rev=1064736&view=diff ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/spring.xml (original) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/InterceptSendToMockEndpointStrategyTest.xml Fri Jan 28 15:40:17 2011 @@ -18,23 +18,14 @@ <beans xmlns="http://www.springframework.org/schema/beans" 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 - "> + http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> - <!-- START SNIPPET: example --> - <camelContext xmlns="http://camel.apache.org/schema/spring"> - <route> - <from uri="file:src/test/data?noop=true"/> - <filter> - <xpath>/person/city = 'London'</xpath> - <to uri="mock:matched"/> - </filter> - </route> - </camelContext> - - <bean id="myBean" class="org.apache.camel.spring.mock.MyAssertions" scope="singleton"/> - <!-- END SNIPPET: example --> + <!-- START SNIPPET: e1 --> + <!-- the Camel route is defined in another XML file --> + <import resource="camel-route.xml"/> + <!-- bean which enables mocking all endpoints --> + <bean id="mockAllEndpoints" class="org.apache.camel.impl.InterceptSendToMockEndpointStrategy"/> + <!-- END SNIPPET: e1 --> </beans> Added: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/camel-route.xml URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/camel-route.xml?rev=1064736&view=auto ============================================================================== --- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/camel-route.xml (added) +++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/mock/camel-route.xml Fri Jan 28 15:40:17 2011 @@ -0,0 +1,46 @@ +<?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: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 + "> + + <!-- START SNIPPET: e1 --> + <!-- this camel route is in the camel-route.xml file --> + <camelContext xmlns="http://camel.apache.org/schema/spring"> + + <route> + <from uri="direct:start"/> + <to uri="direct:foo"/> + <to uri="log:foo"/> + <to uri="mock:result"/> + </route> + + <route> + <from uri="direct:foo"/> + <transform> + <constant>Bye World</constant> + </transform> + </route> + + </camelContext> + <!-- END SNIPPET: e1 --> + +</beans>