From a4c9859578b4c32632572c1641cf7ed4bb3efd4a Mon Sep 17 00:00:00 2001 From: Dirk Mahler Date: Wed, 7 Aug 2024 19:46:56 +0200 Subject: [PATCH] #569 validate unknown properties only when debug is active --- .../ConfigurationMappingLoader.java | 30 +++++++++++++----- .../ConfigurationMappingLoaderTest.java | 31 ------------------- 2 files changed, 22 insertions(+), 39 deletions(-) diff --git a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java index a4dd02bba..d73fa06a8 100644 --- a/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java +++ b/core/runtime/src/main/java/com/buschmais/jqassistant/core/runtime/api/configuration/ConfigurationMappingLoader.java @@ -207,25 +207,39 @@ public Builder withIgnoreProperties(Collection ignoreProperties) { */ public C load(ConfigSource... additionalConfigSources) { // Create intermediate configuration with applied profiles and interpolated properties (without validation) - SmallRyeConfig interpolatedConfig = new SmallRyeConfigBuilder().withSources(this.configSources) + SmallRyeConfig config = new SmallRyeConfigBuilder().withSources(this.configSources) .withSources(additionalConfigSources) .withProfiles(this.profiles) .withInterceptors(new ExpressionConfigSourceInterceptor()) + .withMapping(configurationMapping) .withValidateUnknown(false) .build(); + if (log.isDebugEnabled()) { + logConfigProblems(config); + } + C configMapping = config.getConfigMapping(configurationMapping); + if (log.isDebugEnabled()) { + log.debug("Loaded configuration from {} config sources:\n{}", additionalConfigSources.length, configurationSerializer.toYaml(configMapping)); + } + return configMapping; + } + + private void logConfigProblems(SmallRyeConfig interpolatedConfig) { // Create final config including validation, including only jqassistant properties Map interpolatedProperties = stream(interpolatedConfig.getPropertyNames() .spliterator(), false).filter(property -> property.startsWith(Configuration.PREFIX)) .filter(property -> !ignoreProperties.contains(property)) .collect(toMap(property -> property, interpolatedConfig::getRawValue)); - SmallRyeConfig config = new SmallRyeConfigBuilder().withMapping(configurationMapping) - .withSources(new PropertiesConfigSource(interpolatedProperties, "jQAssistant Configuration", ConfigSource.DEFAULT_ORDINAL)) - .build(); - C configMapping = config.getConfigMapping(configurationMapping); - if (log.isDebugEnabled()) { - log.debug("Loaded configuration from {} config sources:\n{}", additionalConfigSources.length, configurationSerializer.toYaml(configMapping)); + try { + new SmallRyeConfigBuilder().withMapping(configurationMapping) + .withSources(new PropertiesConfigSource(interpolatedProperties, "jQAssistant Configuration", ConfigSource.DEFAULT_ORDINAL)) + .build(); + } catch (ConfigValidationException configValidationException) { + for (int i = 0; i < configValidationException.getProblemCount(); i++) { + log.debug(configValidationException.getProblem(i) + .getMessage()); + } } - return configMapping; } private List getExternalYamlConfigSources(File directory, List configLocations, int ordinal) { diff --git a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationMappingLoaderTest.java b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationMappingLoaderTest.java index ff50bd1ae..f99a73715 100644 --- a/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationMappingLoaderTest.java +++ b/core/runtime/src/test/java/com/buschmais/jqassistant/core/runtime/impl/configuration/ConfigurationMappingLoaderTest.java @@ -2,24 +2,18 @@ import java.io.File; import java.util.List; -import java.util.Map; -import java.util.Set; import com.buschmais.jqassistant.core.runtime.api.configuration.ConfigurationMappingLoader; import com.buschmais.jqassistant.core.scanner.api.configuration.Scan; import com.buschmais.jqassistant.core.shared.configuration.Plugin; -import io.smallrye.config.ConfigValidationException; -import io.smallrye.config.PropertiesConfigSource; import io.smallrye.config.SysPropConfigSource; -import org.eclipse.microprofile.config.spi.ConfigSource; import org.junit.jupiter.api.Test; import org.junitpioneer.jupiter.SetEnvironmentVariable; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * Tests for the {@link ConfigurationMappingLoader}. @@ -83,31 +77,6 @@ void profile() { .properties()).containsEntry("profile-user-value", "test-value"); } - @Test - void unknownProperty() { - String unknownProperty = "jqassistant.unknown"; - assertThatExceptionOfType(ConfigValidationException.class).isThrownBy(() -> { - ConfigurationMappingLoader.builder(TestConfiguration.class, emptyList()) - .withUserHome(USER_HOME) - .withWorkingDirectory(WORKING_DIRECTORY) - .load(new PropertiesConfigSource(Map.of(unknownProperty, "test value"), "Test", ConfigSource.DEFAULT_ORDINAL)); - }) - .withMessageContaining(unknownProperty); - } - - @Test - void ignoreProperty() { - String unknownProperty = "jqassistant.unknown"; - - TestConfiguration configuration = ConfigurationMappingLoader.builder(TestConfiguration.class, emptyList()) - .withUserHome(USER_HOME) - .withWorkingDirectory(WORKING_DIRECTORY) - .withIgnoreProperties(Set.of(unknownProperty)) - .load(new PropertiesConfigSource(Map.of(unknownProperty, "test value"), "Test", ConfigSource.DEFAULT_ORDINAL)); - - assertThat(configuration).isNotNull(); - } - @Test @SetEnvironmentVariable(key = "jqassistant_scan_continue_on_error", value = "false") void overrideFromEnvVariable() {