From 85f95d65d6322f569082de36dab9523bb6089f01 Mon Sep 17 00:00:00 2001 From: Jorge Bescos Gascon Date: Thu, 2 May 2024 05:33:06 +0200 Subject: [PATCH] 4.x: Add text block support (#8655) Add text block support Signed-off-by: Jorge Bescos Gascon --- .../testing/junit5/AddConfigBlock.java | 50 ++++++++++++++++++ .../testing/junit5/HelidonJunitExtension.java | 38 +++++++++++++- .../testing/testng/AddConfigBlock.java | 50 ++++++++++++++++++ .../testing/testng/HelidonTestNgListener.java | 38 +++++++++++++- .../junit5/TestAddConfigBlockProperties.java | 50 ++++++++++++++++++ .../junit5/TestAddConfigBlockYaml.java | 52 +++++++++++++++++++ .../testng/TestAddConfigBlockProperties.java | 50 ++++++++++++++++++ .../testng/TestAddConfigBlockYaml.java | 52 +++++++++++++++++++ 8 files changed, 378 insertions(+), 2 deletions(-) create mode 100644 microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddConfigBlock.java create mode 100644 microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddConfigBlock.java create mode 100644 microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigBlockProperties.java create mode 100644 microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigBlockYaml.java create mode 100644 microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestAddConfigBlockProperties.java create mode 100644 microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestAddConfigBlockYaml.java diff --git a/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddConfigBlock.java b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddConfigBlock.java new file mode 100644 index 00000000000..595cb8bed1d --- /dev/null +++ b/microprofile/testing/junit5/src/main/java/io/helidon/microprofile/testing/junit5/AddConfigBlock.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2024 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.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Defines the configuration as a String in {@link #value()} for the + * given type. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +@Inherited +public @interface AddConfigBlock { + + /** + * Specifies the format type of the {@link #value()}. + * + * It defaults to 'properties'. + * + * @return the supported type + */ + String type() default "properties"; + + /** + * Configuration value. + * + * @return String with value. + */ + String value(); + +} 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 3f2484feec0..f14010dbe2b 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2023 Oracle and/or its affiliates. + * Copyright (c) 2020, 2024 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. @@ -16,7 +16,10 @@ package io.helidon.microprofile.testing.junit5; +import java.io.IOException; import java.io.Serial; +import java.io.StringReader; +import java.io.UncheckedIOException; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; @@ -30,6 +33,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.Set; import io.helidon.config.mp.MpConfigSources; @@ -117,6 +121,7 @@ public void beforeAll(ExtensionContext context) { AddConfig[] configs = getAnnotations(testClass, AddConfig.class); classLevelConfigMeta.addConfig(configs); classLevelConfigMeta.configuration(testClass.getAnnotation(Configuration.class)); + classLevelConfigMeta.addConfigBlock(testClass.getAnnotation(AddConfigBlock.class)); configProviderResolver = ConfigProviderResolver.instance(); AddExtension[] extensions = getAnnotations(testClass, AddExtension.class); @@ -193,6 +198,7 @@ public void beforeEach(ExtensionContext context) throws Exception { ConfigMeta methodLevelConfigMeta = classLevelConfigMeta.nextMethod(); methodLevelConfigMeta.addConfig(configs); methodLevelConfigMeta.configuration(method.getAnnotation(Configuration.class)); + methodLevelConfigMeta.addConfigBlock(method.getAnnotation(AddConfigBlock.class)); configure(methodLevelConfigMeta); @@ -320,6 +326,9 @@ private void configure(ConfigMeta configMeta) { builder.withSources(MpConfigSources.classPath(it).toArray(new ConfigSource[0])); } }); + if (configMeta.type != null && configMeta.block != null) { + builder.withSources(configSource(configMeta.type, configMeta.block)); + } config = builder .withSources(MpConfigSources.create(configMeta.additionalKeys)) .addDefaultSources() @@ -330,6 +339,23 @@ private void configure(ConfigMeta configMeta) { } } + private ConfigSource configSource(String type, String block) { + String lowerCase = type.toLowerCase(); + if ("properties".equals(lowerCase)) { + Properties p = new Properties(); + try { + p.load(new StringReader(block)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + return MpConfigSources.create("PropertiesAddConfigBlock", p); + } else if ("yaml".equals(lowerCase)) { + return YamlMpConfigSource.create("YamlAddConfigBlock", new StringReader(block)); + } else { + throw new IllegalArgumentException(type + " is not supported"); + } + } + private void releaseConfig() { if (configProviderResolver != null && config != null) { configProviderResolver.releaseConfig(config); @@ -600,6 +626,8 @@ private boolean hasBda(Class value) { private static final class ConfigMeta { private final Map additionalKeys = new HashMap<>(); private final List additionalSources = new ArrayList<>(); + private String type; + private String block; private boolean useExisting; private String profile; @@ -632,6 +660,14 @@ private void configuration(Configuration config) { additionalKeys.put("mp.config.profile", profile); } + private void addConfigBlock(AddConfigBlock config) { + if (config == null) { + return; + } + this.type = config.type(); + this.block = config.value(); + } + ConfigMeta nextMethod() { ConfigMeta methodMeta = new ConfigMeta(); diff --git a/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddConfigBlock.java b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddConfigBlock.java new file mode 100644 index 00000000000..3ba558d2fc3 --- /dev/null +++ b/microprofile/testing/testng/src/main/java/io/helidon/microprofile/testing/testng/AddConfigBlock.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2024 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.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Defines the configuration as a String in {@link #value()} for the + * given type. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +@Inherited +public @interface AddConfigBlock { + + /** + * Specifies the format type of the {@link #value()}. + * + * It defaults to 'properties'. + * + * @return the supported type + */ + String type() default "properties"; + + /** + * Configuration value. + * + * @return String with value. + */ + String value(); + +} 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 e2ddb7f3fc8..1c2b106e5ca 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 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, 2023 Oracle and/or its affiliates. + * Copyright (c) 2022, 2024 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. @@ -16,7 +16,10 @@ package io.helidon.microprofile.testing.testng; +import java.io.IOException; import java.io.Serial; +import java.io.StringReader; +import java.io.UncheckedIOException; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Array; @@ -29,6 +32,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.Set; import io.helidon.config.mp.MpConfigSources; @@ -105,6 +109,7 @@ public void onBeforeClass(ITestClass iTestClass) { AddConfig[] configs = getAnnotations(testClass, AddConfig.class); classLevelConfigMeta.addConfig(configs); classLevelConfigMeta.configuration(testClass.getAnnotation(Configuration.class)); + classLevelConfigMeta.addConfigBlock(testClass.getAnnotation(AddConfigBlock.class)); configProviderResolver = ConfigProviderResolver.instance(); AddExtension[] extensions = getAnnotations(testClass, AddExtension.class); @@ -171,6 +176,7 @@ public void onTestStart(ITestResult result) { ConfigMeta methodLevelConfigMeta = classLevelConfigMeta.nextMethod(); methodLevelConfigMeta.addConfig(configs); methodLevelConfigMeta.configuration(method.getAnnotation(Configuration.class)); + methodLevelConfigMeta.addConfigBlock(method.getAnnotation(AddConfigBlock.class)); configure(methodLevelConfigMeta); @@ -333,6 +339,9 @@ private void configure(ConfigMeta configMeta) { builder.withSources(MpConfigSources.classPath(it).toArray(new ConfigSource[0])); } }); + if (configMeta.type != null && configMeta.block != null) { + builder.withSources(configSource(configMeta.type, configMeta.block)); + } config = builder .withSources(MpConfigSources.create(configMeta.additionalKeys)) .addDefaultSources() @@ -343,6 +352,23 @@ private void configure(ConfigMeta configMeta) { } } + private ConfigSource configSource(String type, String block) { + String lowerCase = type.toLowerCase(); + if ("properties".equals(lowerCase)) { + Properties p = new Properties(); + try { + p.load(new StringReader(block)); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + return MpConfigSources.create("PropertiesAddConfigBlock", p); + } else if ("yaml".equals(lowerCase)) { + return YamlMpConfigSource.create("YamlAddConfigBlock", new StringReader(block)); + } else { + throw new IllegalArgumentException(type + " is not supported"); + } + } + private void releaseConfig() { if (configProviderResolver != null && config != null) { configProviderResolver.releaseConfig(config); @@ -485,6 +511,8 @@ private boolean hasBda(Class value) { private static final class ConfigMeta { private final Map additionalKeys = new HashMap<>(); private final List additionalSources = new ArrayList<>(); + private String type; + private String block; private boolean useExisting; private String profile; @@ -517,6 +545,14 @@ private void configuration(Configuration config) { additionalKeys.put("mp.config.profile", profile); } + private void addConfigBlock(AddConfigBlock config) { + if (config == null) { + return; + } + this.type = config.type(); + this.block = config.value(); + } + ConfigMeta nextMethod() { ConfigMeta methodMeta = new ConfigMeta(); diff --git a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigBlockProperties.java b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigBlockProperties.java new file mode 100644 index 00000000000..0240d32dd01 --- /dev/null +++ b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigBlockProperties.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2024 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 static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import jakarta.inject.Inject; + +import io.helidon.microprofile.testing.junit5.AddConfigBlock; +import io.helidon.microprofile.testing.junit5.HelidonTest; + +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.junit.jupiter.api.Test; + +@HelidonTest +@AddConfigBlock(""" + some.key1=some.value1 + some.key2=some.value2 +""") +class TestAddConfigBlockProperties { + + @Inject + @ConfigProperty(name = "some.key1") + private String value1; + + @Inject + @ConfigProperty(name = "some.key2") + private String value2; + + @Test + void testValue() { + assertThat(value1, is("some.value1")); + assertThat(value2, is("some.value2")); + } +} diff --git a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigBlockYaml.java b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigBlockYaml.java new file mode 100644 index 00000000000..ab7d9217832 --- /dev/null +++ b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigBlockYaml.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2024 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 static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import jakarta.inject.Inject; + +import io.helidon.microprofile.testing.junit5.AddConfigBlock; +import io.helidon.microprofile.testing.junit5.HelidonTest; + +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.junit.jupiter.api.Test; + +@HelidonTest +@AddConfigBlock(type = "Yaml", value = """ + another1: + key: "another1.value" + another2: + key: "another2.value" +""") +class TestAddConfigBlockYaml { + + @Inject + @ConfigProperty(name = "another1.key") + private String another1; + + @Inject + @ConfigProperty(name = "another2.key") + private String another2; + + @Test + void testValue() { + assertThat(another1, is("another1.value")); + assertThat(another2, is("another2.value")); + } +} diff --git a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestAddConfigBlockProperties.java b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestAddConfigBlockProperties.java new file mode 100644 index 00000000000..e7bbbf90373 --- /dev/null +++ b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestAddConfigBlockProperties.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2024 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; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import jakarta.inject.Inject; + +import io.helidon.microprofile.testing.testng.AddConfigBlock; +import io.helidon.microprofile.testing.testng.HelidonTest; + +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.testng.annotations.Test; + +@HelidonTest +@AddConfigBlock(""" + some.key1=some.value1 + some.key2=some.value2 +""") +class TestAddConfigBlockProperties { + + @Inject + @ConfigProperty(name = "some.key1") + private String value1; + + @Inject + @ConfigProperty(name = "some.key2") + private String value2; + + @Test + void testValue() { + assertThat(value1, is("some.value1")); + assertThat(value2, is("some.value2")); + } +} diff --git a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestAddConfigBlockYaml.java b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestAddConfigBlockYaml.java new file mode 100644 index 00000000000..5feb907b6c8 --- /dev/null +++ b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestAddConfigBlockYaml.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2024 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; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +import jakarta.inject.Inject; + +import io.helidon.microprofile.testing.testng.AddConfigBlock; +import io.helidon.microprofile.testing.testng.HelidonTest; + +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.testng.annotations.Test; + +@HelidonTest +@AddConfigBlock(type = "Yaml", value = """ + another1: + key: "another1.value" + another2: + key: "another2.value" +""") +class TestAddConfigBlockYaml { + + @Inject + @ConfigProperty(name = "another1.key") + private String another1; + + @Inject + @ConfigProperty(name = "another2.key") + private String another2; + + @Test + void testValue() { + assertThat(another1, is("another1.value")); + assertThat(another2, is("another2.value")); + } +}