Skip to content

Commit

Permalink
refactor: Remove properties and singleton InputConfiguration (#904) FIX
Browse files Browse the repository at this point in the history
#826

* refactor: almost remove the singleton InputConfiguration

* fix: multiples fixes to make MainTest pass

* test: fix the tests in amplifier subpackage

* refactor: remove the singleton InputConfiguration

* test: update test on selector

* test: make assertiongenerator test pass again

* test: make test on amplifier pass again

* test: make TestFinderTest pass again

* test: make input ampl distributor tests pass again

* test: make test framework test pass again

* test: make test on automatic builder maven pass again

* test: make gradle test pass

* test: fix test of compilation package

* test: make all the helpers test pass again

* test: remove old test classes that are redundant

* style: remove unused imports

* pom: fix the version DSpot to 2.2.1

* test: fix tests

* test: ignore mongodb test

* test: fix test

* ci: update amplification of dhell command line options

* style: remove unused imports

* ci: fix the command line to run dspot on dhell

* ci: update amplification of dhell command line options

* fix: reporting to mongodb

* fix: the mongodbcollector is now working properly

* test: update the test
  • Loading branch information
danglotb authored Oct 9, 2019
1 parent 5e06ec9 commit 245156e
Show file tree
Hide file tree
Showing 137 changed files with 3,159 additions and 5,298 deletions.
5 changes: 2 additions & 3 deletions .travis/travis-dhell.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ DPSOT_VERSION=${1}

cd dspot
git clone https://github.com/STAMP-project/dhell.git
cd dhell

java -jar ../target/dspot-${DSPOT_VERSION}-jar-with-dependencies.jar --path-to-properties dhell.dspot --iteration 1 --amplifiers MethodAdd --test eu.stamp_project.examples.dhell.HelloAppTest
java -jar target/dspot-${DSPOT_VERSION}-jar-with-dependencies.jar --absolute-path-to-project-root dhell/ --iteration 1 --amplifiers MethodDuplicationAmplifier --test eu.stamp_project.examples.dhell.HelloAppTest

if [ -f dspot-out/eu/stamp_project/examples/dhell/HelloAppTest.java ]; then
if [ -f target/dspot/output/eu/stamp_project/examples/dhell/HelloAppTest.java ]; then
exit 0
else
exit 1
Expand Down
2 changes: 1 addition & 1 deletion dspot-maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<dependency>
<groupId>eu.stamp-project</groupId>
<artifactId>dspot</artifactId>
<version>${project.version}</version>
<version>2.2.1</version>
</dependency>

<dependency>
Expand Down
2 changes: 1 addition & 1 deletion dspot-prettifier/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<dependency>
<groupId>eu.stamp-project</groupId>
<artifactId>dspot</artifactId>
<version>${project.version}</version>
<version>2.2.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
Expand Down
8 changes: 7 additions & 1 deletion dspot/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-common-utilities</artifactId>
<version>2.4.1</version>
</dependency>
</dependency>

<!-- JUnit4 parallel execution -->
<dependency>
Expand Down Expand Up @@ -182,6 +182,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.0.4</version>
</dependency>

</dependencies>

<build>
Expand Down
295 changes: 223 additions & 72 deletions dspot/src/main/java/eu/stamp_project/Main.java
Original file line number Diff line number Diff line change
@@ -1,103 +1,254 @@
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.utils.collector.CollectorConfig;
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.options.JSAPOptions;
import eu.stamp_project.utils.compilation.TestCompiler;
import eu.stamp_project.utils.execution.TestRunner;
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.RandomHelper;
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 spoon.reflect.factory.Factory;

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 void main(String[] args) {
try {
FileUtils.forceDelete(new File("target/dspot/"));
} catch (Exception ignored) {

}
JSAPOptions.parse(args);
run();
}

public static void run() {
RandomHelper.setSeedRandom(InputConfiguration.get().getSeed());
createOutputDirectories();
final long startTime = System.currentTimeMillis();

final TestFinder testFinder = new TestFinder(
Arrays.stream(InputConfiguration.get().getExcludedClasses().split(",")).collect(Collectors.toList()),
Arrays.stream(InputConfiguration.get().getExcludedTestCases().split(",")).collect(Collectors.toList())
);
final DSpotCompiler compiler = DSpotCompiler.createDSpotCompiler(
InputConfiguration.get(),
InputConfiguration.get().getDependencies()
);
InputConfiguration.get().setFactory(compiler.getLauncher().getFactory());

final List<CtType<?>> testClassesToBeAmplified = testFinder.findTestClasses(InputConfiguration.get().getTestClasses());
final List<String> testMethodsToBeAmplifiedNames = InputConfiguration.get().getTestCases();
final InputAmplDistributor inputAmplDistributor = InputConfiguration.get().getBudgetizer().getInputAmplDistributor();
Output output = new Output(InputConfiguration.get().getAbsolutePathToProjectRoot(), InputConfiguration.get().getOutputDirectory());
final DSpot dspot = new DSpot(
testFinder,
compiler,
InputConfiguration.get().getSelector(),
inputAmplDistributor,
output,
InputConfiguration.get().getNbIteration(),
InputConfiguration.get().shouldGenerateAmplifiedTestClass()
);

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();
Main.GLOBAL_REPORT.reset();
DSpotPOMCreator.delete();
// Send info collected.
CollectorConfig.getInstance().getInformationCollector().sendInfo();
}

public static void createOutputDirectories() {
final File outputDirectory = new File(InputConfiguration.get().getOutputDirectory());
try {
if (InputConfiguration.get().shouldClean() && outputDirectory.exists()) {
FileUtils.forceDelete(outputDirectory);
}
if (!outputDirectory.exists()) {
FileUtils.forceMkdir(outputDirectory);
}
} catch (IOException ignored) {
// ignored
}
}
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 = new InputConfiguration();
final CommandLine commandLine = new CommandLine(inputConfiguration);
try {
commandLine.parseArgs(args);
} catch (Exception e) {
e.printStackTrace();
commandLine.usage(System.err);
return;
}
if (commandLine.isUsageHelpRequested()) {
commandLine.usage(System.out);
return;
}
if (commandLine.isVersionHelpRequested()) {
commandLine.printVersionHelp(System.out);
return;
}
if (inputConfiguration.shouldRunExample()) {
inputConfiguration.configureExample();
}
try {
Checker.preChecking(inputConfiguration);
} catch (InputErrorException e) {
e.printStackTrace();
commandLine.usage(System.err);
return;
}
Main.verbose = inputConfiguration.isVerbose();
run(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())
);
final AutomaticBuilder automaticBuilder = inputConfiguration.getBuilderEnum().getAutomaticBuilder(inputConfiguration);
final String dependencies = completeDependencies(inputConfiguration, automaticBuilder);
final DSpotCompiler compiler = DSpotCompiler.createDSpotCompiler(
inputConfiguration,
dependencies
);
initHelpers(inputConfiguration, compiler.getLauncher().getFactory());
inputConfiguration.setFactory(compiler.getLauncher().getFactory());
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();
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
);

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();
}

private static void initHelpers(InputConfiguration configuration, Factory factory){
TestFramework.init(factory);
AmplificationHelper.init(
configuration.getTimeOutInMs(),
configuration.shouldGenerateAmplifiedTestClass(),
configuration.shouldKeepOriginalTestMethods()
);
RandomHelper.setSeedRandom(configuration.getSeed());
createOutputDirectories(configuration);
DSpotCache.init(configuration.getCacheSize());
TestCompiler.init(
configuration.getNumberParallelExecutionProcessors(),
configuration.shouldExecuteTestsInParallel(),
configuration.getAbsolutePathToProjectRoot(),
configuration.getClasspathClassesProject(),
configuration.getTimeOutInMs()
);
DSpotUtils.init(
configuration.withComment(),
configuration.getOutputDirectory(),
configuration.getFullClassPathWithExtraDependencies(),
configuration.getAbsolutePathToProjectRoot()
);
initSystemProperties(configuration.getSystemProperties());
AssertionGeneratorUtils.init(configuration.shouldAllowPathInAssertion());
CloneHelper.init(configuration.shouldExecuteTestsInParallel());
TestRunner.init(
configuration.getAbsolutePathToProjectRoot(),
configuration.getPreGoalsTestExecution(),
configuration.shouldUseMavenToExecuteTest()
);
}

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
Expand Up @@ -9,6 +9,8 @@
*/
public interface AutomaticBuilder {

void setAbsolutePathToProjectRoot(String absolutePathToProjectRoot);

String compileAndBuildClasspath();

void compile();
Expand Down
Loading

0 comments on commit 245156e

Please sign in to comment.