Skip to content

Commit

Permalink
Add CamelContextAware support tests
Browse files Browse the repository at this point in the history
  • Loading branch information
astefanutti committed Aug 29, 2014
1 parent ce47fc7 commit f757c88
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@
final class CdiCamelBeanPostProcessor extends DefaultCamelBeanPostProcessor {

private final BeanManager manager;

private final CdiCamelExtension extension;

CdiCamelBeanPostProcessor(BeanManager manager, CdiCamelExtension extension) {
CdiCamelBeanPostProcessor(BeanManager manager) {
this.manager = manager;
this.extension = extension;
}

@Override
public CamelContext getOrLookupCamelContext() {
return extension.getCamelContext(manager);
return BeanManagerHelper.getContextualReference(manager, CamelContext.class, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class CdiCamelContext extends DefaultCamelContext {

protected CdiCamelContext() {
}

@Inject
protected CdiCamelContext(BeanManager beanManager) {
setRegistry(new CdiBeanRegistry(beanManager));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package io.astefanutti.camel.cdi;

import org.apache.camel.CamelContext;
import org.apache.camel.model.ModelCamelContext;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.AnnotatedType;
Expand All @@ -38,7 +41,7 @@ final class CdiCamelContextBean implements Bean<CdiCamelContext>, PassivationCap

CdiCamelContextBean(BeanManager manager) {
AnnotatedType<CdiCamelContext> annotatedType = manager.createAnnotatedType(CdiCamelContext.class);
this.types = annotatedType.getTypeClosure();
this.types = Collections.unmodifiableSet(new HashSet<Type>(Arrays.asList(CamelContext.class, ModelCamelContext.class)));
this.target = manager.createInjectionTarget(annotatedType);
}

Expand Down Expand Up @@ -94,7 +97,7 @@ public Set<Class<? extends Annotation>> getStereotypes() {

@Override
public Set<Type> getTypes() {
return Collections.unmodifiableSet(types);
return types;
}

@Override
Expand Down
8 changes: 2 additions & 6 deletions src/main/java/io/astefanutti/camel/cdi/CdiCamelExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ private void processCamelAnnotations(@Observes @WithAnnotations({BeanInject.clas

private <T> void camelBeanIntegrationPostProcessor(@Observes ProcessInjectionTarget<T> pit, BeanManager manager) {
if (camelBeans.contains(pit.getAnnotatedType()))
pit.setInjectionTarget(new CdiCamelInjectionTarget<>(pit.getInjectionTarget(), manager, this));
pit.setInjectionTarget(new CdiCamelInjectionTarget<>(pit.getInjectionTarget(), manager));
}

// FIXME: remove when bean manager solution with ProcessInjectionTarget decorator works
Expand All @@ -69,7 +69,7 @@ private void processCamelContextBean(@Observes ProcessBean<? extends CamelContex
}

private void addDefaultCamelContext(@Observes AfterBeanDiscovery abd, BeanManager manager) {
// FIXME: understand why this is not working anymore when ProcessInjectionTarget is decorated in injectCamelAnnotations
// FIXME: understand why this is not working anymore when ProcessInjectionTarget is decorated in processCamelAnnotations
//if (manager.getBeans(CamelContext.class, AnyLiteral.INSTANCE, DefaultLiteral.INSTANCE).isEmpty())
if (!hasCamelContext)
abd.addBean(new CdiCamelContextBean(manager));
Expand All @@ -94,8 +94,4 @@ private void configureCamelContext(@Observes AfterDeploymentValidation adv, Bean
}
}
}

CamelContext getCamelContext(BeanManager manager) {
return BeanManagerHelper.getContextualReference(manager, CamelContext.class, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@

@Vetoed
final class CdiCamelInjectionTarget<T> implements InjectionTarget<T> {

private final InjectionTarget<T> delegate;

private final CdiCamelBeanPostProcessor processor;

CdiCamelInjectionTarget(InjectionTarget<T> delegate, BeanManager manager, CdiCamelExtension extension) {
CdiCamelInjectionTarget(InjectionTarget<T> delegate, BeanManager manager) {
this.delegate = delegate;
this.processor = new CdiCamelBeanPostProcessor(manager, extension);
this.processor = new CdiCamelBeanPostProcessor(manager);
}

@Override
public void inject(T instance, CreationalContext<T> ctx) {
delegate.inject(instance, ctx);
Expand Down Expand Up @@ -74,6 +74,11 @@ public Set<InjectionPoint> getInjectionPoints() {
return delegate.getInjectionPoints();
}

@Override
public String toString() {
return delegate.toString();
}

@Override
public int hashCode() {
return delegate.hashCode();
Expand Down
62 changes: 62 additions & 0 deletions src/test/java/io/astefanutti/camel/cdi/CamelContextAwareTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* Copyright (C) 2014 Antonin Stefanutti (antonin.stefanutti@gmail.com)
*
* Licensed 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 io.astefanutti.camel.cdi;

import io.astefanutti.camel.cdi.bean.CamelContextAwareBean;
import org.apache.camel.impl.DefaultCamelContextNameStrategy;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.Filters;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;

@RunWith(Arquillian.class)
public class CamelContextAwareTest {

@Deployment
public static Archive<?> deployment() {
// hack to reset the name strategy static counter as the tests run in a single JVM
DefaultCamelContextNameStrategy.setCounter(0);

return ShrinkWrap.create(JavaArchive.class)
// Camel CDI
.addPackages(false, Filters.exclude(".*Test.*"), CdiCamelExtension.class.getPackage())
// Test class
.addClass(CamelContextAwareBean.class)
// Bean archive deployment descriptor
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}

@Inject
private CamelContextAwareBean bean;

@Test
public void camelContextAware() {
assertThat(bean.getCamelContext(), is(notNullValue()));
assertThat(bean.getCamelContext().getName(), is(equalTo("camel-1")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright (C) 2014 Antonin Stefanutti (antonin.stefanutti@gmail.com)
*
* Licensed 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 io.astefanutti.camel.cdi.bean;

import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class CamelContextAwareBean implements CamelContextAware {

private CamelContext camelContext;

@Override
public void setCamelContext(CamelContext camelContext) {
this.camelContext = camelContext;
}

@Override
public CamelContext getCamelContext() {
return camelContext;
}
}
4 changes: 2 additions & 2 deletions src/test/resources/log4j.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
<!-- Loggers -->

<logger name="org.jboss.weld">
<level value="INFO" />
<level value="DEBUG" />
</logger>

<logger name="org.apache.webbeans">
<level value="INFO" />
<level value="DEBUG" />
</logger>

<logger name="org.apache.camel">
Expand Down

0 comments on commit f757c88

Please sign in to comment.