Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 669F419850 for ; Thu, 10 Mar 2016 14:16:49 +0000 (UTC) Received: (qmail 46016 invoked by uid 500); 10 Mar 2016 14:16:49 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 45965 invoked by uid 500); 10 Mar 2016 14:16:49 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 45947 invoked by uid 99); 10 Mar 2016 14:16:49 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Mar 2016 14:16:49 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 03700DFBDE; Thu, 10 Mar 2016 14:16:48 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: astefanutti@apache.org To: commits@camel.apache.org Message-Id: X-Mailer: ASF-Git Admin Mailer Subject: camel git commit: Support declaring additional test classes and packages in Camel CDI test Date: Thu, 10 Mar 2016 14:16:48 +0000 (UTC) Repository: camel Updated Branches: refs/heads/master 58d14be5e -> 67351b36a Support declaring additional test classes and packages in 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/67351b36 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/67351b36 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/67351b36 Branch: refs/heads/master Commit: 67351b36a388a55e8f343e1397cb37627ed65199 Parents: 58d14be Author: Antonin Stefanutti Authored: Thu Mar 10 15:16:02 2016 +0100 Committer: Antonin Stefanutti Committed: Thu Mar 10 15:16:33 2016 +0100 ---------------------------------------------------------------------- .../src/main/docs/cdi-testing.adoc | 35 +++++++++++++++++--- .../java/org/apache/camel/test/cdi/Beans.java | 18 ++++++++++ .../camel/test/cdi/CamelCdiDeployment.java | 7 ++++ .../camel/example/cdi/test/AlternativeTest.java | 14 +++----- .../example/cdi/test/ApplicationScopedTest.java | 11 ++---- .../camel/example/cdi/test/TestRoute.java | 34 +++++++++++++++++++ 6 files changed, 96 insertions(+), 23 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/67351b36/components/camel-test-cdi/src/main/docs/cdi-testing.adoc ---------------------------------------------------------------------- diff --git a/components/camel-test-cdi/src/main/docs/cdi-testing.adoc b/components/camel-test-cdi/src/main/docs/cdi-testing.adoc index ca03d1b..becd40d 100644 --- a/components/camel-test-cdi/src/main/docs/cdi-testing.adoc +++ b/components/camel-test-cdi/src/main/docs/cdi-testing.adoc @@ -96,7 +96,7 @@ Here is a simple unit test using the `CamelCdiRunner`: [source,java] ---- @RunWith(CamelCdiRunner.class) -public class CamelCdiRunnerTest { +public class CamelCdiTest { @Inject CamelContext context; @@ -115,7 +115,7 @@ CDI injection is also available for test method parameters, e.g.: [source,java] ---- @RunWith(CamelCdiRunner.class) -public class CamelCdiRunnerTest { +public class CamelCdiTest { @Test public void test(@Uri("direct:foo") ProducerTemplate producer) { @@ -130,7 +130,7 @@ execute the test methods in a particular sequence, e.g.: [source,java] ---- @RunWith(CamelCdiRunner.class) -public class CamelCdiRunnerTest { +public class CamelCdiTest { @Test @Order(1) @@ -157,7 +157,7 @@ entire test class execution using the `@ApplicationScoped` scope, e.g.: ---- @ApplicationScoped @RunWith(CamelCdiRunner.class) -public class CamelCdiRunnerTest { +public class CamelCdiTest { int counter; @@ -175,6 +175,33 @@ public class CamelCdiRunnerTest { } ---- +In case you need to add additional test beans, you can use the `@Beans` +annotation provided by Camel CDI test. For example, if you need to add +a route to your Camel context, instead of declaring a `RouteBuilder` bean +with a nested class, you can declare a managed bean, e.g.: + +[source,java] +---- +class TestRoute extends RouteBuilder { + + @Override + public void configure() { + from("direct:foo").to("mock:bar"); + } +} +---- + +And add it with the `@Beans` annotation, e.g.: + +[source,java] +---- +@RunWith(CamelCdiRunner.class) +@Beans(classes = TestRoute.class) +public class CamelCdiTest { + +} +---- + [[CDITesting-Arquillian]] Arquillian ^^^^^^^^^^ http://git-wip-us.apache.org/repos/asf/camel/blob/67351b36/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/Beans.java ---------------------------------------------------------------------- diff --git a/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/Beans.java b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/Beans.java index 611964a..3b6cde1 100644 --- a/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/Beans.java +++ b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/Beans.java @@ -70,4 +70,22 @@ public @interface Beans { * @see javax.enterprise.inject.Alternative */ Class[] alternatives() default {}; + + /** + * Returns the list of classes to be added as beans in the application. + * + * That can be used to add classes to the deployment for test purpose + * in addition to the test class which is automatically added as bean. + * + */ + Class[] classes() default {}; + + /** + * Returns the list of classes whose packages are to be added for beans + * discovery. + * + * That can be used to add packages to the deployment for test purpose + * in addition to the test class which is automatically added as bean. + */ + Class[] packages() default {}; } http://git-wip-us.apache.org/repos/asf/camel/blob/67351b36/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/CamelCdiDeployment.java ---------------------------------------------------------------------- diff --git a/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/CamelCdiDeployment.java b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/CamelCdiDeployment.java index 4142583..1e90d43 100644 --- a/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/CamelCdiDeployment.java +++ b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/CamelCdiDeployment.java @@ -43,6 +43,8 @@ final class CamelCdiDeployment implements TestRule { .addBeanClass(test.getJavaClass()) .addExtension(new CdiCamelExtension()); + // Apply deployment customization provided by the @Beans annotation + // if present on the test class if (test.getJavaClass().isAnnotationPresent(Beans.class)) { Beans beans = test.getJavaClass().getAnnotation(Beans.class); weld.addExtension(new CamelCdiTestExtension(beans)); @@ -50,12 +52,17 @@ final class CamelCdiDeployment implements TestRule { weld.addBeanClass(alternative) .addAlternative(alternative); } + for (Class clazz : beans.classes()) { + weld.addBeanClass(clazz); + } + weld.addPackages(false, beans.packages()); } } @Override public Statement apply(final Statement base, Description description) { return new Statement() { + @Override public void evaluate() throws Throwable { WeldContainer container = weld.initialize(); http://git-wip-us.apache.org/repos/asf/camel/blob/67351b36/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 index bb45782..95d81c8 100644 --- 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 @@ -20,7 +20,6 @@ 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; @@ -31,7 +30,10 @@ import org.junit.runner.RunWith; import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; @RunWith(CamelCdiRunner.class) -@Beans(alternatives = AlternativeBean.class) +@Beans( + alternatives = AlternativeBean.class, + classes = TestRoute.class +) public class AlternativeTest { @Inject @@ -51,12 +53,4 @@ public class AlternativeTest { assertIsSatisfied(1L, TimeUnit.SECONDS, mock); } - - static class TestRoute extends RouteBuilder { - - @Override - public void configure() { - from("direct:out").routeId("test").to("mock:out"); - } - } } http://git-wip-us.apache.org/repos/asf/camel/blob/67351b36/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/ApplicationScopedTest.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/ApplicationScopedTest.java b/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/ApplicationScopedTest.java index 7a460da..01ee04a 100644 --- a/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/ApplicationScopedTest.java +++ b/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/ApplicationScopedTest.java @@ -24,9 +24,9 @@ import javax.enterprise.event.Observes; 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; @@ -35,6 +35,7 @@ import static org.apache.camel.component.mock.MockEndpoint.assertIsSatisfied; @ApplicationScoped @RunWith(CamelCdiRunner.class) +@Beans(classes = TestRoute.class) public class ApplicationScopedTest { private static final AtomicInteger COUNTER = new AtomicInteger(); @@ -59,12 +60,4 @@ public class ApplicationScopedTest { public void testTwo(@Uri("mock:out") MockEndpoint mock) throws InterruptedException { assertIsSatisfied(1L, TimeUnit.SECONDS, mock); } - - static class TestRoute extends RouteBuilder { - - @Override - public void configure() { - from("direct:out").to("mock:out"); - } - } } http://git-wip-us.apache.org/repos/asf/camel/blob/67351b36/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/TestRoute.java ---------------------------------------------------------------------- diff --git a/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/TestRoute.java b/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/TestRoute.java new file mode 100644 index 0000000..ac54c10 --- /dev/null +++ b/examples/camel-example-cdi-test/src/test/java/org/apache/camel/example/cdi/test/TestRoute.java @@ -0,0 +1,34 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.example.cdi.test; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.cdi.Beans; + +/** + * Example of a test route that can be reused across multiple test classes + * using the {@code Beans} annotation. + * + * @see Beans#classes() + */ +class TestRoute extends RouteBuilder { + + @Override + public void configure() { + from("direct:out").to("mock:out"); + } +} \ No newline at end of file