From 3615a71c5e959367a89c7999435baba1434a9411 Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Fri, 4 Aug 2023 12:33:02 +0200 Subject: [PATCH] QuarkusComponentTest: test config source ordinal improvements - change the default value to 500 so that test config properties take predence over system properties, ENV variables and application.properties - make the ordinal configurable - fixes #35199 --- .../test/component/QuarkusComponentTest.java | 7 +++ .../QuarkusComponentTestExtension.java | 25 ++++++++++- .../config/ConfigSourceOrdinalTest.java | 45 +++++++++++++++++++ 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 test-framework/junit5-component/src/test/java/io/quarkus/test/component/config/ConfigSourceOrdinalTest.java diff --git a/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTest.java b/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTest.java index 36a4c4941eeff..7753391c71fcd 100644 --- a/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTest.java +++ b/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTest.java @@ -50,4 +50,11 @@ * @see #value() */ boolean addNestedClassesAsComponents() default true; + + /** + * The ordinal of the config source used for all test config properties. + * + * @see QuarkusComponentTestExtension#setConfigSourceOrdinal(int) + */ + int configSourceOrdinal() default QuarkusComponentTestExtension.DEFAULT_CONFIG_SOURCE_ORDINAL; } diff --git a/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestExtension.java b/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestExtension.java index da3c11db25b87..a5eaebcb5c164 100644 --- a/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestExtension.java +++ b/test-framework/junit5-component/src/main/java/io/quarkus/test/component/QuarkusComponentTestExtension.java @@ -27,6 +27,7 @@ import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.function.Consumer; import java.util.function.Function; @@ -142,6 +143,13 @@ public class QuarkusComponentTestExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback, AfterEachCallback, TestInstancePostProcessor, TestInstancePreDestroyCallback, ConfigSource { + /** + * By default, test config properties take precedence over system properties (400), ENV variables (300) and + * application.properties (250) + * + */ + public static final int DEFAULT_CONFIG_SOURCE_ORDINAL = 500; + private static final Logger LOG = Logger.getLogger(QuarkusComponentTestExtension.class); private static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace @@ -162,6 +170,7 @@ public class QuarkusComponentTestExtension private final List> mockConfigurators; private final AtomicBoolean useDefaultConfigProperties = new AtomicBoolean(); private final AtomicBoolean addNestedClassesAsComponents = new AtomicBoolean(true); + private final AtomicInteger configSourceOrdinal = new AtomicInteger(DEFAULT_CONFIG_SOURCE_ORDINAL); // Used for declarative registration public QuarkusComponentTestExtension() { @@ -233,6 +242,18 @@ public QuarkusComponentTestExtension ignoreNestedClasses() { return this; } + /** + * Set the ordinal of the config source used for all test config properties. By default, + * {@value #DEFAULT_CONFIG_SOURCE_ORDINAL} is used. + * + * @param val + * @return the extension + */ + public QuarkusComponentTestExtension setConfigSourceOrdinal(int val) { + this.configSourceOrdinal.set(val); + return this; + } + @Override public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception { long start = System.nanoTime(); @@ -273,6 +294,7 @@ public void beforeAll(ExtensionContext context) throws Exception { this.useDefaultConfigProperties.set(true); } this.addNestedClassesAsComponents.set(testAnnotation.addNestedClassesAsComponents()); + this.configSourceOrdinal.set(testAnnotation.configSourceOrdinal()); } // All fields annotated with @Inject represent component classes Class current = testClass; @@ -398,8 +420,7 @@ public String getName() { @Override public int getOrdinal() { - // System properties (400) and ENV variables (300) take precedence but application.properties has lower priority (250) - return 275; + return configSourceOrdinal.get(); } void registerMockBean(MockBeanConfiguratorImpl mock) { diff --git a/test-framework/junit5-component/src/test/java/io/quarkus/test/component/config/ConfigSourceOrdinalTest.java b/test-framework/junit5-component/src/test/java/io/quarkus/test/component/config/ConfigSourceOrdinalTest.java new file mode 100644 index 0000000000000..6c0a3881189cd --- /dev/null +++ b/test-framework/junit5-component/src/test/java/io/quarkus/test/component/config/ConfigSourceOrdinalTest.java @@ -0,0 +1,45 @@ +package io.quarkus.test.component.config; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import jakarta.inject.Inject; +import jakarta.inject.Singleton; + +import org.eclipse.microprofile.config.inject.ConfigProperty; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import io.quarkus.test.component.QuarkusComponentTest; +import io.quarkus.test.component.TestConfigProperty; + +@QuarkusComponentTest(configSourceOrdinal = 275) +@TestConfigProperty(key = "foo", value = "baz") +public class ConfigSourceOrdinalTest { + + @BeforeAll + static void beforeAll() { + System.setProperty("foo", "bar"); + } + + @AfterAll + static void afterAll() { + System.clearProperty("foo"); + } + + @Inject + FooConsumer consumer; + + @Test + public void testOrdinal() { + assertEquals("bar", consumer.foo); + } + + @Singleton + public static class FooConsumer { + + @ConfigProperty(name = "foo") + String foo; + + } +}