Add alternative example for Camel CDI test
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/554e3ab2 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/554e3ab2 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/554e3ab2 Branch: refs/heads/master Commit: 554e3ab25caa1a769bcbb61b8e3efac098e4c6fa Parents: 1b89ecd Author: Antonin Stefanutti <anto...@stefanutti.fr> Authored: Wed Mar 9 12:21:49 2016 +0100 Committer: Antonin Stefanutti <anto...@stefanutti.fr> Committed: Wed Mar 9 12:40:03 2016 +0100 ---------------------------------------------------------------------- examples/camel-example-cdi-test/README.md | 14 +++-- .../camel/example/cdi/test/Application.java | 12 +++- .../camel/example/cdi/test/AlternativeBean.java | 31 ++++++++++ .../camel/example/cdi/test/AlternativeTest.java | 62 ++++++++++++++++++++ 4 files changed, 112 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/554e3ab2/examples/camel-example-cdi-test/README.md ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-test/README.md b/examples/camel-example-cdi-test/README.md index 6ee83a5..6bc73e4 100644 --- a/examples/camel-example-cdi-test/README.md +++ b/examples/camel-example-cdi-test/README.md @@ -12,14 +12,16 @@ in any CDI compliant container. This example comes with a series of test classes that each demonstrates particular features provided by the `camel-test-cdi` module: -| Test class | Description | -| --------------------------- | ----------------------------------------------------- | -| [`AdviceTest`][] | Adds a test route using Camel advice API | -| [`ApplicationScopedTest`][] | A stateful `@ApplicationScoped` test class | -| [`CustomContextTest`][] | Declares a custom Camel context bean for test purpose | -| [`OrderTest`][] | Orders the test methods execution with `@Order` | +| Test class | Description | +| --------------------------- | --------------------------------------------------------- | +| [`AdviceTest`][] | Adds a test route using Camel advice API | +| [`AlternativeTest`][] | Mocks a bean used in a Camel route with a CDI alternative | +| [`ApplicationScopedTest`][] | A stateful `@ApplicationScoped` test class | +| [`CustomContextTest`][] | Declares a custom Camel context bean for test purpose | +| [`OrderTest`][] | Orders the test methods execution with `@Order` | [`AdviceTest`]: src/test/java/org/apache/camel/example/cdi/test/AdviceTest.java +[`AlternativeTest`]: src/test/java/org/apache/camel/example/cdi/test/AlternativeTest.java [`ApplicationScopedTest`]: src/test/java/org/apache/camel/example/cdi/test/ApplicationScopedTest.java [`CustomContextTest`]: src/test/java/org/apache/camel/example/cdi/test/CustomContextTest.java [`OrderTest`]: src/test/java/org/apache/camel/example/cdi/test/OrderTest.java http://git-wip-us.apache.org/repos/asf/camel/blob/554e3ab2/examples/camel-example-cdi-test/src/main/java/org/apache/camel/example/cdi/test/Application.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-test/src/main/java/org/apache/camel/example/cdi/test/Application.java b/examples/camel-example-cdi-test/src/main/java/org/apache/camel/example/cdi/test/Application.java index a99f3cf..d78d007 100644 --- a/examples/camel-example-cdi-test/src/main/java/org/apache/camel/example/cdi/test/Application.java +++ b/examples/camel-example-cdi-test/src/main/java/org/apache/camel/example/cdi/test/Application.java @@ -18,7 +18,9 @@ package org.apache.camel.example.cdi.test; import javax.enterprise.event.Observes; import javax.inject.Inject; +import javax.inject.Named; +import org.apache.camel.Body; import org.apache.camel.ProducerTemplate; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.cdi.ContextName; @@ -40,7 +42,7 @@ public class Application { .routeId("route") .log("${body} from ${camelContext.name} at ${date:now:hh:mm:ss a}!"); - from("direct:in").to("direct:out"); + from("direct:in").routeId("in»out").bean("bean").to("direct:out"); } } @@ -55,4 +57,12 @@ public class Application { void bye(@Observes CamelContextStoppingEvent event) { producer.sendBody("Bye"); } + + @Named("bean") + public static class Bean { + + public String process(@Body String body) { + return body; + } + } } http://git-wip-us.apache.org/repos/asf/camel/blob/554e3ab2/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeBean.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeBean.java b/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeBean.java new file mode 100644 index 0000000..d3746de --- /dev/null +++ b/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeBean.java @@ -0,0 +1,31 @@ +/** + * 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.example.cdi.test; + +import javax.enterprise.inject.Alternative; +import javax.inject.Named; + +import org.apache.camel.Body; + +@Alternative +@Named("bean") +public class AlternativeBean { + + public String process(@Body String body) { + return body + " with alternative bean!"; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/camel/blob/554e3ab2/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeTest.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeTest.java b/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeTest.java new file mode 100644 index 0000000..bb45782 --- /dev/null +++ b/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/AlternativeTest.java @@ -0,0 +1,62 @@ +/** + * 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.example.cdi.test; + +import java.util.concurrent.TimeUnit; +import javax.inject.Inject; + +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.cdi.Uri; +import org.apache.camel.component.mock.MockEndpoint; +import org.apache.camel.test.cdi.Beans; +import org.apache.camel.test.cdi.CamelCdiRunner; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; + +@RunWith(CamelCdiRunner.class) +@Beans(alternatives = AlternativeBean.class) +public class AlternativeTest { + + @Inject + @Uri("mock:out") + MockEndpoint mock; + + @Inject + @Uri("direct:in") + ProducerTemplate producer; + + @Test + public void testAlternativeBean() throws InterruptedException { + mock.expectedMessageCount(1); + mock.expectedBodiesReceived("test with alternative bean!"); + + producer.sendBody("test"); + + assertIsSatisfied(1L, TimeUnit.SECONDS, mock); + } + + static class TestRoute extends RouteBuilder { + + @Override + public void configure() { + from("direct:out").routeId("test").to("mock:out"); + } + } +}