Author: jstrachan Date: Fri Aug 31 13:51:50 2012 New Revision: 1379449 URL: http://svn.apache.org/viewvc?rev=1379449&view=rev Log: improved CAMEL-5553 so we can also inject MockEndpoint instances; though we require @Mock as a qualifier unfortunately unless anyone figures out any neater way to differentiate Endpoint and MockEndpoint injection
Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/Mock.java (with props) Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/MockEndpointInjectTest.java camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java Added: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/Mock.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/Mock.java?rev=1379449&view=auto ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/Mock.java (added) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/Mock.java Fri Aug 31 13:51:50 2012 @@ -0,0 +1,34 @@ +/** + * + * 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.cdi; + +import javax.inject.Qualifier; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import static java.lang.annotation.ElementType.*; +import static java.lang.annotation.RetentionPolicy.*; + +/** + * A qualifier for injecting instances of {@link org.apache.camel.component.mock.MockEndpoint} into a bean. + */ +@Qualifier +@Retention(RUNTIME) +@Target({TYPE, METHOD, FIELD, PARAMETER}) +public @interface Mock { +} Propchange: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/Mock.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java?rev=1379449&r1=1379448&r2=1379449&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java (original) +++ camel/trunk/components/camel-cdi/src/main/java/org/apache/camel/component/cdi/internal/EndpointInjector.java Fri Aug 31 13:51:50 2012 @@ -20,6 +20,8 @@ package org.apache.camel.component.cdi.i import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.EndpointInject; +import org.apache.camel.component.cdi.Mock; +import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.util.ObjectHelper; import javax.enterprise.inject.Produces; @@ -35,19 +37,24 @@ public class EndpointInjector { private CamelContext camelContext; -/* - @Produces - public Endpoint createEndpoint(InjectionPoint point) { - return createEndpoint(point, Endpoint.class); - } - - // Note that there does not appear to be a way in CDI to say we can inject - // all types from Endpoint onwards so lets make it easy to also inject mock endpoints too @Produces + @Mock protected MockEndpoint createMockEndpoint(InjectionPoint point) { - return createEndpoint(point, MockEndpoint.class); + String url = ""; + String name = ""; + EndpointInject annotation = point.getAnnotated().getAnnotation(EndpointInject.class); + if (annotation != null) { + url = annotation.uri(); + name = annotation.ref(); + } + if (ObjectHelper.isEmpty(name)) { + name = point.getMember().getName(); + } + if (ObjectHelper.isEmpty(url)) { + url = "mock:" + name; + } + return camelContext.getEndpoint(url, MockEndpoint.class); } -*/ @Produces public Endpoint createEndpoint(InjectionPoint point) { @@ -56,20 +63,17 @@ public class EndpointInjector { if (pointType instanceof Class<?>) { endpointType = (Class<? extends Endpoint>) pointType; } - EndpointInject annotation = point.getAnnotated().getAnnotation(EndpointInject.class); if (annotation != null) { - if (annotation != null) { - String uri = annotation.uri(); - if (ObjectHelper.isNotEmpty(uri)) { - return camelContext.getEndpoint(uri, endpointType); - } - String ref = annotation.ref(); - if (ObjectHelper.isNotEmpty(ref)) { - return camelContext.getEndpoint("ref:" + ref, endpointType); - } + String uri = annotation.uri(); + if (ObjectHelper.isNotEmpty(uri)) { + return camelContext.getEndpoint(uri, endpointType); + } + String ref = annotation.ref(); + if (ObjectHelper.isNotEmpty(ref)) { + return camelContext.getEndpoint("ref:" + ref, endpointType); } } - return null; + throw new IllegalArgumentException("Could not create instance of Endpoint for the given injection point " + point); } } Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/MockEndpointInjectTest.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/MockEndpointInjectTest.java?rev=1379449&r1=1379448&r2=1379449&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/MockEndpointInjectTest.java (original) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/MockEndpointInjectTest.java Fri Aug 31 13:51:50 2012 @@ -18,30 +18,31 @@ package org.apache.camel.cdi; import org.apache.camel.cdi.support.EndpointInjectedBean; import org.apache.camel.cdi.support.MockEndpointInjectedBean; +import org.apache.camel.component.mock.MockEndpoint; import org.junit.Ignore; import org.junit.Test; +import javax.enterprise.inject.Produces; import javax.inject.Inject; /** - * Test endpoint injection + * Test mock endpoint injection */ public class MockEndpointInjectTest extends CdiTestSupport { - @Inject private MockEndpointInjectedBean bean; - @Ignore + @Test public void shouldInjectMockEndpoint() { assertNotNull(bean); - /* - TODO - assertNotNull("Could not find injected endpoint!", bean.getEndpoint()); - - cannot currently figure out how to be able to inject both Endpoint and MockEndpoint using a @Produces - plugin without using explicit qualifier annotations to separate the two scenarios which is a bit ugly - */ + MockEndpoint foo = bean.getFoo(); + MockEndpoint bar = bean.getBar(); + assertNotNull("Could not find injected foo endpoint!", foo); + assertNotNull("Could not find injected bar endpoint!", bar); + + assertEquals("foo URI", "mock://foo", foo.getEndpointUri()); + assertEquals("bar URI", "mock://something", bar.getEndpointUri()); } } Modified: camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java?rev=1379449&r1=1379448&r2=1379449&view=diff ============================================================================== --- camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java (original) +++ camel/trunk/components/camel-cdi/src/test/java/org/apache/camel/cdi/support/MockEndpointInjectedBean.java Fri Aug 31 13:51:50 2012 @@ -18,26 +18,42 @@ package org.apache.camel.cdi.support; import org.apache.camel.EndpointInject; +import org.apache.camel.component.cdi.Mock; import org.apache.camel.component.mock.MockEndpoint; import javax.inject.Inject; +import javax.inject.Named; public class MockEndpointInjectedBean { /* - TODO disabled - see the TODO in MockEndpointInjectTest.java + TODO - cannot currently figure out how to be able to inject both Endpoint and MockEndpoint + using a @Produces plugin with a single method without using explicit qualifier annotations + to separate the two scenarios which is a bit ugly. + + See discussion here: + https://issues.apache.org/jira/browse/CAMEL-5553 + + Ideally it would be nice to be able to do this: @Inject @EndpointInject(uri = "mock:blah") private MockEndpoint endpoint; - public MockEndpoint getEndpoint() { - return endpoint; + */ + + @Inject @Mock + private MockEndpoint foo; + + @Inject @Mock @EndpointInject(uri = "mock:something") + private MockEndpoint bar; + + public MockEndpoint getBar() { + return bar; } - public void setEndpoint(MockEndpoint endpoint) { - this.endpoint = endpoint; + public MockEndpoint getFoo() { + return foo; } - */ }