Skip to content

Commit

Permalink
Implemented map level inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
dipak-pawar committed Nov 24, 2017
1 parent bf8964b commit 220e383
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 182 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,14 @@ public static Configuration load(File projectDir, Function<File, Boolean> stopCo
configFile = projectDir;
}

return loadInheritedConfiguration(configFile);
return loadEffectiveConfiguration(configFile);
}

private static Configuration loadInheritedConfiguration(File configFile) {
Path configFileDir = configFile.isFile()? configFile.getParentFile().toPath(): configFile.toPath();
private static Configuration loadEffectiveConfiguration(File configFile) {
ConfigurationReader configurationReader = new ConfigurationReader();
final Map<String, Object> effectiveConfig = configurationReader.readConfiguration(configFile);

final ConfigurationReader configurationReader = new ConfigurationReader();
final Map<String, Object> yamlConfiguration = configurationReader.readConfiguration(configFile);

final Configuration configuration = ConfigurationLoader.loadAsConfiguration(yamlConfiguration);

final ConfigurationInheriter inheriter = new ConfigurationInheriter();
return inheriter.overWriteNotDefinedValuesFromInherit(configuration, configFileDir);
return ConfigurationLoader.loadAsConfiguration(effectiveConfig);
}

/**
Expand Down Expand Up @@ -117,11 +112,6 @@ static Configuration loadConfigurationFromFile(File configFile) {
}
}

// testing
static Configuration load(Path path) {
return loadInheritedConfiguration(path.toFile());
}

private static Configuration loadAsConfiguration(Map<String, Object> yamlConfiguration) {
final Object strategiesConfiguration = yamlConfiguration.get("strategiesConfiguration");
final ObjectMapper objectMapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.arquillian.smart.testing.logger.Log;
import org.arquillian.smart.testing.logger.Logger;
import org.yaml.snakeyaml.Yaml;

import static org.arquillian.smart.testing.configuration.Configuration.INHERIT;
import static org.arquillian.smart.testing.configuration.ConfigurationLoader.SMART_TESTING_YAML;
import static org.arquillian.smart.testing.configuration.ConfigurationLoader.SMART_TESTING_YML;

Expand All @@ -22,7 +25,7 @@ class ConfigurationReader {

Map<String, Object> readConfiguration(File configPath) {
if (!configPath.isDirectory()) {
return getConfigParametersFromFile(getConfigurationFilePath(configPath));
return readEffectiveConfig(getConfigurationFilePath(configPath));
}

final File[] files =
Expand All @@ -36,12 +39,12 @@ Map<String, Object> readConfiguration(File configPath) {
logger.info("Config file `" + SMART_TESTING_YAML + "` OR `" + SMART_TESTING_YML + "` is not found. "
+ "Using system properties to load configuration for smart testing.");
} else {
return getConfigParametersFromFile(getConfigurationFilePath(files));
return readEffectiveConfig(getConfigurationFilePath(files));
}
return Collections.emptyMap();
}

Map<String, Object> getConfigParametersFromFile(Path filePath) {
private Map<String, Object> getConfigParametersFromFile(Path filePath) {
if (!filePath.toFile().exists()) {
logger.warn(String.format("The configuration file %s is not exists.", filePath));
return Collections.emptyMap();
Expand Down Expand Up @@ -88,4 +91,27 @@ private Path getDefaultConfigFile(File... files) {
.findFirst()
.get();
}

private Map<String, Object> readEffectiveConfig(Path filePath){
Map<String, Object> config = getConfigParametersFromFile(filePath);

List<Map<String, Object>> configs = new ArrayList<>();
configs.add(config);
while (config.get(INHERIT) != null) {
String inherit = String.valueOf(config.get(INHERIT));
filePath = filePath.getParent().resolve(inherit);
config = getConfigParametersFromFile(filePath);
if (!config.isEmpty()) {
configs.add(0, config);
}
}
Map<String, Object> effectiveConfig = configs.get(0);
for (int i = 1; i < configs.size(); i++){
effectiveConfig.putAll(configs.get(i));
}

effectiveConfig.remove(INHERIT);

return effectiveConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <T extends ConfigurationSection> T readValue(Class<T> aClass, Map<String, Object
return instance;
}

<T> void invokeMethodWithMappedValue(List<ConfigurationItem> configItems, Method method, T instance,
private <T> void invokeMethodWithMappedValue(List<ConfigurationItem> configItems, Method method, T instance,
Map<String, Object> map) {
method.setAccessible(true);
if (method.getParameterTypes().length != 1) {
Expand Down Expand Up @@ -228,7 +228,7 @@ private <T> List<T> getConvertedList(Class<T> parameterType, Object mappedValue)
return null;
}

boolean isSetter(Method candidate) {
private boolean isSetter(Method candidate) {
return candidate.getName().matches("^(set|add)[A-Z].*")
&& (candidate.getReturnType().equals(Void.TYPE) || candidate.getReturnType()
.equals(candidate.getDeclaringClass()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,27 +46,4 @@ public List<ConfigurationItem> registerConfigurationItems() {

return configItems;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

final Range range = (Range) o;

return (head != null ? head.equals(range.head) : range.head == null)
&& (tail != null ? tail.equals(range.tail) : range.tail == null);
}

@Override
public int hashCode() {
int result = head != null ? head.hashCode() : 0;
result = 31 * result + (tail != null ? tail.hashCode() : 0);

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,4 @@ public List<ConfigurationItem> registerConfigurationItems() {
configItems.add(new ConfigurationItem("dir", null, TARGET));
return configItems;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

final Report report = (Report) o;

return enable == report.enable;
}

@Override
public int hashCode() {
return (enable ? 1 : 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,4 @@ public List<ConfigurationItem> registerConfigurationItems() {

return configItems;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

final Scm scm = (Scm) o;

return range != null ? range.equals(scm.range) : scm.range == null;
}

@Override
public int hashCode() {
return range != null ? range.hashCode() : 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,44 @@
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

class ConfigurationFile {
class ConfigurationFileBuilder {

private HashMap<String, Object> properties;

private ConfigurationFile() {
private ConfigurationFileBuilder() {
properties = new HashMap<>();
}

static ConfigurationFile SmartTestingConfigurationFile() {
return new ConfigurationFile();
static ConfigurationFileBuilder SmartTestingConfigurationFile() {
return new ConfigurationFileBuilder();
}

ConfigurationFile inherit(String inherit) {
ConfigurationFileBuilder inherit(String inherit) {
properties.put("inherit", inherit);
return this;
}

ConfigurationFile mode(String mode) {
ConfigurationFileBuilder mode(String mode) {
properties.put("mode", mode);
return this;
}

ConfigurationFile applyTo(String applyTo) {
ConfigurationFileBuilder applyTo(String applyTo) {
properties.put("applyTo", applyTo);
return this;
}

ConfigurationFile strategies(String strategies) {
ConfigurationFileBuilder strategies(String strategies) {
properties.put("strategies", strategies);
return this;
}

ConfigurationFile debug(boolean debug) {
ConfigurationFileBuilder debug(boolean debug) {
properties.put("debug", debug);
return this;
}

ConfigurationFile disable(boolean disable) {
ConfigurationFileBuilder disable(boolean disable) {
properties.put("disable", disable);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import static org.arquillian.smart.testing.RunMode.ORDERING;
import static org.arquillian.smart.testing.RunMode.SELECTING;
import static org.arquillian.smart.testing.configuration.ConfigurationFile.SmartTestingConfigurationFile;
import static org.arquillian.smart.testing.configuration.ConfigurationFileBuilder.SmartTestingConfigurationFile;
import static org.arquillian.smart.testing.configuration.ConfigurationLoader.SMART_TESTING_YAML;
import static org.arquillian.smart.testing.configuration.ConfigurationLoader.SMART_TESTING_YML;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -66,7 +66,7 @@ public void should_load_configuration_properties_from_relative_inherit_if_not_de
.create(Paths.get(root, SMART_TESTING_YML));

// when
final Configuration configuration = ConfigurationLoader.load(Paths.get(root, CONFIG, IMPL_BASE));
final Configuration configuration = ConfigurationLoader.load(Paths.get(root, CONFIG, IMPL_BASE).toFile());

// then
assertThat(configuration.getMode()).isEqualTo(SELECTING);
Expand All @@ -92,7 +92,7 @@ public void should_not_overwrite_disable_parameter_from_inherit() throws IOExcep
.create(Paths.get(root, SMART_TESTING_YML));

// when
final Configuration configuration = ConfigurationLoader.load(Paths.get(root, CONFIG));
final Configuration configuration = ConfigurationLoader.load(Paths.get(root, CONFIG).toFile());

// then
assertThat(configuration.getMode()).isEqualTo(ORDERING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import static org.arquillian.smart.testing.RunMode.ORDERING;
import static org.arquillian.smart.testing.configuration.Configuration.SMART_TESTING;
import static org.arquillian.smart.testing.configuration.ConfigurationFile.SmartTestingConfigurationFile;
import static org.arquillian.smart.testing.configuration.ConfigurationFileBuilder.SmartTestingConfigurationFile;
import static org.arquillian.smart.testing.configuration.ConfigurationLoader.SMART_TESTING_YML;
import static org.arquillian.smart.testing.configuration.ConfigurationOverWriteUsingInheritTest.CONFIG;
import static org.arquillian.smart.testing.scm.ScmRunnerProperties.HEAD;
Expand Down Expand Up @@ -50,7 +50,7 @@ public void system_properties_should_take_precedence_over_config_file() throws I
range.setTail(HEAD + "~3");

// when
final Configuration configuration = ConfigurationLoader.load(Paths.get(root, CONFIG));
final Configuration configuration = ConfigurationLoader.load(Paths.get(root, CONFIG).toFile());

// then
assertThat(configuration.getStrategies()).isEqualTo(new String[] {"changed"});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void should_load_configuration_with_default_values_if_property_is_not_spe

// when
final Configuration actualConfiguration =
ConfigurationLoader.load(getResourceAsPath("configuration/smart-testing.yml"));
ConfigurationLoader.load(getResourceAsFile("configuration/smart-testing.yml"));

// then
assertThat(actualConfiguration).isEqualToComparingFieldByFieldRecursively(expectedConfiguration);
Expand Down
Loading

0 comments on commit 220e383

Please sign in to comment.