From aa6198e7fe3e2abba3151b878dfaf53bffa23ecf Mon Sep 17 00:00:00 2001 From: Dirk Mahler Date: Fri, 2 Aug 2024 19:12:18 +0200 Subject: [PATCH] #569 added option to ignore jQA properties --- .../jqassistant/commandline/Main.java | 8 +++---- .../ConfigurationMappingLoader.java | 22 ++++++++++++++----- .../ConfigurationMappingLoaderTest.java | 14 ++++++++++++ 3 files changed, 35 insertions(+), 9 deletions(-) diff --git a/cli/application/src/main/java/com/buschmais/jqassistant/commandline/Main.java b/cli/application/src/main/java/com/buschmais/jqassistant/commandline/Main.java index 5efe8701a8..a8a205bcf8 100644 --- a/cli/application/src/main/java/com/buschmais/jqassistant/commandline/Main.java +++ b/cli/application/src/main/java/com/buschmais/jqassistant/commandline/Main.java @@ -1,10 +1,7 @@ package com.buschmais.jqassistant.commandline; import java.io.File; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import com.buschmais.jqassistant.commandline.configuration.CliConfiguration; import com.buschmais.jqassistant.commandline.plugin.ArtifactProviderFactory; @@ -52,6 +49,8 @@ public class Main { private static final String CMDLINE_OPTION_PROFILES = "-profiles"; + private static final Set IGNORE_PROPERTIES = Set.of("jqassistant.opts", "jqassistant.home"); // env variables provided by jqassistant shell scripts + /** * The main method. * @@ -245,6 +244,7 @@ private CliConfiguration getCliConfiguration(CommandLine commandLine, File worki .withClasspath() .withEnvVariables() .withProfiles(profiles) + .withIgnoreProperties(IGNORE_PROPERTIES) .load(configSource, new SysPropConfigSource(), commandLineProperties, mavenSettingsConfigSource); } 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 4417fe2489..322fa0f1d2 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 @@ -9,10 +9,7 @@ import java.nio.file.Paths; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; -import java.util.Enumeration; -import java.util.List; -import java.util.Map; +import java.util.*; import io.smallrye.config.*; import io.smallrye.config.source.yaml.YamlConfigSource; @@ -96,6 +93,8 @@ public static class Builder { private final List profiles = new ArrayList<>(); + private final Set ignoreProperties = new HashSet<>(); + private Builder(Class configurationMapping, List configLocations) { this.configurationMapping = configurationMapping; if (configLocations.isEmpty()) { @@ -183,6 +182,18 @@ public Builder withProfiles(List profiles) { return this; } + /** + * Add properties to ignore. + * + * @param ignoreProperties + * The properties to ignore. + * @return The {@link Builder}. + */ + public Builder withIgnoreProperties(Collection ignoreProperties) { + this.ignoreProperties.addAll(ignoreProperties); + return this; + } + /** * Load the {@link Configuration} using the given directory including *

@@ -205,9 +216,10 @@ public C load(ConfigSource... additionalConfigSources) { // 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, "Interpolated Configuration", ConfigSource.DEFAULT_ORDINAL)) + .withSources(new PropertiesConfigSource(interpolatedProperties, "jQAssistant Configuration", ConfigSource.DEFAULT_ORDINAL)) .build(); C configMapping = config.getConfigMapping(configurationMapping); if (log.isDebugEnabled()) { 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 22e959360b..ff50bd1aeb 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 @@ -3,6 +3,7 @@ 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; @@ -94,6 +95,19 @@ void unknownProperty() { .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() {