From ec3b0dbaafcd576c7e11ac18eb430e1778c6bb0a Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Mon, 30 Oct 2023 16:26:28 +0200 Subject: [PATCH 01/28] Add @RequestScoped support Signed-off-by: Dmitry Aleksandrov --- microprofile/testing/junit5/pom.xml | 6 ++ .../microprofile/testing/junit5/AddJaxRs.java | 29 ++++++++++ .../testing/junit5/HelidonJunitExtension.java | 55 ++++++++++++++++++- .../junit5/src/main/java/module-info.java | 7 ++- microprofile/testing/testng/pom.xml | 5 ++ .../microprofile/testing/testng/AddJaxRs.java | 29 ++++++++++ .../testing/testng/HelidonTestNgListener.java | 55 +++++++++++++++++++ .../testng/src/main/java/module-info.java | 2 + .../junit5/TestReqScopeDisabledDiscovery.java | 46 ++++++++++++++++ .../testng/TestReqScopeDisabledDiscovery.java | 48 ++++++++++++++++ 10 files changed, 279 insertions(+), 3 deletions(-) create mode 100644 microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java create mode 100644 microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java create mode 100644 microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java create mode 100644 microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java diff --git a/microprofile/testing/junit5/pom.xml b/microprofile/testing/junit5/pom.xml index 36fdb1089dc..73e302bcd88 100644 --- a/microprofile/testing/junit5/pom.xml +++ b/microprofile/testing/junit5/pom.xml @@ -44,6 +44,11 @@ junit-jupiter-api provided + + org.glassfish.jersey.ext.cdi + jersey-weld2-se + provided + io.helidon.jersey helidon-jersey-client @@ -57,6 +62,7 @@ hamcrest-core test + diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java new file mode 100644 index 00000000000..9f460f64509 --- /dev/null +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.microprofile.testing.junit5; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Add JaxRS support for Request-scoped beans. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +public @interface AddJaxRs { +} diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java index 6f80bd7d461..28e469ed14f 100644 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java @@ -47,6 +47,7 @@ import jakarta.enterprise.inject.spi.InjectionPoint; import jakarta.enterprise.inject.spi.ProcessInjectionPoint; import jakarta.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator; +import jakarta.enterprise.util.AnnotationLiteral; import jakarta.inject.Inject; import jakarta.inject.Singleton; import jakarta.ws.rs.client.Client; @@ -80,7 +81,7 @@ class HelidonJunitExtension implements BeforeAllCallback, InvocationInterceptor, ParameterResolver { private static final Set> HELIDON_TEST_ANNOTATIONS = - Set.of(AddBean.class, AddConfig.class, AddExtension.class, Configuration.class); + Set.of(AddBean.class, AddConfig.class, AddExtension.class, Configuration.class, AddJaxRs.class); private static final Map, Annotation> BEAN_DEFINING = new HashMap<>(); private static final List YAML_SUFFIXES = List.of(".yml", ".yaml"); @@ -137,6 +138,13 @@ public void beforeAll(ExtensionContext context) { } validatePerClass(); + // add beans when using JaxRS + AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); + if (addJaxRsAnnotation != null){ + classLevelExtensions.add(AddExtensionJaxRsLiteral.INSTANCE); + classLevelBeans.add(AddBeanJaxRsLiteral.INSTANCE); + } + configure(classLevelConfigMeta); if (!classLevelConfigMeta.useExisting) { @@ -234,6 +242,13 @@ private void validatePerClass() { } } } + + AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); + if (addJaxRsAnnotation != null){ + if (testClass.getAnnotation(DisableDiscovery.class) == null){ + throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation."); + } + } } private boolean hasHelidonTestAnnotation(AnnotatedElement element) { @@ -621,4 +636,42 @@ ConfigMeta nextMethod() { return methodMeta; } } + + + /** + * Add JaxRs Bean when {@code AddJaxRs} annotation is used. + */ + private static final class AddBeanJaxRsLiteral extends AnnotationLiteral implements AddBean { + + static final AddBeanJaxRsLiteral INSTANCE = new AddBeanJaxRsLiteral(); + + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return org.glassfish.jersey.weld.se.WeldRequestScope.class; + } + + @Override + public Class scope() { + return RequestScoped.class; + } + } + + + /** + * Add JaxRs Extension when {@code AddJaxRs} annotation is used. + */ + private static final class AddExtensionJaxRsLiteral extends AnnotationLiteral implements AddExtension { + + static final AddExtensionJaxRsLiteral INSTANCE = new AddExtensionJaxRsLiteral(); + + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class; + } + } + } diff --git a/microprofile/testing/junit5/src/main/java/module-info.java b/microprofile/testing/junit5/src/main/java/module-info.java index 1e0a02603e0..51680318718 100644 --- a/microprofile/testing/junit5/src/main/java/module-info.java +++ b/microprofile/testing/junit5/src/main/java/module-info.java @@ -19,11 +19,14 @@ */ module io.helidon.microprofile.testing.junit5 { - requires io.helidon.microprofile.cdi; requires io.helidon.config.mp; requires io.helidon.config.yaml.mp; - requires org.junit.jupiter.api; + requires io.helidon.microprofile.cdi; requires jakarta.inject; + requires jersey.cdi1x; + requires jersey.weld2.se; + requires org.junit.jupiter.api; + requires transitive jakarta.cdi; requires transitive jakarta.ws.rs; diff --git a/microprofile/testing/testng/pom.xml b/microprofile/testing/testng/pom.xml index b7827f9d1a6..1155b7e5315 100644 --- a/microprofile/testing/testng/pom.xml +++ b/microprofile/testing/testng/pom.xml @@ -47,6 +47,11 @@ io.helidon.config helidon-config-yaml-mp + + org.glassfish.jersey.ext.cdi + jersey-weld2-se + provided + org.testng testng diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java new file mode 100644 index 00000000000..cd185c0d076 --- /dev/null +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.microprofile.testing.testng; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Add JaxRS support for Request-scoped beans. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +public @interface AddJaxRs { +} diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java index 7843643781d..ea22afa73cf 100644 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java @@ -49,6 +49,7 @@ import jakarta.enterprise.inject.spi.InjectionTarget; import jakarta.enterprise.inject.spi.InjectionTargetFactory; import jakarta.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator; +import jakarta.enterprise.util.AnnotationLiteral; import jakarta.inject.Inject; import jakarta.inject.Singleton; import jakarta.ws.rs.client.Client; @@ -174,6 +175,14 @@ public void onTestStart(ITestResult result) { methodLevelDisableDiscovery = discovery.value(); } + + // add beans when using JaxRS + AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); + if (addJaxRsAnnotation != null){ + classLevelExtensions.add(AddExtensionJaxRsLiteral.INSTANCE); + classLevelBeans.add(AddBeanJaxRsLiteral.INSTANCE); + } + startContainer(methodLevelBeans, methodLevelExtensions, methodLevelDisableDiscovery); } } @@ -246,6 +255,14 @@ private void validatePerClass() { } } } + + + AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); + if (addJaxRsAnnotation != null){ + if (testClass.getAnnotation(DisableDiscovery.class) == null){ + throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation."); + } + } } private boolean hasHelidonTestAnnotation(AnnotatedElement element) { @@ -505,4 +522,42 @@ ConfigMeta nextMethod() { return methodMeta; } } + + + + /** + * Add JaxRs Bean when {@code AddJaxRs} annotation is used. + */ + private static final class AddBeanJaxRsLiteral extends AnnotationLiteral implements AddBean { + + static final AddBeanJaxRsLiteral INSTANCE = new AddBeanJaxRsLiteral(); + + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return org.glassfish.jersey.weld.se.WeldRequestScope.class; + } + + @Override + public Class scope() { + return RequestScoped.class; + } + } + + + /** + * Add JaxRs Extension when {@code AddJaxRs} annotation is used. + */ + private static final class AddExtensionJaxRsLiteral extends AnnotationLiteral implements AddExtension { + + static final AddExtensionJaxRsLiteral INSTANCE = new AddExtensionJaxRsLiteral(); + + private static final long serialVersionUID = 1L; + + @Override + public Class value() { + return org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class; + } + } } diff --git a/microprofile/testing/testng/src/main/java/module-info.java b/microprofile/testing/testng/src/main/java/module-info.java index 271be2a6022..9b9d0bd71d2 100644 --- a/microprofile/testing/testng/src/main/java/module-info.java +++ b/microprofile/testing/testng/src/main/java/module-info.java @@ -27,6 +27,8 @@ requires jakarta.cdi; requires jakarta.inject; requires jakarta.ws.rs; + requires jersey.cdi1x; + requires jersey.weld2.se; requires microprofile.config.api; requires org.testng; diff --git a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java new file mode 100644 index 00000000000..8f35a9b9c6e --- /dev/null +++ b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java @@ -0,0 +1,46 @@ +package io.helidon.microprofile.tests.testing.junit5; + +import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.ServerCdiExtension; +import io.helidon.microprofile.testing.junit5.*; +import jakarta.enterprise.context.RequestScoped; +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Response; +import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +@HelidonTest +@DisableDiscovery +@AddExtension(ServerCdiExtension.class) +@AddExtension(JaxRsCdiExtension.class) +@AddExtension(CdiComponentProvider.class) + +// JAX-RS Request scope +@AddJaxRs +@AddBean(TestReqScopeDisabledDiscovery.MyController.class) +public class TestReqScopeDisabledDiscovery { + @Inject + private WebTarget target; + + @Test + void testGet() { + assertEquals("Hallo!", target + .path("/greeting") + .request() + .get(String.class)); + } + + @Path("/greeting") + @RequestScoped + public static class MyController { + @GET + public Response get() { + return Response.ok("Hallo!").build(); + } + } +} \ No newline at end of file diff --git a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java new file mode 100644 index 00000000000..24ee0ca1a95 --- /dev/null +++ b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java @@ -0,0 +1,48 @@ +package io.helidon.microprofile.tests.testing.testng; + + +import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.ServerCdiExtension; +import io.helidon.microprofile.testing.testng.*; +import jakarta.enterprise.context.RequestScoped; +import jakarta.inject.Inject; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.Response; +import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; +import org.testng.annotations.Test; + +import static org.testng.AssertJUnit.assertEquals; + +@HelidonTest +@DisableDiscovery +@AddExtension(ServerCdiExtension.class) +@AddExtension(JaxRsCdiExtension.class) +@AddExtension(CdiComponentProvider.class) + +// JAX-RS Request scope +@AddJaxRs +@AddBean(TestReqScopeDisabledDiscovery.MyController.class) +public class TestReqScopeDisabledDiscovery { + + @Inject + private WebTarget target; + + //@Test + void testGet() { + assertEquals("Hallo!", target + .path("/greeting") + .request() + .get(String.class)); + } + + @Path("/greeting") + @RequestScoped + public static class MyController { + @GET + public Response get() { + return Response.ok("Hallo!").build(); + } + } +} \ No newline at end of file From 3c28cdf00fa98c41b53c53e7995044f989aac7fa Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Tue, 31 Oct 2023 10:54:34 +0200 Subject: [PATCH 02/28] Add JaxRs support for Request-scoped beans. Signed-off-by: Dmitry Aleksandrov --- docs/mp/testing-ng.adoc | 65 +++++++++++++++-- docs/mp/testing.adoc | 70 ++++++++++++++++--- .../microprofile/testing/junit5/AddJaxRs.java | 2 +- .../microprofile/testing/testng/AddJaxRs.java | 2 +- .../testing/testng/HelidonTestNgListener.java | 15 ++-- .../junit5/TestReqScopeDisabledDiscovery.java | 17 ++++- .../testng/TestReqScopeDisabledDiscovery.java | 19 ++++- 7 files changed, 162 insertions(+), 28 deletions(-) diff --git a/docs/mp/testing-ng.adoc b/docs/mp/testing-ng.adoc index 280d6fb98e5..09345048921 100644 --- a/docs/mp/testing-ng.adoc +++ b/docs/mp/testing-ng.adoc @@ -110,6 +110,9 @@ In addition to this simplification, the following annotations are supported: |`@io.helidon.microprofile.testing.testng.DisableDiscovery` |Used to disable automated discovery of beans and extensions + +|Used `@io.helidon.microprofile.testing.junit5.AddJaxRs` +|add JaxRs support to the test class. Useful for `@RequestScoped` beans. Only used with `@DisableDiscovery` annotation, otherwise an exception will be thrown. |=== == Examples @@ -119,23 +122,71 @@ In current example Helidon container will be launched prior test. The _Bean Disc [source,java] .Code sample ---- -@HelidonTest -@DisableDiscovery -@AddBean(MyBean.class) -@AddExtension(ConfigCdiExtension.class) -@AddConfig(key = "app.greeting", value = "TestHello") +@HelidonTest <1> +@DisableDiscovery <2> +@AddBean(MyBean.class) <3> +@AddExtension(ConfigCdiExtension.class) <4> +@AddConfig(key = "app.greeting", value = "TestHello") <5> class TestExample { @Inject - private MyBean myBean; + private MyBean myBean; <6> @Test - void testGreeting() { + void testGreeting() { <7> assertThat(myBean, notNullValue()); assertThat(myBean.greeting(), is("TestHello")); } } ---- +<1> Start the Helidon container. +<2> Set disabled Bean Discovery for the current test class. +<3> Add `MyBean` to current context. +<4> Add a configuration CDI extension to the current test. +<5> Add configuration properties. +<6> Inject `MyBean` as it is available in the CDI context. +<7> Run rests. + +To test `@RequestScoped` bean with JaxRs support: + +[source,java] +.Test `RequestScoped` bean +---- + +@HelidonTest <1> +@DisableDiscovery <2> +@AddExtension(ServerCdiExtension.class) +@AddExtension(JaxRsCdiExtension.class) +@AddExtension(CdiComponentProvider.class) +@AddJaxRs <3> +@AddBean(TestReqScopeDisabledDiscovery.MyController.class) +public class TestReqScopeDisabledDiscovery { + + @Inject + private WebTarget target; + + @Test + void testGet() { + assertEquals("Hallo!", target + .path("/greeting") + .request() + .get(String.class)); + } + + @Path("/greeting") + @RequestScoped <4> + public static class MyController { + @GET + public Response get() { + return Response.ok("Hallo!").build(); + } + } +} +---- +<1> Start the Helidon container. +<2> Set disabled Bean discovery. +<3> Add JaxRs support to the current test class. +<4> Define a `RequestScoped` bean. == Reference diff --git a/docs/mp/testing.adoc b/docs/mp/testing.adoc index 5387490ec28..2e072073f50 100644 --- a/docs/mp/testing.adoc +++ b/docs/mp/testing.adoc @@ -115,32 +115,86 @@ In addition to this simplification, the following annotations are supported: |Used `@io.helidon.microprofile.testing.junit5.DisableDiscovery` |to disable automated discovery of beans and extensions + +|Used `@io.helidon.microprofile.testing.junit5.DisableDiscovery` +|to disable automated discovery of beans and extensions + +|Used `@io.helidon.microprofile.testing.junit5.AddJaxRs` +|add JaxRs support to the test class. Useful for `@RequestScoped` beans. Only used with `@DisableDiscovery` annotation, otherwise an exception will be thrown. |=== == Examples -In current example Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation. +In the current example, Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation. [source,java] .Code sample ---- -@HelidonTest -@DisableDiscovery -@AddBean(MyBean.class) -@AddExtension(ConfigCdiExtension.class) -@AddConfig(key = "app.greeting", value = "TestHello") +@HelidonTest <1> +@DisableDiscovery <2> +@AddBean(MyBean.class) <3> +@AddExtension(ConfigCdiExtension.class) <4> +@AddConfig(key = "app.greeting", value = "TestHello") <5> class TestExample { @Inject - private MyBean myBean; + private MyBean myBean; <6> @Test - void testGreeting() { + void testGreeting() { <7> assertThat(myBean, notNullValue()); assertThat(myBean.greeting(), is("TestHello")); } } ---- +<1> Start the Helidon container. +<2> Set disabled Bean Discovery for the current test class. +<3> Add `MyBean` to current context. +<4> Add a configuration CDI extension to the current test. +<5> Add configuration properties. +<6> Inject `MyBean` as it is available in the CDI context. +<7> Run rests. + +To test `@RequestScoped` bean with JaxRs support: + +[source,java] +.Test `RequestScoped` bean +---- + +@HelidonTest <1> +@DisableDiscovery <2> +@AddExtension(ServerCdiExtension.class) +@AddExtension(JaxRsCdiExtension.class) +@AddExtension(CdiComponentProvider.class) +@AddJaxRs <3> +@AddBean(TestReqScopeDisabledDiscovery.MyController.class) +public class TestReqScopeDisabledDiscovery { + + @Inject + private WebTarget target; + + @Test + void testGet() { + assertEquals("Hallo!", target + .path("/greeting") + .request() + .get(String.class)); + } + + @Path("/greeting") + @RequestScoped <4> + public static class MyController { + @GET + public Response get() { + return Response.ok("Hallo!").build(); + } + } +} +---- +<1> Start the Helidon container. +<2> Set disabled Bean discovery. +<3> Add JaxRs support to the current test class. +<4> Define a `RequestScoped` bean. == Additional Information diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java index 9f460f64509..1f776358f0d 100644 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java @@ -24,6 +24,6 @@ * Add JaxRS support for Request-scoped beans. */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE, ElementType.METHOD}) +@Target({ElementType.TYPE}) public @interface AddJaxRs { } diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java index cd185c0d076..250d2fb192e 100644 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java @@ -24,6 +24,6 @@ * Add JaxRS support for Request-scoped beans. */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE, ElementType.METHOD}) +@Target({ElementType.TYPE}) public @interface AddJaxRs { } diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java index ea22afa73cf..079f6c6b689 100644 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java @@ -125,6 +125,13 @@ public void onBeforeClass(ITestClass iTestClass) { } validatePerClass(); + // add beans when using JaxRS + AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); + if (addJaxRsAnnotation != null){ + classLevelExtensions.add(AddExtensionJaxRsLiteral.INSTANCE); + classLevelBeans.add(AddBeanJaxRsLiteral.INSTANCE); + } + configure(classLevelConfigMeta); if (!classLevelConfigMeta.useExisting) { @@ -175,14 +182,6 @@ public void onTestStart(ITestResult result) { methodLevelDisableDiscovery = discovery.value(); } - - // add beans when using JaxRS - AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); - if (addJaxRsAnnotation != null){ - classLevelExtensions.add(AddExtensionJaxRsLiteral.INSTANCE); - classLevelBeans.add(AddBeanJaxRsLiteral.INSTANCE); - } - startContainer(methodLevelBeans, methodLevelExtensions, methodLevelDisableDiscovery); } } diff --git a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java index 8f35a9b9c6e..204e18fdebf 100644 --- a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java +++ b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package io.helidon.microprofile.tests.testing.junit5; import io.helidon.microprofile.server.JaxRsCdiExtension; @@ -23,7 +38,7 @@ // JAX-RS Request scope @AddJaxRs @AddBean(TestReqScopeDisabledDiscovery.MyController.class) -public class TestReqScopeDisabledDiscovery { +class TestReqScopeDisabledDiscovery { @Inject private WebTarget target; diff --git a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java index 24ee0ca1a95..da21a3cff17 100644 --- a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java +++ b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package io.helidon.microprofile.tests.testing.testng; @@ -24,12 +39,12 @@ // JAX-RS Request scope @AddJaxRs @AddBean(TestReqScopeDisabledDiscovery.MyController.class) -public class TestReqScopeDisabledDiscovery { +class TestReqScopeDisabledDiscovery { @Inject private WebTarget target; - //@Test + @Test void testGet() { assertEquals("Hallo!", target .path("/greeting") From a7f196db78a95afa53796dcb18026a09aa487624 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Mon, 13 Nov 2023 19:31:31 +0200 Subject: [PATCH 03/28] Refactoring to separate module Signed-off-by: Dmitry Aleksandrov --- docs/mp/testing-ng.adoc | 13 +- docs/mp/testing.adoc | 11 +- microprofile/testing/common/pom.xml | 43 ++++++ .../testing/common/CdiExtension.java} | 20 ++- .../testing/common/CdiExtensions.java} | 16 ++- .../common/src/main/java/module-info.java | 27 ++++ microprofile/testing/jaxrs/pom.xml | 60 ++++++++ .../microprofile/testing/jaxrs/AddJaxRs.java | 32 +++++ .../jaxrs/src/main/java/module-info.java | 36 +++++ microprofile/testing/junit5/pom.xml | 16 ++- .../testing/junit5/HelidonJunitExtension.java | 128 +++++++++++------- .../junit5/src/main/java/module-info.java | 7 +- microprofile/testing/pom.xml | 2 + microprofile/testing/testng/pom.xml | 11 +- .../testing/testng/HelidonTestNgListener.java | 37 +++-- .../testng/src/main/java/module-info.java | 2 +- microprofile/tests/testing/junit5/pom.xml | 6 + .../junit5/TestReqScopeDisabledDiscovery.java | 9 +- .../testng/TestReqScopeDisabledDiscovery.java | 8 +- 19 files changed, 380 insertions(+), 104 deletions(-) create mode 100644 microprofile/testing/common/pom.xml rename microprofile/testing/{testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java => common/src/main/java/io/helidon/microprofile/testing/common/CdiExtension.java} (67%) rename microprofile/testing/{junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java => common/src/main/java/io/helidon/microprofile/testing/common/CdiExtensions.java} (74%) create mode 100644 microprofile/testing/common/src/main/java/module-info.java create mode 100644 microprofile/testing/jaxrs/pom.xml create mode 100644 microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/AddJaxRs.java create mode 100644 microprofile/testing/jaxrs/src/main/java/module-info.java diff --git a/docs/mp/testing-ng.adoc b/docs/mp/testing-ng.adoc index 09345048921..d1ffb80c488 100644 --- a/docs/mp/testing-ng.adoc +++ b/docs/mp/testing-ng.adoc @@ -112,12 +112,18 @@ In addition to this simplification, the following annotations are supported: |Used to disable automated discovery of beans and extensions |Used `@io.helidon.microprofile.testing.junit5.AddJaxRs` -|add JaxRs support to the test class. Useful for `@RequestScoped` beans. Only used with `@DisableDiscovery` annotation, otherwise an exception will be thrown. +a|add JaxRs support to the test class. Only used with `@DisableDiscovery` annotation, otherwise an exception will be thrown. Automatically adds the following Beans and Extensions to the test class: + +* `ServerCdiExtension` +* `JaxRsCdiExtension` +* `CdiComponentProvider` +* `org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes` +* `org.glassfish.jersey.weld.se.WeldRequestScope` |=== == Examples -In current example Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation. +In the current example, Helidon container will be launched prior test. The _Bean Discovery_ will be disabled. _MyBean_ will be added to the test, so that it can be injected. _ConfigCdiExtension_ will be enabled for this test. And finally, a configuration property will be added using `@AddConfig` annotation. [source,java] .Code sample @@ -154,9 +160,6 @@ To test `@RequestScoped` bean with JaxRs support: @HelidonTest <1> @DisableDiscovery <2> -@AddExtension(ServerCdiExtension.class) -@AddExtension(JaxRsCdiExtension.class) -@AddExtension(CdiComponentProvider.class) @AddJaxRs <3> @AddBean(TestReqScopeDisabledDiscovery.MyController.class) diff --git a/docs/mp/testing.adoc b/docs/mp/testing.adoc index 2e072073f50..e3ac24975ab 100644 --- a/docs/mp/testing.adoc +++ b/docs/mp/testing.adoc @@ -120,7 +120,13 @@ In addition to this simplification, the following annotations are supported: |to disable automated discovery of beans and extensions |Used `@io.helidon.microprofile.testing.junit5.AddJaxRs` -|add JaxRs support to the test class. Useful for `@RequestScoped` beans. Only used with `@DisableDiscovery` annotation, otherwise an exception will be thrown. +a|add JaxRs support to the test class. Only used with `@DisableDiscovery` annotation, otherwise an exception will be thrown. Automatically adds the following Beans and Extensions to the test class: + +* `ServerCdiExtension` +* `JaxRsCdiExtension` +* `CdiComponentProvider` +* `org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes` +* `org.glassfish.jersey.weld.se.WeldRequestScope` |=== == Examples @@ -162,9 +168,6 @@ To test `@RequestScoped` bean with JaxRs support: @HelidonTest <1> @DisableDiscovery <2> -@AddExtension(ServerCdiExtension.class) -@AddExtension(JaxRsCdiExtension.class) -@AddExtension(CdiComponentProvider.class) @AddJaxRs <3> @AddBean(TestReqScopeDisabledDiscovery.MyController.class) diff --git a/microprofile/testing/common/pom.xml b/microprofile/testing/common/pom.xml new file mode 100644 index 00000000000..6aaeac9b503 --- /dev/null +++ b/microprofile/testing/common/pom.xml @@ -0,0 +1,43 @@ + + + + + 4.0.0 + + io.helidon.microprofile.testing + helidon-microprofile-testing-project + 4.0.0-SNAPSHOT + ../pom.xml + + + helidon-microprofile-testing-common + Helidon Microprofile Testing JUnit5 + + + Integration with Junit5 to support tests with CDI injection + + + + + jakarta.enterprise + jakarta.enterprise.cdi-api + + + + diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtension.java similarity index 67% rename from microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java rename to microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtension.java index 250d2fb192e..9461915f14c 100644 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtension.java @@ -13,17 +13,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.helidon.microprofile.testing.testng; +package io.helidon.microprofile.testing.common; import java.lang.annotation.ElementType; +import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import jakarta.enterprise.inject.spi.Extension; + + /** - * Add JaxRS support for Request-scoped beans. + * Common CDI Extension. */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE}) -public @interface AddJaxRs { +@Target({ElementType.TYPE, ElementType.METHOD}) +@Repeatable(CdiExtensions.class) +public @interface CdiExtension { + + /** + * CDI Extension. + * + * @return The CDI Extension Class. + */ + Class value(); } diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtensions.java similarity index 74% rename from microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java rename to microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtensions.java index 1f776358f0d..87709219946 100644 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtensions.java @@ -13,17 +13,25 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.helidon.microprofile.testing.junit5; +package io.helidon.microprofile.testing.common; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; + /** - * Add JaxRS support for Request-scoped beans. + * Common CDI Extensions for testing. */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE}) -public @interface AddJaxRs { +@Target({ElementType.TYPE, ElementType.METHOD}) +public @interface CdiExtensions { + + /** + * Return CDI Extension. + * + * @return CDIExtension[] + */ + CdiExtension[] value(); } diff --git a/microprofile/testing/common/src/main/java/module-info.java b/microprofile/testing/common/src/main/java/module-info.java new file mode 100644 index 00000000000..ad3e8208376 --- /dev/null +++ b/microprofile/testing/common/src/main/java/module-info.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2020, 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * JUnit5 extension module to run CDI tests. + */ +module io.helidon.microprofile.testing.common { + + requires jakarta.inject; + requires transitive jakarta.cdi; + + exports io.helidon.microprofile.testing.common; + +} diff --git a/microprofile/testing/jaxrs/pom.xml b/microprofile/testing/jaxrs/pom.xml new file mode 100644 index 00000000000..78e8f873a02 --- /dev/null +++ b/microprofile/testing/jaxrs/pom.xml @@ -0,0 +1,60 @@ + + + + + 4.0.0 + + io.helidon.microprofile.testing + helidon-microprofile-testing-project + 4.0.0-SNAPSHOT + ../pom.xml + + + helidon-microprofile-testing-jaxrs + Helidon Microprofile Testing JAX-RS support + + + Integration with Junit5 to support tests with CDI injection + + + + + io.helidon.microprofile.cdi + helidon-microprofile-cdi + provided + + + io.helidon.microprofile.server + helidon-microprofile-server + provided + + + org.junit.jupiter + junit-jupiter-api + provided + + + io.helidon.microprofile.testing + helidon-microprofile-testing-common + 4.0.0-SNAPSHOT + compile + + + + diff --git a/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/AddJaxRs.java b/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/AddJaxRs.java new file mode 100644 index 00000000000..76a57956102 --- /dev/null +++ b/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/AddJaxRs.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.microprofile.testing.jaxrs; + +import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.ServerCdiExtension; +import io.helidon.microprofile.testing.common.CdiExtension; + +import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; + +/** + * JAX_RS Testing annotation. + */ +@CdiExtension(ServerCdiExtension.class) +@CdiExtension(JaxRsCdiExtension.class) +@CdiExtension(CdiComponentProvider.class) +public @interface AddJaxRs { +} diff --git a/microprofile/testing/jaxrs/src/main/java/module-info.java b/microprofile/testing/jaxrs/src/main/java/module-info.java new file mode 100644 index 00000000000..cff2052441e --- /dev/null +++ b/microprofile/testing/jaxrs/src/main/java/module-info.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * JUnit5 extension module to run CDI tests. + */ +module io.helidon.microprofile.testing.jaxrs { + + requires io.helidon.config.mp; + requires io.helidon.config.yaml.mp; + requires io.helidon.microprofile.cdi; + requires io.helidon.microprofile.server; + requires io.helidon.microprofile.testing.common; + requires jakarta.inject; + requires jersey.cdi1x; + requires org.junit.jupiter.api; + + requires transitive jakarta.cdi; + requires transitive jakarta.ws.rs; + + exports io.helidon.microprofile.testing.jaxrs; + +} diff --git a/microprofile/testing/junit5/pom.xml b/microprofile/testing/junit5/pom.xml index 73e302bcd88..d525cca1431 100644 --- a/microprofile/testing/junit5/pom.xml +++ b/microprofile/testing/junit5/pom.xml @@ -37,7 +37,10 @@ io.helidon.microprofile.cdi helidon-microprofile-cdi - provided + + + io.helidon.config + helidon-config-yaml-mp org.junit.jupiter @@ -53,16 +56,17 @@ io.helidon.jersey helidon-jersey-client - - io.helidon.config - helidon-config-yaml-mp - org.hamcrest hamcrest-core test - + + io.helidon.microprofile.testing + helidon-microprofile-testing-common + 4.0.0-SNAPSHOT + compile + diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java index 28e469ed14f..a271dbb5dc0 100644 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java @@ -16,6 +16,7 @@ package io.helidon.microprofile.testing.junit5; +import java.io.Serial; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; @@ -33,6 +34,7 @@ import io.helidon.config.mp.MpConfigSources; import io.helidon.config.yaml.mp.YamlMpConfigSource; +import io.helidon.microprofile.testing.common.CdiExtension; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; @@ -81,7 +83,7 @@ class HelidonJunitExtension implements BeforeAllCallback, InvocationInterceptor, ParameterResolver { private static final Set> HELIDON_TEST_ANNOTATIONS = - Set.of(AddBean.class, AddConfig.class, AddExtension.class, Configuration.class, AddJaxRs.class); + Set.of(AddBean.class, AddConfig.class, AddExtension.class, Configuration.class); private static final Map, Annotation> BEAN_DEFINING = new HashMap<>(); private static final List YAML_SUFFIXES = List.of(".yml", ".yaml"); @@ -139,11 +141,11 @@ public void beforeAll(ExtensionContext context) { validatePerClass(); // add beans when using JaxRS - AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); - if (addJaxRsAnnotation != null){ - classLevelExtensions.add(AddExtensionJaxRsLiteral.INSTANCE); - classLevelBeans.add(AddBeanJaxRsLiteral.INSTANCE); - } +// AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); +// if (addJaxRsAnnotation != null){ +// classLevelExtensions.add(AddProcessAnnotatedTypesLiteral.INSTANCE); +// classLevelBeans.add(AddWeldRequestScopeLiteral.INSTANCE); +// } configure(classLevelConfigMeta); @@ -156,6 +158,16 @@ public void beforeAll(ExtensionContext context) { } @SuppressWarnings("unchecked") + private Class[] getFeatureExtensions(Class testClass) { + return Arrays.stream(testClass.getDeclaredAnnotations()) + .flatMap(a -> Arrays.stream(a.getClass().getDeclaredAnnotations())) + .filter(a -> a.annotationType() == CdiExtension.class) + .map(CdiExtension.class::cast) + .map(CdiExtension::value) + .toList() + .toArray(new Class[0]); + } + private T[] getAnnotations(Class testClass, Class annotClass) { // inherited does not help, as it only returns annot from superclass if // child has none @@ -243,12 +255,28 @@ private void validatePerClass() { } } - AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); - if (addJaxRsAnnotation != null){ - if (testClass.getAnnotation(DisableDiscovery.class) == null){ - throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation."); - } - } +// AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); +// if (addJaxRsAnnotation != null){ +// if (testClass.getAnnotation(DisableDiscovery.class) == null){ +// throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation."); +// } +// +// List> beans = classLevelBeans.stream().map(AddBean::value).toList(); +// if (beans.contains(org.glassfish.jersey.weld.se.WeldRequestScope.class)) { +// throw new RuntimeException("@AddJaxRs annotation already includes `WeldRequestScope` bean"); +// } +// +// List> extensions = classLevelExtensions.stream().map(AddExtension::value).toList(); +// if (!extensions.isEmpty()) { +// if (extensions.contains(org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class)) { +// throw new RuntimeException("@AddJaxRs annotation already includes `ProcessAllAnnotatedTypes` extension"); +// } +// if (extensions.contains(CdiExtension.class)) { +// throw new RuntimeException("@AddJaxRs annotation already includes `CDI` extension"); +// } +// } +// +// } } private boolean hasHelidonTestAnnotation(AnnotatedElement element) { @@ -636,42 +664,44 @@ ConfigMeta nextMethod() { return methodMeta; } } - - - /** - * Add JaxRs Bean when {@code AddJaxRs} annotation is used. - */ - private static final class AddBeanJaxRsLiteral extends AnnotationLiteral implements AddBean { - - static final AddBeanJaxRsLiteral INSTANCE = new AddBeanJaxRsLiteral(); - - private static final long serialVersionUID = 1L; - - @Override - public Class value() { - return org.glassfish.jersey.weld.se.WeldRequestScope.class; - } - - @Override - public Class scope() { - return RequestScoped.class; - } - } - - - /** - * Add JaxRs Extension when {@code AddJaxRs} annotation is used. - */ - private static final class AddExtensionJaxRsLiteral extends AnnotationLiteral implements AddExtension { - - static final AddExtensionJaxRsLiteral INSTANCE = new AddExtensionJaxRsLiteral(); - - private static final long serialVersionUID = 1L; - - @Override - public Class value() { - return org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class; - } - } +// +// +// /** +// * Add Weld Request Scope Bean Literal when {@code AddJaxRs} annotation is used. +// */ +// private static final class AddWeldRequestScopeLiteral extends AnnotationLiteral implements AddBean { +// +// static final AddWeldRequestScopeLiteral INSTANCE = new AddWeldRequestScopeLiteral(); +// +// @Serial +// private static final long serialVersionUID = 1L; +// +// @Override +// public Class value() { +// return org.glassfish.jersey.weld.se.WeldRequestScope.class; +// } +// +// @Override +// public Class scope() { +// return RequestScoped.class; +// } +// } +// +// +// /** +// * Add Process Annotated Types Literal when {@code AddJaxRs} annotation is used. +// */ +// private static final class AddProcessAnnotatedTypesLiteral extends AnnotationLiteral implements AddExtension { +// +// static final AddProcessAnnotatedTypesLiteral INSTANCE = new AddProcessAnnotatedTypesLiteral(); +// +// @Serial +// private static final long serialVersionUID = 1L; +// +// @Override +// public Class value() { +// return org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class; +// } +// } } diff --git a/microprofile/testing/junit5/src/main/java/module-info.java b/microprofile/testing/junit5/src/main/java/module-info.java index 51680318718..c831c5bc5c2 100644 --- a/microprofile/testing/junit5/src/main/java/module-info.java +++ b/microprofile/testing/junit5/src/main/java/module-info.java @@ -19,13 +19,12 @@ */ module io.helidon.microprofile.testing.junit5 { + requires io.helidon.microprofile.cdi; requires io.helidon.config.mp; requires io.helidon.config.yaml.mp; - requires io.helidon.microprofile.cdi; - requires jakarta.inject; - requires jersey.cdi1x; - requires jersey.weld2.se; requires org.junit.jupiter.api; + requires jakarta.inject; + requires io.helidon.microprofile.testing.common; requires transitive jakarta.cdi; requires transitive jakarta.ws.rs; diff --git a/microprofile/testing/pom.xml b/microprofile/testing/pom.xml index 4d5e9897d0f..9917f6c070f 100644 --- a/microprofile/testing/pom.xml +++ b/microprofile/testing/pom.xml @@ -33,7 +33,9 @@ Helidon Microprofile Testing Project + common junit5 testng + jaxrs diff --git a/microprofile/testing/testng/pom.xml b/microprofile/testing/testng/pom.xml index 1155b7e5315..0101ab77e13 100644 --- a/microprofile/testing/testng/pom.xml +++ b/microprofile/testing/testng/pom.xml @@ -34,11 +34,6 @@ - - io.helidon.microprofile.cdi - helidon-microprofile-cdi - provided - io.helidon.jersey helidon-jersey-client @@ -57,6 +52,12 @@ testng provided + + io.helidon.microprofile.testing + helidon-microprofile-testing-jaxrs + 4.0.0-SNAPSHOT + compile + \ No newline at end of file diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java index 079f6c6b689..b62be5d48ae 100644 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java @@ -16,6 +16,7 @@ package io.helidon.microprofile.testing.testng; +import java.io.Serial; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; @@ -32,6 +33,7 @@ import io.helidon.config.mp.MpConfigSources; import io.helidon.config.yaml.mp.YamlMpConfigSource; +import io.helidon.microprofile.testing.jaxrs.AddJaxRs; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; @@ -58,6 +60,7 @@ import org.eclipse.microprofile.config.spi.ConfigBuilder; import org.eclipse.microprofile.config.spi.ConfigProviderResolver; import org.eclipse.microprofile.config.spi.ConfigSource; +import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; import org.testng.IClassListener; import org.testng.ITestClass; import org.testng.ITestListener; @@ -128,8 +131,8 @@ public void onBeforeClass(ITestClass iTestClass) { // add beans when using JaxRS AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); if (addJaxRsAnnotation != null){ - classLevelExtensions.add(AddExtensionJaxRsLiteral.INSTANCE); - classLevelBeans.add(AddBeanJaxRsLiteral.INSTANCE); + classLevelExtensions.add(AddProcessAnnotatedTypesLiteral.INSTANCE); + classLevelBeans.add(AddWeldRequestScopeLiteral.INSTANCE); } configure(classLevelConfigMeta); @@ -261,6 +264,22 @@ private void validatePerClass() { if (testClass.getAnnotation(DisableDiscovery.class) == null){ throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation."); } + + List> beans = classLevelBeans.stream().map(AddBean::value).toList(); + if (beans.contains(org.glassfish.jersey.weld.se.WeldRequestScope.class)) { + throw new RuntimeException("@AddJaxRs annotation already includes `WeldRequestScope` bean"); + } + + List> extensions = classLevelExtensions.stream().map(AddExtension::value).toList(); + if (!extensions.isEmpty()) { + if (extensions.contains(org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class)) { + throw new RuntimeException("@AddJaxRs annotation already includes `ProcessAllAnnotatedTypes` extension"); + } + if (extensions.contains(CdiComponentProvider.class)) { + throw new RuntimeException("@AddJaxRs annotation already includes `CdiComponentProvider` extension"); + } + } + } } @@ -525,12 +544,13 @@ ConfigMeta nextMethod() { /** - * Add JaxRs Bean when {@code AddJaxRs} annotation is used. + * Add Weld Request Scope Literal when {@code AddJaxRs} annotation is used. */ - private static final class AddBeanJaxRsLiteral extends AnnotationLiteral implements AddBean { + private static final class AddWeldRequestScopeLiteral extends AnnotationLiteral implements AddBean { - static final AddBeanJaxRsLiteral INSTANCE = new AddBeanJaxRsLiteral(); + static final AddWeldRequestScopeLiteral INSTANCE = new AddWeldRequestScopeLiteral(); + @Serial private static final long serialVersionUID = 1L; @Override @@ -546,12 +566,13 @@ public Class scope() { /** - * Add JaxRs Extension when {@code AddJaxRs} annotation is used. + * Add Process Annotated Types Literal when {@code AddJaxRs} annotation is used. */ - private static final class AddExtensionJaxRsLiteral extends AnnotationLiteral implements AddExtension { + private static final class AddProcessAnnotatedTypesLiteral extends AnnotationLiteral implements AddExtension { - static final AddExtensionJaxRsLiteral INSTANCE = new AddExtensionJaxRsLiteral(); + static final AddProcessAnnotatedTypesLiteral INSTANCE = new AddProcessAnnotatedTypesLiteral(); + @Serial private static final long serialVersionUID = 1L; @Override diff --git a/microprofile/testing/testng/src/main/java/module-info.java b/microprofile/testing/testng/src/main/java/module-info.java index 9b9d0bd71d2..dd2bad119c4 100644 --- a/microprofile/testing/testng/src/main/java/module-info.java +++ b/microprofile/testing/testng/src/main/java/module-info.java @@ -23,7 +23,6 @@ requires io.helidon.config.mp; requires io.helidon.config.yaml.mp; - requires io.helidon.microprofile.cdi; requires jakarta.cdi; requires jakarta.inject; requires jakarta.ws.rs; @@ -31,6 +30,7 @@ requires jersey.weld2.se; requires microprofile.config.api; requires org.testng; + requires io.helidon.microprofile.testing.jaxrs; exports io.helidon.microprofile.testing.testng; diff --git a/microprofile/tests/testing/junit5/pom.xml b/microprofile/tests/testing/junit5/pom.xml index a0b58c82f14..d9b78c2e6c3 100644 --- a/microprofile/tests/testing/junit5/pom.xml +++ b/microprofile/tests/testing/junit5/pom.xml @@ -55,5 +55,11 @@ hamcrest-core test + + io.helidon.microprofile.testing + helidon-microprofile-testing-jaxrs + 4.0.0-SNAPSHOT + test + diff --git a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java index 204e18fdebf..62ce05be34b 100644 --- a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java +++ b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java @@ -15,8 +15,7 @@ */ package io.helidon.microprofile.tests.testing.junit5; -import io.helidon.microprofile.server.JaxRsCdiExtension; -import io.helidon.microprofile.server.ServerCdiExtension; +import io.helidon.microprofile.testing.jaxrs.AddJaxRs; import io.helidon.microprofile.testing.junit5.*; import jakarta.enterprise.context.RequestScoped; import jakarta.inject.Inject; @@ -24,20 +23,16 @@ import jakarta.ws.rs.Path; import jakarta.ws.rs.client.WebTarget; import jakarta.ws.rs.core.Response; -import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @HelidonTest @DisableDiscovery -@AddExtension(ServerCdiExtension.class) -@AddExtension(JaxRsCdiExtension.class) -@AddExtension(CdiComponentProvider.class) // JAX-RS Request scope -@AddJaxRs @AddBean(TestReqScopeDisabledDiscovery.MyController.class) +@AddJaxRs class TestReqScopeDisabledDiscovery { @Inject private WebTarget target; diff --git a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java index da21a3cff17..89fb90285da 100644 --- a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java +++ b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java @@ -15,9 +15,7 @@ */ package io.helidon.microprofile.tests.testing.testng; - -import io.helidon.microprofile.server.JaxRsCdiExtension; -import io.helidon.microprofile.server.ServerCdiExtension; +import io.helidon.microprofile.testing.jaxrs.AddJaxRs; import io.helidon.microprofile.testing.testng.*; import jakarta.enterprise.context.RequestScoped; import jakarta.inject.Inject; @@ -25,16 +23,12 @@ import jakarta.ws.rs.Path; import jakarta.ws.rs.client.WebTarget; import jakarta.ws.rs.core.Response; -import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; import org.testng.annotations.Test; import static org.testng.AssertJUnit.assertEquals; @HelidonTest @DisableDiscovery -@AddExtension(ServerCdiExtension.class) -@AddExtension(JaxRsCdiExtension.class) -@AddExtension(CdiComponentProvider.class) // JAX-RS Request scope @AddJaxRs From 836e6bf723eb0074f393edabff4eafb1ca432b41 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Mon, 13 Nov 2023 22:19:29 +0200 Subject: [PATCH 04/28] Add Bean support Signed-off-by: Dmitry Aleksandrov --- .../testing/common/CommonAddBean.java | 53 ++++++++++++ .../testing/common/CommonAddBeans.java | 36 +++++++++ ...Extension.java => CommonCdiExtension.java} | 8 +- ...tensions.java => CommonCdiExtensions.java} | 8 +- .../testing/common/package-info.java | 20 +++++ .../microprofile/testing/jaxrs/AddJaxRs.java | 20 ++++- .../testing/jaxrs/package-info.java | 20 +++++ .../jaxrs/src/main/java/module-info.java | 1 + .../testing/junit5/HelidonJunitExtension.java | 81 +++++++++---------- 9 files changed, 195 insertions(+), 52 deletions(-) create mode 100644 microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBean.java create mode 100644 microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBeans.java rename microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/{CdiExtension.java => CommonCdiExtension.java} (87%) rename microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/{CdiExtensions.java => CommonCdiExtensions.java} (86%) create mode 100644 microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/package-info.java create mode 100644 microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/package-info.java diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBean.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBean.java new file mode 100644 index 00000000000..d0100d52c1d --- /dev/null +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBean.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.microprofile.testing.common; + +import jakarta.enterprise.context.ApplicationScoped; + +import java.lang.annotation.Annotation; +import java.lang.annotation.ElementType; +import java.lang.annotation.Repeatable; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Add a bean. + * This is intended for test sources where we do not want to add {@code beans.xml} as this would add + * all test classes as beans. + * The bean will be added by default with {@link ApplicationScoped}. + * The class will be instantiated using CDI and will be available for injection into test classes and other beans. + * This annotation can be repeated. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +@Repeatable(CommonAddBeans.class) +public @interface CommonAddBean { + /** + * Class of the bean. + * @return the class of a bean + */ + Class value(); + + /** + * Scope of the bean. + * Only {@link jakarta.inject.Singleton}, {@link ApplicationScoped} + * and {@link jakarta.enterprise.context.RequestScoped} scopes are supported. + * + * @return scope of the bean + */ + Class scope() default ApplicationScoped.class; +} diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBeans.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBeans.java new file mode 100644 index 00000000000..e413f0628a6 --- /dev/null +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBeans.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.microprofile.testing.common; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * A repeatable container for {@link CommonAddBean}. + * No need to use this annotation, just repeat {@link CommonAddBean} annotation + * on test class. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE}) +public @interface CommonAddBeans { + /** + * Beans to be added. + * @return add bean annotations + */ + CommonAddBean[] value(); +} diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtension.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtension.java similarity index 87% rename from microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtension.java rename to microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtension.java index 9461915f14c..71b641690f3 100644 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtension.java +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtension.java @@ -16,6 +16,7 @@ package io.helidon.microprofile.testing.common; import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -28,9 +29,10 @@ * Common CDI Extension. */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE, ElementType.METHOD}) -@Repeatable(CdiExtensions.class) -public @interface CdiExtension { +@Target({ElementType.TYPE}) +@Repeatable(CommonCdiExtensions.class) +@Inherited +public @interface CommonCdiExtension { /** * CDI Extension. diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtensions.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtensions.java similarity index 86% rename from microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtensions.java rename to microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtensions.java index 87709219946..28890f40265 100644 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CdiExtensions.java +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtensions.java @@ -16,6 +16,7 @@ package io.helidon.microprofile.testing.common; import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @@ -25,13 +26,14 @@ * Common CDI Extensions for testing. */ @Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE, ElementType.METHOD}) -public @interface CdiExtensions { +@Target({ElementType.TYPE}) +@Inherited +public @interface CommonCdiExtensions { /** * Return CDI Extension. * * @return CDIExtension[] */ - CdiExtension[] value(); + CommonCdiExtension[] value(); } diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/package-info.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/package-info.java new file mode 100644 index 00000000000..77f71b19fbe --- /dev/null +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Common Testing Extensions. + */ +package io.helidon.microprofile.testing.common; diff --git a/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/AddJaxRs.java b/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/AddJaxRs.java index 76a57956102..a0f99f3d3d2 100644 --- a/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/AddJaxRs.java +++ b/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/AddJaxRs.java @@ -16,17 +16,29 @@ package io.helidon.microprofile.testing.jaxrs; +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + import io.helidon.microprofile.server.JaxRsCdiExtension; import io.helidon.microprofile.server.ServerCdiExtension; -import io.helidon.microprofile.testing.common.CdiExtension; +import io.helidon.microprofile.testing.common.CommonAddBean; +import io.helidon.microprofile.testing.common.CommonCdiExtension; import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; /** * JAX_RS Testing annotation. */ -@CdiExtension(ServerCdiExtension.class) -@CdiExtension(JaxRsCdiExtension.class) -@CdiExtension(CdiComponentProvider.class) +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +@Inherited +@CommonCdiExtension(ServerCdiExtension.class) +@CommonCdiExtension(JaxRsCdiExtension.class) +@CommonCdiExtension(CdiComponentProvider.class) +@CommonCdiExtension(org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class) +@CommonAddBean(org.glassfish.jersey.weld.se.WeldRequestScope.class) public @interface AddJaxRs { } diff --git a/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/package-info.java b/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/package-info.java new file mode 100644 index 00000000000..3a80e8e726e --- /dev/null +++ b/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * JAX-RS Extension for Testing. + */ +package io.helidon.microprofile.testing.jaxrs; diff --git a/microprofile/testing/jaxrs/src/main/java/module-info.java b/microprofile/testing/jaxrs/src/main/java/module-info.java index cff2052441e..b4dadb03e5b 100644 --- a/microprofile/testing/jaxrs/src/main/java/module-info.java +++ b/microprofile/testing/jaxrs/src/main/java/module-info.java @@ -30,6 +30,7 @@ requires transitive jakarta.cdi; requires transitive jakarta.ws.rs; + requires jersey.weld2.se; exports io.helidon.microprofile.testing.jaxrs; diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java index a271dbb5dc0..47d89503a32 100644 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java @@ -16,7 +16,6 @@ package io.helidon.microprofile.testing.junit5; -import java.io.Serial; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; @@ -31,10 +30,14 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import io.helidon.config.mp.MpConfigSources; import io.helidon.config.yaml.mp.YamlMpConfigSource; -import io.helidon.microprofile.testing.common.CdiExtension; +import io.helidon.microprofile.testing.common.CommonAddBean; +import io.helidon.microprofile.testing.common.CommonAddBeans; +import io.helidon.microprofile.testing.common.CommonCdiExtension; +import io.helidon.microprofile.testing.common.CommonCdiExtensions; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; @@ -49,7 +52,6 @@ import jakarta.enterprise.inject.spi.InjectionPoint; import jakarta.enterprise.inject.spi.ProcessInjectionPoint; import jakarta.enterprise.inject.spi.configurator.AnnotatedTypeConfigurator; -import jakarta.enterprise.util.AnnotationLiteral; import jakarta.inject.Inject; import jakarta.inject.Singleton; import jakarta.ws.rs.client.Client; @@ -140,13 +142,6 @@ public void beforeAll(ExtensionContext context) { } validatePerClass(); - // add beans when using JaxRS -// AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); -// if (addJaxRsAnnotation != null){ -// classLevelExtensions.add(AddProcessAnnotatedTypesLiteral.INSTANCE); -// classLevelBeans.add(AddWeldRequestScopeLiteral.INSTANCE); -// } - configure(classLevelConfigMeta); if (!classLevelConfigMeta.useExisting) { @@ -158,14 +153,33 @@ public void beforeAll(ExtensionContext context) { } @SuppressWarnings("unchecked") - private Class[] getFeatureExtensions(Class testClass) { + private List> getFeatureExtensions(Class testClass) { return Arrays.stream(testClass.getDeclaredAnnotations()) - .flatMap(a -> Arrays.stream(a.getClass().getDeclaredAnnotations())) - .filter(a -> a.annotationType() == CdiExtension.class) - .map(CdiExtension.class::cast) - .map(CdiExtension::value) - .toList() - .toArray(new Class[0]); + .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) + .filter(a -> a instanceof CommonCdiExtensions) + .map(CommonCdiExtensions.class::cast) + .flatMap(e -> Arrays.stream(e.value())) + .map(CommonCdiExtension::value) + .collect(Collectors.toList()); + } + + private List> getFeatureBeans(Class testClass) { + + ArrayList> result = new ArrayList<>(Arrays.stream(testClass.getDeclaredAnnotations()) + .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) + .filter(a -> a instanceof CommonAddBean) + .map(CommonAddBean.class::cast) + .map(CommonAddBean::value) + .collect(Collectors.toList())); + + result.addAll(Arrays.stream(testClass.getDeclaredAnnotations()) + .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) + .filter(a -> a instanceof CommonAddBeans) + .map(CommonAddBeans.class::cast) + .flatMap(e -> Arrays.stream(e.value())) + .map(CommonAddBean::value) + .collect(Collectors.toList())); + return result; } private T[] getAnnotations(Class testClass, Class annotClass) { @@ -254,29 +268,6 @@ private void validatePerClass() { } } } - -// AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); -// if (addJaxRsAnnotation != null){ -// if (testClass.getAnnotation(DisableDiscovery.class) == null){ -// throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation."); -// } -// -// List> beans = classLevelBeans.stream().map(AddBean::value).toList(); -// if (beans.contains(org.glassfish.jersey.weld.se.WeldRequestScope.class)) { -// throw new RuntimeException("@AddJaxRs annotation already includes `WeldRequestScope` bean"); -// } -// -// List> extensions = classLevelExtensions.stream().map(AddExtension::value).toList(); -// if (!extensions.isEmpty()) { -// if (extensions.contains(org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class)) { -// throw new RuntimeException("@AddJaxRs annotation already includes `ProcessAllAnnotatedTypes` extension"); -// } -// if (extensions.contains(CdiExtension.class)) { -// throw new RuntimeException("@AddJaxRs annotation already includes `CDI` extension"); -// } -// } -// -// } } private boolean hasHelidonTestAnnotation(AnnotatedElement element) { @@ -370,7 +361,7 @@ private void startContainer(List beanAnnotations, initializer.disableDiscovery(); } - initializer.addExtensions(new AddBeansExtension(testClass, beanAnnotations)); + initializer.addExtensions(new AddBeansExtension(testClass, beanAnnotations, getFeatureBeans(testClass))); for (AddExtension addExtension : extensionAnnotations) { Class extensionClass = addExtension.value(); @@ -382,6 +373,8 @@ private void startContainer(List beanAnnotations, } } + getFeatureExtensions(testClass).forEach(initializer::addExtensions); + container = initializer.initialize(); } @@ -523,12 +516,14 @@ private void callAfterStop() { private static class AddBeansExtension implements Extension { private final Class testClass; private final List addBeans; + private final List> featureBeans; private final HashMap socketAnnotations = new HashMap<>(); - private AddBeansExtension(Class testClass, List addBeans) { + private AddBeansExtension(Class testClass, List addBeans, List> featureBeans) { this.testClass = testClass; this.addBeans = addBeans; + this.featureBeans = featureBeans; } @@ -603,6 +598,8 @@ void registerAddedBeans(@Observes BeforeBeanDiscovery event) { configurator.add(scope); } } + + featureBeans.forEach(e -> event.addAnnotatedType(e, e.getName())); } private boolean hasBda(Class value) { From d8a7f189f30d69212a04b313a5054b99d56919f8 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Tue, 14 Nov 2023 19:44:30 +0200 Subject: [PATCH 05/28] Refactor. Reuse in TestNG. Signed-off-by: Dmitry Aleksandrov --- .../testing/common/CommonAddBean.java | 18 +--- .../testing/common/CommonAddBeans.java | 3 +- .../testing/common/CommonTestUtil.java | 83 +++++++++++++++++++ .../testing/junit5/HelidonJunitExtension.java | 48 ++--------- .../junit5/src/main/java/module-info.java | 6 +- .../testing/testng/HelidonTestNgListener.java | 41 ++------- .../testng/src/main/java/module-info.java | 3 +- .../junit5/TestReqScopeDisabledDiscovery.java | 3 +- .../testng/TestReqScopeDisabledDiscovery.java | 1 - 9 files changed, 107 insertions(+), 99 deletions(-) create mode 100644 microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonTestUtil.java diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBean.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBean.java index d0100d52c1d..6485fc10da5 100644 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBean.java +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBean.java @@ -15,9 +15,6 @@ */ package io.helidon.microprofile.testing.common; -import jakarta.enterprise.context.ApplicationScoped; - -import java.lang.annotation.Annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Repeatable; import java.lang.annotation.Retention; @@ -25,10 +22,7 @@ import java.lang.annotation.Target; /** - * Add a bean. - * This is intended for test sources where we do not want to add {@code beans.xml} as this would add - * all test classes as beans. - * The bean will be added by default with {@link ApplicationScoped}. + * Add a bean to other annotations. * The class will be instantiated using CDI and will be available for injection into test classes and other beans. * This annotation can be repeated. */ @@ -36,18 +30,10 @@ @Target({ElementType.TYPE, ElementType.METHOD}) @Repeatable(CommonAddBeans.class) public @interface CommonAddBean { + /** * Class of the bean. * @return the class of a bean */ Class value(); - - /** - * Scope of the bean. - * Only {@link jakarta.inject.Singleton}, {@link ApplicationScoped} - * and {@link jakarta.enterprise.context.RequestScoped} scopes are supported. - * - * @return scope of the bean - */ - Class scope() default ApplicationScoped.class; } diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBeans.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBeans.java index e413f0628a6..1b304d09dfc 100644 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBeans.java +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBeans.java @@ -22,8 +22,7 @@ /** * A repeatable container for {@link CommonAddBean}. - * No need to use this annotation, just repeat {@link CommonAddBean} annotation - * on test class. + * No need to use this annotation, just repeat {@link CommonAddBean} annotation. */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonTestUtil.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonTestUtil.java new file mode 100644 index 00000000000..430558e51fd --- /dev/null +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonTestUtil.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.microprofile.testing.common; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import jakarta.enterprise.inject.spi.Extension; + +/** + * Common Utility class for tests. + */ +public class CommonTestUtil { + + private CommonTestUtil() { + } + + /** + * Extract all Extensions from the given testClass. + * + * @param testClass Class + * @return List with Extension classes + */ + public static List> getFeatureExtensions(Class testClass) { + + List> result = Arrays.stream(testClass.getDeclaredAnnotations()) + .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) + .filter(a -> a instanceof CommonCdiExtension) + .map(CommonCdiExtension.class::cast) + .map(CommonCdiExtension::value) + .collect(Collectors.toList()); + + result.addAll(Arrays.stream(testClass.getDeclaredAnnotations()) + .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) + .filter(a -> a instanceof CommonCdiExtensions) + .map(CommonCdiExtensions.class::cast) + .flatMap(e -> Arrays.stream(e.value())) + .map(CommonCdiExtension::value) + .collect(Collectors.toList())); + + return result; + } + + /** + * Extract all Beans from the given testClass. + * + * @param testClass Class + * @return List with Beans classes + */ + public static List> getFeatureBeans(Class testClass) { + + ArrayList> result = Arrays.stream(testClass.getDeclaredAnnotations()) + .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) + .filter(a -> a instanceof CommonAddBean) + .map(CommonAddBean.class::cast) + .map(CommonAddBean::value).collect(Collectors.toCollection(ArrayList::new)); + + result.addAll(Arrays.stream(testClass.getDeclaredAnnotations()) + .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) + .filter(a -> a instanceof CommonAddBeans) + .map(CommonAddBeans.class::cast) + .flatMap(e -> Arrays.stream(e.value())) + .map(CommonAddBean::value) + .collect(Collectors.toList())); + return result; + } +} diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java index 47d89503a32..f2c9cbc6b1a 100644 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java @@ -30,14 +30,9 @@ import java.util.List; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; import io.helidon.config.mp.MpConfigSources; import io.helidon.config.yaml.mp.YamlMpConfigSource; -import io.helidon.microprofile.testing.common.CommonAddBean; -import io.helidon.microprofile.testing.common.CommonAddBeans; -import io.helidon.microprofile.testing.common.CommonCdiExtension; -import io.helidon.microprofile.testing.common.CommonCdiExtensions; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; @@ -74,6 +69,9 @@ import org.junit.jupiter.api.extension.ParameterResolver; import org.junit.jupiter.api.extension.ReflectiveInvocationContext; +import static io.helidon.microprofile.testing.common.CommonTestUtil.getFeatureBeans; +import static io.helidon.microprofile.testing.common.CommonTestUtil.getFeatureExtensions; + /** * Junit5 extension to support Helidon CDI container in tests. @@ -109,7 +107,6 @@ class HelidonJunitExtension implements BeforeAllCallback, private SeContainer container; - @SuppressWarnings("unchecked") @Override public void beforeAll(ExtensionContext context) { testClass = context.getRequiredTestClass(); @@ -152,36 +149,6 @@ public void beforeAll(ExtensionContext context) { } } - @SuppressWarnings("unchecked") - private List> getFeatureExtensions(Class testClass) { - return Arrays.stream(testClass.getDeclaredAnnotations()) - .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) - .filter(a -> a instanceof CommonCdiExtensions) - .map(CommonCdiExtensions.class::cast) - .flatMap(e -> Arrays.stream(e.value())) - .map(CommonCdiExtension::value) - .collect(Collectors.toList()); - } - - private List> getFeatureBeans(Class testClass) { - - ArrayList> result = new ArrayList<>(Arrays.stream(testClass.getDeclaredAnnotations()) - .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) - .filter(a -> a instanceof CommonAddBean) - .map(CommonAddBean.class::cast) - .map(CommonAddBean::value) - .collect(Collectors.toList())); - - result.addAll(Arrays.stream(testClass.getDeclaredAnnotations()) - .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) - .filter(a -> a instanceof CommonAddBeans) - .map(CommonAddBeans.class::cast) - .flatMap(e -> Arrays.stream(e.value())) - .map(CommonAddBean::value) - .collect(Collectors.toList())); - return result; - } - private T[] getAnnotations(Class testClass, Class annotClass) { // inherited does not help, as it only returns annot from superclass if // child has none @@ -361,7 +328,7 @@ private void startContainer(List beanAnnotations, initializer.disableDiscovery(); } - initializer.addExtensions(new AddBeansExtension(testClass, beanAnnotations, getFeatureBeans(testClass))); + initializer.addExtensions(new AddBeansExtension(testClass, beanAnnotations)); for (AddExtension addExtension : extensionAnnotations) { Class extensionClass = addExtension.value(); @@ -516,14 +483,12 @@ private void callAfterStop() { private static class AddBeansExtension implements Extension { private final Class testClass; private final List addBeans; - private final List> featureBeans; private final HashMap socketAnnotations = new HashMap<>(); - private AddBeansExtension(Class testClass, List addBeans, List> featureBeans) { + private AddBeansExtension(Class testClass, List addBeans) { this.testClass = testClass; this.addBeans = addBeans; - this.featureBeans = featureBeans; } @@ -599,7 +564,8 @@ void registerAddedBeans(@Observes BeforeBeanDiscovery event) { } } - featureBeans.forEach(e -> event.addAnnotatedType(e, e.getName())); + // Add all Common Feature beans + getFeatureBeans(testClass).forEach(e -> event.addAnnotatedType(e, e.getName())); } private boolean hasBda(Class value) { diff --git a/microprofile/testing/junit5/src/main/java/module-info.java b/microprofile/testing/junit5/src/main/java/module-info.java index c831c5bc5c2..edd016b6c59 100644 --- a/microprofile/testing/junit5/src/main/java/module-info.java +++ b/microprofile/testing/junit5/src/main/java/module-info.java @@ -19,12 +19,12 @@ */ module io.helidon.microprofile.testing.junit5 { - requires io.helidon.microprofile.cdi; requires io.helidon.config.mp; requires io.helidon.config.yaml.mp; - requires org.junit.jupiter.api; - requires jakarta.inject; + requires io.helidon.microprofile.cdi; requires io.helidon.microprofile.testing.common; + requires jakarta.inject; + requires org.junit.jupiter.api; requires transitive jakarta.cdi; requires transitive jakarta.ws.rs; diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java index b62be5d48ae..39b236e648c 100644 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java @@ -33,7 +33,6 @@ import io.helidon.config.mp.MpConfigSources; import io.helidon.config.yaml.mp.YamlMpConfigSource; -import io.helidon.microprofile.testing.jaxrs.AddJaxRs; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; @@ -60,13 +59,15 @@ import org.eclipse.microprofile.config.spi.ConfigBuilder; import org.eclipse.microprofile.config.spi.ConfigProviderResolver; import org.eclipse.microprofile.config.spi.ConfigSource; -import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; import org.testng.IClassListener; import org.testng.ITestClass; import org.testng.ITestListener; import org.testng.ITestResult; import org.testng.annotations.Test; +import static io.helidon.microprofile.testing.common.CommonTestUtil.getFeatureBeans; +import static io.helidon.microprofile.testing.common.CommonTestUtil.getFeatureExtensions; + /** * TestNG extension to support Helidon CDI container in tests. */ @@ -128,13 +129,6 @@ public void onBeforeClass(ITestClass iTestClass) { } validatePerClass(); - // add beans when using JaxRS - AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); - if (addJaxRsAnnotation != null){ - classLevelExtensions.add(AddProcessAnnotatedTypesLiteral.INSTANCE); - classLevelBeans.add(AddWeldRequestScopeLiteral.INSTANCE); - } - configure(classLevelConfigMeta); if (!classLevelConfigMeta.useExisting) { @@ -257,30 +251,6 @@ private void validatePerClass() { } } } - - - AddJaxRs addJaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); - if (addJaxRsAnnotation != null){ - if (testClass.getAnnotation(DisableDiscovery.class) == null){ - throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation."); - } - - List> beans = classLevelBeans.stream().map(AddBean::value).toList(); - if (beans.contains(org.glassfish.jersey.weld.se.WeldRequestScope.class)) { - throw new RuntimeException("@AddJaxRs annotation already includes `WeldRequestScope` bean"); - } - - List> extensions = classLevelExtensions.stream().map(AddExtension::value).toList(); - if (!extensions.isEmpty()) { - if (extensions.contains(org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class)) { - throw new RuntimeException("@AddJaxRs annotation already includes `ProcessAllAnnotatedTypes` extension"); - } - if (extensions.contains(CdiComponentProvider.class)) { - throw new RuntimeException("@AddJaxRs annotation already includes `CdiComponentProvider` extension"); - } - } - - } } private boolean hasHelidonTestAnnotation(AnnotatedElement element) { @@ -392,6 +362,8 @@ private void startContainer(List beanAnnotations, } } + getFeatureExtensions(testClass).forEach(initializer::addExtensions); + container = initializer.initialize(); } @@ -481,6 +453,9 @@ void registerAddedBeans(@Observes BeforeBeanDiscovery event) { configurator.add(scope); } } + + // Add all Common Feature beans + getFeatureBeans(testClass).forEach(e -> event.addAnnotatedType(e, e.getName())); } private boolean hasBda(Class value) { diff --git a/microprofile/testing/testng/src/main/java/module-info.java b/microprofile/testing/testng/src/main/java/module-info.java index dd2bad119c4..faeb80bbcb1 100644 --- a/microprofile/testing/testng/src/main/java/module-info.java +++ b/microprofile/testing/testng/src/main/java/module-info.java @@ -23,6 +23,8 @@ requires io.helidon.config.mp; requires io.helidon.config.yaml.mp; + requires io.helidon.microprofile.testing.common; + requires io.helidon.microprofile.testing.jaxrs; requires jakarta.cdi; requires jakarta.inject; requires jakarta.ws.rs; @@ -30,7 +32,6 @@ requires jersey.weld2.se; requires microprofile.config.api; requires org.testng; - requires io.helidon.microprofile.testing.jaxrs; exports io.helidon.microprofile.testing.testng; diff --git a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java index 62ce05be34b..cbcc5049ec9 100644 --- a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java +++ b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java @@ -28,9 +28,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; @HelidonTest -@DisableDiscovery +//@DisableDiscovery -// JAX-RS Request scope @AddBean(TestReqScopeDisabledDiscovery.MyController.class) @AddJaxRs class TestReqScopeDisabledDiscovery { diff --git a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java index 89fb90285da..423a6c759cd 100644 --- a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java +++ b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java @@ -30,7 +30,6 @@ @HelidonTest @DisableDiscovery -// JAX-RS Request scope @AddJaxRs @AddBean(TestReqScopeDisabledDiscovery.MyController.class) class TestReqScopeDisabledDiscovery { From 38c50db446e9a1c66f282ebc652a0103010add6d Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Wed, 15 Nov 2023 15:52:15 +0200 Subject: [PATCH 06/28] Add validation support. First iteration. Signed-off-by: Dmitry Aleksandrov --- bom/pom.xml | 10 ++++ .../testing/common/JaxRsValidator.java | 46 +++++++++++++++++++ microprofile/testing/jaxrs/pom.xml | 2 - .../jaxrs/src/main/java/module-info.java | 2 +- microprofile/testing/junit5/pom.xml | 2 - .../testing/junit5/HelidonJunitExtension.java | 4 +- microprofile/testing/testng/pom.xml | 2 - .../testing/testng/HelidonTestNgListener.java | 3 ++ .../junit5/TestReqScopeDisabledDiscovery.java | 2 +- 9 files changed, 64 insertions(+), 9 deletions(-) create mode 100644 microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/JaxRsValidator.java diff --git a/bom/pom.xml b/bom/pom.xml index 949d95dbd10..8d019685f02 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -978,6 +978,16 @@ helidon-microprofile-testing-testng ${helidon.version} + + io.helidon.microprofile.testing + helidon-microprofile-testing-common + ${helidon.version} + + + io.helidon.microprofile.testing + helidon-microprofile-testing-jaxrs + ${helidon.version} + io.helidon.messaging.mock helidon-messaging-mock diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/JaxRsValidator.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/JaxRsValidator.java new file mode 100644 index 00000000000..9ccdeaec404 --- /dev/null +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/JaxRsValidator.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.microprofile.testing.common; + +import java.lang.annotation.Annotation; +import java.util.Arrays; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Validator for JAX-RS annotation usage. + */ +public class JaxRsValidator { + + private JaxRsValidator() { + } + + /** + * Perform validation. Runtime exception is thrown if something is wrong. + * @param testClass the annotated test class + */ + public static void validate(Class testClass){ + Set testClassAnnotations = Arrays.stream(testClass.getDeclaredAnnotations()) + .map(Annotation::annotationType) + .map(Class::getName) + .collect(Collectors.toSet()); + if (testClassAnnotations.stream().anyMatch(i -> i.contains("AddJaxRs")) + && testClassAnnotations.stream().noneMatch(i -> i.contains("DisableDiscovery"))){ + throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation."); + } + } +} diff --git a/microprofile/testing/jaxrs/pom.xml b/microprofile/testing/jaxrs/pom.xml index 78e8f873a02..56d1705bd3f 100644 --- a/microprofile/testing/jaxrs/pom.xml +++ b/microprofile/testing/jaxrs/pom.xml @@ -52,8 +52,6 @@ io.helidon.microprofile.testing helidon-microprofile-testing-common - 4.0.0-SNAPSHOT - compile diff --git a/microprofile/testing/jaxrs/src/main/java/module-info.java b/microprofile/testing/jaxrs/src/main/java/module-info.java index b4dadb03e5b..cf43d47e39c 100644 --- a/microprofile/testing/jaxrs/src/main/java/module-info.java +++ b/microprofile/testing/jaxrs/src/main/java/module-info.java @@ -26,11 +26,11 @@ requires io.helidon.microprofile.testing.common; requires jakarta.inject; requires jersey.cdi1x; + requires jersey.weld2.se; requires org.junit.jupiter.api; requires transitive jakarta.cdi; requires transitive jakarta.ws.rs; - requires jersey.weld2.se; exports io.helidon.microprofile.testing.jaxrs; diff --git a/microprofile/testing/junit5/pom.xml b/microprofile/testing/junit5/pom.xml index d525cca1431..fbb9a8fe411 100644 --- a/microprofile/testing/junit5/pom.xml +++ b/microprofile/testing/junit5/pom.xml @@ -64,8 +64,6 @@ io.helidon.microprofile.testing helidon-microprofile-testing-common - 4.0.0-SNAPSHOT - compile diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java index f2c9cbc6b1a..c38707cb61a 100644 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java @@ -33,6 +33,7 @@ import io.helidon.config.mp.MpConfigSources; import io.helidon.config.yaml.mp.YamlMpConfigSource; +import io.helidon.microprofile.testing.common.JaxRsValidator; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; @@ -134,11 +135,12 @@ public void beforeAll(ExtensionContext context) { if (resetPerTest) { validatePerTest(); - return; } validatePerClass(); + JaxRsValidator.validate(testClass); + configure(classLevelConfigMeta); if (!classLevelConfigMeta.useExisting) { diff --git a/microprofile/testing/testng/pom.xml b/microprofile/testing/testng/pom.xml index 0101ab77e13..9022071e36c 100644 --- a/microprofile/testing/testng/pom.xml +++ b/microprofile/testing/testng/pom.xml @@ -55,8 +55,6 @@ io.helidon.microprofile.testing helidon-microprofile-testing-jaxrs - 4.0.0-SNAPSHOT - compile diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java index 39b236e648c..8002af45fc4 100644 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java @@ -33,6 +33,7 @@ import io.helidon.config.mp.MpConfigSources; import io.helidon.config.yaml.mp.YamlMpConfigSource; +import io.helidon.microprofile.testing.common.JaxRsValidator; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; @@ -129,6 +130,8 @@ public void onBeforeClass(ITestClass iTestClass) { } validatePerClass(); + JaxRsValidator.validate(testClass); + configure(classLevelConfigMeta); if (!classLevelConfigMeta.useExisting) { diff --git a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java index cbcc5049ec9..e9237bdb87d 100644 --- a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java +++ b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestReqScopeDisabledDiscovery.java @@ -28,7 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; @HelidonTest -//@DisableDiscovery +@DisableDiscovery @AddBean(TestReqScopeDisabledDiscovery.MyController.class) @AddJaxRs From f6f00f503ec4489ec81642b5f00861bf36f3ae41 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Fri, 17 Nov 2023 17:54:09 +0200 Subject: [PATCH 07/28] extract validation Signed-off-by: Dmitry Aleksandrov --- bom/pom.xml | 5 +++ microprofile/testing/junit5/pom.xml | 4 +++ .../testing/junit5/HelidonJunitExtension.java | 4 +-- .../testing/junit5/JunitJaxRsValidator.java | 34 ++++++++++++++++++ .../junit5/src/main/java/module-info.java | 1 + microprofile/testing/pom.xml | 1 + microprofile/testing/testng/pom.xml | 4 +++ .../testing/testng/HelidonTestNgListener.java | 4 +-- .../testing/testng/TestNgJaxRsValidator.java | 34 ++++++++++++++++++ .../testng/src/main/java/module-info.java | 1 + microprofile/testing/validator/pom.xml | 36 +++++++++++++++++++ .../testing/validator}/JaxRsValidator.java | 21 ++++------- .../testing/validator/TestValidator.java | 28 +++++++++++++++ .../testing/validator/package-info.java | 20 +++++++++++ .../validator/src/main/java/module-info.java | 24 +++++++++++++ 15 files changed, 203 insertions(+), 18 deletions(-) create mode 100644 microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/JunitJaxRsValidator.java create mode 100644 microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/TestNgJaxRsValidator.java create mode 100644 microprofile/testing/validator/pom.xml rename microprofile/testing/{common/src/main/java/io/helidon/microprofile/testing/common => validator/src/main/java/io/helidon/microprofile/testing/validator}/JaxRsValidator.java (62%) create mode 100644 microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/TestValidator.java create mode 100644 microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/package-info.java create mode 100644 microprofile/testing/validator/src/main/java/module-info.java diff --git a/bom/pom.xml b/bom/pom.xml index 8d019685f02..f6fcf1a56db 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -983,6 +983,11 @@ helidon-microprofile-testing-common ${helidon.version} + + io.helidon.microprofile.testing + helidon-microprofile-testing-validator + ${helidon.version} + io.helidon.microprofile.testing helidon-microprofile-testing-jaxrs diff --git a/microprofile/testing/junit5/pom.xml b/microprofile/testing/junit5/pom.xml index fbb9a8fe411..9e6dfddc376 100644 --- a/microprofile/testing/junit5/pom.xml +++ b/microprofile/testing/junit5/pom.xml @@ -65,6 +65,10 @@ io.helidon.microprofile.testing helidon-microprofile-testing-common + + io.helidon.microprofile.testing + helidon-microprofile-testing-validator + diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java index c38707cb61a..3babdbb9340 100644 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/HelidonJunitExtension.java @@ -33,7 +33,6 @@ import io.helidon.config.mp.MpConfigSources; import io.helidon.config.yaml.mp.YamlMpConfigSource; -import io.helidon.microprofile.testing.common.JaxRsValidator; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; @@ -139,7 +138,8 @@ public void beforeAll(ExtensionContext context) { } validatePerClass(); - JaxRsValidator.validate(testClass); + JunitJaxRsValidator junitJaxRsValidator = new JunitJaxRsValidator(); + junitJaxRsValidator.validate(testClass); configure(classLevelConfigMeta); diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/JunitJaxRsValidator.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/JunitJaxRsValidator.java new file mode 100644 index 00000000000..fbd03ed35f6 --- /dev/null +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/JunitJaxRsValidator.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.microprofile.testing.junit5; + +import io.helidon.microprofile.testing.validator.JaxRsValidator; + +/** + * An implementation for JaxRs Validator for Junit. + */ +public class JunitJaxRsValidator extends JaxRsValidator { + + @Override + public void validate(Class testClass) throws RuntimeException { + if (checkAddJaxRs(testClass)){ + DisableDiscovery disableDiscovery = testClass.getAnnotation(DisableDiscovery.class); + if (disableDiscovery == null) { + throw new RuntimeException("@AddJaxRs annotation may only be used with @DisableDiscovery annotation"); + } + } + } +} diff --git a/microprofile/testing/junit5/src/main/java/module-info.java b/microprofile/testing/junit5/src/main/java/module-info.java index edd016b6c59..133ec17ebcf 100644 --- a/microprofile/testing/junit5/src/main/java/module-info.java +++ b/microprofile/testing/junit5/src/main/java/module-info.java @@ -23,6 +23,7 @@ requires io.helidon.config.yaml.mp; requires io.helidon.microprofile.cdi; requires io.helidon.microprofile.testing.common; + requires io.helidon.microprofile.testing.validator; requires jakarta.inject; requires org.junit.jupiter.api; diff --git a/microprofile/testing/pom.xml b/microprofile/testing/pom.xml index 9917f6c070f..158712afd7d 100644 --- a/microprofile/testing/pom.xml +++ b/microprofile/testing/pom.xml @@ -37,5 +37,6 @@ junit5 testng jaxrs + validator diff --git a/microprofile/testing/testng/pom.xml b/microprofile/testing/testng/pom.xml index 9022071e36c..8eb1b1ec50a 100644 --- a/microprofile/testing/testng/pom.xml +++ b/microprofile/testing/testng/pom.xml @@ -56,6 +56,10 @@ io.helidon.microprofile.testing helidon-microprofile-testing-jaxrs + + io.helidon.microprofile.testing + helidon-microprofile-testing-validator + \ No newline at end of file diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java index 8002af45fc4..ad8e9df0a1f 100644 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java @@ -33,7 +33,6 @@ import io.helidon.config.mp.MpConfigSources; import io.helidon.config.yaml.mp.YamlMpConfigSource; -import io.helidon.microprofile.testing.common.JaxRsValidator; import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.Dependent; @@ -130,7 +129,8 @@ public void onBeforeClass(ITestClass iTestClass) { } validatePerClass(); - JaxRsValidator.validate(testClass); + TestNgJaxRsValidator testNgJaxRsValidator = new TestNgJaxRsValidator(); + testNgJaxRsValidator.validate(testClass); configure(classLevelConfigMeta); diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/TestNgJaxRsValidator.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/TestNgJaxRsValidator.java new file mode 100644 index 00000000000..b93045ea26f --- /dev/null +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/TestNgJaxRsValidator.java @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.microprofile.testing.testng; + +import io.helidon.microprofile.testing.validator.JaxRsValidator; + +/** + * An implementation for JaxRs Validator for Junit. + */ +public class TestNgJaxRsValidator extends JaxRsValidator { + + @Override + public void validate(Class testClass) throws RuntimeException { + if (checkAddJaxRs(testClass)){ + DisableDiscovery disableDiscovery = testClass.getAnnotation(DisableDiscovery.class); + if (disableDiscovery == null) { + throw new RuntimeException("@AddJaxRs annotation may only be used with @DisableDiscovery annotation"); + } + } + } +} diff --git a/microprofile/testing/testng/src/main/java/module-info.java b/microprofile/testing/testng/src/main/java/module-info.java index faeb80bbcb1..5e70711f0d1 100644 --- a/microprofile/testing/testng/src/main/java/module-info.java +++ b/microprofile/testing/testng/src/main/java/module-info.java @@ -25,6 +25,7 @@ requires io.helidon.config.yaml.mp; requires io.helidon.microprofile.testing.common; requires io.helidon.microprofile.testing.jaxrs; + requires io.helidon.microprofile.testing.validator; requires jakarta.cdi; requires jakarta.inject; requires jakarta.ws.rs; diff --git a/microprofile/testing/validator/pom.xml b/microprofile/testing/validator/pom.xml new file mode 100644 index 00000000000..3fd34a68ecb --- /dev/null +++ b/microprofile/testing/validator/pom.xml @@ -0,0 +1,36 @@ + + + + + 4.0.0 + + io.helidon.microprofile.testing + helidon-microprofile-testing-project + 4.0.0-SNAPSHOT + ../pom.xml + + + helidon-microprofile-testing-validator + Helidon Microprofile Testing JUnit5 + + + Common validation for Helidon Testing + + + diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/JaxRsValidator.java b/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/JaxRsValidator.java similarity index 62% rename from microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/JaxRsValidator.java rename to microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/JaxRsValidator.java index 9ccdeaec404..12d2e9265ee 100644 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/JaxRsValidator.java +++ b/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/JaxRsValidator.java @@ -14,33 +14,26 @@ * limitations under the License. */ -package io.helidon.microprofile.testing.common; +package io.helidon.microprofile.testing.validator; + import java.lang.annotation.Annotation; import java.util.Arrays; import java.util.Set; import java.util.stream.Collectors; + /** * Validator for JAX-RS annotation usage. */ -public class JaxRsValidator { - - private JaxRsValidator() { - } +public abstract class JaxRsValidator implements TestValidator { - /** - * Perform validation. Runtime exception is thrown if something is wrong. - * @param testClass the annotated test class - */ - public static void validate(Class testClass){ + protected boolean checkAddJaxRs(Class testClass){ Set testClassAnnotations = Arrays.stream(testClass.getDeclaredAnnotations()) .map(Annotation::annotationType) .map(Class::getName) .collect(Collectors.toSet()); - if (testClassAnnotations.stream().anyMatch(i -> i.contains("AddJaxRs")) - && testClassAnnotations.stream().noneMatch(i -> i.contains("DisableDiscovery"))){ - throw new RuntimeException("@AddJaxRs annotation should be used only with @DisableDiscovery annotation."); - } + //FIXME:THIS IS VERY BAD! + return testClassAnnotations.stream().anyMatch(i -> i.contains("AddJaxRs")); } } diff --git a/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/TestValidator.java b/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/TestValidator.java new file mode 100644 index 00000000000..3f667b1c5d4 --- /dev/null +++ b/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/TestValidator.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.microprofile.testing.validator; + +/** + * Validator for test class. + */ +public interface TestValidator { + + /** + * Perform Validation. + * @param testClass for verification. + */ + void validate(Class testClass) throws RuntimeException; +} diff --git a/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/package-info.java b/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/package-info.java new file mode 100644 index 00000000000..e9ad98ded1d --- /dev/null +++ b/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/package-info.java @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Common Validation for Helidon Testing. + */ +package io.helidon.microprofile.testing.validator; diff --git a/microprofile/testing/validator/src/main/java/module-info.java b/microprofile/testing/validator/src/main/java/module-info.java new file mode 100644 index 00000000000..ea7a8672970 --- /dev/null +++ b/microprofile/testing/validator/src/main/java/module-info.java @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Tests validators. + */ +module io.helidon.microprofile.testing.validator { + + exports io.helidon.microprofile.testing.validator; + +} From e319e70cf6fee60864341c992927167ed7b0e367 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Mon, 20 Nov 2023 14:37:12 +0200 Subject: [PATCH 08/28] Move validation back to common Signed-off-by: Dmitry Aleksandrov --- bom/pom.xml | 5 --- .../testing/common}/JaxRsValidator.java | 3 +- .../testing/common}/TestValidator.java | 2 +- microprofile/testing/junit5/pom.xml | 4 --- .../testing/junit5/JunitJaxRsValidator.java | 2 +- .../junit5/src/main/java/module-info.java | 1 - microprofile/testing/pom.xml | 1 - microprofile/testing/testng/pom.xml | 4 --- .../testing/testng/TestNgJaxRsValidator.java | 2 +- .../testng/src/main/java/module-info.java | 1 - microprofile/testing/validator/pom.xml | 36 ------------------- .../testing/validator/package-info.java | 20 ----------- .../validator/src/main/java/module-info.java | 24 ------------- 13 files changed, 4 insertions(+), 101 deletions(-) rename microprofile/testing/{validator/src/main/java/io/helidon/microprofile/testing/validator => common/src/main/java/io/helidon/microprofile/testing/common}/JaxRsValidator.java (93%) rename microprofile/testing/{validator/src/main/java/io/helidon/microprofile/testing/validator => common/src/main/java/io/helidon/microprofile/testing/common}/TestValidator.java (94%) delete mode 100644 microprofile/testing/validator/pom.xml delete mode 100644 microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/package-info.java delete mode 100644 microprofile/testing/validator/src/main/java/module-info.java diff --git a/bom/pom.xml b/bom/pom.xml index f6fcf1a56db..8d019685f02 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -983,11 +983,6 @@ helidon-microprofile-testing-common ${helidon.version} - - io.helidon.microprofile.testing - helidon-microprofile-testing-validator - ${helidon.version} - io.helidon.microprofile.testing helidon-microprofile-testing-jaxrs diff --git a/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/JaxRsValidator.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/JaxRsValidator.java similarity index 93% rename from microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/JaxRsValidator.java rename to microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/JaxRsValidator.java index 12d2e9265ee..8b53eac4522 100644 --- a/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/JaxRsValidator.java +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/JaxRsValidator.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.helidon.microprofile.testing.validator; +package io.helidon.microprofile.testing.common; import java.lang.annotation.Annotation; @@ -33,7 +33,6 @@ protected boolean checkAddJaxRs(Class testClass){ .map(Annotation::annotationType) .map(Class::getName) .collect(Collectors.toSet()); - //FIXME:THIS IS VERY BAD! return testClassAnnotations.stream().anyMatch(i -> i.contains("AddJaxRs")); } } diff --git a/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/TestValidator.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/TestValidator.java similarity index 94% rename from microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/TestValidator.java rename to microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/TestValidator.java index 3f667b1c5d4..31a7863b38c 100644 --- a/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/TestValidator.java +++ b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/TestValidator.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.helidon.microprofile.testing.validator; +package io.helidon.microprofile.testing.common; /** * Validator for test class. diff --git a/microprofile/testing/junit5/pom.xml b/microprofile/testing/junit5/pom.xml index 9e6dfddc376..fbb9a8fe411 100644 --- a/microprofile/testing/junit5/pom.xml +++ b/microprofile/testing/junit5/pom.xml @@ -65,10 +65,6 @@ io.helidon.microprofile.testing helidon-microprofile-testing-common - - io.helidon.microprofile.testing - helidon-microprofile-testing-validator - diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/JunitJaxRsValidator.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/JunitJaxRsValidator.java index fbd03ed35f6..4608cc7f31b 100644 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/JunitJaxRsValidator.java +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/JunitJaxRsValidator.java @@ -15,7 +15,7 @@ */ package io.helidon.microprofile.testing.junit5; -import io.helidon.microprofile.testing.validator.JaxRsValidator; +import io.helidon.microprofile.testing.common.JaxRsValidator; /** * An implementation for JaxRs Validator for Junit. diff --git a/microprofile/testing/junit5/src/main/java/module-info.java b/microprofile/testing/junit5/src/main/java/module-info.java index 133ec17ebcf..edd016b6c59 100644 --- a/microprofile/testing/junit5/src/main/java/module-info.java +++ b/microprofile/testing/junit5/src/main/java/module-info.java @@ -23,7 +23,6 @@ requires io.helidon.config.yaml.mp; requires io.helidon.microprofile.cdi; requires io.helidon.microprofile.testing.common; - requires io.helidon.microprofile.testing.validator; requires jakarta.inject; requires org.junit.jupiter.api; diff --git a/microprofile/testing/pom.xml b/microprofile/testing/pom.xml index 158712afd7d..9917f6c070f 100644 --- a/microprofile/testing/pom.xml +++ b/microprofile/testing/pom.xml @@ -37,6 +37,5 @@ junit5 testng jaxrs - validator diff --git a/microprofile/testing/testng/pom.xml b/microprofile/testing/testng/pom.xml index 8eb1b1ec50a..9022071e36c 100644 --- a/microprofile/testing/testng/pom.xml +++ b/microprofile/testing/testng/pom.xml @@ -56,10 +56,6 @@ io.helidon.microprofile.testing helidon-microprofile-testing-jaxrs - - io.helidon.microprofile.testing - helidon-microprofile-testing-validator - \ No newline at end of file diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/TestNgJaxRsValidator.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/TestNgJaxRsValidator.java index b93045ea26f..8ee2d5a79ad 100644 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/TestNgJaxRsValidator.java +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/TestNgJaxRsValidator.java @@ -15,7 +15,7 @@ */ package io.helidon.microprofile.testing.testng; -import io.helidon.microprofile.testing.validator.JaxRsValidator; +import io.helidon.microprofile.testing.common.JaxRsValidator; /** * An implementation for JaxRs Validator for Junit. diff --git a/microprofile/testing/testng/src/main/java/module-info.java b/microprofile/testing/testng/src/main/java/module-info.java index 5e70711f0d1..faeb80bbcb1 100644 --- a/microprofile/testing/testng/src/main/java/module-info.java +++ b/microprofile/testing/testng/src/main/java/module-info.java @@ -25,7 +25,6 @@ requires io.helidon.config.yaml.mp; requires io.helidon.microprofile.testing.common; requires io.helidon.microprofile.testing.jaxrs; - requires io.helidon.microprofile.testing.validator; requires jakarta.cdi; requires jakarta.inject; requires jakarta.ws.rs; diff --git a/microprofile/testing/validator/pom.xml b/microprofile/testing/validator/pom.xml deleted file mode 100644 index 3fd34a68ecb..00000000000 --- a/microprofile/testing/validator/pom.xml +++ /dev/null @@ -1,36 +0,0 @@ - - - - - 4.0.0 - - io.helidon.microprofile.testing - helidon-microprofile-testing-project - 4.0.0-SNAPSHOT - ../pom.xml - - - helidon-microprofile-testing-validator - Helidon Microprofile Testing JUnit5 - - - Common validation for Helidon Testing - - - diff --git a/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/package-info.java b/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/package-info.java deleted file mode 100644 index e9ad98ded1d..00000000000 --- a/microprofile/testing/validator/src/main/java/io/helidon/microprofile/testing/validator/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Common Validation for Helidon Testing. - */ -package io.helidon.microprofile.testing.validator; diff --git a/microprofile/testing/validator/src/main/java/module-info.java b/microprofile/testing/validator/src/main/java/module-info.java deleted file mode 100644 index ea7a8672970..00000000000 --- a/microprofile/testing/validator/src/main/java/module-info.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Tests validators. - */ -module io.helidon.microprofile.testing.validator { - - exports io.helidon.microprofile.testing.validator; - -} From c9036484b5f831f36e03b60710f5750602d18230 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Mon, 20 Nov 2023 17:18:50 +0200 Subject: [PATCH 09/28] Naming cleanup Signed-off-by: Dmitry Aleksandrov --- microprofile/testing/common/pom.xml | 6 +++--- microprofile/testing/common/src/main/java/module-info.java | 4 ++-- microprofile/testing/jaxrs/pom.xml | 4 ++-- microprofile/testing/jaxrs/src/main/java/module-info.java | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/microprofile/testing/common/pom.xml b/microprofile/testing/common/pom.xml index 6aaeac9b503..ee6da1b2755 100644 --- a/microprofile/testing/common/pom.xml +++ b/microprofile/testing/common/pom.xml @@ -1,6 +1,6 @@ - - - 4.0.0 - - io.helidon.microprofile.testing - helidon-microprofile-testing-project - 4.0.0-SNAPSHOT - ../pom.xml - - - helidon-microprofile-testing-common - Common testing support - - - Common testing support - - - - - jakarta.enterprise - jakarta.enterprise.cdi-api - - - - diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBean.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBean.java deleted file mode 100644 index 6485fc10da5..00000000000 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBean.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.helidon.microprofile.testing.common; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Repeatable; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -/** - * Add a bean to other annotations. - * The class will be instantiated using CDI and will be available for injection into test classes and other beans. - * This annotation can be repeated. - */ -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE, ElementType.METHOD}) -@Repeatable(CommonAddBeans.class) -public @interface CommonAddBean { - - /** - * Class of the bean. - * @return the class of a bean - */ - Class value(); -} diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtension.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtension.java deleted file mode 100644 index 71b641690f3..00000000000 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtension.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.helidon.microprofile.testing.common; -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Repeatable; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import jakarta.enterprise.inject.spi.Extension; - - -/** - * Common CDI Extension. - */ -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE}) -@Repeatable(CommonCdiExtensions.class) -@Inherited -public @interface CommonCdiExtension { - - /** - * CDI Extension. - * - * @return The CDI Extension Class. - */ - Class value(); -} diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonTestUtil.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonTestUtil.java deleted file mode 100644 index 430558e51fd..00000000000 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonTestUtil.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.helidon.microprofile.testing.common; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -import jakarta.enterprise.inject.spi.Extension; - -/** - * Common Utility class for tests. - */ -public class CommonTestUtil { - - private CommonTestUtil() { - } - - /** - * Extract all Extensions from the given testClass. - * - * @param testClass Class - * @return List with Extension classes - */ - public static List> getFeatureExtensions(Class testClass) { - - List> result = Arrays.stream(testClass.getDeclaredAnnotations()) - .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) - .filter(a -> a instanceof CommonCdiExtension) - .map(CommonCdiExtension.class::cast) - .map(CommonCdiExtension::value) - .collect(Collectors.toList()); - - result.addAll(Arrays.stream(testClass.getDeclaredAnnotations()) - .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) - .filter(a -> a instanceof CommonCdiExtensions) - .map(CommonCdiExtensions.class::cast) - .flatMap(e -> Arrays.stream(e.value())) - .map(CommonCdiExtension::value) - .collect(Collectors.toList())); - - return result; - } - - /** - * Extract all Beans from the given testClass. - * - * @param testClass Class - * @return List with Beans classes - */ - public static List> getFeatureBeans(Class testClass) { - - ArrayList> result = Arrays.stream(testClass.getDeclaredAnnotations()) - .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) - .filter(a -> a instanceof CommonAddBean) - .map(CommonAddBean.class::cast) - .map(CommonAddBean::value).collect(Collectors.toCollection(ArrayList::new)); - - result.addAll(Arrays.stream(testClass.getDeclaredAnnotations()) - .flatMap(a -> Arrays.stream(a.annotationType().getDeclaredAnnotations())) - .filter(a -> a instanceof CommonAddBeans) - .map(CommonAddBeans.class::cast) - .flatMap(e -> Arrays.stream(e.value())) - .map(CommonAddBean::value) - .collect(Collectors.toList())); - return result; - } -} diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/TestValidator.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/TestValidator.java deleted file mode 100644 index 92d950f8f9c..00000000000 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/TestValidator.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.helidon.microprofile.testing.common; - -/** - * Validator for test class. - */ -public interface TestValidator { - - /** - * Perform Validation. - * @param testClass for verification. - */ - void validate(Class testClass); -} diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/package-info.java b/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/package-info.java deleted file mode 100644 index 77f71b19fbe..00000000000 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Common Testing Extensions. - */ -package io.helidon.microprofile.testing.common; diff --git a/microprofile/testing/common/src/main/java/module-info.java b/microprofile/testing/common/src/main/java/module-info.java deleted file mode 100644 index 4757100d1be..00000000000 --- a/microprofile/testing/common/src/main/java/module-info.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Helidon common testing support. - */ -module io.helidon.microprofile.testing.common { - - requires jakarta.inject; - requires transitive jakarta.cdi; - - exports io.helidon.microprofile.testing.common; - -} diff --git a/microprofile/testing/jaxrs/pom.xml b/microprofile/testing/jaxrs/pom.xml deleted file mode 100644 index 5d067da9743..00000000000 --- a/microprofile/testing/jaxrs/pom.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - - - 4.0.0 - - io.helidon.microprofile.testing - helidon-microprofile-testing-project - 4.0.0-SNAPSHOT - ../pom.xml - - - helidon-microprofile-testing-jaxrs - Helidon Microprofile Testing JAX-RS support - - - Helidon Microprofile Testing JAX-RS support - - - - - io.helidon.microprofile.cdi - helidon-microprofile-cdi - provided - - - io.helidon.microprofile.server - helidon-microprofile-server - provided - - - org.junit.jupiter - junit-jupiter-api - provided - - - io.helidon.microprofile.testing - helidon-microprofile-testing-common - - - - diff --git a/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/AddJaxRs.java b/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/AddJaxRs.java deleted file mode 100644 index 41df5cf5fc4..00000000000 --- a/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/AddJaxRs.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.helidon.microprofile.testing.jaxrs; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -import io.helidon.microprofile.server.JaxRsCdiExtension; -import io.helidon.microprofile.server.ServerCdiExtension; -import io.helidon.microprofile.testing.common.CommonAddBean; -import io.helidon.microprofile.testing.common.CommonCdiExtension; - -import org.glassfish.jersey.ext.cdi1x.internal.CdiComponentProvider; - -/** - * JAX_RS Testing annotation. - */ -@Retention(RetentionPolicy.RUNTIME) -@Target({ElementType.TYPE}) -@Inherited -@CommonCdiExtension(ServerCdiExtension.class) -@CommonCdiExtension(JaxRsCdiExtension.class) -@CommonCdiExtension(CdiComponentProvider.class) -@CommonCdiExtension(org.glassfish.jersey.ext.cdi1x.internal.ProcessAllAnnotatedTypes.class) -@CommonAddBean(org.glassfish.jersey.weld.se.WeldRequestScope.class) -public @interface AddJaxRs { -} diff --git a/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/package-info.java b/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/package-info.java deleted file mode 100644 index 3a80e8e726e..00000000000 --- a/microprofile/testing/jaxrs/src/main/java/io/helidon/microprofile/testing/jaxrs/package-info.java +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * JAX-RS Extension for Testing. - */ -package io.helidon.microprofile.testing.jaxrs; diff --git a/microprofile/testing/jaxrs/src/main/java/module-info.java b/microprofile/testing/jaxrs/src/main/java/module-info.java deleted file mode 100644 index 4d4347b62dd..00000000000 --- a/microprofile/testing/jaxrs/src/main/java/module-info.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * Helidon Testing JAX-RS support. - */ -module io.helidon.microprofile.testing.jaxrs { - - requires io.helidon.config.mp; - requires io.helidon.config.yaml.mp; - requires io.helidon.microprofile.cdi; - requires io.helidon.microprofile.server; - - requires jakarta.inject; - requires jersey.cdi1x; - requires jersey.weld2.se; - requires org.junit.jupiter.api; - - requires transitive io.helidon.microprofile.testing.common; - requires transitive jakarta.cdi; - requires transitive jakarta.ws.rs; - - exports io.helidon.microprofile.testing.jaxrs; - -} diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtensions.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java similarity index 74% rename from microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtensions.java rename to microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java index 28890f40265..1f776358f0d 100644 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonCdiExtensions.java +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddJaxRs.java @@ -13,27 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package io.helidon.microprofile.testing.junit5; -package io.helidon.microprofile.testing.common; import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; - /** - * Common CDI Extensions for testing. + * Add JaxRS support for Request-scoped beans. */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) -@Inherited -public @interface CommonCdiExtensions { - - /** - * Return CDI Extension. - * - * @return CDIExtension[] - */ - CommonCdiExtension[] value(); +public @interface AddJaxRs { } diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/JunitJaxRsValidator.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/JunitJaxRsValidator.java deleted file mode 100644 index fd0e898e2b0..00000000000 --- a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/JunitJaxRsValidator.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.helidon.microprofile.testing.junit5; - -import io.helidon.microprofile.testing.common.TestValidator; -import io.helidon.microprofile.testing.jaxrs.AddJaxRs; - -/** - * An implementation for JaxRs Validator for Junit. - */ -public class JunitJaxRsValidator implements TestValidator { - - @Override - public void validate(Class testClass) throws RuntimeException { - AddJaxRs jaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); - if (jaxRsAnnotation != null){ - DisableDiscovery disableDiscovery = testClass.getAnnotation(DisableDiscovery.class); - if (disableDiscovery == null) { - throw new RuntimeException("@AddJaxRs annotation may only be used with @DisableDiscovery annotation"); - } - } - } -} diff --git a/microprofile/testing/pom.xml b/microprofile/testing/pom.xml index 9917f6c070f..4d5e9897d0f 100644 --- a/microprofile/testing/pom.xml +++ b/microprofile/testing/pom.xml @@ -33,9 +33,7 @@ Helidon Microprofile Testing Project - common junit5 testng - jaxrs diff --git a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBeans.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java similarity index 72% rename from microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBeans.java rename to microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java index 1b304d09dfc..250d2fb192e 100644 --- a/microprofile/testing/common/src/main/java/io/helidon/microprofile/testing/common/CommonAddBeans.java +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddJaxRs.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.helidon.microprofile.testing.common; +package io.helidon.microprofile.testing.testng; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -21,15 +21,9 @@ import java.lang.annotation.Target; /** - * A repeatable container for {@link CommonAddBean}. - * No need to use this annotation, just repeat {@link CommonAddBean} annotation. + * Add JaxRS support for Request-scoped beans. */ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE}) -public @interface CommonAddBeans { - /** - * Beans to be added. - * @return add bean annotations - */ - CommonAddBean[] value(); +public @interface AddJaxRs { } diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java index e43a0ad47ac..e2ddb7f3fc8 100644 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/HelidonTestNgListener.java @@ -74,7 +74,7 @@ public class HelidonTestNgListener implements IClassListener, ITestListener { private static final Set> HELIDON_TEST_ANNOTATIONS = - Set.of(AddBean.class, AddConfig.class, AddExtension.class, Configuration.class); + Set.of(AddBean.class, AddConfig.class, AddExtension.class, Configuration.class, AddJaxRs.class); private static final Map, Annotation> BEAN_DEFINING = new HashMap<>(); private static final List YAML_SUFFIXES = List.of(".yml", ".yaml"); diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/TestNgJaxRsValidator.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/TestNgJaxRsValidator.java deleted file mode 100644 index 91c4aba6555..00000000000 --- a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/TestNgJaxRsValidator.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2023 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.helidon.microprofile.testing.testng; - -import io.helidon.microprofile.testing.common.TestValidator; -import io.helidon.microprofile.testing.jaxrs.AddJaxRs; - -/** - * An implementation for JaxRs Validator for Junit. - */ -public class TestNgJaxRsValidator implements TestValidator { - - @Override - public void validate(Class testClass) throws RuntimeException { - AddJaxRs jaxRsAnnotation = testClass.getAnnotation(AddJaxRs.class); - if (jaxRsAnnotation != null){ - DisableDiscovery disableDiscovery = testClass.getAnnotation(DisableDiscovery.class); - if (disableDiscovery == null) { - throw new RuntimeException("@AddJaxRs annotation may only be used with @DisableDiscovery annotation"); - } - } - } -} From 67362f93a86eda42c3ea60e58f1e2ee6a9acdfd1 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Thu, 30 Nov 2023 13:57:00 +0200 Subject: [PATCH 23/28] Mino clean up Signed-off-by: Dmitry Aleksandrov --- bom/pom.xml | 10 ---------- microprofile/testing/junit5/pom.xml | 9 +++++---- .../testing/junit5/src/main/java/module-info.java | 4 ++-- microprofile/testing/testng/pom.xml | 12 +++++++----- .../testing/testng/src/main/java/module-info.java | 4 ++-- 5 files changed, 16 insertions(+), 23 deletions(-) diff --git a/bom/pom.xml b/bom/pom.xml index 8d019685f02..949d95dbd10 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -978,16 +978,6 @@ helidon-microprofile-testing-testng ${helidon.version} - - io.helidon.microprofile.testing - helidon-microprofile-testing-common - ${helidon.version} - - - io.helidon.microprofile.testing - helidon-microprofile-testing-jaxrs - ${helidon.version} - io.helidon.messaging.mock helidon-messaging-mock diff --git a/microprofile/testing/junit5/pom.xml b/microprofile/testing/junit5/pom.xml index 02a4d3380aa..ad4b10cb685 100644 --- a/microprofile/testing/junit5/pom.xml +++ b/microprofile/testing/junit5/pom.xml @@ -40,13 +40,14 @@ true - org.junit.jupiter - junit-jupiter-api + org.glassfish.jersey.ext.cdi + jersey-weld2-se + true provided - org.glassfish.jersey.ext.cdi - jersey-weld2-se + org.junit.jupiter + junit-jupiter-api provided diff --git a/microprofile/testing/junit5/src/main/java/module-info.java b/microprofile/testing/junit5/src/main/java/module-info.java index 278c921b5b9..5e1730bdec3 100644 --- a/microprofile/testing/junit5/src/main/java/module-info.java +++ b/microprofile/testing/junit5/src/main/java/module-info.java @@ -22,14 +22,14 @@ requires io.helidon.config.mp; requires io.helidon.config.yaml.mp; requires jakarta.inject; - requires jersey.cdi1x; - requires jersey.weld2.se; requires org.junit.jupiter.api; requires transitive jakarta.cdi; requires transitive jakarta.ws.rs; requires static io.helidon.microprofile.server; + requires static jersey.cdi1x; + requires static jersey.weld2.se; exports io.helidon.microprofile.testing.junit5; diff --git a/microprofile/testing/testng/pom.xml b/microprofile/testing/testng/pom.xml index 85651612b1b..0105b52951d 100644 --- a/microprofile/testing/testng/pom.xml +++ b/microprofile/testing/testng/pom.xml @@ -39,6 +39,12 @@ helidon-microprofile-server true + + org.glassfish.jersey.ext.cdi + jersey-weld2-se + true + provided + io.helidon.jersey helidon-jersey-client @@ -47,11 +53,7 @@ io.helidon.config helidon-config-yaml-mp - - org.glassfish.jersey.ext.cdi - jersey-weld2-se - provided - + org.testng testng diff --git a/microprofile/testing/testng/src/main/java/module-info.java b/microprofile/testing/testng/src/main/java/module-info.java index 40265d2d3a2..40aa0c9b47a 100644 --- a/microprofile/testing/testng/src/main/java/module-info.java +++ b/microprofile/testing/testng/src/main/java/module-info.java @@ -26,12 +26,12 @@ requires jakarta.cdi; requires jakarta.inject; requires jakarta.ws.rs; - requires jersey.cdi1x; - requires jersey.weld2.se; requires microprofile.config.api; requires org.testng; requires static io.helidon.microprofile.server; + requires static jersey.cdi1x; + requires static jersey.weld2.se; exports io.helidon.microprofile.testing.testng; From 8484dc3cacefc3e6082855ef222802cfec3769fa Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Fri, 1 Dec 2023 18:23:32 +0200 Subject: [PATCH 24/28] Clean dependency tree Signed-off-by: Dmitry Aleksandrov --- microprofile/config/pom.xml | 16 +-- .../config/ConfigBeanDescriptorTest.java | 114 ----------------- microprofile/server/pom.xml | 5 - .../microprofile/server/JaxRsService.java | 5 +- microprofile/tests/config/pom.xml | 70 +++++++++++ .../microprofile/config/Converters.java | 0 .../config/MpConfigConvertTest.java | 0 .../config/MpConfigInjectionTest.java | 0 .../microprofile/config/MutableMpTest.java | 0 .../config/ObjectMappingTest.java | 115 ++++++++++++++++++ .../META-INF/microprofile-config.properties | 22 ++++ .../src/test/resources/application.yaml | 18 +++ microprofile/tests/pom.xml | 1 + .../tests}/server/AsyncResourceTest.java | 2 +- .../tests}/server/JerseyPropertiesTest.java | 7 +- .../tests}/server/ProducedRouteTest.java | 8 +- .../tests}/server/RedirectionTest.java | 4 +- 17 files changed, 245 insertions(+), 142 deletions(-) delete mode 100644 microprofile/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java create mode 100644 microprofile/tests/config/pom.xml rename microprofile/{ => tests}/config/src/test/java/io/helidon/microprofile/config/Converters.java (100%) rename microprofile/{ => tests}/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java (100%) rename microprofile/{ => tests}/config/src/test/java/io/helidon/microprofile/config/MpConfigInjectionTest.java (100%) rename microprofile/{ => tests}/config/src/test/java/io/helidon/microprofile/config/MutableMpTest.java (100%) create mode 100644 microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ObjectMappingTest.java create mode 100644 microprofile/tests/config/src/test/resources/META-INF/microprofile-config.properties create mode 100644 microprofile/tests/config/src/test/resources/application.yaml rename microprofile/{server/src/test/java/io/helidon/microprofile => tests/server/src/test/java/io/helidon/microprofile/tests}/server/AsyncResourceTest.java (98%) rename microprofile/{server/src/test/java/io/helidon/microprofile => tests/server/src/test/java/io/helidon/microprofile/tests}/server/JerseyPropertiesTest.java (86%) rename microprofile/{server/src/test/java/io/helidon/microprofile => tests/server/src/test/java/io/helidon/microprofile/tests}/server/ProducedRouteTest.java (95%) rename microprofile/{server/src/test/java/io/helidon/microprofile => tests/server/src/test/java/io/helidon/microprofile/tests}/server/RedirectionTest.java (94%) diff --git a/microprofile/config/pom.xml b/microprofile/config/pom.xml index ac030952e53..c11d4c4cd16 100644 --- a/microprofile/config/pom.xml +++ b/microprofile/config/pom.xml @@ -74,31 +74,17 @@ jakarta.inject-api provided - - io.helidon.microprofile.cdi - helidon-microprofile-cdi - test - org.junit.jupiter junit-jupiter-api test - - org.mockito - mockito-core - test - + org.hamcrest hamcrest-all test - - io.helidon.microprofile.testing - helidon-microprofile-testing-junit5 - test - diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java b/microprofile/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java deleted file mode 100644 index f90f6440948..00000000000 --- a/microprofile/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java +++ /dev/null @@ -1,114 +0,0 @@ -/* - * Copyright (c) 2022 Oracle and/or its affiliates. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package io.helidon.microprofile.config; - -import java.lang.reflect.Field; -import java.lang.reflect.Type; -import java.util.Map; - -import io.helidon.config.mp.MpConfigSources; -import io.helidon.microprofile.config.testsubjects.TestConfiguredBean; - -import jakarta.enterprise.inject.spi.AnnotatedType; -import jakarta.enterprise.inject.spi.InjectionPoint; -import org.eclipse.microprofile.config.Config; -import org.eclipse.microprofile.config.inject.ConfigProperties; -import org.eclipse.microprofile.config.spi.ConfigProviderResolver; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; - -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.collection.IsIterableContainingInOrder.contains; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -/** - * Lower level testing for {@link io.helidon.microprofile.config.ConfigBeanDescriptor}. - */ -class ConfigBeanDescriptorTest { - - private static MutableMpTest.MutableSource source; - private static Config config; - private static io.helidon.config.Config emptyConfig; - - @BeforeAll - static void initClass() { - config = createTestConfiguredBeanConfig(); - - // we need to ensure empty config is initialized before running other tests, - // as this messes up the mapping service counter - emptyConfig = io.helidon.config.Config.empty(); - - source = new MutableMpTest.MutableSource("initial"); - - // register config - ConfigProviderResolver configProvider = ConfigProviderResolver.instance(); - configProvider.registerConfig(config, Thread.currentThread().getContextClassLoader()); - } - - @Test - @SuppressWarnings("unchecked") - void testProduceListFields() throws Exception { - // setup for the test - AnnotatedType annotatedType = mock(AnnotatedType.class); - when(annotatedType.getJavaClass()).thenReturn((Class) TestConfiguredBean.class); - - Field strListField = TestConfiguredBean.class.getField("strList"); - Type strListType = strListField.getGenericType(); - Field intListField = TestConfiguredBean.class.getField("intList"); - Type intListType = intListField.getGenericType(); - - ConfigProperties configProperties = TestConfiguredBean.class.getAnnotation(ConfigProperties.class); - ConfigBeanDescriptor descriptor = ConfigBeanDescriptor.create(annotatedType, configProperties); - - InjectionPoint strListInjectionPoint = mock(InjectionPoint.class); - when(strListInjectionPoint.getType()).thenReturn(strListType); - - InjectionPoint intListInjectionPoint = mock(InjectionPoint.class); - when(intListInjectionPoint.getType()).thenReturn(intListType); - - // the target of the test starts here: - Object instance = descriptor.produce(strListInjectionPoint, config); - assertThat(((TestConfiguredBean) instance).strList, contains("a", "b", "c")); - - instance = descriptor.produce(intListInjectionPoint, config); - assertThat(((TestConfiguredBean) instance).intList, contains(1, 2, 3)); - } - - @Test - @SuppressWarnings("unchecked") - void testProduceGenericTypesNotSupported() throws Exception { - AnnotatedType annotatedType = mock(AnnotatedType.class); - when(annotatedType.getJavaClass()).thenReturn((Class) TestConfiguredBean.Unsupported.class); - ConfigProperties configProperties = TestConfiguredBean.class.getAnnotation(ConfigProperties.class); - UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, - () -> ConfigBeanDescriptor.create(annotatedType, configProperties)); - assertThat(e.getMessage(), is("No idea how to handle ?")); - } - - static Config createTestConfiguredBeanConfig() { - return ConfigProviderResolver.instance() - .getBuilder() - .withSources(MpConfigSources.create(Map.of( - "strList", "a,b,c", - "intList", "1,2,3", - "untypedList", "a,b,c"))) - .build(); - } - -} diff --git a/microprofile/server/pom.xml b/microprofile/server/pom.xml index 408898c8df8..b053fed4313 100644 --- a/microprofile/server/pom.xml +++ b/microprofile/server/pom.xml @@ -156,11 +156,6 @@ - - io.helidon.microprofile.testing - helidon-microprofile-testing-junit5 - test - io.helidon.common helidon-common-reactive diff --git a/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java b/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java index 159bbf5b583..f5298c5a5b3 100644 --- a/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java +++ b/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java @@ -72,7 +72,7 @@ import static org.glassfish.jersey.CommonProperties.PROVIDER_DEFAULT_DISABLE; import static org.glassfish.jersey.server.ServerProperties.WADL_FEATURE_DISABLE; -class JaxRsService implements HttpService { +public class JaxRsService implements HttpService { /** * If set to {@code "true"}, Jersey will ignore responses in exceptions. */ @@ -97,7 +97,8 @@ private JaxRsService(ResourceConfig resourceConfig, this.application = getApplication(resourceConfig); } - static JaxRsService create(ResourceConfig resourceConfig, InjectionManager injectionManager) { + @SuppressWarnings("checkstyle:MissingJavadocMethod") + public static JaxRsService create(ResourceConfig resourceConfig, InjectionManager injectionManager) { resourceConfig.property(PROVIDER_DEFAULT_DISABLE, "ALL"); resourceConfig.property(WADL_FEATURE_DISABLE, "true"); diff --git a/microprofile/tests/config/pom.xml b/microprofile/tests/config/pom.xml new file mode 100644 index 00000000000..da8fedd2810 --- /dev/null +++ b/microprofile/tests/config/pom.xml @@ -0,0 +1,70 @@ + + + + + 4.0.0 + + io.helidon.microprofile.tests + helidon-microprofile-tests-project + 4.0.0-SNAPSHOT + ../pom.xml + + helidon-microprofile-tests-config + Helidon Microprofile Config Tests + + + Microprofile config implementation. + + + + + io.helidon.microprofile.config + helidon-microprofile-config + test + + + io.helidon.microprofile.cdi + helidon-microprofile-cdi + test + + + org.junit.jupiter + junit-jupiter-api + test + + + org.mockito + mockito-core + test + + + org.hamcrest + hamcrest-all + test + + + io.helidon.microprofile.testing + helidon-microprofile-testing-junit5 + test + + + + diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/Converters.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/Converters.java similarity index 100% rename from microprofile/config/src/test/java/io/helidon/microprofile/config/Converters.java rename to microprofile/tests/config/src/test/java/io/helidon/microprofile/config/Converters.java diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java similarity index 100% rename from microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java rename to microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MpConfigConvertTest.java diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigInjectionTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MpConfigInjectionTest.java similarity index 100% rename from microprofile/config/src/test/java/io/helidon/microprofile/config/MpConfigInjectionTest.java rename to microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MpConfigInjectionTest.java diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/MutableMpTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MutableMpTest.java similarity index 100% rename from microprofile/config/src/test/java/io/helidon/microprofile/config/MutableMpTest.java rename to microprofile/tests/config/src/test/java/io/helidon/microprofile/config/MutableMpTest.java diff --git a/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ObjectMappingTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ObjectMappingTest.java new file mode 100644 index 00000000000..108b9622a9b --- /dev/null +++ b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ObjectMappingTest.java @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2020, 2021 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.helidon.microprofile.config; + +import java.util.List; +import java.util.Map; +import java.util.Objects; + +import io.helidon.config.mp.MpConfigSources; + +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.spi.ConfigProviderResolver; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; + +class ObjectMappingTest { + private static ClassLoader classLoader; + private static ConfigProviderResolver resolver; + + @BeforeAll + static void initClass() { + classLoader = Thread.currentThread().getContextClassLoader(); + resolver = ConfigProviderResolver.instance(); + } + + @Test + void testIt() { + // Removed use of system properties, as those stay around after test is finished + Map configMap = Map.of("built.it", "configured", + "list.0.it", "first", + "list.1.it", "second"); + + resolver.registerConfig(resolver.getBuilder() + .withSources(MpConfigSources.create(configMap)) + .build(), + classLoader); + + // need to go through resolver to wrap config in our SE/MP wrapper + Config config = resolver.getConfig(classLoader); + io.helidon.config.Config helidonConfig = (io.helidon.config.Config) config; + + try { + Built built = helidonConfig.get("built").as(Built.class).get(); + assertThat(built.it, is("configured")); + + List list = helidonConfig.get("list").asList(Built.class).get(); + assertThat(list, hasSize(2)); + assertThat(list, Matchers.contains(new Built("first"), new Built("second"))); + + } finally { + resolver.releaseConfig(config); + } + } + + public static class Built { + private final String it; + + private Built(Builder builder) { + this.it = builder.it; + } + + private Built(String it) { + this.it = it; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Built built = (Built) o; + return it.equals(built.it); + } + + @Override + public int hashCode() { + return Objects.hash(it); + } + + public static class Builder { + private String it; + + public Builder it(String it) { + this.it = it; + return this; + } + + public Built build() { + return new Built(this); + } + } + } +} diff --git a/microprofile/tests/config/src/test/resources/META-INF/microprofile-config.properties b/microprofile/tests/config/src/test/resources/META-INF/microprofile-config.properties new file mode 100644 index 00000000000..3c6cbb8bb21 --- /dev/null +++ b/microprofile/tests/config/src/test/resources/META-INF/microprofile-config.properties @@ -0,0 +1,22 @@ +# +# Copyright (c) 2020, 2021 Oracle and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +# needed to run unit tests independently based on explicit beans using SeContainerInitializer +mp.initializer.allow=true +mp.initializer.warn=false + +camelCase=yes +yamlProperty=no diff --git a/microprofile/tests/config/src/test/resources/application.yaml b/microprofile/tests/config/src/test/resources/application.yaml new file mode 100644 index 00000000000..a0570b86859 --- /dev/null +++ b/microprofile/tests/config/src/test/resources/application.yaml @@ -0,0 +1,18 @@ +# +# Copyright (c) 2021 Oracle and/or its affiliates. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +yamlProperty : "yes" +nested: + yamlProperty : "yes" diff --git a/microprofile/tests/pom.xml b/microprofile/tests/pom.xml index ac8c828c8d3..99faa7f815c 100644 --- a/microprofile/tests/pom.xml +++ b/microprofile/tests/pom.xml @@ -45,6 +45,7 @@ arquillian server testing + config diff --git a/microprofile/server/src/test/java/io/helidon/microprofile/server/AsyncResourceTest.java b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/AsyncResourceTest.java similarity index 98% rename from microprofile/server/src/test/java/io/helidon/microprofile/server/AsyncResourceTest.java rename to microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/AsyncResourceTest.java index b4278346bfa..36add13be9c 100644 --- a/microprofile/server/src/test/java/io/helidon/microprofile/server/AsyncResourceTest.java +++ b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/AsyncResourceTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.helidon.microprofile.server; +package io.helidon.microprofile.tests.server; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionStage; diff --git a/microprofile/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/JerseyPropertiesTest.java similarity index 86% rename from microprofile/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java rename to microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/JerseyPropertiesTest.java index 4e590b47f80..da510b4d9c8 100644 --- a/microprofile/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java +++ b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/JerseyPropertiesTest.java @@ -13,9 +13,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.helidon.microprofile.server; +package io.helidon.microprofile.tests.server; import io.helidon.microprofile.config.ConfigCdiExtension; +import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.JaxRsService; +import io.helidon.microprofile.server.ServerCdiExtension; import io.helidon.microprofile.testing.junit5.AddConfig; import io.helidon.microprofile.testing.junit5.AddExtension; import io.helidon.microprofile.testing.junit5.DisableDiscovery; @@ -32,7 +35,7 @@ /** * Test that it is possible to override {@code IGNORE_EXCEPTION_RESPONSE} in - * Jersey using config. See {@link JaxRsService} + * Jersey using config. See {@link io.helidon.microprofile.server.JaxRsService} * for more information. */ @HelidonTest diff --git a/microprofile/server/src/test/java/io/helidon/microprofile/server/ProducedRouteTest.java b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/ProducedRouteTest.java similarity index 95% rename from microprofile/server/src/test/java/io/helidon/microprofile/server/ProducedRouteTest.java rename to microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/ProducedRouteTest.java index d2d734711c5..2df3d159d12 100644 --- a/microprofile/server/src/test/java/io/helidon/microprofile/server/ProducedRouteTest.java +++ b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/ProducedRouteTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package io.helidon.microprofile.server; +package io.helidon.microprofile.tests.server; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -22,6 +22,10 @@ import io.helidon.http.HeaderName; import io.helidon.http.HeaderNames; +import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.RoutingName; +import io.helidon.microprofile.server.RoutingPath; +import io.helidon.microprofile.server.ServerCdiExtension; import io.helidon.microprofile.testing.junit5.AddBean; import io.helidon.microprofile.testing.junit5.AddConfig; import io.helidon.microprofile.testing.junit5.AddExtension; @@ -61,7 +65,7 @@ value = RoutingName.DEFAULT_NAME) public class ProducedRouteTest { - static final String TEST_BEAN_FQDN = "io.helidon.microprofile.server.ProducedRouteTest$TestBean"; + static final String TEST_BEAN_FQDN = "io.helidon.microprofile.tests.server.ProducedRouteTest$TestBean"; static final String FILTERED_PATH = "/filtered"; static final String UNFILTERED_PATH = "/unfiltered"; diff --git a/microprofile/server/src/test/java/io/helidon/microprofile/server/RedirectionTest.java b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/RedirectionTest.java similarity index 94% rename from microprofile/server/src/test/java/io/helidon/microprofile/server/RedirectionTest.java rename to microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/RedirectionTest.java index 1355514fdb4..41cce14fe49 100644 --- a/microprofile/server/src/test/java/io/helidon/microprofile/server/RedirectionTest.java +++ b/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/RedirectionTest.java @@ -14,10 +14,12 @@ * limitations under the License. */ -package io.helidon.microprofile.server; +package io.helidon.microprofile.tests.server; import java.net.URI; +import io.helidon.microprofile.server.JaxRsCdiExtension; +import io.helidon.microprofile.server.ServerCdiExtension; import io.helidon.microprofile.testing.junit5.AddBean; import io.helidon.microprofile.testing.junit5.AddExtension; import io.helidon.microprofile.testing.junit5.DisableDiscovery; From a1d4cc766c1743541ddd1646521beca03bf7c9f7 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Fri, 1 Dec 2023 18:31:19 +0200 Subject: [PATCH 25/28] Copyright fix Signed-off-by: Dmitry Aleksandrov --- .../main/java/io/helidon/microprofile/server/JaxRsService.java | 3 +++ .../java/io/helidon/microprofile/config/ObjectMappingTest.java | 2 +- .../src/test/resources/META-INF/microprofile-config.properties | 2 +- microprofile/tests/config/src/test/resources/application.yaml | 2 +- 4 files changed, 6 insertions(+), 3 deletions(-) diff --git a/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java b/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java index f5298c5a5b3..c4ab2833240 100644 --- a/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java +++ b/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java @@ -72,6 +72,9 @@ import static org.glassfish.jersey.CommonProperties.PROVIDER_DEFAULT_DISABLE; import static org.glassfish.jersey.server.ServerProperties.WADL_FEATURE_DISABLE; +/** + * JAX-RS Service. + */ public class JaxRsService implements HttpService { /** * If set to {@code "true"}, Jersey will ignore responses in exceptions. diff --git a/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ObjectMappingTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ObjectMappingTest.java index 108b9622a9b..1064d7f5c13 100644 --- a/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ObjectMappingTest.java +++ b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ObjectMappingTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2021 Oracle and/or its affiliates. + * Copyright (c) 2020, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/microprofile/tests/config/src/test/resources/META-INF/microprofile-config.properties b/microprofile/tests/config/src/test/resources/META-INF/microprofile-config.properties index 3c6cbb8bb21..c6f40ebb087 100644 --- a/microprofile/tests/config/src/test/resources/META-INF/microprofile-config.properties +++ b/microprofile/tests/config/src/test/resources/META-INF/microprofile-config.properties @@ -1,5 +1,5 @@ # -# Copyright (c) 2020, 2021 Oracle and/or its affiliates. +# Copyright (c) 2020, 2023 Oracle and/or its affiliates. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. diff --git a/microprofile/tests/config/src/test/resources/application.yaml b/microprofile/tests/config/src/test/resources/application.yaml index a0570b86859..2de7ba50ffa 100644 --- a/microprofile/tests/config/src/test/resources/application.yaml +++ b/microprofile/tests/config/src/test/resources/application.yaml @@ -1,5 +1,5 @@ # -# Copyright (c) 2021 Oracle and/or its affiliates. +# Copyright (c) 2021, 2023 Oracle and/or its affiliates. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. From c82f046fdd7a7e8f8613aebff7bed10b78d601c4 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Tue, 5 Dec 2023 15:48:46 +0200 Subject: [PATCH 26/28] Apply comments Signed-off-by: Dmitry Aleksandrov --- .../microprofile/server/JaxRsService.java | 8 +- microprofile/testing/junit5/pom.xml | 1 - .../junit5/src/main/java/module-info.java | 3 +- microprofile/testing/testng/pom.xml | 1 - .../testng/src/main/java/module-info.java | 2 +- .../config/ConfigBeanDescriptorTest.java | 114 ++++++++++++++++++ .../testsubjects/TestConfiguredBean.java | 0 .../server/JerseyPropertiesTest.java | 2 +- .../testng/TestReqScopeDisabledDiscovery.java | 2 +- 9 files changed, 121 insertions(+), 12 deletions(-) create mode 100644 microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java rename microprofile/{ => tests}/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java (100%) rename microprofile/tests/server/src/test/java/io/helidon/microprofile/{tests => }/server/JerseyPropertiesTest.java (97%) diff --git a/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java b/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java index c4ab2833240..159bbf5b583 100644 --- a/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java +++ b/microprofile/server/src/main/java/io/helidon/microprofile/server/JaxRsService.java @@ -72,10 +72,7 @@ import static org.glassfish.jersey.CommonProperties.PROVIDER_DEFAULT_DISABLE; import static org.glassfish.jersey.server.ServerProperties.WADL_FEATURE_DISABLE; -/** - * JAX-RS Service. - */ -public class JaxRsService implements HttpService { +class JaxRsService implements HttpService { /** * If set to {@code "true"}, Jersey will ignore responses in exceptions. */ @@ -100,8 +97,7 @@ private JaxRsService(ResourceConfig resourceConfig, this.application = getApplication(resourceConfig); } - @SuppressWarnings("checkstyle:MissingJavadocMethod") - public static JaxRsService create(ResourceConfig resourceConfig, InjectionManager injectionManager) { + static JaxRsService create(ResourceConfig resourceConfig, InjectionManager injectionManager) { resourceConfig.property(PROVIDER_DEFAULT_DISABLE, "ALL"); resourceConfig.property(WADL_FEATURE_DISABLE, "true"); diff --git a/microprofile/testing/junit5/pom.xml b/microprofile/testing/junit5/pom.xml index ad4b10cb685..8015320178e 100644 --- a/microprofile/testing/junit5/pom.xml +++ b/microprofile/testing/junit5/pom.xml @@ -43,7 +43,6 @@ org.glassfish.jersey.ext.cdi jersey-weld2-se true - provided org.junit.jupiter diff --git a/microprofile/testing/junit5/src/main/java/module-info.java b/microprofile/testing/junit5/src/main/java/module-info.java index 5e1730bdec3..3439d26f82f 100644 --- a/microprofile/testing/junit5/src/main/java/module-info.java +++ b/microprofile/testing/junit5/src/main/java/module-info.java @@ -21,13 +21,14 @@ requires io.helidon.config.mp; requires io.helidon.config.yaml.mp; + requires io.helidon.microprofile.server; requires jakarta.inject; requires org.junit.jupiter.api; requires transitive jakarta.cdi; requires transitive jakarta.ws.rs; - requires static io.helidon.microprofile.server; + requires static jersey.cdi1x; requires static jersey.weld2.se; diff --git a/microprofile/testing/testng/pom.xml b/microprofile/testing/testng/pom.xml index 0105b52951d..f96e2061a89 100644 --- a/microprofile/testing/testng/pom.xml +++ b/microprofile/testing/testng/pom.xml @@ -43,7 +43,6 @@ org.glassfish.jersey.ext.cdi jersey-weld2-se true - provided io.helidon.jersey diff --git a/microprofile/testing/testng/src/main/java/module-info.java b/microprofile/testing/testng/src/main/java/module-info.java index 40aa0c9b47a..96d93c5c876 100644 --- a/microprofile/testing/testng/src/main/java/module-info.java +++ b/microprofile/testing/testng/src/main/java/module-info.java @@ -23,13 +23,13 @@ requires io.helidon.config.mp; requires io.helidon.config.yaml.mp; + requires io.helidon.microprofile.server; requires jakarta.cdi; requires jakarta.inject; requires jakarta.ws.rs; requires microprofile.config.api; requires org.testng; - requires static io.helidon.microprofile.server; requires static jersey.cdi1x; requires static jersey.weld2.se; diff --git a/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java new file mode 100644 index 00000000000..b946db51c0a --- /dev/null +++ b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2022 Oracle and/or its affiliates. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.helidon.microprofile.config; + +import java.lang.reflect.Field; +import java.lang.reflect.Type; +import java.util.Map; + +import io.helidon.config.mp.MpConfigSources; +import io.helidon.microprofile.config.testsubjects.TestConfiguredBean; + +import jakarta.enterprise.inject.spi.AnnotatedType; +import jakarta.enterprise.inject.spi.InjectionPoint; +import org.eclipse.microprofile.config.Config; +import org.eclipse.microprofile.config.inject.ConfigProperties; +import org.eclipse.microprofile.config.spi.ConfigProviderResolver; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsIterableContainingInOrder.contains; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * Lower level testing for {@link io.helidon.microprofile.config.ConfigBeanDescriptor}. + */ +class ConfigBeanDescriptorTest { + + private static MutableMpTest.MutableSource source; + private static Config config; + private static io.helidon.config.Config emptyConfig; + + @BeforeAll + static void initClass() { + config = createTestConfiguredBeanConfig(); + + // we need to ensure empty config is initialized before running other tests, + // as this messes up the mapping service counter + emptyConfig = io.helidon.config.Config.empty(); + + source = new MutableMpTest.MutableSource("initial"); + + // register config + ConfigProviderResolver configProvider = ConfigProviderResolver.instance(); + configProvider.registerConfig(config, Thread.currentThread().getContextClassLoader()); + } + + @Test + @SuppressWarnings("unchecked") + void testProduceListFields() throws Exception { + // setup for the test + AnnotatedType annotatedType = mock(AnnotatedType.class); + when(annotatedType.getJavaClass()).thenReturn((Class) TestConfiguredBean.class); + + Field strListField = TestConfiguredBean.class.getField("strList"); + Type strListType = strListField.getGenericType(); + Field intListField = TestConfiguredBean.class.getField("intList"); + Type intListType = intListField.getGenericType(); + + ConfigProperties configProperties = TestConfiguredBean.class.getAnnotation(ConfigProperties.class); + ConfigBeanDescriptor descriptor = ConfigBeanDescriptor.create(annotatedType, configProperties); + + InjectionPoint strListInjectionPoint = mock(InjectionPoint.class); + when(strListInjectionPoint.getType()).thenReturn(strListType); + + InjectionPoint intListInjectionPoint = mock(InjectionPoint.class); + when(intListInjectionPoint.getType()).thenReturn(intListType); + + // the target of the test starts here: + Object instance = descriptor.produce(strListInjectionPoint, config); + assertThat(((TestConfiguredBean) instance).strList, contains("a", "b", "c")); + + instance = descriptor.produce(intListInjectionPoint, config); + assertThat(((TestConfiguredBean) instance).intList, contains(1, 2, 3)); + } + + @Test + @SuppressWarnings("unchecked") + void testProduceGenericTypesNotSupported() throws Exception { + AnnotatedType annotatedType = mock(AnnotatedType.class); + when(annotatedType.getJavaClass()).thenReturn((Class) TestConfiguredBean.Unsupported.class); + ConfigProperties configProperties = TestConfiguredBean.class.getAnnotation(ConfigProperties.class); + UnsupportedOperationException e = assertThrows(UnsupportedOperationException.class, + () -> ConfigBeanDescriptor.create(annotatedType, configProperties)); + assertThat(e.getMessage(), is("No idea how to handle ?")); + } + + static Config createTestConfiguredBeanConfig() { + return ConfigProviderResolver.instance() + .getBuilder() + .withSources(MpConfigSources.create(Map.of( + "strList", "a,b,c", + "intList", "1,2,3", + "untypedList", "a,b,c"))) + .build(); + } + +} \ No newline at end of file diff --git a/microprofile/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java similarity index 100% rename from microprofile/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java rename to microprofile/tests/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java diff --git a/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/JerseyPropertiesTest.java b/microprofile/tests/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java similarity index 97% rename from microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/JerseyPropertiesTest.java rename to microprofile/tests/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java index da510b4d9c8..850bc363646 100644 --- a/microprofile/tests/server/src/test/java/io/helidon/microprofile/tests/server/JerseyPropertiesTest.java +++ b/microprofile/tests/server/src/test/java/io/helidon/microprofile/server/JerseyPropertiesTest.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.helidon.microprofile.tests.server; +package io.helidon.microprofile.server; import io.helidon.microprofile.config.ConfigCdiExtension; import io.helidon.microprofile.server.JaxRsCdiExtension; diff --git a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java index 4d166c7429a..a80f587cd67 100644 --- a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java +++ b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestReqScopeDisabledDiscovery.java @@ -33,7 +33,7 @@ // JAX-RS Request scope @AddJaxRs @AddBean(TestReqScopeDisabledDiscovery.MyController.class) -class TestReqScopeDisabledDiscovery { +public class TestReqScopeDisabledDiscovery { @Inject private WebTarget target; From 722c95f6421d8f09aa6f5b8b3d05ddeaad0be65c Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Tue, 5 Dec 2023 16:23:54 +0200 Subject: [PATCH 27/28] Fix copyright Signed-off-by: Dmitry Aleksandrov --- .../helidon/microprofile/config/ConfigBeanDescriptorTest.java | 2 +- .../microprofile/config/testsubjects/TestConfiguredBean.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java index b946db51c0a..f670e0decb1 100644 --- a/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java +++ b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/ConfigBeanDescriptorTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Oracle and/or its affiliates. + * Copyright (c) 2022, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java index 85a15973bc0..d709e2a7fa5 100644 --- a/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java +++ b/microprofile/tests/config/src/test/java/io/helidon/microprofile/config/testsubjects/TestConfiguredBean.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Oracle and/or its affiliates. + * Copyright (c) 2022, 2023 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. From d3ccf550b3b101db10ba9ddde8e9bc7ae8046c35 Mon Sep 17 00:00:00 2001 From: Dmitry Aleksandrov Date: Wed, 6 Dec 2023 19:09:58 +0200 Subject: [PATCH 28/28] Fix dependencies Signed-off-by: Dmitry Aleksandrov --- microprofile/testing/junit5/pom.xml | 5 +++++ microprofile/testing/junit5/src/main/java/module-info.java | 4 ++-- microprofile/testing/testng/pom.xml | 5 +++++ microprofile/testing/testng/src/main/java/module-info.java | 3 ++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/microprofile/testing/junit5/pom.xml b/microprofile/testing/junit5/pom.xml index 8015320178e..fe4d067c095 100644 --- a/microprofile/testing/junit5/pom.xml +++ b/microprofile/testing/junit5/pom.xml @@ -39,6 +39,11 @@ helidon-microprofile-server true + + io.helidon.microprofile.cdi + helidon-microprofile-cdi + provided + org.glassfish.jersey.ext.cdi jersey-weld2-se diff --git a/microprofile/testing/junit5/src/main/java/module-info.java b/microprofile/testing/junit5/src/main/java/module-info.java index 3439d26f82f..9799b1b0e0b 100644 --- a/microprofile/testing/junit5/src/main/java/module-info.java +++ b/microprofile/testing/junit5/src/main/java/module-info.java @@ -21,14 +21,14 @@ requires io.helidon.config.mp; requires io.helidon.config.yaml.mp; - requires io.helidon.microprofile.server; + requires io.helidon.microprofile.cdi; requires jakarta.inject; requires org.junit.jupiter.api; requires transitive jakarta.cdi; requires transitive jakarta.ws.rs; - + requires static io.helidon.microprofile.server; requires static jersey.cdi1x; requires static jersey.weld2.se; diff --git a/microprofile/testing/testng/pom.xml b/microprofile/testing/testng/pom.xml index f96e2061a89..da432b69862 100644 --- a/microprofile/testing/testng/pom.xml +++ b/microprofile/testing/testng/pom.xml @@ -39,6 +39,11 @@ helidon-microprofile-server true + + io.helidon.microprofile.cdi + helidon-microprofile-cdi + provided + org.glassfish.jersey.ext.cdi jersey-weld2-se diff --git a/microprofile/testing/testng/src/main/java/module-info.java b/microprofile/testing/testng/src/main/java/module-info.java index 96d93c5c876..c2e60fd39aa 100644 --- a/microprofile/testing/testng/src/main/java/module-info.java +++ b/microprofile/testing/testng/src/main/java/module-info.java @@ -23,13 +23,14 @@ requires io.helidon.config.mp; requires io.helidon.config.yaml.mp; - requires io.helidon.microprofile.server; + requires io.helidon.microprofile.cdi; requires jakarta.cdi; requires jakarta.inject; requires jakarta.ws.rs; requires microprofile.config.api; requires org.testng; + requires static io.helidon.microprofile.server; requires static jersey.cdi1x; requires static jersey.weld2.se;