* See also {@link io.helidon.microprofile.testing.Socket}, a CDI qualifier to inject JAX-RS client or URI. diff --git a/microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/AddConfigSource.java b/microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/AddConfigSource.java new file mode 100644 index 00000000000..c3b95cfc981 --- /dev/null +++ b/microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/AddConfigSource.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2025 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; + +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; + +/** + * Mark a static method that provides a {@link org.eclipse.microprofile.config.spi.ConfigSource ConfigSource} + * to add to the {@link Configuration#useExisting() synthetic test configuration}. + *
+ * E.g. + *
+ * @AddConfigSource + * static ConfigSource config() { + * return MpConfigSources.create(Map.of("foo", "bar")); + * }+ * + * @see io.helidon.config.mp.MpConfigSources + * @see AddConfig + * @see AddConfigs + * @see AddConfigBlock + * @see Configuration + */ +@Inherited +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.METHOD}) +public @interface AddConfigSource { +} diff --git a/microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/HelidonTestConfigSynthetic.java b/microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/HelidonTestConfigSynthetic.java index 94da0c4d540..f49219a76ec 100644 --- a/microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/HelidonTestConfigSynthetic.java +++ b/microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/HelidonTestConfigSynthetic.java @@ -66,6 +66,7 @@ class HelidonTestConfigSynthetic extends HelidonTestConfigDelegate { map.put("mp.config.profile", "test"); testInfo.addConfigs().forEach(this::update); testInfo.addConfigBlocks().forEach(this::update); + testInfo.addConfigSources().forEach(this::update); testInfo.configuration().ifPresent(this::update); } diff --git a/microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/HelidonTestDescriptor.java b/microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/HelidonTestDescriptor.java index 112e776e5c5..a3974212fc1 100644 --- a/microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/HelidonTestDescriptor.java +++ b/microprofile/testing/testing/src/main/java/io/helidon/microprofile/testing/HelidonTestDescriptor.java @@ -17,6 +17,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; +import java.lang.reflect.Method; import java.util.List; import java.util.Optional; import java.util.function.Function; @@ -117,6 +118,13 @@ default long pinningThreshold() { */ List
* See also {@link io.helidon.microprofile.testing.Socket}, a CDI qualifier to inject JAX-RS client or URI. diff --git a/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigSource.java b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigSource.java new file mode 100644 index 00000000000..05636523970 --- /dev/null +++ b/microprofile/tests/testing/junit5/src/test/java/io/helidon/microprofile/tests/testing/junit5/TestAddConfigSource.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2025 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 java.util.Map; + +import jakarta.inject.Inject; + +import io.helidon.config.mp.MpConfigSources; +import io.helidon.microprofile.testing.AddConfigSource; +import io.helidon.microprofile.testing.junit5.HelidonTest; + +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@HelidonTest +class TestAddConfigSource { + + @AddConfigSource + static ConfigSource config() { + return MpConfigSources.create(Map.of( + "some.key", "some.value", + "another.key", "another.value")); + } + + @Inject + @ConfigProperty(name = "some.key") + private String someKey; + + @Inject + @ConfigProperty(name = "another.key") + private String anotherKey; + + @Test + void testValue() { + assertThat(someKey, is("some.value")); + assertThat(anotherKey, is("another.value")); + } +} diff --git a/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestAddConfigSource.java b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestAddConfigSource.java new file mode 100644 index 00000000000..6fd2fc3e37c --- /dev/null +++ b/microprofile/tests/testing/testng/src/test/java/io/helidon/microprofile/tests/testing/testng/TestAddConfigSource.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2025 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 java.util.Map; + +import io.helidon.config.mp.MpConfigSources; +import io.helidon.microprofile.testing.AddConfigSource; +import io.helidon.microprofile.testing.testng.HelidonTest; + +import jakarta.inject.Inject; +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.eclipse.microprofile.config.spi.ConfigSource; +import org.testng.annotations.Test; + +import static org.hamcrest.Matchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +@HelidonTest +public class TestAddConfigSource { + + @AddConfigSource + static ConfigSource config() { + return MpConfigSources.create(Map.of( + "some.key", "some.value", + "another.key", "another.value")); + } + + @Inject + @ConfigProperty(name = "some.key") + private String someKey; + + @Inject + @ConfigProperty(name = "another.key") + private String anotherKey; + + @Test + void testValue() { + assertThat(someKey, is("some.value")); + assertThat(anotherKey, is("another.value")); + } +}