Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config: detect injected config value mismatch for missing values #37454

Merged
merged 1 commit into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.quarkus.arc.test.config.staticinit;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.Optional;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Initialized;
import jakarta.enterprise.event.Observes;
import jakarta.inject.Singleton;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.test.QuarkusUnitTest;

public class StaticInitConfigInjectionMissingValueFailureTest {

static final String PROPERTY_NAME = "static.init.missing.apfelstrudel";

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.withApplicationRoot(root -> root
.addClasses(StaticInitBean.class))
.assertException(t -> {
assertThat(t).isInstanceOf(IllegalStateException.class)
.hasMessageContainingAll(
"A runtime config property value differs from the value that was injected during the static intialization phase",
"the runtime value of '" + PROPERTY_NAME
+ "' is [gizmo] but the value [null] was injected into io.quarkus.arc.test.config.staticinit.StaticInitConfigInjectionMissingValueFailureTest$StaticInitBean#value");
});

@Test
public void test() {
fail();
}

@Singleton
public static class StaticInitBean {

@ConfigProperty(name = PROPERTY_NAME)
Optional<String> value;

// bean is instantiated during STATIC_INIT
void onInit(@Observes @Initialized(ApplicationScoped.class) Object event) {
System.setProperty(PROPERTY_NAME, "gizmo");
}

}

@AfterAll
static void afterAll() {
System.clearProperty(PROPERTY_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ static void recordConfigValue(InjectionPoint injectionPoint, ConfigStaticInitVal
value = getDefaultValue(injectionPoint, configProperty);
}
if (value == null) {
LOG.debugf("No config value found for %s", propertyName);
return;
LOG.debugf("No config value found for %s - recording <null> value", propertyName);
}
if (configValues == null) {
configValues = Arc.container().instance(ConfigStaticInitValues.class).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import jakarta.annotation.Priority;
import jakarta.enterprise.event.Observes;
Expand Down Expand Up @@ -43,7 +44,9 @@ void onStart(@Observes @Priority(Integer.MIN_VALUE) StartupEvent event) {
List<String> mismatches = new ArrayList<>();
for (InjectedValue injectedValue : injectedValues) {
ConfigValue currentValue = config.getConfigValue(injectedValue.name);
if (currentValue.getValue() != null && !injectedValue.value.equals(currentValue.getValue())) {
if (currentValue.getValue() != null
&& !Objects.equals(currentValue.getValue(), injectedValue.value)) {
// Config property is set at runtime and the value differs from the value injected during STATIC_INIT bootstrap phase
mismatches.add(
" - the runtime value of '" + injectedValue.name + "' is [" + currentValue.getValue()
+ "] but the value [" + injectedValue.value
Expand Down
Loading