Skip to content

Commit

Permalink
Resolves merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
lordofthejars committed Nov 2, 2017
2 parents 34859ea + 9c1de0c commit 074b51a
Show file tree
Hide file tree
Showing 60 changed files with 1,046 additions and 486 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package org.arquillian.smart.testing.configuration;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.StreamSupport;
import org.arquillian.smart.testing.RunMode;
import org.arquillian.smart.testing.hub.storage.local.LocalStorage;
import org.arquillian.smart.testing.hub.storage.local.LocalStorageFileAction;
import org.arquillian.smart.testing.logger.Log;
import org.arquillian.smart.testing.logger.Logger;
import org.arquillian.smart.testing.spi.JavaSPILoader;
import org.arquillian.smart.testing.spi.StrategyConfiguration;
import org.arquillian.smart.testing.spi.TestExecutionPlannerFactory;
import org.yaml.snakeyaml.Yaml;

public class Configuration implements ConfigurationSection {
import static org.arquillian.smart.testing.configuration.ConfigurationLoader.SMART_TESTING_YML;
import static org.arquillian.smart.testing.configuration.ObjectMapper.mapToObject;

private static final Logger logger = Log.getLogger();
public class Configuration implements ConfigurationSection {

public static final String DEFAULT_MODE = "selecting";
public static final String SMART_TESTING_REPORT_ENABLE = "smart.testing.report.enable";
Expand All @@ -38,9 +36,6 @@ public class Configuration implements ConfigurationSection {
public static final String SMART_TESTING_DEBUG = "smart.testing.debug";
public static final String SMART_TESTING_AUTOCORRECT = "smart.testing.autocorrect";

public static final String SMART_TESTING_YML = "smart-testing.yml";
public static final String SMART_TESTING_YAML = "smart-testing.yaml";

private String[] strategies = new String[0];
private String[] customStrategies = new String[0];
private RunMode mode;
Expand All @@ -53,6 +48,15 @@ public class Configuration implements ConfigurationSection {
private Report report;
private Scm scm;

private Map<String, Object> strategiesConfig = new HashMap<>();

private List<StrategyConfiguration> strategiesConfiguration = new ArrayList<>();

@SuppressWarnings("unused") // Used to map YAML data to Java Objects in snakeYAML
public List<StrategyConfiguration> getStrategiesConfiguration() {
return strategiesConfiguration;
}

public String[] getStrategies() {
return strategies;
}
Expand Down Expand Up @@ -125,6 +129,14 @@ public String[] getCustomStrategies() {
return customStrategies;
}

public void setStrategiesConfiguration(List<StrategyConfiguration> strategiesConfiguration) {
this.strategiesConfiguration = strategiesConfiguration;
}

public void setStrategiesConfig(Map<String, Object> strategiesConfig) {
this.strategiesConfig = strategiesConfig;
}

public List<ConfigurationItem> registerConfigurationItems() {
List<ConfigurationItem> configItems = new ArrayList<>();
configItems.add(new ConfigurationItem("strategies", SMART_TESTING, new String[0]));
Expand All @@ -137,59 +149,37 @@ public List<ConfigurationItem> registerConfigurationItems() {
return configItems;
}

public static Configuration load() {
return load(Paths.get("").toAbsolutePath().toFile());
public void loadStrategyConfigurations(String... strategies) {
this.strategiesConfiguration = getStrategiesConfigurations(strategies);
}

public static Configuration load(File projectDir) {
final File[] files =
projectDir.listFiles((dir, name) -> name.equals(SMART_TESTING_YML) || name.equals(SMART_TESTING_YAML));

Map<String, Object> yamlConfiguration = new LinkedHashMap<>();
private List<StrategyConfiguration> getStrategiesConfigurations(String... strategies) {
List<StrategyConfiguration> convertedList = new ArrayList<>();

if (files == null) {
throw new RuntimeException("I/O errors occurs while listing dir " + projectDir);
}

if (files.length == 0) {
logger.info("Config file `" + SMART_TESTING_YAML + "` OR `" + SMART_TESTING_YML + "` is not found. "
+ "Using system properties to load configuration for smart testing.");
} else {
try (InputStream io = Files.newInputStream(getConfigurationFilePath(files))) {
final Yaml yaml = new Yaml();
yamlConfiguration = yaml.load(io);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
StreamSupport.stream(new JavaSPILoader().all(TestExecutionPlannerFactory.class).spliterator(), false)
.filter(
testExecutionPlannerFactory -> Arrays.asList(strategies).contains(testExecutionPlannerFactory.alias()))
.map(TestExecutionPlannerFactory::strategyConfiguration)
.filter(Objects::nonNull)
.forEach(strategyConfiguration -> {
final Class<StrategyConfiguration> strategyConfigurationClass =
(Class<StrategyConfiguration>) strategyConfiguration.getClass();
final Object strategyConfig = strategiesConfig.get(strategyConfiguration.name());
Map<String, Object> strategyConfigMap = new HashMap<>();
if (strategyConfig != null) {
strategyConfigMap = (Map<String, Object>) strategyConfig;
}
convertedList.add(mapToObject(strategyConfigurationClass, strategyConfigMap));
});

return parseConfiguration(yamlConfiguration);
}

public static Configuration loadPrecalculated(File projectDir) {
final File configFile =
new LocalStorage(projectDir).duringExecution().temporary().file(SMART_TESTING_YML).getFile();
if (configFile.exists()) {
return loadConfigurationFromFile(configFile);
} else {
return load(projectDir);
}
}

static Configuration loadConfigurationFromFile(File configFile) {
try (FileReader fileReader = new FileReader(configFile)) {
final Yaml yaml = new Yaml();
return yaml.loadAs(fileReader, Configuration.class);
} catch (IOException e) {
throw new RuntimeException("Failed to load configuration from file " + configFile, e);
}
return convertedList;
}

public File dump(File rootDir) {
final LocalStorageFileAction configFile = new LocalStorage(rootDir)
.duringExecution()
.temporary()
.file(Configuration.SMART_TESTING_YML);
.file(SMART_TESTING_YML);
try {
configFile.create();
} catch (IOException e) {
Expand All @@ -206,38 +196,6 @@ public File dump(File rootDir) {
}
}

// testing
public static Configuration load(Path path) {
try (InputStream io = Files.newInputStream(path)) {
final Yaml yaml = new Yaml();
Map<String, Object> yamlConfiguration = (Map<String, Object>) yaml.load(io);
return parseConfiguration(yamlConfiguration);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static Path getConfigurationFilePath(File[] files) {
if (files.length == 1) {
final File configFile = files[0];
logger.info("Using configuration from " + configFile.getName());
return configFile.toPath();
}

logger.warn("Found multiple config files with supported names: " + SMART_TESTING_YAML + ", " + SMART_TESTING_YML);
logger.warn("Using configuration from " + SMART_TESTING_YML);

return Arrays.stream(files)
.filter(file -> file.getName().equals(SMART_TESTING_YML))
.map(File::toPath)
.findFirst()
.get();
}

private static Configuration parseConfiguration(Map<String, Object> yamlConfiguration) {
return ObjectMapper.mapToObject(Configuration.class, yamlConfiguration);
}

public boolean isSelectingMode() {
return isModeSet() && RunMode.SELECTING == getMode();
}
Expand All @@ -253,4 +211,17 @@ public boolean areStrategiesDefined() {
public boolean isApplyToDefined() {
return this.applyTo != null;
}

public StrategyConfiguration getStrategyConfiguration(String strategyName) {
return this.strategiesConfiguration.stream()
.filter(strategyConfiguration -> strategyName.equals(strategyConfiguration.name()))
.findFirst()
.orElseGet(() ->
getStrategiesConfigurations(strategyName)
.stream()
.findFirst()
.orElseThrow(() ->
new RuntimeException("The configuration class of strategy " + strategyName
+ " is not available. Make sure you have correct dependencies on you classpath.")));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package org.arquillian.smart.testing.configuration;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.arquillian.smart.testing.hub.storage.local.LocalStorage;
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.ObjectMapper.mapToObject;

public class ConfigurationLoader {

public static final String SMART_TESTING_YML = "smart-testing.yml";
public static final String SMART_TESTING_YAML = "smart-testing.yaml";
private static final Logger logger = Log.getLogger();

public static Configuration load() {
return load(Paths.get("").toAbsolutePath().toFile());
}

public static Configuration load(File projectDir) {
final File[] files =
projectDir.listFiles((dir, name) -> name.equals(SMART_TESTING_YML) || name.equals(SMART_TESTING_YAML));

if (files == null) {
throw new RuntimeException("I/O errors occurs while listing dir " + projectDir);
}

Map<String, Object> yamlConfiguration = new HashMap<>(0);
if (files.length == 0) {
logger.info("Config file `" + SMART_TESTING_YAML + "` OR `" + SMART_TESTING_YML + "` is not found. "
+ "Using system properties to load configuration for smart testing.");
} else {
yamlConfiguration = getConfigParametersFromFile(getConfigurationFilePath(files));
}

final Object strategiesConfiguration = yamlConfiguration.get("strategiesConfiguration");

final Configuration configuration = parseConfiguration(yamlConfiguration);
if (strategiesConfiguration != null) {
configuration.setStrategiesConfig((Map<String, Object>) strategiesConfiguration);
}

return configuration;
}

private static Map<String, Object> getConfigParametersFromFile(Path filePath) {
try (InputStream io = Files.newInputStream(filePath)) {
final Yaml yaml = new Yaml();
Map<String, Object> yamlConfig = yaml.load(io);
if (yamlConfig == null) {
logger.warn(String.format("The configuration file %s is empty.", filePath));
return new HashMap<>();
} else {
return yamlConfig;
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public static Configuration load(File projectDir, String... strategies) {
final Configuration configuration = load(projectDir);
configuration.loadStrategyConfigurations(strategies);

return configuration;
}

public static Configuration loadPrecalculated(File projectDir) {
final File configFile =
new LocalStorage(projectDir).duringExecution().temporary().file(SMART_TESTING_YML).getFile();
if (configFile.exists()) {
return loadConfigurationFromFile(configFile);
} else {
return load(projectDir);
}
}

static Configuration loadConfigurationFromFile(File configFile) {
try (FileReader fileReader = new FileReader(configFile)) {
final Yaml yaml = new Yaml();
return yaml.loadAs(fileReader, Configuration.class);
} catch (IOException e) {
throw new RuntimeException("Failed to load configuration from file " + configFile, e);
}
}

// testing
static Configuration load(Path path) {
try (InputStream io = Files.newInputStream(path)) {
final Yaml yaml = new Yaml();
Map<String, Object> yamlConfiguration = (Map<String, Object>) yaml.load(io);
return parseConfiguration(yamlConfiguration);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static Path getConfigurationFilePath(File[] files) {
if (files.length == 1) {
final File configFile = files[0];
logger.info("Using configuration from " + configFile.getName());
return configFile.toPath();
}

logger.warn("Found multiple config files with supported names: " + SMART_TESTING_YAML + ", " + SMART_TESTING_YML);
logger.warn("Using configuration from " + SMART_TESTING_YML);

return Arrays.stream(files)
.filter(file -> file.getName().equals(SMART_TESTING_YML))
.map(File::toPath)
.findFirst()
.get();
}

private static Configuration parseConfiguration(Map<String, Object> yamlConfiguration) {
return mapToObject(Configuration.class, yamlConfiguration);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.arquillian.smart.testing.api.TestStrategyApplier;
import org.arquillian.smart.testing.api.TestVerifier;
import org.arquillian.smart.testing.configuration.Configuration;
import org.arquillian.smart.testing.configuration.ConfigurationLoader;
import org.arquillian.smart.testing.spi.JavaSPILoader;

public class ConfiguredSmartTestingImpl implements ConfiguredSmartTesting {
Expand Down Expand Up @@ -58,7 +59,7 @@ private TestStrategyApplier createApplier() {
projectDir = new File(basedir != null ? basedir : ".");
}
if (configuration == null) {
configuration = Configuration.loadPrecalculated(projectDir);
configuration = ConfigurationLoader.loadPrecalculated(projectDir);
}
if (testExecutionPlannerLoader == null) {
testExecutionPlannerLoader =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Set;
import java.util.stream.Collectors;
import org.arquillian.smart.testing.configuration.Configuration;
import org.arquillian.smart.testing.configuration.ConfigurationLoader;
import org.arquillian.smart.testing.configuration.Scm;
import org.arquillian.smart.testing.logger.Log;
import org.arquillian.smart.testing.logger.Logger;
Expand Down Expand Up @@ -55,7 +56,7 @@ private void closeGitIfExists() {
public Set<Change> diff(File projectDir, Configuration configuration) {
Scm scm;
if (configuration == null){
scm = Configuration.loadPrecalculated(projectDir).getScm();
scm = ConfigurationLoader.loadPrecalculated(projectDir).getScm();
} else {
scm = configuration.getScm();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.arquillian.smart.testing.spi;

import org.arquillian.smart.testing.configuration.ConfigurationSection;

public interface StrategyConfiguration extends ConfigurationSection {

String name();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface TestExecutionPlannerFactory {

TestExecutionPlanner create(File projectDir, TestVerifier testVerifier, Configuration configuration);

StrategyConfiguration strategyConfiguration();

}
Loading

0 comments on commit 074b51a

Please sign in to comment.