Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Main, DSpot and Amplification #930

Merged
merged 14 commits into from
Nov 28, 2019
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import eu.stamp_project.prettifier.output.report.ReportJSON;
import eu.stamp_project.test_framework.TestFramework;
import eu.stamp_project.utils.compilation.DSpotCompiler;
import eu.stamp_project.utils.configuration.DSpotState;
import eu.stamp_project.utils.configuration.InitializeDSpot;
import eu.stamp_project.utils.options.check.Checker;
import eu.stamp_project.utils.options.check.InputErrorException;
import org.slf4j.Logger;
Expand All @@ -33,7 +35,6 @@
import java.util.*;
import java.util.stream.Collectors;

import static eu.stamp_project.Main.completeDependencies;

/**
* created by Benjamin DANGLOT
Expand Down Expand Up @@ -75,7 +76,7 @@ public static void main(String[] args) {
commandLine.usage(System.err);
return;
}
eu.stamp_project.Main.verbose = inputConfiguration.isVerbose();
DSpotState.verbose = inputConfiguration.isVerbose();
run(inputConfiguration);
}

Expand All @@ -100,15 +101,15 @@ public static CtType<?> loadAmplifiedTestClass(InputConfiguration configuration)

public static List<CtMethod<?>> run(CtType<?> amplifiedTestClass,
InputConfiguration configuration) {

InitializeDSpot initializeDSpot = new InitializeDSpot();
final AutomaticBuilder automaticBuilder = configuration.getBuilderEnum().getAutomaticBuilder(configuration);
final String dependencies = completeDependencies(configuration, automaticBuilder);
final String dependencies = initializeDSpot.completeDependencies(configuration, automaticBuilder);
final DSpotCompiler compiler = DSpotCompiler.createDSpotCompiler(
configuration,
dependencies
);
configuration.setFactory(compiler.getLauncher().getFactory());
eu.stamp_project.Main.initHelpers(configuration);
initializeDSpot.initHelpers(configuration);

final List<CtMethod<?>> testMethods = TestFramework.getAllTest(amplifiedTestClass);
Main.report.nbTestMethods = testMethods.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import eu.stamp_project.dspot.amplifier.value.ValueCreator;
import eu.stamp_project.utils.RandomHelper;
import eu.stamp_project.utils.configuration.DSpotState;
import eu.stamp_project.utils.options.AutomaticBuilderEnum;
import eu.stamp_project.utils.program.InputConfiguration;
import org.junit.Before;
Expand All @@ -28,7 +29,7 @@ public void setUp() throws Exception {
this.configuration.setVerbose(true);
this.configuration.setBuilderEnum(AutomaticBuilderEnum.Maven);
this.configuration.setGregorMode(true);
Main.verbose = true;
DSpotState.verbose = true;
Utils.init(this.configuration);
}
}
11 changes: 5 additions & 6 deletions dspot-prettifier/src/test/java/eu/stamp_project/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import eu.stamp_project.automaticbuilder.AutomaticBuilder;
import eu.stamp_project.test_framework.TestFramework;
import eu.stamp_project.utils.compilation.DSpotCompiler;
import eu.stamp_project.utils.configuration.DSpotState;
import eu.stamp_project.utils.configuration.InitializeDSpot;
import eu.stamp_project.utils.program.InputConfiguration;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spoon.reflect.code.CtLiteral;
Expand All @@ -15,12 +16,9 @@
import spoon.reflect.visitor.chain.CtQueryable;
import spoon.reflect.visitor.filter.TypeFilter;

import java.io.File;
import java.util.List;
import java.util.Set;

import static eu.stamp_project.Main.completeDependencies;


/**
* User: Simon
Expand All @@ -34,14 +32,15 @@ public class Utils {
private static InputConfiguration configuration;

public static void init(InputConfiguration configuration) {
InitializeDSpot initializeDSpot = new InitializeDSpot();
final AutomaticBuilder automaticBuilder = configuration.getBuilderEnum().getAutomaticBuilder(configuration);
final String dependencies = completeDependencies(configuration, automaticBuilder);
final String dependencies = initializeDSpot.completeDependencies(configuration, automaticBuilder);
final DSpotCompiler compiler = DSpotCompiler.createDSpotCompiler(
configuration,
dependencies
);
configuration.setFactory(compiler.getLauncher().getFactory());
eu.stamp_project.Main.initHelpers(configuration);
initializeDSpot.initHelpers(configuration);
Utils.configuration = configuration;
}

Expand Down
235 changes: 15 additions & 220 deletions dspot/src/main/java/eu/stamp_project/Main.java
Original file line number Diff line number Diff line change
@@ -1,59 +1,26 @@
package eu.stamp_project;

import eu.stamp_project.automaticbuilder.AutomaticBuilder;
import eu.stamp_project.automaticbuilder.maven.DSpotPOMCreator;
import eu.stamp_project.dspot.amplifier.Amplifier;
import eu.stamp_project.dspot.assertiongenerator.assertiongenerator.AssertionGeneratorUtils;
import eu.stamp_project.dspot.input_ampl_distributor.InputAmplDistributor;
import eu.stamp_project.dspot.selector.TestSelector;
import eu.stamp_project.test_framework.TestFramework;
import eu.stamp_project.utils.*;
import eu.stamp_project.utils.collector.Collector;
import eu.stamp_project.dspot.DSpot;
import eu.stamp_project.utils.collector.CollectorFactory;
import eu.stamp_project.utils.compilation.DSpotCompiler;
import eu.stamp_project.utils.compilation.TestCompiler;
import eu.stamp_project.utils.options.AmplifierEnum;
import eu.stamp_project.utils.options.check.Checker;
import eu.stamp_project.utils.options.check.InputErrorException;
import eu.stamp_project.utils.report.output.Output;
import eu.stamp_project.utils.program.InputConfiguration;
import eu.stamp_project.utils.report.GlobalReport;
import eu.stamp_project.utils.report.error.ErrorReportImpl;
import eu.stamp_project.utils.report.output.OutputReportImpl;
import eu.stamp_project.utils.report.output.selector.TestSelectorReportImpl;
import eu.stamp_project.utils.smtp.EmailSender;
import eu.stamp_project.utils.test_finder.TestFinder;
import org.apache.commons.io.FileUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import picocli.CommandLine;
import spoon.reflect.declaration.CtType;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static eu.stamp_project.utils.AmplificationHelper.PATH_SEPARATOR;

/**
* Created by Benjamin DANGLOT [email protected] on 2/9/17
*/
public class Main {

public static final GlobalReport GLOBAL_REPORT =
new GlobalReport(new OutputReportImpl(), new ErrorReportImpl(), new TestSelectorReportImpl());

private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);

public static boolean verbose;

public static void main(String[] args) {
InputConfiguration inputConfiguration = parse(args);
if(inputConfiguration == null){
return;
}
final DSpot dspot = new DSpot(inputConfiguration);
dspot.run();
}

public static InputConfiguration parse(String[] args) {
InputConfiguration inputConfiguration = new InputConfiguration();
final CommandLine commandLine = new CommandLine(inputConfiguration);
commandLine.setUsageHelpWidth(120);
Expand All @@ -62,15 +29,15 @@ public static void main(String[] args) {
} catch (Exception e) {
e.printStackTrace();
commandLine.usage(System.err);
return;
return null;
}
if (commandLine.isUsageHelpRequested()) {
commandLine.usage(System.out);
return;
return null;
}
if (commandLine.isVersionHelpRequested()) {
commandLine.printVersionHelp(System.out);
return;
return null;
}
if (inputConfiguration.shouldRunExample()) {
inputConfiguration.configureExample();
Expand All @@ -80,180 +47,8 @@ public static void main(String[] args) {
} catch (InputErrorException e) {
e.printStackTrace();
commandLine.usage(System.err);
return;
return null;
}
Main.verbose = inputConfiguration.isVerbose();
run(inputConfiguration);
return inputConfiguration;
}

public static void run(InputConfiguration inputConfiguration) {
final long startTime = System.currentTimeMillis();
final TestFinder testFinder = new TestFinder(
Arrays.stream(inputConfiguration.getExcludedClasses().split(",")).collect(Collectors.toList()),
Arrays.stream(inputConfiguration.getExcludedTestCases().split(",")).collect(Collectors.toList())
);
if (!inputConfiguration.getPathToTestListCSV().isEmpty()) {
inputConfiguration.initTestsToBeAmplified();
}
final AutomaticBuilder automaticBuilder = inputConfiguration.getBuilderEnum().getAutomaticBuilder(inputConfiguration);
final String dependencies = completeDependencies(inputConfiguration, automaticBuilder);
final DSpotCompiler compiler = DSpotCompiler.createDSpotCompiler(
inputConfiguration,
dependencies
);
inputConfiguration.setFactory(compiler.getLauncher().getFactory());
initHelpers(inputConfiguration);
final TestCompiler testCompiler = new TestCompiler(
inputConfiguration.getNumberParallelExecutionProcessors(),
inputConfiguration.shouldExecuteTestsInParallel(),
inputConfiguration.getAbsolutePathToProjectRoot(),
inputConfiguration.getClasspathClassesProject(),
inputConfiguration.getTimeOutInMs(),
inputConfiguration.getPreGoalsTestExecution(),
inputConfiguration.shouldUseMavenToExecuteTest()
);
final EmailSender emailSender = new EmailSender(
inputConfiguration.getSmtpUsername(),
inputConfiguration.getSmtpPassword(),
inputConfiguration.getSmtpHost(),
inputConfiguration.getSmtpPort(),
inputConfiguration.isSmtpAuth(),
inputConfiguration.getSmtpTls()
);
final Collector collector = CollectorFactory.build(inputConfiguration, emailSender);
collector.reportInitInformation(
inputConfiguration.getAmplifiers(),
inputConfiguration.getSelector(),
inputConfiguration.getNbIteration(),
inputConfiguration.isGregorMode(),
!inputConfiguration.isGregorMode(),
inputConfiguration.getNumberParallelExecutionProcessors()
);
final List<CtType<?>> testClassesToBeAmplified = testFinder.findTestClasses(inputConfiguration.getTestClasses());
final List<String> testMethodsToBeAmplifiedNames = inputConfiguration.getTestCases();
if (testMethodsToBeAmplifiedNames.size() == 1 &&
testMethodsToBeAmplifiedNames.get(0).isEmpty()) {
testMethodsToBeAmplifiedNames.clear();
}
final TestSelector testSelector =
inputConfiguration.getSelector().buildSelector(automaticBuilder, inputConfiguration);
final List<Amplifier> amplifiers = inputConfiguration
.getAmplifiers()
.stream()
.map(AmplifierEnum::getAmplifier)
.filter(Objects::nonNull)
.collect(Collectors.toList());
final InputAmplDistributor inputAmplDistributor = inputConfiguration
.getInputAmplDistributor()
.getInputAmplDistributor(inputConfiguration.getMaxTestAmplified(), amplifiers);
final Output output = new Output(
inputConfiguration.getAbsolutePathToProjectRoot(),
inputConfiguration.getOutputDirectory(),
collector

);
final DSpot dspot = new DSpot(
inputConfiguration.getDelta(),
testFinder,
compiler,
testSelector,
inputAmplDistributor,
output,
inputConfiguration.getNbIteration(),
inputConfiguration.shouldGenerateAmplifiedTestClass(),
automaticBuilder,
testCompiler
);

Checker.postChecking(inputConfiguration);

// starting amplification
final List<CtType<?>> amplifiedTestClasses = dspot.amplify(testClassesToBeAmplified, testMethodsToBeAmplifiedNames);
LOGGER.info("Amplification {}.", amplifiedTestClasses.isEmpty() ? "failed" : "succeed");
final long elapsedTime = System.currentTimeMillis() - startTime;
LOGGER.info("Elapsed time {} ms", elapsedTime);
// global report handling
Main.GLOBAL_REPORT.output(inputConfiguration.getOutputDirectory());
DSpotCache.reset();
Main.GLOBAL_REPORT.reset();
AmplificationHelper.reset();
DSpotPOMCreator.delete();
// Send info collected.
collector.sendInfo();
}

public static void initHelpers(InputConfiguration configuration){
TestFramework.init(configuration.getFactory());
AmplificationHelper.init(
configuration.getTimeOutInMs(),
configuration.shouldGenerateAmplifiedTestClass(),
configuration.shouldKeepOriginalTestMethods()
);
RandomHelper.setSeedRandom(configuration.getSeed());
createOutputDirectories(configuration);
DSpotCache.init(configuration.getCacheSize());
DSpotUtils.init(
configuration.withComment(),
configuration.getOutputDirectory(),
configuration.getFullClassPathWithExtraDependencies(),
configuration.getAbsolutePathToProjectRoot()
);
initSystemProperties(configuration.getSystemProperties());
AssertionGeneratorUtils.init(configuration.shouldAllowPathInAssertion());
CloneHelper.init(configuration.shouldExecuteTestsInParallel());
}

private static void initSystemProperties(String systemProperties) {
if (!systemProperties.isEmpty()) {
Arrays.stream(systemProperties.split(","))
.forEach(systemProperty -> {
String[] keyValueInArray = systemProperty.split("=");
System.getProperties().put(keyValueInArray[0], keyValueInArray[1]);
});
}
}

public static String completeDependencies(InputConfiguration configuration,
AutomaticBuilder automaticBuilder) {
String dependencies = configuration.getDependencies();
final String additionalClasspathElements = configuration.getAdditionalClasspathElements();
final String absolutePathToProjectRoot = configuration.getAbsolutePathToProjectRoot();
if (dependencies.isEmpty()) {
dependencies = automaticBuilder.compileAndBuildClasspath();
configuration.setDependencies(dependencies);
}
// TODO checks this. Since we support different Test Support, we may not need to add artificially junit in the classpath
if (!dependencies.contains("junit" + File.separator + "junit" + File.separator + "4")) {
dependencies = Test.class
.getProtectionDomain()
.getCodeSource()
.getLocation()
.getFile() +
AmplificationHelper.PATH_SEPARATOR + dependencies;
}
if (!additionalClasspathElements.isEmpty()) {
String pathToAdditionalClasspathElements = additionalClasspathElements;
if (!Paths.get(additionalClasspathElements).isAbsolute()) {
pathToAdditionalClasspathElements =
DSpotUtils.shouldAddSeparator.apply(absolutePathToProjectRoot + additionalClasspathElements);
}
dependencies += PATH_SEPARATOR + pathToAdditionalClasspathElements;
}
return dependencies;
}

public static void createOutputDirectories(InputConfiguration configuration) {
final File outputDirectory = new File(configuration.getOutputDirectory());
try {
if (configuration.shouldClean() && outputDirectory.exists()) {
FileUtils.forceDelete(outputDirectory);
}
if (!outputDirectory.exists()) {
FileUtils.forceMkdir(outputDirectory);
}
} catch (IOException ignored) {
// ignored
}
}

}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package eu.stamp_project.automaticbuilder.gradle;

import eu.stamp_project.Main;
import eu.stamp_project.automaticbuilder.AutomaticBuilder;
import eu.stamp_project.utils.configuration.DSpotState;
import eu.stamp_project.utils.program.InputConfiguration;
import org.gradle.tooling.BuildLauncher;
import org.gradle.tooling.GradleConnector;
Expand Down Expand Up @@ -123,7 +123,7 @@ protected void runTasks(boolean skipTest, String... tasks) {
if (skipTest) {
buildLauncher.withArguments(ARGUMENTS_SKIP_COMPILE_TEST);
}
if (Main.verbose) {
if (DSpotState.verbose) {
buildLauncher.setStandardError(System.err);
buildLauncher.setStandardOutput(System.out);
}
Expand Down
Loading