Skip to content

Commit

Permalink
Merge pull request #35202 from mkouba/issue-35199
Browse files Browse the repository at this point in the history
QuarkusComponentTest: test config source ordinal improvements
  • Loading branch information
manovotn authored Aug 7, 2023
2 parents 33482f5 + 3615a71 commit 772d5e1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -162,6 +170,7 @@ public class QuarkusComponentTestExtension
private final List<MockBeanConfiguratorImpl<?>> 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() {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}
}

0 comments on commit 772d5e1

Please sign in to comment.