Skip to content

Commit

Permalink
Merge acbe1cf into 1a06df1
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler authored Aug 5, 2024
2 parents 1a06df1 + acbe1cf commit 0f678e5
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -52,6 +49,8 @@ public class Main {

private static final String CMDLINE_OPTION_PROFILES = "-profiles";

private static final Set<String> IGNORE_PROPERTIES = Set.of("jqassistant.opts", "jqassistant.home"); // env variables provided by jqassistant shell scripts

/**
* The main method.
*
Expand Down Expand Up @@ -245,6 +244,7 @@ private CliConfiguration getCliConfiguration(CommandLine commandLine, File worki
.withClasspath()
.withEnvVariables()
.withProfiles(profiles)
.withIgnoreProperties(IGNORE_PROPERTIES)
.load(configSource, new SysPropConfigSource(), commandLineProperties, mavenSettingsConfigSource);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@
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 io.smallrye.config.EnvConfigSource;
import io.smallrye.config.ExpressionConfigSourceInterceptor;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;
import java.util.*;

import io.smallrye.config.*;
import io.smallrye.config.source.yaml.YamlConfigSource;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.microprofile.config.spi.ConfigSource;
Expand All @@ -25,8 +20,8 @@
import static java.nio.file.Files.walkFileTree;
import static java.util.Collections.emptyList;
import static java.util.Collections.list;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toUnmodifiableList;
import static java.util.stream.Collectors.*;
import static java.util.stream.StreamSupport.stream;

/**
* Defines the interface for loading runtime configuration.
Expand Down Expand Up @@ -98,6 +93,8 @@ public static class Builder<C extends Configuration> {

private final List<String> profiles = new ArrayList<>();

private final Set<String> ignoreProperties = new HashSet<>();

private Builder(Class<C> configurationMapping, List<String> configLocations) {
this.configurationMapping = configurationMapping;
if (configLocations.isEmpty()) {
Expand Down Expand Up @@ -185,6 +182,18 @@ public Builder<C> withProfiles(List<String> profiles) {
return this;
}

/**
* Add properties to ignore.
*
* @param ignoreProperties
* The properties to ignore.
* @return The {@link Builder}.
*/
public Builder<C> withIgnoreProperties(Collection<String> ignoreProperties) {
this.ignoreProperties.addAll(ignoreProperties);
return this;
}

/**
* Load the {@link Configuration} using the given directory including
* <p/>
Expand All @@ -197,12 +206,20 @@ public Builder<C> withProfiles(List<String> profiles) {
* @return The {@link Configuration}.
*/
public C load(ConfigSource... additionalConfigSources) {
SmallRyeConfig config = new SmallRyeConfigBuilder().withMapping(configurationMapping)
.withSources(this.configSources)
// Create intermediate configuration with applied profiles and interpolated properties (without validation)
SmallRyeConfig interpolatedConfig = new SmallRyeConfigBuilder().withSources(this.configSources)
.withSources(additionalConfigSources)
.withValidateUnknown(false)
.withInterceptors(new ExpressionConfigSourceInterceptor())
.withProfiles(this.profiles)
.withInterceptors(new ExpressionConfigSourceInterceptor())
.withValidateUnknown(false)
.build();
// Create final config including validation, including only jqassistant properties
Map<String, String> 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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,18 @@ protected MappingNode representJavaBean(Set<Property> properties, Object javaBea
* - value to be represented
* @param customTag
* - user defined Tag
* @return
* @return The {@link NodeTuple}.
*/
@Override
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) {
return isNullOrEmpty(propertyValue) ? null : super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
}

private static boolean isNullOrEmpty(Object propertyValue) {
return propertyValue == null || (propertyValue instanceof Optional<?> && !((Optional<?>) propertyValue).isPresent());
return propertyValue == null //
|| (propertyValue instanceof Optional<?> && ((Optional<?>) propertyValue).isEmpty()) //
|| (propertyValue instanceof Collection && ((Collection<?>) propertyValue).isEmpty()) //
|| (propertyValue instanceof Map && ((Map<?, ?>) propertyValue).isEmpty());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
package com.buschmais.jqassistant.core.runtime.impl.configuration;

import java.io.File;
import java.net.URISyntaxException;
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}.
Expand All @@ -29,7 +34,7 @@ class ConfigurationMappingLoaderTest {
* Load all yaml/yml config files from the working directory.
*/
@Test
void loadFromDefaultConfigLocations() throws URISyntaxException {
void loadFromDefaultConfigLocations() {
TestConfiguration configuration = getConfiguration(emptyList());

assertThat(configuration).isNotNull();
Expand Down Expand Up @@ -78,6 +83,31 @@ 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,3 @@ jqassistant:
artifact-id: full-test-plugin
type: jqp
version: 1.0.0
active: false
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
invoker.goals.1 = install \
-Djqassistant.store.directory=${project.build.directory}/it/multimodule/multiparent/singlestore/target/jqassistant/store \
-Djqassistant.store.reset=false \
-Djqassistant.store.uri=file://${project.build.directory}/it/multimodule/multiparent/singlestore/target/jqassistant/store \
-Djqassistant.scan.reset=false \
-T2 \
-e
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
assert new File(basedir, 'target/jqassistant').exists()
assert !new File(basedir, 'module1/target/jqassistant').exists()
assert !new File(basedir, 'module2/target/jqassistant').exists()
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public abstract class AbstractMojo extends org.apache.maven.plugin.AbstractMojo

private static final int CONFIGURATION_ORDINAL_EXECUTION_ROOT = 100;

private static final String PROPERTY_CONFIGURATION_LOCATIONS = "jqassistant.configuration.locations";

private static String createExecutionKey(MojoExecution mojoExecution) {
// Do NOT use a custom class for execution keys, as different modules may use
// different classloaders
Expand All @@ -54,7 +56,7 @@ private static String createExecutionKey(MojoExecution mojoExecution) {
/**
* The config locations.
*/
@Parameter(property = "jqassistant.configuration.locations")
@Parameter(property = PROPERTY_CONFIGURATION_LOCATIONS)
private List<String> configurationLocations;

@Parameter
Expand Down Expand Up @@ -276,7 +278,8 @@ private MavenConfiguration getConfiguration() {
.withEnvVariables()
.withClasspath()
.withProfiles(session.getProjectBuildingRequest()
.getActiveProfileIds());
.getActiveProfileIds())
.withIgnoreProperties(Set.of(PROPERTY_CONFIGURATION_LOCATIONS));
if (!executionRootDirectory.equals(currentProject.getBasedir())) {
builder.withWorkingDirectory(currentProject.getBasedir());
}
Expand Down

0 comments on commit 0f678e5

Please sign in to comment.