Skip to content

Commit

Permalink
Remove the @mock qualifier and rely on the @uri only
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Aug 27, 2014
1 parent 99dd927 commit 87e0bf6
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 67 deletions.
7 changes: 0 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -313,13 +313,6 @@
<type>pom</type>
</dependency>

<dependency>
<groupId>org.jboss.weld</groupId>
<artifactId>weld-core-impl</artifactId>
<version>${weld.version}</version>
<scope>test</scope>
</dependency>

<!-- OpenWebBeans -->

<dependency>
Expand Down
38 changes: 21 additions & 17 deletions src/main/java/io/astefanutti/camel/cdi/CdiCamelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package io.astefanutti.camel.cdi;

import javax.enterprise.inject.Produces;
import javax.enterprise.inject.Typed;
import javax.enterprise.inject.spi.InjectionPoint;

import org.apache.camel.CamelContext;
Expand All @@ -32,17 +33,25 @@
class CdiCamelFactory {

@Produces
private static TypeConverter createTypeConverter(CamelContext camelContext) {
private static TypeConverter typeConverter(CamelContext camelContext) {
return camelContext.getTypeConverter();
}

@Mock
@Produces
private static MockEndpoint createMockEndpoint(InjectionPoint point, CamelContext camelContext) {
String uri = getMandatoryFirstElementOfType(point.getQualifiers(), Mock.class).value();
if (ObjectHelper.isEmpty(uri))
uri = "mock:" + point.getMember().getName();
private static MockEndpoint mockEndpointFromMember(InjectionPoint point, CamelContext camelContext) {
String uri = "mock:" + point.getMember().getName();
MockEndpoint endpoint = camelContext.getEndpoint(uri, MockEndpoint.class);
if (endpoint == null)
throw new NoSuchEndpointException(uri);

return endpoint;
}

@Uri("")
@Produces
@Typed(MockEndpoint.class)
private static MockEndpoint mockEndpointFromUri(InjectionPoint point, CamelContext camelContext) {
String uri = getFirstElementOfType(point.getQualifiers(), Uri.class).value();
MockEndpoint endpoint = camelContext.getEndpoint(uri, MockEndpoint.class);
if (endpoint == null)
throw new NoSuchEndpointException(uri);
Expand All @@ -52,31 +61,26 @@ private static MockEndpoint createMockEndpoint(InjectionPoint point, CamelContex

@Uri("")
@Produces
private static Endpoint createEndpoint(InjectionPoint point, CamelContext camelContext) {
String uri = getMandatoryFirstElementOfType(point.getQualifiers(), Uri.class).value();
private static Endpoint endpoint(InjectionPoint point, CamelContext camelContext) {
String uri = getFirstElementOfType(point.getQualifiers(), Uri.class).value();
return CamelContextHelper.getMandatoryEndpoint(camelContext, uri);
}

@Uri("")
@Produces
private static ProducerTemplate createProducerTemplate(InjectionPoint point, CamelContext camelContext) {
Uri uri = getMandatoryFirstElementOfType(point.getQualifiers(), Uri.class);
private static ProducerTemplate producerTemplate(InjectionPoint point, CamelContext camelContext) {
Uri uri = getFirstElementOfType(point.getQualifiers(), Uri.class);
ProducerTemplate producerTemplate = camelContext.createProducerTemplate();
Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, uri.value());
producerTemplate.setDefaultEndpoint(endpoint);
return producerTemplate;
}

private static <E, T extends E> T getMandatoryFirstElementOfType(Collection<E> collection, Class<T> type) {
private static <E, T extends E> T getFirstElementOfType(Collection<E> collection, Class<T> type) {
for (E item : collection)
if ((item != null) && type.isAssignableFrom(item.getClass()))
return uncheckedCast(item);
return ObjectHelper.cast(type, item);

throw new IllegalArgumentException("No element of type [" + type.getName() + "] in [" + collection + "]");
}

@SuppressWarnings("unchecked")
private static <T> T uncheckedCast(Object object) {
return (T) object;
}
}
37 changes: 0 additions & 37 deletions src/main/java/io/astefanutti/camel/cdi/Mock.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public class InjectedEndpointRouteTest {
private ProducerTemplate inbound;

@Inject
@Mock("mock:outbound")
private MockEndpoint outbound;

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package io.astefanutti.camel.cdi;

import io.astefanutti.camel.cdi.bean.SimpleRoute;
import io.astefanutti.camel.cdi.bean.UriEndpointRoute;
import org.apache.camel.CamelContext;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.mock.MockEndpoint;
Expand All @@ -36,14 +36,14 @@
import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied;

@RunWith(Arquillian.class)
public class SimpleRouteTest {
public class UriEndpointRouteTest {

@Inject
@Uri("direct:inbound")
private ProducerTemplate inbound;

@Inject
@Mock("mock:outbound")
@Uri("mock:outbound")
private MockEndpoint outbound;

@Inject
Expand All @@ -55,7 +55,7 @@ public static Archive<?> deployment() {
// Camel CDI
.addPackages(false, Filters.exclude(".*Test.*"), CdiCamelExtension.class.getPackage())
// Test class
.addClass(SimpleRoute.class)
.addClass(UriEndpointRoute.class)
// Bean archive deployment descriptor
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import org.apache.camel.builder.RouteBuilder;

public class SimpleRoute extends RouteBuilder {
public class UriEndpointRoute extends RouteBuilder {

@Override
public void configure() {
Expand Down

0 comments on commit 87e0bf6

Please sign in to comment.