http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/AdvisedRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/AdvisedRouteTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/AdvisedRouteTest.java new file mode 100644 index 0000000..61c33bb --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/AdvisedRouteTest.java @@ -0,0 +1,105 @@ +/** + * 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.cdi.test; + +import java.util.Properties; +import java.util.concurrent.TimeUnit; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.inject.Named; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.AdviceWithRouteBuilder; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.ManualStartupCamelContext; +import org.apache.camel.cdi.bean.PropertyEndpointRoute; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.component.properties.PropertiesComponent; +import org.apache.camel.model.ModelCamelContext; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.junit.InSequence; +import org.jboss.shrinkwrap.api.Archive; +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 static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; + +@RunWith(Arquillian.class) +public class AdvisedRouteTest { + + @Inject + @Uri("direct:inbound") + private ProducerTemplate inbound; + + @Inject + @Uri("mock:outbound") + private MockEndpoint outbound; + + @Produces + @ApplicationScoped + @Named("properties") + private static PropertiesComponent configuration() { + Properties properties = new Properties(); + properties.put("from", "inbound"); + properties.put("to", "direct:outbound"); + properties.put("header.message", "n/a"); + PropertiesComponent component = new PropertiesComponent(); + component.setInitialProperties(properties); + return component; + } + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test classes + .addClasses(ManualStartupCamelContext.class, PropertyEndpointRoute.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + @InSequence(1) + public void adviseCamelContext(ModelCamelContext context) throws Exception { + context.getRouteDefinition("route").adviceWith(context, new AdviceWithRouteBuilder() { + @Override + public void configure() { + interceptSendToEndpoint("{{to}}").skipSendToOriginalEndpoint().to("mock:outbound"); + } + }); + context.startAllRoutes(); + } + + @Test + @InSequence(2) + public void sendMessageToInbound() throws InterruptedException { + outbound.expectedMessageCount(1); + outbound.expectedBodiesReceived("test"); + outbound.expectedHeaderReceived("header", "n/a"); + + inbound.sendBody("test"); + + assertIsSatisfied(2L, TimeUnit.SECONDS, outbound); + } +}
http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/AmbiguousCamelContextTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/AmbiguousCamelContextTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/AmbiguousCamelContextTest.java new file mode 100644 index 0000000..808c363 --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/AmbiguousCamelContextTest.java @@ -0,0 +1,55 @@ +/** + * 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.cdi.test; + +import org.apache.camel.ServiceStatus; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.bean.CustomPropertiesCamelContext; +import org.apache.camel.cdi.bean.ManualStartupCamelContext; +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.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 static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class AmbiguousCamelContextTest { + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test classes + .addClasses(ManualStartupCamelContext.class, CustomPropertiesCamelContext.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void test(ManualStartupCamelContext lifecycleCamelContext, CustomPropertiesCamelContext propertiesCamelContext) { + assertThat(lifecycleCamelContext.getStatus(), is(equalTo(ServiceStatus.Started))); + assertThat(propertiesCamelContext.getStatus(), is(equalTo(ServiceStatus.Started))); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/BeanInjectTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/BeanInjectTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/BeanInjectTest.java new file mode 100644 index 0000000..f5447ea --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/BeanInjectTest.java @@ -0,0 +1,88 @@ +/** + * 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.cdi.test; + +import java.util.Properties; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.inject.Named; + +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.bean.BeanInjectBean; +import org.apache.camel.cdi.bean.NamedCamelBean; +import org.apache.camel.cdi.bean.PropertyInjectBean; +import org.apache.camel.component.properties.PropertiesComponent; +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.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 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 BeanInjectTest { + + @Inject + private BeanInjectBean bean; + + @Produces + @ApplicationScoped + @Named("properties") + private static PropertiesComponent configuration() { + Properties properties = new Properties(); + properties.put("property", "value"); + PropertiesComponent component = new PropertiesComponent(); + component.setInitialProperties(properties); + return component; + } + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test classes + .addClasses(BeanInjectBean.class, PropertyInjectBean.class, NamedCamelBean.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void beanInjectField() { + assertThat(bean.getInjectBeanField(), is(notNullValue())); + assertThat(bean.getInjectBeanField().getProperty(), is(equalTo("value"))); + } + + @Test + public void beanInjectMethod() { + assertThat(bean.getInjectBeanMethod(), is(notNullValue())); + assertThat(bean.getInjectBeanMethod().getProperty(), is(equalTo("value"))); + } + + @Test + public void beanInjectNamed() { + assertThat(bean.getInjectBeanNamed(), is(notNullValue())); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextAwareTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextAwareTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextAwareTest.java new file mode 100644 index 0000000..5b432ec --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextAwareTest.java @@ -0,0 +1,57 @@ +/** + * 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.cdi.test; + +import javax.inject.Inject; + +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.bean.CamelContextAwareBean; +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.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 static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class CamelContextAwareTest { + + @Inject + private CamelContextAwareBean bean; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test class + .addClass(CamelContextAwareBean.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void camelContextAware() { + assertThat(bean.getCamelContext(), is(notNullValue())); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextProducerFieldTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextProducerFieldTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextProducerFieldTest.java new file mode 100644 index 0000000..656ad5a --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextProducerFieldTest.java @@ -0,0 +1,97 @@ +/** + * 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.cdi.test; + +import java.util.concurrent.TimeUnit; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.inject.Produces; +import javax.inject.Inject; +import javax.inject.Named; + +import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.ServiceStatus; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.NamedCamelBean; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.impl.DefaultCamelContext; +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.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 static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class CamelContextProducerFieldTest { + + @Named + @Produces + @ApplicationScoped + private static CamelContext context = new DefaultCamelContext(); + + @Inject + @Uri("direct:inbound") + private ProducerTemplate inbound; + + @Inject + @Uri("mock:outbound") + private MockEndpoint outbound; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test class + .addClass(NamedCamelBean.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void verifyProducedCamelContext(CamelContext context) { + assertThat("The producer field sets the context name!", context.getName(), is(equalTo("context"))); + assertThat("The producer field starts the Camel context!", context.getStatus(), is(equalTo(ServiceStatus.Started))); + } + + @Test + public void sendMessageToInbound() throws InterruptedException { + outbound.expectedMessageCount(1); + outbound.expectedBodiesReceived("test-processed"); + + inbound.sendBody("test"); + + assertIsSatisfied(2L, TimeUnit.SECONDS, outbound); + } + + private static class NamedBeanRoute extends RouteBuilder { + @Override + public void configure() { + from("direct:inbound").bean("beanName").to("mock:outbound"); + } + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextProducerMethodTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextProducerMethodTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextProducerMethodTest.java new file mode 100644 index 0000000..24e9319 --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelContextProducerMethodTest.java @@ -0,0 +1,89 @@ +/** + * 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.cdi.test; + +import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + +import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.ServiceStatus; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.CamelContextProducerMethod; +import org.apache.camel.cdi.bean.NamedCamelBean; +import org.apache.camel.component.mock.MockEndpoint; +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.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 static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class CamelContextProducerMethodTest { + + @Inject + @Uri("direct:inbound") + private ProducerTemplate inbound; + + @Inject + @Uri("mock:outbound") + private MockEndpoint outbound; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test classes + .addClasses(CamelContextProducerMethod.class, NamedCamelBean.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void verifyProducedCamelContext(CamelContext context) { + assertThat("The producer method sets the context name!", context.getName(), is(equalTo("camel-producer-method"))); + assertThat("The producer method starts the Camel context!", context.getStatus(), is(equalTo(ServiceStatus.Started))); + } + + @Test + public void sendMessageToInbound() throws InterruptedException { + outbound.expectedMessageCount(1); + outbound.expectedBodiesReceived("test-processed"); + + inbound.sendBody("test"); + + assertIsSatisfied(2L, TimeUnit.SECONDS, outbound); + } + + private static class NamedBeanRoute extends RouteBuilder { + @Override + public void configure() { + from("direct:inbound").bean("beanName").to("mock:outbound"); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventEndpointTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventEndpointTest.java new file mode 100644 index 0000000..f298546 --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventEndpointTest.java @@ -0,0 +1,143 @@ +/** + * 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.cdi.test; + +import java.util.EventObject; +import javax.inject.Inject; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.CdiEventEndpoint; +import org.apache.camel.cdi.Uri; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.management.event.CamelContextStartedEvent; +import org.apache.camel.management.event.ExchangeCompletedEvent; +import org.apache.camel.management.event.ExchangeCreatedEvent; +import org.apache.camel.management.event.ExchangeSendingEvent; +import org.apache.camel.management.event.ExchangeSentEvent; +import org.apache.camel.management.event.RouteStartedEvent; +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.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 static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.either; +import static org.hamcrest.Matchers.hasProperty; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class CamelEventEndpointTest { + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test class + .addClass(CamelEventRoute.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void camelStartedEvent(@Uri("mock:started") MockEndpoint started) { + assertThat("Event fired is incorrect!", started.getExchanges(), + contains( + hasProperty("in", + hasProperty("body", instanceOf(CamelContextStartedEvent.class))))); + } + + @Test + public void camelAllEvents(@Uri("mock:events") MockEndpoint events) { + assertThat("Events fired are incorrect!", events.getExchanges(), + // We cannot rely on the delivery order of the camel context started event being fired and observed by both CDI event endpoints + either( + contains( + // Started route: route1 + hasProperty("in", hasProperty("body", instanceOf(ExchangeCreatedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSendingEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(RouteStartedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSentEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeCompletedEvent.class))), + // Started route: route2 + hasProperty("in", hasProperty("body", instanceOf(ExchangeCreatedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSendingEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(RouteStartedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSentEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeCompletedEvent.class))), + // Started CamelContext: camel-cdi + hasProperty("in", hasProperty("body", instanceOf(ExchangeCreatedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSendingEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(CamelContextStartedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSentEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeCompletedEvent.class))), + // Started CamelContext: camel-cdi (for CdiEventEndpoint<CamelContextStartedEvent> started) + hasProperty("in", hasProperty("body", instanceOf(ExchangeCreatedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSendingEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSentEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeCompletedEvent.class))) + )).or( + contains( + // Started route: route1 + hasProperty("in", hasProperty("body", instanceOf(ExchangeCreatedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSendingEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(RouteStartedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSentEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeCompletedEvent.class))), + // Started route: route2 + hasProperty("in", hasProperty("body", instanceOf(ExchangeCreatedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSendingEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(RouteStartedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSentEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeCompletedEvent.class))), + // Started CamelContext: camel-cdi (for CdiEventEndpoint<CamelContextStartedEvent> started) + hasProperty("in", hasProperty("body", instanceOf(ExchangeCreatedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSendingEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSentEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeCompletedEvent.class))), + // Started CamelContext: camel-cdi + hasProperty("in", hasProperty("body", instanceOf(ExchangeCreatedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSendingEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(CamelContextStartedEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeSentEvent.class))), + hasProperty("in", hasProperty("body", instanceOf(ExchangeCompletedEvent.class))) + ) + ) + ); + } +} + +class CamelEventRoute extends RouteBuilder { + + @Inject + private CdiEventEndpoint<CamelContextStartedEvent> started; + + @Inject + private CdiEventEndpoint<EventObject> events; + + @Override + public void configure() { + from(events).startupOrder(1).to("mock:events"); + from(started).startupOrder(2).to("mock:started"); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventNotifierTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventNotifierTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventNotifierTest.java new file mode 100644 index 0000000..3a07d9f --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CamelEventNotifierTest.java @@ -0,0 +1,151 @@ +/** + * 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.cdi.test; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Observes; +import javax.enterprise.inject.Produces; +import javax.inject.Inject; + +import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.SimpleCamelRoute; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.management.event.AbstractExchangeEvent; +import org.apache.camel.management.event.CamelContextStartedEvent; +import org.apache.camel.management.event.CamelContextStartingEvent; +import org.apache.camel.management.event.CamelContextStoppedEvent; +import org.apache.camel.management.event.CamelContextStoppingEvent; +import org.apache.camel.management.event.ExchangeCompletedEvent; +import org.apache.camel.management.event.ExchangeCreatedEvent; +import org.apache.camel.management.event.ExchangeSendingEvent; +import org.apache.camel.management.event.ExchangeSentEvent; +import org.hamcrest.Matchers; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.junit.InSequence; +import org.jboss.shrinkwrap.api.Archive; +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 static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class CamelEventNotifierTest { + + @Inject + @Uri("direct:start") + private ProducerTemplate inbound; + + @Inject + @Uri("mock:result") + private MockEndpoint outbound; + + @Produces + @ApplicationScoped + private List<Class> firedEvents = new ArrayList<>(); + + private void onCamelContextStartingEvent(@Observes CamelContextStartingEvent event, List<Class> events) { + events.add(CamelContextStartingEvent.class); + } + + private void onCamelContextStartedEvent(@Observes CamelContextStartedEvent event, List<Class> events) { + events.add(CamelContextStartedEvent.class); + } + + private void onExchangeEvent(@Observes AbstractExchangeEvent event, List<Class> events) { + events.add(event.getClass()); + } + + private void onCamelContextStoppingEvent(@Observes CamelContextStoppingEvent event, List<Class> events) { + events.add(CamelContextStoppingEvent.class); + } + + private void onCamelContextStoppedEvent(@Observes CamelContextStoppedEvent event, List<Class> events) { + events.add(CamelContextStoppedEvent.class); + } + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test class + .addClass(SimpleCamelRoute.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + @InSequence(1) + public void startedCamelContext(List<Class> events) throws Exception { + assertThat("Events fired are incorrect", events, + Matchers.<Class>contains( + CamelContextStartingEvent.class, + CamelContextStartedEvent.class)); + } + + @Test + @InSequence(2) + public void sendMessageToInbound(List<Class> events) throws InterruptedException { + outbound.expectedMessageCount(1); + outbound.expectedBodiesReceived("test"); + + inbound.sendBody("test"); + + assertIsSatisfied(2L, TimeUnit.SECONDS, outbound); + + assertThat("Events fired are incorrect", events, + Matchers.<Class>contains( + CamelContextStartingEvent.class, + CamelContextStartedEvent.class, + ExchangeSendingEvent.class, + ExchangeCreatedEvent.class, + ExchangeSendingEvent.class, + ExchangeSentEvent.class, + ExchangeCompletedEvent.class, + ExchangeSentEvent.class)); + } + + @Test + @InSequence(3) + public void stopCamelContext(CamelContext context, List<Class> events) throws Exception { + context.stop(); + + assertThat("Events fired are incorrect", events, + Matchers.<Class>contains( + CamelContextStartingEvent.class, + CamelContextStartedEvent.class, + ExchangeSendingEvent.class, + ExchangeCreatedEvent.class, + ExchangeSendingEvent.class, + ExchangeSentEvent.class, + ExchangeCompletedEvent.class, + ExchangeSentEvent.class, + CamelContextStoppingEvent.class, + CamelContextStoppedEvent.class)); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ConsumeMethodTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ConsumeMethodTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ConsumeMethodTest.java new file mode 100644 index 0000000..3f112c9 --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ConsumeMethodTest.java @@ -0,0 +1,69 @@ +/** + * 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.cdi.test; + +import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.ConsumeMethodBean; +import org.apache.camel.component.mock.MockEndpoint; +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.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 static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; + +@RunWith(Arquillian.class) +public class ConsumeMethodTest { + + @Inject + @Uri("seda:inbound") + private ProducerTemplate inbound; + + @Inject + @Uri("mock:outbound") + private MockEndpoint outbound; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test class + .addClass(ConsumeMethodBean.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void consumeAnnotation() throws InterruptedException { + outbound.expectedMessageCount(1); + outbound.expectedBodiesReceived("test"); + + inbound.sendBody("test"); + + assertIsSatisfied(2L, TimeUnit.SECONDS, outbound); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ContextComponentTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ContextComponentTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ContextComponentTest.java new file mode 100644 index 0000000..fe1a80e --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/ContextComponentTest.java @@ -0,0 +1,92 @@ +/** + * 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.cdi.test; + +import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + +import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.DefaultCamelContextBean; +import org.apache.camel.cdi.bean.FirstNamedCamelContextBean; +import org.apache.camel.cdi.bean.FirstNamedCamelContextRoute; +import org.apache.camel.cdi.bean.SecondNamedCamelContextBean; +import org.apache.camel.cdi.bean.SecondNamedCamelContextRoute; +import org.apache.camel.component.mock.MockEndpoint; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.junit.InSequence; +import org.jboss.shrinkwrap.api.Archive; +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; + +@RunWith(Arquillian.class) +public class ContextComponentTest { + + @Inject + private CamelContext main; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test classes + .addClasses( + DefaultCamelContextBean.class, + FirstNamedCamelContextBean.class, + FirstNamedCamelContextRoute.class, + SecondNamedCamelContextBean.class, + SecondNamedCamelContextRoute.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + @InSequence(1) + public void addRouteToMainContext() throws Exception { + main.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:inbound").to("first:in"); + // FIXME: The context component does not support multiple logical endpoints + // with the same remaining defined in two distinct Camel contexts. + // See https://issues.apache.org/jira/browse/CAMEL-9200. + // from("first:out").to("second:in"); + from("first:out").to("mock:outbound"); + } + }); + } + + @Test + @InSequence(2) + public void sendMessageToInbound(@Uri("direct:inbound") ProducerTemplate inbound, + @Uri("mock:outbound") MockEndpoint outbound) throws InterruptedException { + outbound.expectedMessageCount(1); + outbound.expectedBodiesReceived("first-test"); + + inbound.sendBody("test"); + + MockEndpoint.assertIsSatisfied(1L, TimeUnit.SECONDS, outbound); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CustomCamelContextTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CustomCamelContextTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CustomCamelContextTest.java new file mode 100644 index 0000000..cf46c74 --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/CustomCamelContextTest.java @@ -0,0 +1,90 @@ +/** + * 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.cdi.test; + +import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + +import org.apache.camel.CamelContext; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.ServiceStatus; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.ManualStartupCamelContext; +import org.apache.camel.cdi.bean.UriEndpointRoute; +import org.apache.camel.component.mock.MockEndpoint; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.junit.InSequence; +import org.jboss.shrinkwrap.api.Archive; +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 static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class CustomCamelContextTest { + + @Inject + @Uri("direct:inbound") + private ProducerTemplate inbound; + + @Inject + @Uri("mock:outbound") + private MockEndpoint outbound; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test classes + .addClasses(ManualStartupCamelContext.class, UriEndpointRoute.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + @InSequence(1) + public void verifyCamelContext(CamelContext context) { + assertThat(context.getName(), is(equalTo("manual-startup"))); + + assertThat(inbound.getCamelContext().getName(), is(equalTo(context.getName()))); + assertThat(outbound.getCamelContext().getName(), is(equalTo(context.getName()))); + + assertThat(context.getRouteStatus("uri-route"), is(equalTo(ServiceStatus.Stopped))); + } + + @Test + @InSequence(2) + public void sendMessageToInbound(CamelContext context) throws Exception { + context.startAllRoutes(); + + outbound.expectedMessageCount(1); + outbound.expectedBodiesReceived("test"); + + inbound.sendBody("test"); + + assertIsSatisfied(2L, TimeUnit.SECONDS, outbound); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/DefaultCamelContextTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/DefaultCamelContextTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/DefaultCamelContextTest.java new file mode 100644 index 0000000..761ea8e --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/DefaultCamelContextTest.java @@ -0,0 +1,69 @@ +/** + * 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.cdi.test; + +import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.SimpleCamelRoute; +import org.apache.camel.component.mock.MockEndpoint; +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.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 static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; + +@RunWith(Arquillian.class) +public class DefaultCamelContextTest { + + @Inject + @Uri("direct:start") + private ProducerTemplate inbound; + + @Inject + @Uri("mock:result") + private MockEndpoint outbound; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test class + .addClass(SimpleCamelRoute.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void sendMessageToInbound() throws InterruptedException { + outbound.expectedMessageCount(1); + outbound.expectedBodiesReceived("test"); + + inbound.sendBody("test"); + + assertIsSatisfied(2L, TimeUnit.SECONDS, outbound); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EndpointInjectTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EndpointInjectTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EndpointInjectTest.java new file mode 100644 index 0000000..a49dca4 --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EndpointInjectTest.java @@ -0,0 +1,69 @@ +/** + * 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.cdi.test; + +import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.EndpointInjectRoute; +import org.apache.camel.component.mock.MockEndpoint; +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.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 static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; + +@RunWith(Arquillian.class) +public class EndpointInjectTest { + + @Inject + @Uri("direct:inbound") + private ProducerTemplate inbound; + + @Inject + @Uri("mock:outbound") + private MockEndpoint outbound; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test class + .addClass(EndpointInjectRoute.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void sendMessageToInbound() throws InterruptedException { + outbound.expectedMessageCount(1); + outbound.expectedBodiesReceived("test"); + + inbound.sendBody("test"); + + assertIsSatisfied(2L, TimeUnit.SECONDS, outbound); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventComponentTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventComponentTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventComponentTest.java new file mode 100644 index 0000000..424a7a8 --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventComponentTest.java @@ -0,0 +1,91 @@ +/** + * 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.cdi.test; + +import javax.enterprise.context.ApplicationScoped; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.impl.DefaultCamelContext; +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.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 static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +@RunWith(Arquillian.class) +public class EventComponentTest { + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + // We should ideally use an ExpectedException JUnit rule to assert the content of the exception + // thrown at deployment time. Unfortunately, OpenWebBeans does not enable access to the underlying + // cause added as deployment exception. To work-around that, we delay the start of the Camel context + // at runtime. + + @Test + public void createEventEndpointByUri(NotStartedCamelContext context) { + try { + context.start(true); + } catch (Exception exception) { + Throwable cause = exception.getCause().getCause(); + assertThat("Exception cause is not an UnsupportedOperationException!", cause, is(instanceOf(UnsupportedOperationException.class))); + assertThat("Incorrect exception message!", cause.getMessage(), is(equalTo("Creating CDI event endpoint isn't supported. Use @Inject CdiEventEndpoint instead"))); + return; + } + fail("CDI event endpoint creation by URI should throw an exception!"); + } + + static class CdiEventComponentRoute extends RouteBuilder { + + @Override + public void configure() { + from("cdi-event://Object").log("Unsupported operation!"); + } + } + + @ApplicationScoped + static class NotStartedCamelContext extends DefaultCamelContext { + + @Override + public void start() throws Exception { + start(false); + } + + void start(boolean start) throws Exception { + if (start) { + super.start(); + } + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventEndpointCdi12Test.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventEndpointCdi12Test.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventEndpointCdi12Test.java new file mode 100644 index 0000000..bf78aa3 --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventEndpointCdi12Test.java @@ -0,0 +1,277 @@ +/** + * 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.cdi.test; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Event; +import javax.enterprise.event.Observes; +import javax.enterprise.inject.Default; +import javax.enterprise.util.TypeLiteral; +import javax.inject.Inject; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.EventConsumingRoute; +import org.apache.camel.cdi.bean.EventProducingRoute; +import org.apache.camel.cdi.pojo.EventPayload; +import org.apache.camel.cdi.qualifier.BarQualifier; +import org.apache.camel.cdi.qualifier.FooQualifier; +import org.apache.camel.component.mock.MockEndpoint; +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.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class EventEndpointCdi12Test { + + @Inject + @Uri("mock:consumeObject") + private MockEndpoint consumeObject; + + @Inject + @Uri("mock:consumeString") + private MockEndpoint consumeString; + + @Inject + @Uri("mock:consumeStringPayload") + private MockEndpoint consumeStringPayload; + + @Inject + @Uri("mock:consumeIntegerPayload") + private MockEndpoint consumeIntegerPayload; + + @Inject + @Uri("mock:consumeFooQualifier") + private MockEndpoint consumeFooQualifier; + + @Inject + @Uri("mock:consumeBarQualifier") + private MockEndpoint consumeBarQualifier; + + @Inject + @Uri("direct:produceObject") + private ProducerTemplate produceObject; + + @Inject + @Uri("direct:produceString") + private ProducerTemplate produceString; + + @Inject + @Uri("direct:produceStringPayload") + private ProducerTemplate produceStringPayload; + + @Inject + @Uri("direct:produceIntegerPayload") + private ProducerTemplate produceIntegerPayload; + + @Inject + @Uri("direct:produceFooQualifier") + private ProducerTemplate produceFooQualifier; + + @Inject + @Uri("direct:produceBarQualifier") + private ProducerTemplate produceBarQualifier; + + @Inject + private Event<Object> objectEvent; + + @Inject + private Event<EventPayload<String>> stringPayloadEvent; + + @Inject + private Event<EventPayload<Integer>> integerPayloadEvent; + + @Inject + private EventObserver observer; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test classes + .addClasses(EventConsumingRoute.class, EventProducingRoute.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Before + public void resetCollectedEventsAndMockEndpoints() { + observer.reset(); + consumeObject.reset(); + consumeString.reset(); + consumeStringPayload.reset(); + consumeIntegerPayload.reset(); + consumeFooQualifier.reset(); + consumeBarQualifier.reset(); + } + + @Test + public void sendEventsToConsumers() throws InterruptedException { + consumeObject.expectedMessageCount(8); + consumeObject.expectedBodiesReceived(1234, new EventPayload<>("foo"), new EventPayload<>("bar"), "test", new EventPayload<>(1), new EventPayload<>(2), 123L, 987L); + + consumeString.expectedMessageCount(1); + consumeString.expectedBodiesReceived("test"); + + consumeStringPayload.expectedMessageCount(2); + consumeStringPayload.expectedBodiesReceived(new EventPayload<>("foo"), new EventPayload<>("bar")); + + consumeIntegerPayload.expectedMessageCount(2); + consumeIntegerPayload.expectedBodiesReceived(new EventPayload<>(1), new EventPayload<>(2)); + + consumeFooQualifier.expectedMessageCount(1); + consumeFooQualifier.expectedBodiesReceived(123L); + + consumeBarQualifier.expectedMessageCount(1); + consumeBarQualifier.expectedBodiesReceived(987L); + + objectEvent.select(Integer.class).fire(1234); + objectEvent.select(new TypeLiteral<EventPayload<String>>() { + }).fire(new EventPayload<>("foo")); + stringPayloadEvent.select(new BarQualifier.Literal()).fire(new EventPayload<>("bar")); + objectEvent.select(String.class).fire("test"); + integerPayloadEvent.fire(new EventPayload<>(1)); + integerPayloadEvent.fire(new EventPayload<>(2)); + objectEvent.select(Long.class, new FooQualifier.Literal()).fire(123L); + objectEvent.select(Long.class, new BarQualifier.Literal()).fire(987L); + + assertIsSatisfied(2L, TimeUnit.SECONDS, consumeObject, consumeString, consumeStringPayload, consumeIntegerPayload, consumeFooQualifier, consumeBarQualifier); + } + + @Test + public void sendMessagesToProducers() { + produceObject.sendBody("string"); + EventPayload foo = new EventPayload<>("foo"); + produceStringPayload.sendBody(foo); + produceObject.sendBody(1234); + produceString.sendBody("test"); + EventPayload<Integer> bar = new EventPayload<>(2); + produceIntegerPayload.sendBody(bar); + EventPayload<Integer> baz = new EventPayload<>(12); + produceIntegerPayload.sendBody(baz); + produceFooQualifier.sendBody(456L); + produceBarQualifier.sendBody(495L); + produceObject.sendBody(777L); + + assertThat(observer.getObjectEvents(), contains("string", foo, 1234, "test", bar, baz, 456L, 495L, 777L)); + assertThat(observer.getStringEvents(), contains("string", "test")); + assertThat(observer.getStringPayloadEvents(), contains(foo)); + assertThat(observer.getIntegerPayloadEvents(), contains(bar, baz)); + assertThat(observer.getDefaultQualifierEvents(), contains("string", foo, 1234, "test", bar, baz, 777L)); + assertThat(observer.getFooQualifierEvents(), contains(456L)); + assertThat(observer.getBarQualifierEvents(), contains(495L)); + } + + @ApplicationScoped + static class EventObserver { + + private final List<Object> objectEvents = new ArrayList<>(); + + private final List<Object> defaultQualifierEvents = new ArrayList<>(); + + private final List<String> stringEvents = new ArrayList<>(); + + private final List<EventPayload<String>> stringPayloadEvents = new ArrayList<>(); + + private final List<EventPayload<Integer>> integerPayloadEvents = new ArrayList<>(); + + private final List<Long> fooQualifierEvents = new ArrayList<>(); + + private final List<Long> barQualifierEvents = new ArrayList<>(); + + void collectObjectEvents(@Observes Object event) { + objectEvents.add(event); + } + + void collectStringEvents(@Observes String event) { + stringEvents.add(event); + } + + void collectStringPayloadEvents(@Observes EventPayload<String> event) { + stringPayloadEvents.add(event); + } + + void collectIntegerPayloadEvents(@Observes EventPayload<Integer> event) { + integerPayloadEvents.add(event); + } + + void collectDefaultQualifierEvents(@Observes @Default Object event) { + defaultQualifierEvents.add(event); + } + + void collectFooQualifierEvents(@Observes @FooQualifier Long event) { + fooQualifierEvents.add(event); + } + + void collectBarQualifierEvents(@Observes @BarQualifier Long event) { + barQualifierEvents.add(event); + } + + List<Object> getObjectEvents() { + return objectEvents; + } + + List<String> getStringEvents() { + return stringEvents; + } + + List<EventPayload<String>> getStringPayloadEvents() { + return stringPayloadEvents; + } + + List<EventPayload<Integer>> getIntegerPayloadEvents() { + return integerPayloadEvents; + } + + List<Object> getDefaultQualifierEvents() { + return defaultQualifierEvents; + } + + List<Long> getFooQualifierEvents() { + return fooQualifierEvents; + } + + List<Long> getBarQualifierEvents() { + return barQualifierEvents; + } + + void reset() { + objectEvents.clear(); + stringEvents.clear(); + stringPayloadEvents.clear(); + integerPayloadEvents.clear(); + defaultQualifierEvents.clear(); + fooQualifierEvents.clear(); + barQualifierEvents.clear(); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventEndpointTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventEndpointTest.java new file mode 100644 index 0000000..fde16fe --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/EventEndpointTest.java @@ -0,0 +1,282 @@ +/** + * 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.cdi.test; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.event.Event; +import javax.enterprise.event.Observes; +import javax.enterprise.inject.Default; +import javax.inject.Inject; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.EventConsumingRouteCdi10; +import org.apache.camel.cdi.bean.EventProducingRouteCdi10; +import org.apache.camel.cdi.pojo.EventPayload; +import org.apache.camel.cdi.pojo.EventPayloadInteger; +import org.apache.camel.cdi.pojo.EventPayloadString; +import org.apache.camel.cdi.qualifier.BarQualifier; +import org.apache.camel.cdi.qualifier.FooQualifier; +import org.apache.camel.component.mock.MockEndpoint; +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.ShrinkWrap; +import org.jboss.shrinkwrap.api.asset.EmptyAsset; +import org.jboss.shrinkwrap.api.spec.JavaArchive; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class EventEndpointTest { + + @Inject + @Uri("mock:consumeObject") + private MockEndpoint consumeObject; + + @Inject + @Uri("mock:consumeString") + private MockEndpoint consumeString; + + @Inject + @Uri("mock:consumeStringPayload") + private MockEndpoint consumeStringPayload; + + @Inject + @Uri("mock:consumeIntegerPayload") + private MockEndpoint consumeIntegerPayload; + + @Inject + @Uri("mock:consumeFooQualifier") + private MockEndpoint consumeFooQualifier; + + @Inject + @Uri("mock:consumeBarQualifier") + private MockEndpoint consumeBarQualifier; + + @Inject + @Uri("direct:produceObject") + private ProducerTemplate produceObject; + + @Inject + @Uri("direct:produceString") + private ProducerTemplate produceString; + + @Inject + @Uri("direct:produceStringPayload") + private ProducerTemplate produceStringPayload; + + @Inject + @Uri("direct:produceIntegerPayload") + private ProducerTemplate produceIntegerPayload; + + @Inject + @Uri("direct:produceFooQualifier") + private ProducerTemplate produceFooQualifier; + + @Inject + @Uri("direct:produceBarQualifier") + private ProducerTemplate produceBarQualifier; + + @Inject + private Event<Object> objectEvent; + + @Inject + private Event<EventPayload<String>> stringPayloadEvent; + + @Inject + private Event<EventPayload<Integer>> integerPayloadEvent; + + @Inject + private EventObserver observer; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test classes + .addClasses( + EventConsumingRouteCdi10.class, + EventProducingRouteCdi10.class, + EventPayloadString.class, + EventPayloadInteger.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Before + public void resetCollectedEventsAndMockEndpoints() { + observer.reset(); + consumeObject.reset(); + consumeString.reset(); + consumeStringPayload.reset(); + consumeIntegerPayload.reset(); + consumeFooQualifier.reset(); + consumeBarQualifier.reset(); + } + + @Test + public void sendEventsToConsumers() throws InterruptedException { + consumeObject.expectedMessageCount(8); + consumeObject.expectedBodiesReceived(1234, new EventPayloadString("foo"), new EventPayloadString("bar"), "test", new EventPayloadInteger(1), new EventPayloadInteger(2), 123L, 987L); + + consumeString.expectedMessageCount(1); + consumeString.expectedBodiesReceived("test"); + + consumeStringPayload.expectedMessageCount(2); + consumeStringPayload.expectedBodiesReceived(new EventPayloadString("foo"), new EventPayloadString("bar")); + + consumeIntegerPayload.expectedMessageCount(2); + consumeIntegerPayload.expectedBodiesReceived(new EventPayloadInteger(1), new EventPayloadInteger(2)); + + consumeFooQualifier.expectedMessageCount(1); + consumeFooQualifier.expectedBodiesReceived(123L); + + consumeBarQualifier.expectedMessageCount(1); + consumeBarQualifier.expectedBodiesReceived(987L); + + objectEvent.select(Integer.class).fire(1234); + objectEvent.select(EventPayloadString.class).fire(new EventPayloadString("foo")); + stringPayloadEvent.select(new BarQualifier.Literal()).fire(new EventPayloadString("bar")); + objectEvent.select(String.class).fire("test"); + integerPayloadEvent.fire(new EventPayloadInteger(1)); + integerPayloadEvent.fire(new EventPayloadInteger(2)); + objectEvent.select(Long.class, new FooQualifier.Literal()).fire(123L); + objectEvent.select(Long.class, new BarQualifier.Literal()).fire(987L); + + //assertIsSatisfied(2L, TimeUnit.SECONDS, consumeObject, consumeString, consumeStringPayload, consumeIntegerPayload, consumeFooQualifier, consumeBarQualifier); + assertIsSatisfied(2L, TimeUnit.SECONDS, consumeObject, consumeString, consumeFooQualifier, consumeBarQualifier); + } + + @Test + public void sendMessagesToProducers() { + produceObject.sendBody("string"); + EventPayload foo = new EventPayloadString("foo"); + produceStringPayload.sendBody(foo); + produceObject.sendBody(1234); + produceString.sendBody("test"); + EventPayload<Integer> bar = new EventPayloadInteger(2); + produceIntegerPayload.sendBody(bar); + EventPayload<Integer> baz = new EventPayloadInteger(12); + produceIntegerPayload.sendBody(baz); + produceFooQualifier.sendBody(456L); + produceBarQualifier.sendBody(495L); + produceObject.sendBody(777L); + + assertThat(observer.getObjectEvents(), contains("string", foo, 1234, "test", bar, baz, 456L, 495L, 777L)); + // assertThat(observer.getStringEvents(), contains("string", "test")); + assertThat(observer.getStringPayloadEvents(), contains(foo)); + assertThat(observer.getIntegerPayloadEvents(), contains(bar, baz)); + assertThat(observer.getDefaultQualifierEvents(), contains("string", foo, 1234, "test", bar, baz, 777L)); + assertThat(observer.getFooQualifierEvents(), contains(456L)); + assertThat(observer.getBarQualifierEvents(), contains(495L)); + } + + @ApplicationScoped + static class EventObserver { + + private final List<Object> objectEvents = new ArrayList<>(); + + private final List<Object> defaultQualifierEvents = new ArrayList<>(); + + private final List<String> stringEvents = new ArrayList<>(); + + private final List<EventPayloadString> stringPayloadEvents = new ArrayList<>(); + + private final List<EventPayloadInteger> integerPayloadEvents = new ArrayList<>(); + + private final List<Long> fooQualifierEvents = new ArrayList<>(); + + private final List<Long> barQualifierEvents = new ArrayList<>(); + + void collectObjectEvents(@Observes Object event) { + objectEvents.add(event); + } + + void collectStringEvents(@Observes String event) { + stringEvents.add(event); + } + + void collectStringPayloadEvents(@Observes EventPayloadString event) { + stringPayloadEvents.add(event); + } + + void collectIntegerPayloadEvents(@Observes EventPayloadInteger event) { + integerPayloadEvents.add(event); + } + + void collectDefaultQualifierEvents(@Observes @Default Object event) { + defaultQualifierEvents.add(event); + } + + void collectFooQualifierEvents(@Observes @FooQualifier Long event) { + fooQualifierEvents.add(event); + } + + void collectBarQualifierEvents(@Observes @BarQualifier Long event) { + barQualifierEvents.add(event); + } + + List<Object> getObjectEvents() { + return objectEvents; + } + + List<String> getStringEvents() { + return stringEvents; + } + + List<EventPayloadString> getStringPayloadEvents() { + return stringPayloadEvents; + } + + List<EventPayloadInteger> getIntegerPayloadEvents() { + return integerPayloadEvents; + } + + List<Object> getDefaultQualifierEvents() { + return defaultQualifierEvents; + } + + List<Long> getFooQualifierEvents() { + return fooQualifierEvents; + } + + List<Long> getBarQualifierEvents() { + return barQualifierEvents; + } + + void reset() { + objectEvents.clear(); + stringEvents.clear(); + stringPayloadEvents.clear(); + integerPayloadEvents.clear(); + defaultQualifierEvents.clear(); + fooQualifierEvents.clear(); + barQualifierEvents.clear(); + } + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/0421c24d/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/InjectedEndpointTest.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/InjectedEndpointTest.java b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/InjectedEndpointTest.java new file mode 100644 index 0000000..45ee927 --- /dev/null +++ b/components/camel-cdi/src/test/java/org/apache/camel/cdi/test/InjectedEndpointTest.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.cdi.test; + +import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.cdi.CdiCamelExtension; +import org.apache.camel.cdi.Uri; +import org.apache.camel.cdi.bean.InjectedEndpointRoute; +import org.apache.camel.component.mock.MockEndpoint; +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.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 static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; + +@RunWith(Arquillian.class) +public class InjectedEndpointTest { + + @Inject + @Uri("direct:inbound") + private ProducerTemplate inbound; + + @Inject + private MockEndpoint outbound; + + @Deployment + public static Archive<?> deployment() { + return ShrinkWrap.create(JavaArchive.class) + // Camel CDI + .addPackage(CdiCamelExtension.class.getPackage()) + // Test class + .addClass(InjectedEndpointRoute.class) + // Bean archive deployment descriptor + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); + } + + @Test + public void sendMessageToInbound() throws InterruptedException { + outbound.expectedMessageCount(1); + outbound.expectedBodiesReceived("test"); + + inbound.sendBody("test"); + + assertIsSatisfied(2L, TimeUnit.SECONDS, outbound); + } +}