This is an automated email from the ASF dual-hosted git repository. aldettinger pushed a commit to branch CAMEL-13342-JUNIT5-EXPLORATORY in repository https://gitbox.apache.org/repos/asf/camel.git
commit 3b6b55e7a63ccb0caa31a3d4ca8ac3c0192bb42d Author: aldettinger <aldettin...@gmail.com> AuthorDate: Wed Jun 26 19:10:13 2019 +0200 CAMEL-13342: Implemented a first version of a proof-of-concept for camel-cdi-test in junit 5 --- components/camel-test-cdi/pom.xml | 15 +++++ .../apache/camel/test/cdi/junit5/AnyLiteral.java | 30 +++++++++ .../test/cdi/junit5/CamelCdiJUnit5Extension.java | 71 ++++++++++++++++++++ .../cdi/junit5/CamelCdiJUnit5ExtensionStore.java | 52 +++++++++++++++ .../apache/camel/test/cdi/junit5/CamelCdiTest.java | 31 +++++++++ .../apache/camel/test/cdi/junit5/FilterTest.java | 75 ++++++++++++++++++++++ 6 files changed, 274 insertions(+) diff --git a/components/camel-test-cdi/pom.xml b/components/camel-test-cdi/pom.xml index 7cc65da..bb4db09 100644 --- a/components/camel-test-cdi/pom.xml +++ b/components/camel-test-cdi/pom.xml @@ -57,6 +57,21 @@ <artifactId>weld-se-core</artifactId> <version>${weld3-version}</version> </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> + <version>5.4.2</version> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <version>5.4.2</version> + </dependency> + <dependency> + <groupId>org.junit.vintage</groupId> + <artifactId>junit-vintage-engine</artifactId> + <version>5.4.2</version> + </dependency> <!-- test dependencies --> <dependency> diff --git a/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/AnyLiteral.java b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/AnyLiteral.java new file mode 100644 index 0000000..b71ecd3 --- /dev/null +++ b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/AnyLiteral.java @@ -0,0 +1,30 @@ +/* + * 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.test.cdi.junit5; + +import javax.enterprise.inject.Any; +import javax.enterprise.util.AnnotationLiteral; + +final class AnyLiteral extends AnnotationLiteral<Any> implements Any { + + static final Any INSTANCE = new AnyLiteral(); + + private static final long serialVersionUID = 1L; + + private AnyLiteral() { + } +} diff --git a/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/CamelCdiJUnit5Extension.java b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/CamelCdiJUnit5Extension.java new file mode 100644 index 0000000..0da533c --- /dev/null +++ b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/CamelCdiJUnit5Extension.java @@ -0,0 +1,71 @@ +/* + * 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.test.cdi.junit5; + +import java.util.Set; + +import javax.enterprise.inject.spi.Bean; +import javax.enterprise.inject.spi.BeanManager; + +import org.apache.camel.cdi.CdiCamelExtension; +import org.jboss.weld.config.ConfigurationKey; +import org.jboss.weld.environment.se.Weld; +import org.jboss.weld.environment.se.WeldContainer; +import org.junit.jupiter.api.extension.AfterEachCallback; +import org.junit.jupiter.api.extension.BeforeAllCallback; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.TestInstanceFactory; +import org.junit.jupiter.api.extension.TestInstanceFactoryContext; +import org.junit.jupiter.api.extension.TestInstantiationException; + +import static org.apache.camel.test.cdi.junit5.CamelCdiJUnit5ExtensionStore.getWeldBuilderFromStore; +import static org.apache.camel.test.cdi.junit5.CamelCdiJUnit5ExtensionStore.getWeldContainerFromStore; +import static org.apache.camel.test.cdi.junit5.CamelCdiJUnit5ExtensionStore.putWeldBuilderInStore; +import static org.apache.camel.test.cdi.junit5.CamelCdiJUnit5ExtensionStore.putWeldContainerInStore; + +public class CamelCdiJUnit5Extension implements BeforeAllCallback, AfterEachCallback, TestInstanceFactory { + + @Override + public void beforeAll(ExtensionContext eCtx) throws Exception { + Class<?> testClass = eCtx.getRequiredTestClass(); + Weld weld = new Weld() + // TODO: check parallel execution + .containerId("camel-context-cdi").property(ConfigurationKey.RELAXED_CONSTRUCTION.get(), true).property(Weld.SHUTDOWN_HOOK_SYSTEM_PROPERTY, false).enableDiscovery() + .beanClasses(testClass.getDeclaredClasses()).addBeanClass(testClass).addExtension(new CdiCamelExtension()); + + putWeldBuilderInStore(eCtx, weld); + } + + @Override + public void afterEach(ExtensionContext eCtx) throws Exception { + WeldContainer container = getWeldContainerFromStore(eCtx, eCtx.getRequiredTestInstance()); + container.shutdown(); + } + + @Override + public Object createTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext eCtx) throws TestInstantiationException { + WeldContainer container = getWeldBuilderFromStore(eCtx).initialize(); + BeanManager manager = container.getBeanManager(); + Set<Bean<?>> beans = manager.getBeans(eCtx.getRequiredTestClass(), AnyLiteral.INSTANCE); + Bean<?> bean = beans.iterator().next(); + Object testInstance = manager.getReference(bean, bean.getBeanClass(), manager.createCreationalContext(bean)); + + putWeldContainerInStore(eCtx, testInstance, container); + + return testInstance; + } +} diff --git a/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/CamelCdiJUnit5ExtensionStore.java b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/CamelCdiJUnit5ExtensionStore.java new file mode 100644 index 0000000..9f0c8ad --- /dev/null +++ b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/CamelCdiJUnit5ExtensionStore.java @@ -0,0 +1,52 @@ +/* + * 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.test.cdi.junit5; + +import org.jboss.weld.environment.se.Weld; +import org.jboss.weld.environment.se.WeldContainer; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ExtensionContext.Namespace; + +public final class CamelCdiJUnit5ExtensionStore { + + private static final String TEST_CLASS_WELD_BUILDER_STORE_KEY = "TEST_CLASS_WELD_BUILDER_STORE_KEY"; + private static final String TEST_INSTANCE_WELD_CONTAINER_STORE_KEY = "TEST_INSTANCE_WELD_CONTAINER_STORE_KEY"; + + private CamelCdiJUnit5ExtensionStore() { + } + + static void putWeldBuilderInStore(ExtensionContext eCtx, Weld weld) { + Namespace ns = Namespace.create(CamelCdiJUnit5ExtensionStore.class, eCtx.getRequiredTestClass()); + eCtx.getStore(ns).put(TEST_CLASS_WELD_BUILDER_STORE_KEY, weld); + } + + static Weld getWeldBuilderFromStore(ExtensionContext eCtx) { + Namespace ns = Namespace.create(CamelCdiJUnit5ExtensionStore.class, eCtx.getRequiredTestClass()); + return eCtx.getStore(ns).get(TEST_CLASS_WELD_BUILDER_STORE_KEY, Weld.class); + } + + static void putWeldContainerInStore(ExtensionContext eCtx, Object testInstance, WeldContainer container) { + Namespace ns = Namespace.create(CamelCdiJUnit5ExtensionStore.class, testInstance); + eCtx.getStore(ns).put(TEST_INSTANCE_WELD_CONTAINER_STORE_KEY, container); + } + + static WeldContainer getWeldContainerFromStore(ExtensionContext eCtx, Object testInstance) { + Namespace ns = Namespace.create(CamelCdiJUnit5ExtensionStore.class, testInstance); + return eCtx.getStore(ns).get(TEST_INSTANCE_WELD_CONTAINER_STORE_KEY, WeldContainer.class); + } + +} diff --git a/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/CamelCdiTest.java b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/CamelCdiTest.java new file mode 100644 index 0000000..b1f3fee --- /dev/null +++ b/components/camel-test-cdi/src/main/java/org/apache/camel/test/cdi/junit5/CamelCdiTest.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.test.cdi.junit5; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.junit.jupiter.api.extension.ExtendWith; + +@Target({ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +@ExtendWith(CamelCdiJUnit5Extension.class) +public @interface CamelCdiTest { + +} diff --git a/components/camel-test-cdi/src/test/java/org/apache/camel/test/cdi/junit5/FilterTest.java b/components/camel-test-cdi/src/test/java/org/apache/camel/test/cdi/junit5/FilterTest.java new file mode 100644 index 0000000..e7fe510 --- /dev/null +++ b/components/camel-test-cdi/src/test/java/org/apache/camel/test/cdi/junit5/FilterTest.java @@ -0,0 +1,75 @@ +/* + * 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.test.cdi.junit5; + +import org.apache.camel.EndpointInject; +import org.apache.camel.Produce; +import org.apache.camel.ProducerTemplate; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; + +@CamelCdiTest +public class FilterTest { + + @EndpointInject("mock:result") + protected MockEndpoint resultEndpoint; + + @Produce("direct:start") + protected ProducerTemplate template; + + @BeforeEach + public void before() { + resultEndpoint.reset(); + } + + @Test + public void testSendMatchingMessage() throws Exception { + String expectedBody = "<matched/>"; + + resultEndpoint.expectedBodiesReceived(expectedBody); + + template.sendBodyAndHeader(expectedBody, "foo", "bar"); + + resultEndpoint.assertIsSatisfied(); + } + + @Test + public void testSendNotMatchingMessage() throws Exception { + resultEndpoint.expectedMessageCount(0); + + template.sendBodyAndHeader("<notMatched/>", "foo", "notMatchedHeaderValue"); + + resultEndpoint.assertIsSatisfied(); + } + + @Test + public void testInfoParamShouldBeResolved(TestInfo testInfo) throws Exception { + Assertions.assertNotNull(testInfo); + } + + static class ContextConfig extends RouteBuilder { + + @Override + public void configure() { + from("direct:start").filter(header("foo").isEqualTo("bar")).to("mock:result"); + } + } +}