Skip to content

Commit

Permalink
Config: detect injected config value mismatch for missing values
Browse files Browse the repository at this point in the history
- resolves #37444
- follow-up of #36281

(cherry picked from commit bd98d02)
  • Loading branch information
mkouba authored and gsmet committed Dec 4, 2023
1 parent 0469ec8 commit 58db82b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
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

0 comments on commit 58db82b

Please sign in to comment.