Updated Branches: refs/heads/master de4cb2b98 -> 8a93ae75d
[Spring-JavaConfig] Added autowiring support to JavaConfig CamelConfiguration. Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/8a93ae75 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/8a93ae75 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/8a93ae75 Branch: refs/heads/master Commit: 8a93ae75db774a32c7822542b912026be190e610 Parents: de4cb2b Author: Henryk Konsek <[email protected]> Authored: Thu Oct 17 10:46:56 2013 +0200 Committer: Henryk Konsek <[email protected]> Committed: Thu Oct 17 10:46:56 2013 +0200 ---------------------------------------------------------------------- .../spring/javaconfig/CamelConfiguration.java | 20 +++++- .../autowire/AutodetectNoRoutesConfigTest.java | 48 ++++++++++++++ .../autowire/AutodetectingConfigTest.java | 68 ++++++++++++++++++++ .../autowire/AutowiringContextConfig.java | 38 +++++++++++ .../javaconfig/autowire/ModuleConfig.java | 40 ++++++++++++ 5 files changed, 212 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/8a93ae75/components/camel-spring-javaconfig/src/main/java/org/apache/camel/spring/javaconfig/CamelConfiguration.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-javaconfig/src/main/java/org/apache/camel/spring/javaconfig/CamelConfiguration.java b/components/camel-spring-javaconfig/src/main/java/org/apache/camel/spring/javaconfig/CamelConfiguration.java index 0b6d1e9..636a7e7 100644 --- a/components/camel-spring-javaconfig/src/main/java/org/apache/camel/spring/javaconfig/CamelConfiguration.java +++ b/components/camel-spring-javaconfig/src/main/java/org/apache/camel/spring/javaconfig/CamelConfiguration.java @@ -16,7 +16,10 @@ */ package org.apache.camel.spring.javaconfig; +import java.util.ArrayList; import java.util.List; +import java.util.Map; +import static java.util.Collections.emptyList; import org.apache.camel.CamelContext; import org.apache.camel.RoutesBuilder; @@ -154,8 +157,21 @@ public abstract class CamelConfiguration implements BeanFactoryAware, Applicatio } /** - * Returns the list of routes to use in this configuration + * Returns the list of routes to use in this configuration. By default autowires all + * {@link org.apache.camel.builder.RouteBuilder} instances available in the + * {@link org.springframework.context.ApplicationContext}. */ - public abstract List<RouteBuilder> routes(); + public List<RouteBuilder> routes() { + if (this.applicationContext != null) { + Map<String, RouteBuilder> routeBuildersMap = applicationContext.getBeansOfType(RouteBuilder.class); + List<RouteBuilder> routeBuilders = new ArrayList<RouteBuilder>(routeBuildersMap.size()); + for (RouteBuilder routeBuilder : routeBuildersMap.values()) { + routeBuilders.add(routeBuilder); + } + return routeBuilders; + } else { + return emptyList(); + } + } } http://git-wip-us.apache.org/repos/asf/camel/blob/8a93ae75/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutodetectNoRoutesConfigTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutodetectNoRoutesConfigTest.java b/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutodetectNoRoutesConfigTest.java new file mode 100644 index 0000000..419fda2 --- /dev/null +++ b/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutodetectNoRoutesConfigTest.java @@ -0,0 +1,48 @@ +/** + * 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.javaconfig.autowire; + +import static junit.framework.Assert.assertEquals; +import org.apache.camel.CamelContext; +import org.apache.camel.spring.javaconfig.CamelConfiguration; +import org.apache.camel.spring.javaconfig.test.JavaConfigContextLoader; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + +@ContextConfiguration(locations = "org.apache.camel.spring.javaconfig.autowire.NoRoutesConfig", loader = JavaConfigContextLoader.class) +public class AutodetectNoRoutesConfigTest extends AbstractJUnit4SpringContextTests { + + @Autowired + CamelContext camelContext; + + @Test + public void shouldAutodetectRouteBuilders() throws InterruptedException { + // When + int routesNumber = camelContext.getRoutes().size(); + + //Then + assertEquals(0, routesNumber); + } + +} + +@Configuration +class NoRoutesConfig extends CamelConfiguration { +} http://git-wip-us.apache.org/repos/asf/camel/blob/8a93ae75/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutodetectingConfigTest.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutodetectingConfigTest.java b/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutodetectingConfigTest.java new file mode 100644 index 0000000..ef04b95 --- /dev/null +++ b/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutodetectingConfigTest.java @@ -0,0 +1,68 @@ +/** + * 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.javaconfig.autowire; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.spring.javaconfig.test.JavaConfigContextLoader; +import org.junit.Test; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + +@ContextConfiguration(locations = "org.apache.camel.spring.javaconfig.autowire.AutowiringContextConfig", loader = JavaConfigContextLoader.class) +public class AutodetectingConfigTest extends AbstractJUnit4SpringContextTests { + + @EndpointInject(uri = "mock:autowire") + MockEndpoint autowireMockEndpoint; + + @EndpointInject(uri = "mock:module") + MockEndpoint moduleMockEndpoint; + + @Produce + ProducerTemplate producer; + + String message = "Hello!"; + + @Test + public void shouldAutodetectRouteBuilders() throws InterruptedException { + // Given + autowireMockEndpoint.expectedMessageCount(1); + autowireMockEndpoint.message(0).body().isEqualTo(message); + + // When + producer.sendBody("direct:autowire", message); + + // Then + autowireMockEndpoint.assertIsSatisfied(); + } + + @Test + public void shouldWireRouteBuildersFromModule() throws InterruptedException { + // Given + moduleMockEndpoint.expectedMessageCount(1); + moduleMockEndpoint.message(0).body().isEqualTo(message); + + // When + producer.sendBody("direct:module", message); + + // Then + moduleMockEndpoint.assertIsSatisfied(); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8a93ae75/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutowiringContextConfig.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutowiringContextConfig.java b/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutowiringContextConfig.java new file mode 100644 index 0000000..b24cfab --- /dev/null +++ b/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/AutowiringContextConfig.java @@ -0,0 +1,38 @@ +/** + * 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.javaconfig.autowire; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.spring.javaconfig.CamelConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; + +@Configuration +@ComponentScan("org.apache.camel.spring.javaconfig.autowire") +public class AutowiringContextConfig extends CamelConfiguration { +} + +@Component +class MockedRoute extends RouteBuilder { + + @Override + public void configure() throws Exception { + from("direct:autowire").to("mock:autowire"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/8a93ae75/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/ModuleConfig.java ---------------------------------------------------------------------- diff --git a/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/ModuleConfig.java b/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/ModuleConfig.java new file mode 100644 index 0000000..4078751 --- /dev/null +++ b/components/camel-spring-javaconfig/src/test/java/org/apache/camel/spring/javaconfig/autowire/ModuleConfig.java @@ -0,0 +1,40 @@ +/** + * 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.javaconfig.autowire; + +import org.apache.camel.builder.RouteBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ModuleConfig { + + @Bean + public MockedRouteLoadedByModule mockedRouteLoadedByModule() { + return new MockedRouteLoadedByModule(); + } + +} + +class MockedRouteLoadedByModule extends RouteBuilder { + + @Override + public void configure() throws Exception { + from("direct:module").to("mock:module"); + } + +}
