diff --git a/dspot-prettifier/src/main/java/eu/stamp_project/prettifier/Main.java b/dspot-prettifier/src/main/java/eu/stamp_project/prettifier/Main.java index 7aac3421e..69a9aeb15 100644 --- a/dspot-prettifier/src/main/java/eu/stamp_project/prettifier/Main.java +++ b/dspot-prettifier/src/main/java/eu/stamp_project/prettifier/Main.java @@ -15,6 +15,7 @@ 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.options.check.Checker; import eu.stamp_project.utils.options.check.InputErrorException; import org.slf4j.Logger; @@ -33,7 +34,6 @@ import java.util.*; import java.util.stream.Collectors; -import static eu.stamp_project.Main.completeDependencies; /** * created by Benjamin DANGLOT @@ -75,7 +75,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); } @@ -100,15 +100,15 @@ public static CtType loadAmplifiedTestClass(InputConfiguration configuration) public static List> run(CtType amplifiedTestClass, InputConfiguration configuration) { - + DSpotState dSpotState = new DSpotState(); final AutomaticBuilder automaticBuilder = configuration.getBuilderEnum().getAutomaticBuilder(configuration); - final String dependencies = completeDependencies(configuration, automaticBuilder); + final String dependencies = dSpotState.completeDependencies(configuration, automaticBuilder); final DSpotCompiler compiler = DSpotCompiler.createDSpotCompiler( configuration, dependencies ); configuration.setFactory(compiler.getLauncher().getFactory()); - eu.stamp_project.Main.initHelpers(configuration); + dSpotState.initHelpers(configuration); final List> testMethods = TestFramework.getAllTest(amplifiedTestClass); Main.report.nbTestMethods = testMethods.size(); diff --git a/dspot-prettifier/src/test/java/eu/stamp_project/AbstractTest.java b/dspot-prettifier/src/test/java/eu/stamp_project/AbstractTest.java index 372c05658..3e5aa3e20 100644 --- a/dspot-prettifier/src/test/java/eu/stamp_project/AbstractTest.java +++ b/dspot-prettifier/src/test/java/eu/stamp_project/AbstractTest.java @@ -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; @@ -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); } } diff --git a/dspot-prettifier/src/test/java/eu/stamp_project/Utils.java b/dspot-prettifier/src/test/java/eu/stamp_project/Utils.java index 50aa903ad..2e38cd9c8 100644 --- a/dspot-prettifier/src/test/java/eu/stamp_project/Utils.java +++ b/dspot-prettifier/src/test/java/eu/stamp_project/Utils.java @@ -3,8 +3,8 @@ 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.program.InputConfiguration; -import org.apache.commons.io.FileUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import spoon.reflect.code.CtLiteral; @@ -15,12 +15,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 @@ -34,14 +31,15 @@ public class Utils { private static InputConfiguration configuration; public static void init(InputConfiguration configuration) { + DSpotState dSpotState = new DSpotState(); final AutomaticBuilder automaticBuilder = configuration.getBuilderEnum().getAutomaticBuilder(configuration); - final String dependencies = completeDependencies(configuration, automaticBuilder); + final String dependencies = dSpotState.completeDependencies(configuration, automaticBuilder); final DSpotCompiler compiler = DSpotCompiler.createDSpotCompiler( configuration, dependencies ); configuration.setFactory(compiler.getLauncher().getFactory()); - eu.stamp_project.Main.initHelpers(configuration); + dSpotState.initHelpers(configuration); Utils.configuration = configuration; } diff --git a/dspot/src/main/java/eu/stamp_project/Main.java b/dspot/src/main/java/eu/stamp_project/Main.java index 6b1aadb52..69759ea52 100644 --- a/dspot/src/main/java/eu/stamp_project/Main.java +++ b/dspot/src/main/java/eu/stamp_project/Main.java @@ -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 benjamin.danglot@inria.fr 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); @@ -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(); @@ -80,176 +47,8 @@ public static void main(String[] args) { } 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()) - ); - 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> testClassesToBeAmplified = testFinder.findTestClasses(inputConfiguration.getTestClasses()); - final List testMethodsToBeAmplifiedNames = inputConfiguration.getTestCases(); - final TestSelector testSelector = - inputConfiguration.getSelector().buildSelector(automaticBuilder, inputConfiguration); - final List 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> 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]); - }); + return null; } + return inputConfiguration; } - - 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 - } - } - -} \ No newline at end of file +} diff --git a/dspot/src/main/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilder.java b/dspot/src/main/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilder.java index d8f2d70b6..c6822c618 100644 --- a/dspot/src/main/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilder.java +++ b/dspot/src/main/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilder.java @@ -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; @@ -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); } diff --git a/dspot/src/main/java/eu/stamp_project/automaticbuilder/maven/MavenAutomaticBuilder.java b/dspot/src/main/java/eu/stamp_project/automaticbuilder/maven/MavenAutomaticBuilder.java index 8915b0b2a..839f35227 100644 --- a/dspot/src/main/java/eu/stamp_project/automaticbuilder/maven/MavenAutomaticBuilder.java +++ b/dspot/src/main/java/eu/stamp_project/automaticbuilder/maven/MavenAutomaticBuilder.java @@ -1,7 +1,7 @@ package eu.stamp_project.automaticbuilder.maven; -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 eu.stamp_project.utils.DSpotUtils; import org.apache.commons.io.FileUtils; @@ -192,7 +192,7 @@ private int _runGoals(boolean specificPom, final String pomPathname, String... g LOGGER.info("Using {} for maven home", mavenHome); invoker.setMavenHome(new File(mavenHome)); LOGGER.info(String.format("run maven: %s/bin/mvn %s", mavenHome, String.join(" ", goals))); - if (Main.verbose) { + if (DSpotState.verbose) { invoker.setOutputHandler(System.out::println); invoker.setErrorHandler(System.err::println); } else { diff --git a/dspot/src/main/java/eu/stamp_project/dspot/Amplification.java b/dspot/src/main/java/eu/stamp_project/dspot/Amplification.java deleted file mode 100644 index afea971f2..000000000 --- a/dspot/src/main/java/eu/stamp_project/dspot/Amplification.java +++ /dev/null @@ -1,216 +0,0 @@ -package eu.stamp_project.dspot; - -import eu.stamp_project.Main; -import eu.stamp_project.dspot.assertiongenerator.AssertionGenerator; -import eu.stamp_project.dspot.input_ampl_distributor.InputAmplDistributor; -import eu.stamp_project.dspot.selector.TestSelector; -import eu.stamp_project.utils.compilation.DSpotCompiler; -import eu.stamp_project.utils.compilation.TestCompiler; -import eu.stamp_project.utils.report.error.Error; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import spoon.reflect.declaration.CtMethod; -import spoon.reflect.declaration.CtType; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import static eu.stamp_project.utils.report.error.ErrorEnum.*; - - -/** - * User: Simon - * Date: 03/12/15 - * Time: 13:52 - */ -public class Amplification { - - private static final Logger LOGGER = LoggerFactory.getLogger(Amplification.class); - - private AssertionGenerator assertionGenerator; - - private DSpotCompiler compiler; - - private TestSelector testSelector; - - private InputAmplDistributor inputAmplDistributor; - - private int globalNumberOfSelectedAmplification; - - private int numberOfIteration; - - private TestCompiler testCompiler; - - public Amplification(double delta, - DSpotCompiler compiler, - TestSelector testSelector, - InputAmplDistributor inputAmplDistributor, - int numberOfIteration, - TestCompiler testCompiler) { - this.compiler = compiler; - this.testCompiler = testCompiler; - this.assertionGenerator = new AssertionGenerator(delta, this.compiler, this.testCompiler); - this.testSelector = testSelector; - this.inputAmplDistributor = inputAmplDistributor; - this.globalNumberOfSelectedAmplification = 0; - this.numberOfIteration = numberOfIteration; - } - - - public List> amplification(CtType testClassToBeAmplified, List> testMethodsToBeAmplified) { - if(testMethodsToBeAmplified.isEmpty()) { - LOGGER.warn("No test provided for amplification in class {}", testClassToBeAmplified.getQualifiedName()); - return Collections.emptyList(); - } - - LOGGER.info("Amplification of {} ({} test(s))", testClassToBeAmplified.getQualifiedName(), testMethodsToBeAmplified.size()); - LOGGER.info("Assertion amplification of {} ({} test(s))", testClassToBeAmplified.getQualifiedName(), testMethodsToBeAmplified.size()); - - if (!this.testSelector.init()) { - return Collections.emptyList(); - } - final List> passingTests; - try { - passingTests = - this.testCompiler.compileRunAndDiscardUncompilableAndFailingTestMethods( - testClassToBeAmplified, - testMethodsToBeAmplified, - this.compiler - ); - } catch (Exception | java.lang.Error e) { - Main.GLOBAL_REPORT.addError(new Error(ERROR_EXEC_TEST_BEFORE_AMPLIFICATION, e)); - return Collections.emptyList(); - } - final List> selectedToBeAmplified; - try { - // set up the selector with tests to amplify - selectedToBeAmplified = this.testSelector.selectToAmplify(testClassToBeAmplified, passingTests); - } catch (Exception | java.lang.Error e) { - Main.GLOBAL_REPORT.addError(new Error(ERROR_PRE_SELECTION, e)); - return Collections.emptyList(); - } - - // generate tests with additional assertions - final List> assertionAmplifiedTestMethods = this.assertionsAmplification(testClassToBeAmplified, selectedToBeAmplified); - final List> amplifiedTestMethodsToKeep; - try { - // keep tests that improve the test suite - amplifiedTestMethodsToKeep = this.testSelector.selectToKeep(assertionAmplifiedTestMethods); - } catch (Exception | java.lang.Error e) { - Main.GLOBAL_REPORT.addError(new Error(ERROR_SELECTION, e)); - return Collections.emptyList(); - } - this.globalNumberOfSelectedAmplification += amplifiedTestMethodsToKeep.size(); - LOGGER.info("{} amplified test methods has been selected to be kept. (global: {})", amplifiedTestMethodsToKeep.size(), this.globalNumberOfSelectedAmplification); - // in case there is no amplifier, we can leave - if (!this.inputAmplDistributor.shouldBeRun()) { - return amplifiedTestMethodsToKeep; - } - - // generate tests with input modification and associated new assertions - LOGGER.info("Applying Input-amplification and Assertion-amplification test by test."); - for (int i = 0; i < testMethodsToBeAmplified.size(); i++) { - CtMethod test = testMethodsToBeAmplified.get(i); - LOGGER.info("Amplification of {}, ({}/{})", test.getSimpleName(), i + 1, testMethodsToBeAmplified.size()); - final List> amplifiedTestMethodsFromCurrentIteration = amplificationIteration(testClassToBeAmplified, test); - amplifiedTestMethodsToKeep.addAll(amplifiedTestMethodsFromCurrentIteration); - this.globalNumberOfSelectedAmplification += amplifiedTestMethodsToKeep.size(); - LOGGER.info("{} amplified test methods has been selected to be kept. (global: {})", amplifiedTestMethodsFromCurrentIteration.size(), this.globalNumberOfSelectedAmplification); - } - return amplifiedTestMethodsToKeep; - } - - private List> amplificationIteration(CtType testClassToBeAmplified, CtMethod test) { - // tmp list for current test methods to be amplified - // this list must be a implementation that support remove / clear methods - List> currentTestList = new ArrayList<>(); - currentTestList.add(test); - // output - final List> amplifiedTests = new ArrayList<>(); - for (int i = 0; i < this.numberOfIteration ; i++) { - LOGGER.info("iteration {} / {}", i, this.numberOfIteration); - currentTestList = this.amplification(testClassToBeAmplified, currentTestList, amplifiedTests, i); - } - return amplifiedTests; - } - - /** - * Amplification of test methods - * - * DSpot combines the different kinds of I-Amplification iteratively: at each iteration all kinds of - * I-Amplification are applied, resulting in new tests. From one iteration to another, DSpot reuses the - * previously amplified tests, and further applies I-Amplification. - * - * @param testClassToBeAmplified Test class - * @param currentTestListToBeAmplified Methods to amplify - * @return Valid amplified tests - */ - public List> amplification(CtType testClassToBeAmplified, - List> currentTestListToBeAmplified, - List> amplifiedTests, - int currentIteration) { - final List> selectedToBeAmplified; - try { - // set up the selector with tests to amplify - selectedToBeAmplified = this.testSelector.selectToAmplify(testClassToBeAmplified, currentTestListToBeAmplified); - } catch (Exception | java.lang.Error e) { - Main.GLOBAL_REPORT.addError(new Error(ERROR_PRE_SELECTION, e)); - return Collections.emptyList(); - } - if (selectedToBeAmplified.isEmpty()) { - LOGGER.warn("No test could be selected to be amplified."); - return selectedToBeAmplified; // todo should we break the loop? - } - LOGGER.info("{} tests selected to be amplified over {} available tests", - selectedToBeAmplified.size(), - currentTestListToBeAmplified.size() - ); - final List> inputAmplifiedTests; - try { - // amplify tests and shrink amplified set with inputAmplDistributor - inputAmplifiedTests = this.inputAmplDistributor.inputAmplify(selectedToBeAmplified, currentIteration); - } catch (Exception | java.lang.Error e) { - Main.GLOBAL_REPORT.addError(new Error(ERROR_INPUT_AMPLIFICATION, e)); - return Collections.emptyList(); - } - // add assertions to input modified tests and return them - // new amplified tests will be the basis for further amplification - final List> currentTestList = this.assertionsAmplification(testClassToBeAmplified, inputAmplifiedTests); - - final List> amplifiedTestMethodsToKeep; - try { - // keep tests that improve the test suite - amplifiedTestMethodsToKeep = this.testSelector.selectToKeep(currentTestList); - } catch (Exception | java.lang.Error e) { - Main.GLOBAL_REPORT.addError(new Error(ERROR_SELECTION, e)); - return Collections.emptyList(); - } - LOGGER.info("{} amplified test methods has been selected to be kept.", amplifiedTestMethodsToKeep.size()); - amplifiedTests.addAll(amplifiedTestMethodsToKeep); - return currentTestList; - } - - public List> assertionsAmplification(CtType classTest, List> testMethods) { - final List> testsWithAssertions; - try { - testsWithAssertions = this.assertionGenerator.assertionAmplification(classTest, testMethods); - } catch (Exception | java.lang.Error e) { - Main.GLOBAL_REPORT.addError(new Error(ERROR_ASSERT_AMPLIFICATION, e)); - return Collections.emptyList(); - } - if (testsWithAssertions.isEmpty()) { - return testsWithAssertions; - } - // final check on A-amplified test, see if they all pass. - // If they don't, we just discard them. - final List> amplifiedPassingTests = - this.testCompiler.compileRunAndDiscardUncompilableAndFailingTestMethods( - classTest, - testsWithAssertions, - this.compiler - ); - LOGGER.info("Assertion amplification: {} test method(s) has been successfully amplified.", amplifiedPassingTests.size()); - return amplifiedPassingTests; - } -} diff --git a/dspot/src/main/java/eu/stamp_project/dspot/DSpot.java b/dspot/src/main/java/eu/stamp_project/dspot/DSpot.java index 58b0db3a8..8f232db79 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/DSpot.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/DSpot.java @@ -1,27 +1,20 @@ package eu.stamp_project.dspot; -import eu.stamp_project.Main; -import eu.stamp_project.automaticbuilder.AutomaticBuilder; -import eu.stamp_project.automaticbuilder.maven.DSpotPOMCreator; -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.testrunner.EntryPoint; -import eu.stamp_project.utils.AmplificationHelper; -import eu.stamp_project.utils.compilation.TestCompiler; -import eu.stamp_project.utils.report.output.Output; -import eu.stamp_project.utils.Counter; -import eu.stamp_project.utils.compilation.DSpotCompiler; -import eu.stamp_project.utils.report.output.selector.TestSelectorElementReport; -import eu.stamp_project.utils.test_finder.TestFinder; -import org.apache.commons.io.FileUtils; +import eu.stamp_project.utils.configuration.AmplificationSetup; +import eu.stamp_project.utils.configuration.DSpotState; +import eu.stamp_project.utils.configuration.TestTuple; +import eu.stamp_project.utils.program.InputConfiguration; +import eu.stamp_project.utils.report.GlobalReport; +import eu.stamp_project.utils.report.error.Error; import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import spoon.reflect.declaration.CtMethod; import spoon.reflect.declaration.CtType; - -import java.io.*; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import static eu.stamp_project.utils.report.error.ErrorEnum.ERROR_ASSERT_AMPLIFICATION; +import static eu.stamp_project.utils.report.error.ErrorEnum.ERROR_INPUT_AMPLIFICATION; +import static eu.stamp_project.utils.report.error.ErrorEnum.ERROR_SELECTION; /** * User: Simon @@ -30,153 +23,177 @@ */ public class DSpot { - private static final Logger LOGGER = LoggerFactory.getLogger(DSpot.class); - - private TestFinder testFinder; - - private DSpotCompiler compiler; - - private TestSelector testSelector; - - private InputAmplDistributor inputAmplDistributor; - - private Output output; - - private int numberOfIterations; + private DSpotState configuration; + private AmplificationSetup setup; + private int globalNumberOfSelectedAmplification; + private Logger LOGGER; + private GlobalReport GLOBAL_REPORT; - private boolean shouldGenerateAmplifiedTestClass; - - private AutomaticBuilder automaticBuilder; - - private double delta; - - private TestCompiler testCompiler; - - public DSpot(double delta, - TestFinder testFinder, - DSpotCompiler compiler, - TestSelector testSelector, - InputAmplDistributor inputAmplDistributor, - Output output, - int numberOfIterations, - boolean shouldGenerateAmplifiedTestClass, - AutomaticBuilder automaticBuilder, - TestCompiler testCompiler) { - this.delta = delta; - this.testSelector = testSelector; - this.inputAmplDistributor = inputAmplDistributor; - this.numberOfIterations = numberOfIterations; - this.testFinder = testFinder; - this.compiler = compiler; - this.output = output; - this.shouldGenerateAmplifiedTestClass = shouldGenerateAmplifiedTestClass; - this.automaticBuilder = automaticBuilder; - this.testCompiler = testCompiler; + public DSpot(InputConfiguration inputConfiguration){ + configuration = new DSpotState(inputConfiguration); + setup(configuration); } - public CtType amplify(CtType testClassToBeAmplified) { - return this.amplify(Collections.singletonList(testClassToBeAmplified), Collections.emptyList()).get(0); + public DSpot(DSpotState configuration) { + setup(configuration); } - public CtType amplify(CtType testClassToBeAmplified, String testMethodToBeAmplifiedAsString) { - return this.amplify(Collections.singletonList(testClassToBeAmplified), Collections.singletonList(testMethodToBeAmplifiedAsString)).get(0); + private void setup(DSpotState configuration){ + this.configuration = configuration; + setup = new AmplificationSetup(configuration); + LOGGER = configuration.getLogger(); + globalNumberOfSelectedAmplification = 0; + GLOBAL_REPORT = configuration.getGlobalReport(); } - public CtType amplify(CtType testClassToBeAmplified, List testMethodsToBeAmplifiedAsString) { - return this.amplify(Collections.singletonList(testClassToBeAmplified), testMethodsToBeAmplifiedAsString).get(0); + public void run() { + for (CtType testClassToBeAmplified : configuration.getTestClassesToBeAmplified()) { + TestTuple tuple = setup.preAmplification(testClassToBeAmplified,configuration.getTestMethodsToBeAmplifiedNames()); + final List> amplifiedTestMethods = amplification(tuple.testClassToBeAmplified,tuple.testMethodsToBeAmplified); + setup.postAmplification(testClassToBeAmplified,amplifiedTestMethods); + globalNumberOfSelectedAmplification = 0; + } + configuration.report(setup.getAmplifiedTestClasses()); } - public List> amplify(List> testClassesToBeAmplified, String testMethodToBeAmplifiedAsString) { - return this.amplify(testClassesToBeAmplified, Collections.singletonList(testMethodToBeAmplifiedAsString)); + private List> amplification(CtType testClassToBeAmplified, List> testMethodsToBeAmplified) { + final List> amplifiedTestMethodsToKeep = onlyAssertionGeneration(testClassToBeAmplified,testMethodsToBeAmplified); + if (configuration.getInputAmplDistributor().shouldBeRun()) { + fullyAmplifyAllMethods(testClassToBeAmplified,testMethodsToBeAmplified,amplifiedTestMethodsToKeep); + } + return amplifiedTestMethodsToKeep; } - public List> amplify(List> testClassesToBeAmplified) { - return this.amplify(testClassesToBeAmplified, Collections.emptyList()); - } + private List> onlyAssertionGeneration(CtType testClassToBeAmplified, List> testMethodsToBeAmplified){ + final List> selectedToBeAmplified; + final List> amplifiedTestMethodsToKeep; + try { + selectedToBeAmplified = setup.firstSelectorSetup(testClassToBeAmplified,testMethodsToBeAmplified); + + // generate tests with additional assertions + final List> assertionAmplifiedTestMethods = this.assertionAmplification(testClassToBeAmplified, + selectedToBeAmplified); - public List> amplify(List> testClassesToBeAmplified, List testMethodsToBeAmplifiedAsString) { - final List> amplifiedTestClasses = new ArrayList<>(); - for (CtType testClassToBeAmplified : testClassesToBeAmplified) { - inputAmplDistributor.resetAmplifiers(testClassToBeAmplified); - Amplification testAmplification = new Amplification( - this.delta, - this.compiler, - this.testSelector, - this.inputAmplDistributor, - this.numberOfIterations, - this.testCompiler - ); - final List> testMethodsToBeAmplified = - testFinder.findTestMethods(testClassToBeAmplified, testMethodsToBeAmplifiedAsString); - - // here, we base the execution mode to the first test method given. - // the user should provide whether JUnit3/4 OR JUnit5 but not both at the same time. - // TODO DSpot could be able to switch from one to another version of JUnit, but I believe that the ROI is not worth it. - final boolean jUnit5 = TestFramework.isJUnit5(testMethodsToBeAmplified.get(0)); - EntryPoint.jUnit5Mode = jUnit5; - DSpotPOMCreator.isCurrentlyJUnit5 = jUnit5; - - final CtType amplifiedTestClass = this.amplify(testAmplification, testClassToBeAmplified, testMethodsToBeAmplified); - amplifiedTestClasses.add(amplifiedTestClass); - cleanAfterAmplificationOfOneTestClass(compiler, testClassToBeAmplified); + // keep tests that improve the test suite + amplifiedTestMethodsToKeep = selectOnlyAssertionGeneration(assertionAmplifiedTestMethods); + } catch (Exception e) { + return Collections.emptyList(); } - return amplifiedTestClasses; + return amplifiedTestMethodsToKeep; } - - private CtType amplify(Amplification testAmplification, - CtType testClassToBeAmplified, - List> testMethodsToBeAmplified) { - Counter.reset(); - if (this.shouldGenerateAmplifiedTestClass) { - testClassToBeAmplified = AmplificationHelper.renameTestClassUnderAmplification(testClassToBeAmplified); + // iteratively generate tests with input modification and associated new assertions for all methods + private void fullyAmplifyAllMethods(CtType testClassToBeAmplified,List> testMethodsToBeAmplified, + List> amplifiedTestMethodsToKeep){ + LOGGER.info("Applying Input-amplification and Assertion-amplification test by test."); + for (int i = 0; i < testMethodsToBeAmplified.size(); i++) { + CtMethod test = testMethodsToBeAmplified.get(i); + LOGGER.info("Amplification of {}, ({}/{})", test.getSimpleName(), i + 1, testMethodsToBeAmplified.size()); + + // tmp list for current test methods to be amplified + // this list must be a implementation that support remove / clear methods + List> currentTestList = new ArrayList<>(); + currentTestList.add(test); + final List> amplifiedTests = new ArrayList<>(); + for (int j = 0; j < configuration.getNbIteration() ; j++) { + LOGGER.info("iteration {} / {}", j, configuration.getNbIteration()); + currentTestList = this.fullAmplification(testClassToBeAmplified, currentTestList, amplifiedTests, j); + } + amplifiedTestMethodsToKeep.addAll(amplifiedTests); + this.globalNumberOfSelectedAmplification += amplifiedTestMethodsToKeep.size(); + LOGGER.info("{} amplified test methods has been selected to be kept. (global: {})", amplifiedTests.size(), + this.globalNumberOfSelectedAmplification); } - long time = System.currentTimeMillis(); + } + + /** + * Amplification of test methods + * + * DSpot combines the different kinds of I-Amplification iteratively: at each iteration all kinds of + * I-Amplification are applied, resulting in new tests. From one iteration to another, DSpot reuses the + * previously amplified tests, and further applies I-Amplification. + * + * @param testClassToBeAmplified Test class + * @param currentTestListToBeAmplified Methods to amplify + * @return Valid amplified tests + */ + public List> fullAmplification(CtType testClassToBeAmplified, + List> currentTestListToBeAmplified, + List> amplifiedTests, + int currentIteration) { + final List> selectedToBeAmplified; + final List> inputAmplifiedTests; + final List> currentTestList; + try { + selectedToBeAmplified = setup.fullSelectorSetup(testClassToBeAmplified,currentTestListToBeAmplified); - // Amplification of the given test methods of the given test class - final List> amplifiedTestMethods = - testAmplification.amplification(testClassToBeAmplified, testMethodsToBeAmplified); + // amplify tests and shrink amplified set with inputAmplDistributor + inputAmplifiedTests = configuration.getInputAmplDistributor().inputAmplify(selectedToBeAmplified, currentIteration); - final long elapsedTime = System.currentTimeMillis() - time; - LOGGER.info("elapsedTime {}", elapsedTime); - this.output.addClassTimeJSON(testClassToBeAmplified.getQualifiedName(), elapsedTime); + // add assertions to input modified tests + currentTestList = this.assertionAmplification(testClassToBeAmplified, inputAmplifiedTests); - //Optimization: this object is not required anymore - //and holds a dictionary with large number of cloned CtMethods. - testAmplification = null; - //but it is clear before iterating again for next test class - LOGGER.debug("OPTIMIZATION: GC invoked"); - System.gc(); //Optimization: cleaning up heap before printing the amplified class + // keep tests that improve the test suite + selectFullAmplification(currentTestList,amplifiedTests); + } catch (AmplificationException e) { + return Collections.emptyList(); + } catch (Exception | java.lang.Error e) { + GLOBAL_REPORT.addError(new Error(ERROR_INPUT_AMPLIFICATION, e)); + return Collections.emptyList(); + } + return currentTestList; + } - this.automaticBuilder.reset(); + private List> assertionAmplification(CtType classTest, List> testMethods) { + final List> testsWithAssertions; try { - final TestSelectorElementReport report = this.testSelector.report(); - this.output.reportSelectorInformation(report.getReportForCollector()); - Main.GLOBAL_REPORT.addTestSelectorReportForTestClass(testClassToBeAmplified, report); - } catch (Exception e) { - e.printStackTrace(); - LOGGER.error("Something bad happened during the report fot test-criterion."); - LOGGER.error("Dspot might not have output correctly!"); + testsWithAssertions = configuration.getAssertionGenerator().assertionAmplification(classTest, testMethods); + } catch (Exception | java.lang.Error e) { + GLOBAL_REPORT.addError(new Error(ERROR_ASSERT_AMPLIFICATION, e)); + return Collections.emptyList(); + } + if (testsWithAssertions.isEmpty()) { + return testsWithAssertions; } - return this.output.output(testClassToBeAmplified, amplifiedTestMethods); + // final check on A-amplified test, see if they all pass. if they don't, we just discard them. + final List> amplifiedPassingTests = + configuration.getTestCompiler().compileRunAndDiscardUncompilableAndFailingTestMethods( + classTest, + testsWithAssertions, + configuration.getCompiler() + ); + LOGGER.info("Assertion amplification: {} test method(s) has been successfully amplified.", + amplifiedPassingTests.size()); + return amplifiedPassingTests; } - private static void cleanAfterAmplificationOfOneTestClass(DSpotCompiler compiler, CtType testClassToBeAmplified) { - /* Cleaning modified source directory by DSpot */ + private List> selectOnlyAssertionGeneration(List> assertionAmplifiedTestMethods) + throws Exception { + final List> amplifiedTestMethodsToKeep; try { - FileUtils.cleanDirectory(compiler.getSourceOutputDirectory()); - } catch (Exception exception) { - exception.printStackTrace(); - LOGGER.warn("Something went wrong when trying to cleaning temporary sources directory: {}", compiler.getSourceOutputDirectory()); + amplifiedTestMethodsToKeep = configuration.getTestSelector().selectToKeep(assertionAmplifiedTestMethods); + } catch (Exception | java.lang.Error e) { + GLOBAL_REPORT.addError(new Error(ERROR_SELECTION, e)); + throw new Exception(); } - /* Cleaning binary generated by Dspot */ + this.globalNumberOfSelectedAmplification += amplifiedTestMethodsToKeep.size(); + LOGGER.info("{} amplified test methods has been selected to be kept. (global: {})", + amplifiedTestMethodsToKeep.size(), this.globalNumberOfSelectedAmplification); + + return amplifiedTestMethodsToKeep; + } + + private void selectFullAmplification(List> currentTestList,List> amplifiedTests) + throws AmplificationException { + final List> amplifiedTestMethodsToKeep; try { - String pathToDotClass = compiler.getBinaryOutputDirectory().getAbsolutePath() + "/" + - testClassToBeAmplified.getQualifiedName().replaceAll("\\.", "/") + ".class"; - FileUtils.forceDelete(new File(pathToDotClass)); - } catch (IOException ignored) { - //ignored + amplifiedTestMethodsToKeep = configuration.getTestSelector().selectToKeep(currentTestList); + } catch (Exception | java.lang.Error e) { + GLOBAL_REPORT.addError(new Error(ERROR_SELECTION, e)); + throw new AmplificationException(""); } + LOGGER.info("{} amplified test methods has been selected to be kept.", amplifiedTestMethodsToKeep.size()); + amplifiedTests.addAll(amplifiedTestMethodsToKeep); } } diff --git a/dspot/src/main/java/eu/stamp_project/dspot/selector/PitMutantScoreSelector.java b/dspot/src/main/java/eu/stamp_project/dspot/selector/PitMutantScoreSelector.java index e0c19d3c1..948206a1b 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/selector/PitMutantScoreSelector.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/selector/PitMutantScoreSelector.java @@ -2,8 +2,8 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import eu.stamp_project.Main; import eu.stamp_project.automaticbuilder.AutomaticBuilder; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.pit.*; import eu.stamp_project.test_framework.TestFramework; import eu.stamp_project.utils.compilation.DSpotCompiler; @@ -109,7 +109,7 @@ public boolean init() { this.automaticBuilder.runPit(); } catch (Throwable e) { LOGGER.error(ErrorEnum.ERROR_ORIGINAL_MUTATION_SCORE.getMessage()); - Main.GLOBAL_REPORT.addError(new Error(ErrorEnum.ERROR_ORIGINAL_MUTATION_SCORE, e)); + DSpotState.GLOBAL_REPORT.addError(new Error(ErrorEnum.ERROR_ORIGINAL_MUTATION_SCORE, e)); return false; } } diff --git a/dspot/src/main/java/eu/stamp_project/utils/DSpotUtils.java b/dspot/src/main/java/eu/stamp_project/utils/DSpotUtils.java index f562e4faa..582febf6d 100644 --- a/dspot/src/main/java/eu/stamp_project/utils/DSpotUtils.java +++ b/dspot/src/main/java/eu/stamp_project/utils/DSpotUtils.java @@ -1,8 +1,8 @@ package eu.stamp_project.utils; -import eu.stamp_project.Main; import eu.stamp_project.utils.collector.Collector; import eu.stamp_project.utils.compilation.DSpotCompiler; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.report.error.Error; import eu.stamp_project.utils.report.error.ErrorEnum; import org.apache.commons.io.FileUtils; @@ -105,14 +105,14 @@ static void printCtTypUsingToStringToGivenDirectory(CtType type, File directo try { FileUtils.forceMkdir(new File(directoryPathname)); } catch (IOException e) { - Main.GLOBAL_REPORT.addError(new Error(ErrorEnum.ERROR_PRINT_USING_TO_STRING, e)); + DSpotState.GLOBAL_REPORT.addError(new Error(ErrorEnum.ERROR_PRINT_USING_TO_STRING, e)); } try (FileWriter fileWriter = new FileWriter( DSpotUtils.shouldAddSeparator.apply(directoryPathname) + type.getSimpleName() + JAVA_EXTENSION) ) { fileWriter.write(type.toString()); } catch (Exception e) { - Main.GLOBAL_REPORT.addError(new Error(ErrorEnum.ERROR_PRINT_USING_TO_STRING, e)); + DSpotState.GLOBAL_REPORT.addError(new Error(ErrorEnum.ERROR_PRINT_USING_TO_STRING, e)); } } diff --git a/dspot/src/main/java/eu/stamp_project/utils/compilation/DSpotCompiler.java b/dspot/src/main/java/eu/stamp_project/utils/compilation/DSpotCompiler.java index ef389d1ca..b1ecaf045 100644 --- a/dspot/src/main/java/eu/stamp_project/utils/compilation/DSpotCompiler.java +++ b/dspot/src/main/java/eu/stamp_project/utils/compilation/DSpotCompiler.java @@ -1,7 +1,7 @@ package eu.stamp_project.utils.compilation; -import eu.stamp_project.Main; import eu.stamp_project.utils.DSpotUtils; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.program.InputConfiguration; import org.apache.commons.io.FileUtils; import org.eclipse.jdt.core.compiler.CategorizedProblem; @@ -122,7 +122,7 @@ public static Launcher getSpoonModelOf(String pathToSources, String pathToDepend public static boolean compile(String pathToSources, String dependencies, File binaryOutputDirectory) { Launcher launcher = new Launcher(); - if (Main.verbose) { + if (DSpotState.verbose) { launcher.getEnvironment().setLevel("INFO"); } launcher.getEnvironment().setNoClasspath(true); diff --git a/dspot/src/main/java/eu/stamp_project/utils/configuration/AmplificationSetup.java b/dspot/src/main/java/eu/stamp_project/utils/configuration/AmplificationSetup.java new file mode 100644 index 000000000..624b0e31d --- /dev/null +++ b/dspot/src/main/java/eu/stamp_project/utils/configuration/AmplificationSetup.java @@ -0,0 +1,171 @@ +package eu.stamp_project.utils.configuration; + +import eu.stamp_project.automaticbuilder.maven.DSpotPOMCreator; +import eu.stamp_project.dspot.AmplificationException; +import eu.stamp_project.dspot.selector.TestSelector; +import eu.stamp_project.test_framework.TestFramework; +import eu.stamp_project.testrunner.EntryPoint; +import eu.stamp_project.utils.AmplificationHelper; +import eu.stamp_project.utils.Counter; +import eu.stamp_project.utils.compilation.DSpotCompiler; +import eu.stamp_project.utils.compilation.TestCompiler; +import eu.stamp_project.utils.report.GlobalReport; +import eu.stamp_project.utils.report.error.Error; +import eu.stamp_project.utils.report.output.Output; +import eu.stamp_project.utils.report.output.selector.TestSelectorElementReport; +import org.apache.commons.io.FileUtils; +import org.slf4j.Logger; +import spoon.reflect.declaration.CtMethod; +import spoon.reflect.declaration.CtType; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import static eu.stamp_project.utils.report.error.ErrorEnum.ERROR_EXEC_TEST_BEFORE_AMPLIFICATION; +import static eu.stamp_project.utils.report.error.ErrorEnum.ERROR_PRE_SELECTION; + +/** + * Created by Andrew Bwogi + * abwogi@kth.se + * on 31/10/19 + */ +public class AmplificationSetup { + + public final GlobalReport GLOBAL_REPORT; + private final Logger LOGGER; + final List> amplifiedTestClasses = new ArrayList<>(); + private DSpotState configuration; + private DSpotCompiler compiler; + private TestCompiler testCompiler; + private TestSelector testSelector; + private Output output; + private long time; + + public AmplificationSetup(DSpotState configuration){ + this.configuration = configuration; + compiler = configuration.getCompiler(); + testCompiler = configuration.getTestCompiler(); + testSelector = configuration.getTestSelector(); + output = configuration.getOutput(); + LOGGER = configuration.getLogger(); + GLOBAL_REPORT = configuration.getGlobalReport(); + } + + public TestTuple preAmplification(CtType testClassToBeAmplified, List testMethodsToBeAmplifiedAsString){ + configuration.getInputAmplDistributor().resetAmplifiers(testClassToBeAmplified); + configuration.clearData(); + final List> testMethodsToBeAmplified = + configuration.getTestFinder().findTestMethods(testClassToBeAmplified, testMethodsToBeAmplifiedAsString); + + // here, we base the execution mode to the first test method given. + // the user should provide whether JUnit3/4 OR JUnit5 but not both at the same time. + // TODO DSpot could be able to switch from one to another version of JUnit, but I believe that the ROI is not worth it. + final boolean jUnit5 = TestFramework.isJUnit5(testMethodsToBeAmplified.get(0)); + EntryPoint.jUnit5Mode = jUnit5; + DSpotPOMCreator.isCurrentlyJUnit5 = jUnit5; + Counter.reset(); + if (configuration.shouldGenerateAmplifiedTestClass()) { + testClassToBeAmplified = AmplificationHelper.renameTestClassUnderAmplification(testClassToBeAmplified); + } + time = System.currentTimeMillis(); + TestTuple tuple = new TestTuple(testClassToBeAmplified,testMethodsToBeAmplified); + return tuple; + } + + public void postAmplification(CtType testClassToBeAmplified,List> amplifiedTestMethods){ + final long elapsedTime = System.currentTimeMillis() - time; + LOGGER.info("elapsedTime {}", elapsedTime); + this.output.addClassTimeJSON(testClassToBeAmplified.getQualifiedName(), elapsedTime); + LOGGER.debug("OPTIMIZATION: GC invoked"); + System.gc(); //Optimization: cleaning up heap before printing the amplified class + configuration.getAutomaticBuilder().reset(); + try { + final TestSelectorElementReport report = configuration.getTestSelector().report(); + this.output.reportSelectorInformation(report.getReportForCollector()); + GLOBAL_REPORT.addTestSelectorReportForTestClass(testClassToBeAmplified, report); + } catch (Exception e) { + e.printStackTrace(); + LOGGER.error("Something bad happened during the report fot test-criterion."); + LOGGER.error("Dspot might not have output correctly!"); + } + final CtType amplifiedTestClass = this.output.output(testClassToBeAmplified, amplifiedTestMethods); + amplifiedTestClasses.add(amplifiedTestClass); + cleanAfterAmplificationOfOneTestClass(configuration.getCompiler(), testClassToBeAmplified); + } + + private void cleanAfterAmplificationOfOneTestClass(DSpotCompiler compiler, CtType testClassToBeAmplified) { + // Cleaning modified source directory by DSpot + try { + FileUtils.cleanDirectory(compiler.getSourceOutputDirectory()); + } catch (Exception exception) { + exception.printStackTrace(); + LOGGER.warn("Something went wrong when trying to cleaning temporary sources directory: {}", + compiler.getSourceOutputDirectory()); + } + + // Cleaning binary generated by Dspot + try { + String pathToDotClass = compiler.getBinaryOutputDirectory().getAbsolutePath() + "/" + + testClassToBeAmplified.getQualifiedName().replaceAll("\\.", "/") + ".class"; + FileUtils.forceDelete(new File(pathToDotClass)); + } catch (IOException ignored) { + //ignored + } + } + + public List> firstSelectorSetup(CtType testClassToBeAmplified, List> testMethodsToBeAmplified) + throws Exception { + if(testMethodsToBeAmplified.isEmpty()) { + LOGGER.warn("No test provided for amplification in class {}", testClassToBeAmplified.getQualifiedName()); + throw new Exception(); + } + LOGGER.info("Amplification of {} ({} test(s))", testClassToBeAmplified.getQualifiedName(), testMethodsToBeAmplified.size()); + LOGGER.info("Assertion amplification of {} ({} test(s))", testClassToBeAmplified.getQualifiedName(), testMethodsToBeAmplified.size()); + if (!testSelector.init()) { + throw new Exception(); + } + final List> passingTests; + try { + passingTests = testCompiler.compileRunAndDiscardUncompilableAndFailingTestMethods( + testClassToBeAmplified, + testMethodsToBeAmplified, + compiler); + } catch (Exception | java.lang.Error e) { + GLOBAL_REPORT.addError(new Error(ERROR_EXEC_TEST_BEFORE_AMPLIFICATION, e)); + throw new Exception(); + } + final List> selectedToBeAmplified; + try { + selectedToBeAmplified = testSelector.selectToAmplify(testClassToBeAmplified, passingTests); + } catch (Exception | java.lang.Error e) { + GLOBAL_REPORT.addError(new Error(ERROR_PRE_SELECTION, e)); + throw new Exception(); + } + return selectedToBeAmplified; + } + + public List> fullSelectorSetup(CtType testClassToBeAmplified, + List> currentTestListToBeAmplified) throws AmplificationException { + final List> selectedToBeAmplified; + try { + selectedToBeAmplified = testSelector.selectToAmplify(testClassToBeAmplified, currentTestListToBeAmplified); + } catch (Exception | java.lang.Error e) { + GLOBAL_REPORT.addError(new Error(ERROR_PRE_SELECTION, e)); + throw new AmplificationException(""); + } + if (selectedToBeAmplified.isEmpty()) { + LOGGER.warn("No test could be selected to be amplified."); + // todo should we break the loop? + throw new AmplificationException(""); + } + LOGGER.info("{} tests selected to be amplified over {} available tests", + selectedToBeAmplified.size(), + currentTestListToBeAmplified.size() + ); + return selectedToBeAmplified; + } + + public List> getAmplifiedTestClasses(){ + return amplifiedTestClasses; + } +} diff --git a/dspot/src/main/java/eu/stamp_project/utils/configuration/DSpotState.java b/dspot/src/main/java/eu/stamp_project/utils/configuration/DSpotState.java new file mode 100644 index 000000000..99322f6a3 --- /dev/null +++ b/dspot/src/main/java/eu/stamp_project/utils/configuration/DSpotState.java @@ -0,0 +1,343 @@ +package eu.stamp_project.utils.configuration; + +import eu.stamp_project.automaticbuilder.AutomaticBuilder; +import eu.stamp_project.automaticbuilder.maven.DSpotPOMCreator; +import eu.stamp_project.dspot.DSpot; +import eu.stamp_project.dspot.amplifier.Amplifier; +import eu.stamp_project.dspot.assertiongenerator.AssertionGenerator; +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.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.program.InputConfiguration; +import eu.stamp_project.utils.report.GlobalReport; +import eu.stamp_project.utils.report.error.ErrorReportImpl; +import eu.stamp_project.utils.report.output.Output; +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 spoon.reflect.declaration.CtType; +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import static eu.stamp_project.utils.AmplificationHelper.PATH_SEPARATOR; + +/** + * Created by Andrew Bwogi + * abwogi@kth.se + * on 31/10/19 + */ +public class DSpotState { + + private int nbIteration; + private InputConfiguration inputConfiguration; + private List> testClassesToBeAmplified; + private List testMethodsToBeAmplifiedNames; + private TestSelector testSelector; + private InputAmplDistributor inputAmplDistributor; + private Output output; + private Collector collector; + private boolean collectData; + private DSpotCompiler compiler; + private AutomaticBuilder automaticBuilder; + private TestFinder testFinder; + private long startTime; + private AssertionGenerator assertionGenerator; + private TestCompiler testCompiler; + public static final GlobalReport GLOBAL_REPORT = + new GlobalReport(new OutputReportImpl(), new ErrorReportImpl(), new TestSelectorReportImpl()); + public static boolean verbose = false; + private final Logger LOGGER = LoggerFactory.getLogger(DSpot.class); + private double delta; + + public DSpotState(InputConfiguration inputConfiguration) { + this.inputConfiguration = inputConfiguration; + verbose = inputConfiguration.isVerbose(); + startTime = System.currentTimeMillis(); + testFinder = new TestFinder( + Arrays.stream(inputConfiguration.getExcludedClasses().split(",")).collect(Collectors.toList()), + Arrays.stream(inputConfiguration.getExcludedTestCases().split(",")).collect(Collectors.toList()) + ); + automaticBuilder = inputConfiguration.getBuilderEnum().getAutomaticBuilder(inputConfiguration); + final String dependencies = completeDependencies(inputConfiguration, automaticBuilder); + compiler = DSpotCompiler.createDSpotCompiler( + inputConfiguration, + dependencies + ); + inputConfiguration.setFactory(compiler.getLauncher().getFactory()); + initHelpers(inputConfiguration); + 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() + ); + collector = CollectorFactory.build(inputConfiguration, emailSender); + collector.reportInitInformation( + inputConfiguration.getAmplifiers(), + inputConfiguration.getSelector(), + inputConfiguration.getNbIteration(), + inputConfiguration.isGregorMode(), + !inputConfiguration.isGregorMode(), + inputConfiguration.getNumberParallelExecutionProcessors() + ); + testClassesToBeAmplified = testFinder.findTestClasses(inputConfiguration.getTestClasses()); + testMethodsToBeAmplifiedNames = inputConfiguration.getTestCases(); + testSelector = inputConfiguration.getSelector().buildSelector(automaticBuilder, inputConfiguration); + final List amplifiers = inputConfiguration + .getAmplifiers() + .stream() + .map(AmplifierEnum::getAmplifier) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + inputAmplDistributor = inputConfiguration + .getInputAmplDistributor() + .getInputAmplDistributor(inputConfiguration.getMaxTestAmplified(), amplifiers); + output = new Output( + inputConfiguration.getAbsolutePathToProjectRoot(), + inputConfiguration.getOutputDirectory(), + collector + + ); + assertionGenerator = new AssertionGenerator(inputConfiguration.getDelta(), compiler, testCompiler); + Checker.postChecking(inputConfiguration); + collectData = true; + delta = inputConfiguration.getDelta(); + nbIteration = inputConfiguration.getNbIteration(); + verbose = inputConfiguration.isVerbose(); + } + + public DSpotState() { + inputConfiguration = new InputConfiguration(); + testMethodsToBeAmplifiedNames = Collections.emptyList(); + collectData = false; + } + + public 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 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 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 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 + } + } + + public void report(List> amplifiedTestClasses) { + LOGGER.info("Amplification {}.", amplifiedTestClasses.isEmpty() ? "failed" : "succeed"); + final long elapsedTime = System.currentTimeMillis() - startTime; + LOGGER.info("Elapsed time {} ms", elapsedTime); + GLOBAL_REPORT.output(inputConfiguration.getOutputDirectory()); + DSpotCache.reset(); + GLOBAL_REPORT.reset(); + AmplificationHelper.reset(); + DSpotPOMCreator.delete(); + if(collectData) { + collector.sendInfo(); + } + } + + /** + * Optimization: an object that holds a dictionary + * with large number of cloned CtMethods is not required anymore. + * it is cleared before iterating again for next test class. + */ + public void clearData(){ + this.assertionGenerator = new AssertionGenerator(delta, this.compiler, this.testCompiler); + } + + public AssertionGenerator getAssertionGenerator() { + return assertionGenerator; + } + + public void setAssertionGenerator(AssertionGenerator assertionGenerator) { + this.assertionGenerator = assertionGenerator; + } + + public TestCompiler getTestCompiler() { + return testCompiler; + } + + public void setTestCompiler(TestCompiler testCompiler) { + this.testCompiler = testCompiler; + } + + public List> getTestClassesToBeAmplified() { + return testClassesToBeAmplified; + } + + public void setTestClassesToBeAmplified(List> testClassesToBeAmplified) { + this.testClassesToBeAmplified = testClassesToBeAmplified; + } + + public List getTestMethodsToBeAmplifiedNames() { + return testMethodsToBeAmplifiedNames; + } + + public TestSelector getTestSelector() { + return testSelector; + } + + public void setTestSelector(TestSelector testSelector) { + this.testSelector = testSelector; + } + + public InputAmplDistributor getInputAmplDistributor() { + return inputAmplDistributor; + } + + public Output getOutput() { + return output; + } + + public void setOutput(Output output) { + this.output = output; + } + + public DSpotCompiler getCompiler() { + return compiler; + } + + public void setCompiler(DSpotCompiler compiler) { + this.compiler = compiler; + } + + public AutomaticBuilder getAutomaticBuilder() { + return automaticBuilder; + } + + public void setAutomaticBuilder(AutomaticBuilder automaticBuilder) { + this.automaticBuilder = automaticBuilder; + } + + public TestFinder getTestFinder() { + return testFinder; + } + + public void setTestFinder(TestFinder testFinder) { + this.testFinder = testFinder; + } + + public Logger getLogger() { + return LOGGER; + } + + public GlobalReport getGlobalReport() { + return GLOBAL_REPORT; + } + + public void setInputAmplDistributor(InputAmplDistributor inputAmplDistributor) { + this.inputAmplDistributor = inputAmplDistributor; + } + + public int getNbIteration() { + return nbIteration; + } + + public void setNbIteration(int nbIteration) { + this.nbIteration = nbIteration; + } + + public void setCollector(Collector collector) { + this.collector = collector; + } + + public void setDelta(double delta) { + this.delta = delta; + } + + public boolean shouldGenerateAmplifiedTestClass() { + return inputConfiguration.shouldGenerateAmplifiedTestClass(); + } +} diff --git a/dspot/src/main/java/eu/stamp_project/utils/configuration/TestTuple.java b/dspot/src/main/java/eu/stamp_project/utils/configuration/TestTuple.java new file mode 100644 index 000000000..652398eb0 --- /dev/null +++ b/dspot/src/main/java/eu/stamp_project/utils/configuration/TestTuple.java @@ -0,0 +1,16 @@ +package eu.stamp_project.utils.configuration; + +import spoon.reflect.declaration.CtMethod; +import spoon.reflect.declaration.CtType; + +import java.util.List; + +public class TestTuple { + public CtType testClassToBeAmplified; + public List> testMethodsToBeAmplified; + + public TestTuple(CtType testClassToBeAmplified, List> testMethodsToBeAmplified){ + this.testClassToBeAmplified = testClassToBeAmplified; + this.testMethodsToBeAmplified = testMethodsToBeAmplified; + } +} diff --git a/dspot/src/main/java/eu/stamp_project/utils/execution/TestRunner.java b/dspot/src/main/java/eu/stamp_project/utils/execution/TestRunner.java index 8c472e4d1..075ca7ac1 100644 --- a/dspot/src/main/java/eu/stamp_project/utils/execution/TestRunner.java +++ b/dspot/src/main/java/eu/stamp_project/utils/execution/TestRunner.java @@ -1,6 +1,5 @@ package eu.stamp_project.utils.execution; -import eu.stamp_project.Main; import eu.stamp_project.automaticbuilder.maven.DSpotPOMCreator; import eu.stamp_project.dspot.AmplificationException; import eu.stamp_project.testrunner.listener.TestResult; @@ -8,6 +7,7 @@ import eu.stamp_project.utils.AmplificationHelper; import eu.stamp_project.utils.DSpotUtils; +import eu.stamp_project.utils.configuration.DSpotState; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import spoon.reflect.declaration.CtMethod; @@ -40,7 +40,7 @@ public TestRunner(String absolutePathToProjectRoot, String preGoals, boolean sho this.absolutePathToProjectRoot = absolutePathToProjectRoot; this.preGoals = preGoals; this.shouldUseMavenToExecuteTest = shouldUseMavenToExecuteTest; - EntryPoint.verbose = Main.verbose; + EntryPoint.verbose = DSpotState.verbose; } public TestResult runSubClassesForAbstractTestClass(CtType testClass, List> testsToRun, String classPath) throws AmplificationException { diff --git a/dspot/src/main/java/eu/stamp_project/utils/options/check/Checker.java b/dspot/src/main/java/eu/stamp_project/utils/options/check/Checker.java index 5d818a20c..a29cf78f4 100644 --- a/dspot/src/main/java/eu/stamp_project/utils/options/check/Checker.java +++ b/dspot/src/main/java/eu/stamp_project/utils/options/check/Checker.java @@ -1,8 +1,8 @@ package eu.stamp_project.utils.options.check; -import eu.stamp_project.Main; import eu.stamp_project.utils.AmplificationHelper; import eu.stamp_project.utils.DSpotUtils; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.program.InputConfiguration; import eu.stamp_project.utils.report.error.Error; import eu.stamp_project.utils.report.error.ErrorEnum; @@ -133,7 +133,7 @@ public static boolean checkSystemProperties(String systemProperties) { public static void checkIsACorrectVersion(final String proposedVersion) { if (!Pattern.compile("(\\p{Digit})+(\\.(\\p{Digit})+)*(-SNAPSHOT)?").matcher(proposedVersion).matches()) { - Main.GLOBAL_REPORT.addInputError(new Error( + DSpotState.GLOBAL_REPORT.addInputError(new Error( ErrorEnum.ERROR_INVALID_VERSION, "Version " + proposedVersion + " is not a valid version" ) ); @@ -163,7 +163,7 @@ private static void checkPathnameNotNullAndFileExist(final String pathname, String additionalMessageWhenIsNull, String additionalMessageWhenDoesNotExist) { if (pathname == null) { - Main.GLOBAL_REPORT.addInputError(new Error(errorEnumInCaseOfError, additionalMessageWhenIsNull)); + DSpotState.GLOBAL_REPORT.addInputError(new Error(errorEnumInCaseOfError, additionalMessageWhenIsNull)); throw new InputErrorException(); } else { checkFileExists(pathname, errorEnumInCaseOfError, additionalMessageWhenDoesNotExist); @@ -172,7 +172,7 @@ private static void checkPathnameNotNullAndFileExist(final String pathname, private static void checkFileExists(final String pathname, ErrorEnum errorEnumInCaseOfError, String additionalMessage) { if (!new File(pathname).exists()) { - Main.GLOBAL_REPORT.addInputError(new Error(errorEnumInCaseOfError, additionalMessage + "(" + pathname + ")")); + DSpotState.GLOBAL_REPORT.addInputError(new Error(errorEnumInCaseOfError, additionalMessage + "(" + pathname + ")")); throw new InputErrorException(); } } diff --git a/dspot/src/main/java/eu/stamp_project/utils/report/output/Output.java b/dspot/src/main/java/eu/stamp_project/utils/report/output/Output.java index 247983f4b..51d6db231 100644 --- a/dspot/src/main/java/eu/stamp_project/utils/report/output/Output.java +++ b/dspot/src/main/java/eu/stamp_project/utils/report/output/Output.java @@ -2,12 +2,12 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import eu.stamp_project.Main; import eu.stamp_project.test_framework.TestFramework; import eu.stamp_project.utils.AmplificationHelper; import eu.stamp_project.utils.DSpotUtils; import eu.stamp_project.utils.collector.Collector; import eu.stamp_project.utils.collector.NullCollector; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.json.ClassTimeJSON; import eu.stamp_project.utils.json.ProjectTimeJSON; import eu.stamp_project.utils.program.InputConfiguration; @@ -58,8 +58,8 @@ public CtType output(CtType testClassToBeAmplified, List> ampl final CtType amplification = AmplificationHelper.createAmplifiedTest(amplifiedTestMethods, clone); final File outputDirectory = new File(this.outputPathDirectory); if (!amplifiedTestMethods.isEmpty()) { - Main.GLOBAL_REPORT.addNumberAmplifiedTestMethodsToTotal(amplifiedTestMethods.size()); - Main.GLOBAL_REPORT.addPrintedTestClasses( + DSpotState.GLOBAL_REPORT.addNumberAmplifiedTestMethodsToTotal(amplifiedTestMethods.size()); + DSpotState.GLOBAL_REPORT.addPrintedTestClasses( String.format("Print %s with %d amplified test cases in %s", amplification.getQualifiedName() + ".java", amplifiedTestMethods.size(), @@ -91,8 +91,8 @@ private void outputSeedTestClassWithSuccessTestMethods(CtType testClassToBeAm .distinct() .forEach(clone::addMethod); final File outputDirectory = new File(this.outputPathDirectory + "/original/"); - Main.GLOBAL_REPORT.addNumberAmplifiedTestMethodsToTotal(amplifiedTestMethods.size()); - Main.GLOBAL_REPORT.addPrintedTestClasses( + DSpotState.GLOBAL_REPORT.addNumberAmplifiedTestMethodsToTotal(amplifiedTestMethods.size()); + DSpotState.GLOBAL_REPORT.addPrintedTestClasses( String.format("Print %s with %d amplified test cases in %s", clone.getQualifiedName() + ".java", amplifiedTestMethods.size(), diff --git a/dspot/src/main/java/eu/stamp_project/utils/test_finder/TestFinder.java b/dspot/src/main/java/eu/stamp_project/utils/test_finder/TestFinder.java index d877c774d..08c6d717a 100644 --- a/dspot/src/main/java/eu/stamp_project/utils/test_finder/TestFinder.java +++ b/dspot/src/main/java/eu/stamp_project/utils/test_finder/TestFinder.java @@ -1,8 +1,8 @@ package eu.stamp_project.utils.test_finder; -import eu.stamp_project.Main; import eu.stamp_project.test_framework.TestFramework; import eu.stamp_project.utils.AmplificationHelper; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.program.InputConfiguration; import eu.stamp_project.utils.report.error.Error; import eu.stamp_project.utils.report.error.ErrorEnum; @@ -99,7 +99,7 @@ private boolean checkIfNoneTestClassMatched(List testClassNames, .stream() .filter(testClassName -> { if (collect.get(testClassName).isEmpty()) { - Main.GLOBAL_REPORT.addError( + DSpotState.GLOBAL_REPORT.addError( new Error(ErrorEnum.ERROR_NO_TEST_COULD_BE_FOUND_MATCHING_REGEX, String.format("Your input:%n\t%s", testClassName) ) @@ -120,7 +120,7 @@ private boolean checkIfNoneTestClassMatched(List testClassNames, LOGGER.error("\t Please, refer to the Java documentation of java.util.regex.Pattern."); LOGGER.error("\t (-c | --cases) should be followed by correct method name,"); LOGGER.error("\t that are contained in the test classes that match the previous option, i.e. (-t | --test)."); - Main.GLOBAL_REPORT.addError( + DSpotState.GLOBAL_REPORT.addError( new Error(ErrorEnum.ERROR_NO_TEST_COULD_BE_FOUND, String.format("Your input:%n\t%s", testClassToBeAmplifiedJoined) ) diff --git a/dspot/src/test/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilderTest.java b/dspot/src/test/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilderTest.java index 90a452a36..409b2a7a2 100644 --- a/dspot/src/test/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilderTest.java +++ b/dspot/src/test/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilderTest.java @@ -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.options.AutomaticBuilderEnum; import eu.stamp_project.utils.pit.AbstractPitResult; import eu.stamp_project.utils.pit.PitXMLResultParser; @@ -44,7 +44,7 @@ public class GradleAutomaticBuilderTest { @Before public void setUp() throws Exception { - Main.verbose = true; + DSpotState.verbose = true; cleanTestEnv(); LOGGER.debug("Test Set-up - Reading input parameters..."); LOGGER.debug("Test Set-up - instantiating Automatic Builder (SUT)..."); diff --git a/dspot/src/test/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilderWithDescartesTest.java b/dspot/src/test/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilderWithDescartesTest.java index c58984048..7b92518d4 100644 --- a/dspot/src/test/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilderWithDescartesTest.java +++ b/dspot/src/test/java/eu/stamp_project/automaticbuilder/gradle/GradleAutomaticBuilderWithDescartesTest.java @@ -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.options.AutomaticBuilderEnum; import eu.stamp_project.utils.pit.AbstractPitResult; import eu.stamp_project.utils.pit.PitXMLResultParser; @@ -40,7 +40,7 @@ public class GradleAutomaticBuilderWithDescartesTest { @Before public void setUp() throws Exception { - Main.verbose = true; + DSpotState.verbose = true; cleanTestEnv(); LOGGER.debug("Test Set-up - Reading input parameters..."); LOGGER.debug("Test Set-up - instantiating Automatic Builder (SUT)..."); diff --git a/dspot/src/test/java/eu/stamp_project/automaticbuilder/maven/MavenAutomaticBuilderTest.java b/dspot/src/test/java/eu/stamp_project/automaticbuilder/maven/MavenAutomaticBuilderTest.java index fd2d0f5d4..e4e4c6b72 100644 --- a/dspot/src/test/java/eu/stamp_project/automaticbuilder/maven/MavenAutomaticBuilderTest.java +++ b/dspot/src/test/java/eu/stamp_project/automaticbuilder/maven/MavenAutomaticBuilderTest.java @@ -1,7 +1,7 @@ package eu.stamp_project.automaticbuilder.maven; -import eu.stamp_project.Main; import eu.stamp_project.automaticbuilder.AutomaticBuilder; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.options.AutomaticBuilderEnum; import eu.stamp_project.utils.pit.AbstractPitResult; import eu.stamp_project.utils.pit.PitXMLResultParser; @@ -47,7 +47,7 @@ public void setUp(String path, String filter, boolean isDescartesMode, String ad this.launcher.addInputResource(path + "/src/"); this.launcher.getEnvironment().setNoClasspath(true); this.launcher.buildModel(); - Main.verbose = true; + DSpotState.verbose = true; try { FileUtils.deleteDirectory(new File(path + "/target/")); } catch (IOException ignored) { diff --git a/dspot/src/test/java/eu/stamp_project/dspot/ProjectJSONTest.java b/dspot/src/test/java/eu/stamp_project/dspot/ProjectJSONTest.java index 853c1e93d..1b819087e 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/ProjectJSONTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/ProjectJSONTest.java @@ -2,10 +2,10 @@ import com.google.gson.Gson; import com.google.gson.GsonBuilder; -import eu.stamp_project.Main; import eu.stamp_project.automaticbuilder.AutomaticBuilder; import eu.stamp_project.automaticbuilder.maven.DSpotPOMCreator; import eu.stamp_project.dspot.amplifier.value.ValueCreator; +import eu.stamp_project.dspot.assertiongenerator.AssertionGenerator; import eu.stamp_project.dspot.assertiongenerator.assertiongenerator.AssertionGeneratorUtils; import eu.stamp_project.dspot.input_ampl_distributor.InputAmplDistributor; import eu.stamp_project.dspot.selector.JacocoCoverageSelector; @@ -17,7 +17,7 @@ import eu.stamp_project.utils.RandomHelper; import eu.stamp_project.utils.compilation.DSpotCompiler; import eu.stamp_project.utils.compilation.TestCompiler; -import eu.stamp_project.utils.execution.TestRunner; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.json.ProjectTimeJSON; import eu.stamp_project.utils.options.AutomaticBuilderEnum; import eu.stamp_project.utils.options.InputAmplDistributorEnum; @@ -59,6 +59,8 @@ public class ProjectJSONTest extends AbstractTestOnSample { private TestCompiler testCompiler; + private DSpotState dspotState; + @Before public void setUp() { super.setUp(); @@ -66,7 +68,8 @@ public void setUp() { this.configuration.setAbsolutePathToProjectRoot(getPathToProjectRoot()); this.configuration.setOutputDirectory(outputDirectory); this.builder = AutomaticBuilderEnum.Maven.getAutomaticBuilder(configuration); - String dependencies = Main.completeDependencies(configuration, this.builder); + this.dspotState = new DSpotState(); + String dependencies = dspotState.completeDependencies(configuration, this.builder); DSpotUtils.init(false, outputDirectory, this.configuration.getFullClassPathWithExtraDependencies(), this.getPathToProjectRoot() @@ -110,22 +113,23 @@ public void test() throws Exception { file.delete(); } - final InputAmplDistributor inputAmplDistributor = InputAmplDistributorEnum.RandomInputAmplDistributor.getInputAmplDistributor(200, Collections.emptyList()); - DSpot dspot = new DSpot( - 0.1f, - TestFinder.get(), - compiler, - testSelector, - inputAmplDistributor, - Output.get(configuration), - 1, - false, - builder, - testCompiler - ); + final InputAmplDistributor inputAmplDistributor = + InputAmplDistributorEnum.RandomInputAmplDistributor.getInputAmplDistributor(200, Collections.emptyList()); + DSpotState dspotState = new DSpotState(); + dspotState.setDelta(0.1f); + dspotState.setTestFinder(TestFinder.get()); + dspotState.setCompiler(compiler); + dspotState.setTestSelector(testSelector); + dspotState.setInputAmplDistributor(inputAmplDistributor); + dspotState.setOutput(Output.get(configuration)); + dspotState.setNbIteration(1); + dspotState.setAutomaticBuilder(builder); + dspotState.setTestCompiler(testCompiler); + dspotState.setTestClassesToBeAmplified(Collections.singletonList(findClass("fr.inria.amp.TestJavaPoet"))); + dspotState.setAssertionGenerator(new AssertionGenerator(0.1f, compiler, testCompiler)); + DSpot dspot = new DSpot(dspotState); final CtClass clone = findClass("fr.inria.amp.TestJavaPoet").clone(); - - dspot.amplify(findClass("fr.inria.amp.TestJavaPoet"), Collections.emptyList()); + dspot.run(); ProjectTimeJSON projectJson = getProjectJson(file); assertTrue(projectJson.classTimes. stream() @@ -136,7 +140,8 @@ public void test() throws Exception { assertEquals(1, projectJson.classTimes.size()); assertEquals("sample", projectJson.projectName); - dspot.amplify(findClass("fr.inria.mutation.ClassUnderTestTest"), Collections.emptyList()); + dspotState.setTestClassesToBeAmplified(Collections.singletonList(findClass("fr.inria.mutation.ClassUnderTestTest"))); + dspot.run(); projectJson = getProjectJson(file); assertTrue(projectJson.classTimes.stream().anyMatch(classTimeJSON -> classTimeJSON.fullQualifiedName.equals("fr.inria.amp.TestJavaPoet"))); assertTrue(projectJson.classTimes.stream().anyMatch(classTimeJSON -> classTimeJSON.fullQualifiedName.equals("fr.inria.mutation.ClassUnderTestTest"))); @@ -150,7 +155,8 @@ public void test() throws Exception { aPackage.removeType(amplifiedClassToBeRemoved); aPackage.addType(clone); - dspot.amplify(findClass("fr.inria.amp.TestJavaPoet"), Collections.emptyList()); + dspotState.setTestClassesToBeAmplified(Collections.singletonList(findClass("fr.inria.amp.TestJavaPoet"))); + dspot.run(); projectJson = getProjectJson(file); assertTrue(projectJson.classTimes.stream().anyMatch(classTimeJSON -> classTimeJSON.fullQualifiedName.equals("fr.inria.amp.TestJavaPoet"))); assertTrue(projectJson.classTimes.stream().anyMatch(classTimeJSON -> classTimeJSON.fullQualifiedName.equals("fr.inria.mutation.ClassUnderTestTest"))); diff --git a/dspot/src/test/java/eu/stamp_project/dspot/RecoveryDSpotTest.java b/dspot/src/test/java/eu/stamp_project/dspot/RecoveryDSpotTest.java index 97ce06be9..930c06950 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/RecoveryDSpotTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/RecoveryDSpotTest.java @@ -1,16 +1,16 @@ package eu.stamp_project.dspot; -import eu.stamp_project.Main; import eu.stamp_project.automaticbuilder.AutomaticBuilder; import eu.stamp_project.dspot.amplifier.Amplifier; import eu.stamp_project.dspot.assertiongenerator.AssertionGenerator; import eu.stamp_project.dspot.input_ampl_distributor.TextualDistanceInputAmplDistributor; +import eu.stamp_project.dspot.selector.TakeAllSelector; +import eu.stamp_project.utils.compilation.DSpotCompiler; import eu.stamp_project.utils.compilation.TestCompiler; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.options.AutomaticBuilderEnum; -import eu.stamp_project.utils.report.error.ErrorEnum; -import eu.stamp_project.dspot.selector.TakeAllSelector; import eu.stamp_project.utils.program.InputConfiguration; -import eu.stamp_project.utils.compilation.DSpotCompiler; +import eu.stamp_project.utils.report.error.ErrorEnum; import eu.stamp_project.utils.test_finder.TestFinder; import org.junit.After; import org.junit.Before; @@ -19,12 +19,12 @@ import spoon.reflect.declaration.CtMethod; import spoon.reflect.declaration.CtType; -import java.lang.reflect.Field; import java.util.Collections; import java.util.List; import java.util.stream.Stream; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; /** * created by Benjamin DANGLOT @@ -43,7 +43,7 @@ public class RecoveryDSpotTest extends AbstractTestOnSample { @Before public void setUp() { super.setUp(); - Main.GLOBAL_REPORT.reset(); + DSpotState.GLOBAL_REPORT.reset(); configuration = new InputConfiguration(); configuration.setAbsolutePathToProjectRoot(this.getPathToProjectRoot()); this.builder = AutomaticBuilderEnum.Maven.getAutomaticBuilder(configuration); @@ -60,7 +60,7 @@ public void setUp() { @After public void tearDown() throws Exception { - Main.GLOBAL_REPORT.reset(); + DSpotState.GLOBAL_REPORT.reset(); } public class SelectorThatThrowsError extends TakeAllSelector { @@ -131,66 +131,52 @@ public void testThatDSpotCanRecoverFromError() throws Exception { final SelectorThatThrowsError selector = new SelectorThatThrowsError(builder, configuration); selector.setThrowsToAmplify(true); DSpotCompiler compiler = DSpotCompiler.createDSpotCompiler(configuration, ""); - Amplification amplification = new Amplification( - 0.1F, - compiler, - selector, - new TextualDistanceInputAmplDistributor(200, Collections.emptyList()), - 1, - testCompiler - ); final TestFinder testFinder = new TestFinder(Collections.emptyList(), Collections.emptyList()); final CtClass testClassToBeAmplified = findClass("fr.inria.amp.OneLiteralTest"); final List> testListToBeAmplified = testFinder.findTestMethods(testClassToBeAmplified, Collections.emptyList()); - amplification.amplification(testClassToBeAmplified, testListToBeAmplified, Collections.emptyList(), 1); - assertEquals(1, Main.GLOBAL_REPORT.getErrors().size()); - assertSame(ErrorEnum.ERROR_PRE_SELECTION, Main.GLOBAL_REPORT.getErrors().get(0).type); - Main.GLOBAL_REPORT.reset(); + DSpotState dspotState = new DSpotState(); + dspotState.setDelta(0.1f); + dspotState.setCompiler(compiler); + dspotState.setTestSelector(selector); + dspotState.setInputAmplDistributor(new TextualDistanceInputAmplDistributor(200, Collections.emptyList())); + dspotState.setNbIteration(1); + dspotState.setTestCompiler(testCompiler); + dspotState.setAssertionGenerator(new AssertionGenerator(0.1f, compiler, testCompiler)); + DSpot dspot = new DSpot(dspotState); + dspot.fullAmplification(testClassToBeAmplified, testListToBeAmplified, Collections.emptyList(), 1); + assertEquals(1, DSpotState.GLOBAL_REPORT.getErrors().size()); + assertSame(ErrorEnum.ERROR_PRE_SELECTION, DSpotState.GLOBAL_REPORT.getErrors().get(0).type); + DSpotState.GLOBAL_REPORT.reset(); selector.setThrowsToAmplify(false); selector.setThrowsToKeep(true); - amplification.amplification(testClassToBeAmplified, testListToBeAmplified, Collections.emptyList(), 1); - assertEquals(1, Main.GLOBAL_REPORT.getErrors().size()); - assertSame(ErrorEnum.ERROR_SELECTION, Main.GLOBAL_REPORT.getErrors().get(0).type); - Main.GLOBAL_REPORT.reset(); + dspot.fullAmplification(testClassToBeAmplified, testListToBeAmplified, Collections.emptyList(), 1); + assertEquals(1, DSpotState.GLOBAL_REPORT.getErrors().size()); + assertSame(ErrorEnum.ERROR_SELECTION, DSpotState.GLOBAL_REPORT.getErrors().get(0).type); + DSpotState.GLOBAL_REPORT.reset(); final List amplifiers = Collections.singletonList(new AmplifierThatThrowsError()); - amplification = new Amplification( - 0.1f, - compiler, - new TakeAllSelector(this.builder, this.configuration), - new TextualDistanceInputAmplDistributor(200, amplifiers), - 1, - testCompiler - ); - amplification.amplification(testClassToBeAmplified, testListToBeAmplified, Collections.emptyList(), 1); - assertEquals(1, Main.GLOBAL_REPORT.getErrors().size()); - assertSame(ErrorEnum.ERROR_INPUT_AMPLIFICATION, Main.GLOBAL_REPORT.getErrors().get(0).type); - Main.GLOBAL_REPORT.reset(); - - amplification = new Amplification( - 0.1f, - compiler, - new TakeAllSelector(this.builder, this.configuration), - new TextualDistanceInputAmplDistributor(200, Collections.emptyList()), - 1, - testCompiler - ); - final Field assertGenerator = amplification.getClass().getDeclaredField("assertionGenerator"); - assertGenerator.setAccessible(true); - assertGenerator.set(amplification, new AssertionGeneratorThatThrowsError(compiler)); - amplification.amplification(testClassToBeAmplified, testListToBeAmplified, Collections.emptyList(), 1); - assertEquals(1, Main.GLOBAL_REPORT.getErrors().size()); - assertSame(ErrorEnum.ERROR_ASSERT_AMPLIFICATION, Main.GLOBAL_REPORT.getErrors().get(0).type); - Main.GLOBAL_REPORT.reset(); + dspotState.setTestSelector(new TakeAllSelector(this.builder, this.configuration)); + dspotState.setInputAmplDistributor(new TextualDistanceInputAmplDistributor(200, amplifiers)); + dspot.fullAmplification(testClassToBeAmplified, testListToBeAmplified, Collections.emptyList(), 1); + assertEquals(1, DSpotState.GLOBAL_REPORT.getErrors().size()); + assertSame(ErrorEnum.ERROR_INPUT_AMPLIFICATION, DSpotState.GLOBAL_REPORT.getErrors().get(0).type); + DSpotState.GLOBAL_REPORT.reset(); + + dspotState.setInputAmplDistributor(new TextualDistanceInputAmplDistributor(200, Collections.emptyList())); + dspotState.setAssertionGenerator(new AssertionGeneratorThatThrowsError(compiler)); + dspot.fullAmplification(testClassToBeAmplified, testListToBeAmplified, Collections.emptyList(), 1); + assertEquals(1, DSpotState.GLOBAL_REPORT.getErrors().size()); + assertSame(ErrorEnum.ERROR_ASSERT_AMPLIFICATION, DSpotState.GLOBAL_REPORT.getErrors().get(0).type); + DSpotState.GLOBAL_REPORT.reset(); } @Test public void testNoMatchingTestClasses() { final TestFinder testFinder = new TestFinder(Collections.emptyList(), Collections.emptyList()); testFinder.findTestClasses(Collections.singletonList("this.is.not.a.correct.package")); - assertEquals(2, Main.GLOBAL_REPORT.getErrors().size()); - assertSame(ErrorEnum.ERROR_NO_TEST_COULD_BE_FOUND_MATCHING_REGEX, Main.GLOBAL_REPORT.getErrors().get(0).type); - assertSame(ErrorEnum.ERROR_NO_TEST_COULD_BE_FOUND, Main.GLOBAL_REPORT.getErrors().get(1).type); + assertEquals(2, DSpotState.GLOBAL_REPORT.getErrors().size()); + assertSame(ErrorEnum.ERROR_NO_TEST_COULD_BE_FOUND_MATCHING_REGEX, DSpotState.GLOBAL_REPORT.getErrors().get(0).type); + assertSame(ErrorEnum.ERROR_NO_TEST_COULD_BE_FOUND, DSpotState.GLOBAL_REPORT.getErrors().get(1).type); } } diff --git a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/AssertionGeneratorTest.java b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/AssertionGeneratorTest.java index 79859d50e..37686280d 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/AssertionGeneratorTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/AssertionGeneratorTest.java @@ -1,6 +1,5 @@ package eu.stamp_project.dspot.assertiongenerator; -import eu.stamp_project.Main; import eu.stamp_project.automaticbuilder.AutomaticBuilder; import eu.stamp_project.dspot.AbstractTestOnSample; import eu.stamp_project.dspot.amplifier.value.ValueCreator; @@ -11,7 +10,7 @@ import eu.stamp_project.utils.RandomHelper; import eu.stamp_project.utils.compilation.DSpotCompiler; import eu.stamp_project.utils.compilation.TestCompiler; -import eu.stamp_project.utils.execution.TestRunner; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.options.AutomaticBuilderEnum; import eu.stamp_project.utils.program.InputConfiguration; import org.eclipse.jdt.core.compiler.CategorizedProblem; @@ -57,13 +56,16 @@ public class AssertionGeneratorTest extends AbstractTestOnSample { private static String dependencies; + private static DSpotState dspotState; + @BeforeClass public static void setUpClass() { configuration = new InputConfiguration(); configuration.setAbsolutePathToProjectRoot(new File("src/test/resources/sample/").getAbsolutePath()); AutomaticBuilder builder = AutomaticBuilderEnum.Maven.getAutomaticBuilder(configuration); - dependencies = Main.completeDependencies(configuration, builder); + dspotState = new DSpotState(); + dependencies = dspotState.completeDependencies(configuration, builder); DSpotUtils.init(false, "target/dspot/", configuration.getFullClassPathWithExtraDependencies(), "src/test/resources/sample/" diff --git a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemoverTest.java b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemoverTest.java index 1d9e56fec..5c62d60ef 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemoverTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemoverTest.java @@ -1,13 +1,11 @@ package eu.stamp_project.dspot.assertiongenerator.assertiongenerator; -import eu.stamp_project.Main; import eu.stamp_project.automaticbuilder.AutomaticBuilder; import eu.stamp_project.dspot.AbstractTestOnSample; import eu.stamp_project.dspot.assertiongenerator.assertiongenerator.methodreconstructor.observer.TestWithLogGenerator; import eu.stamp_project.test_framework.TestFramework; import eu.stamp_project.utils.AmplificationHelper; -import eu.stamp_project.utils.DSpotUtils; -import eu.stamp_project.utils.compilation.TestCompiler; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.options.AutomaticBuilderEnum; import eu.stamp_project.utils.program.InputConfiguration; import org.junit.Test; @@ -22,7 +20,6 @@ import spoon.reflect.visitor.filter.TypeFilter; import java.io.File; -import java.util.Arrays; import java.util.Collections; import static org.junit.Assert.*; @@ -273,7 +270,8 @@ public void testOnDifferentKindOfAssertions() throws Exception { // // ); AutomaticBuilder builder = AutomaticBuilderEnum.Maven.getAutomaticBuilder(configuration); - String dependencies = Main.completeDependencies(configuration, builder); + DSpotState dspotState = new DSpotState(); + String dependencies = dspotState.completeDependencies(configuration, builder); launcher = new Launcher(); launcher.addInputResource(getPathToProjectRoot()); launcher.getEnvironment().setOutputType(OutputType.CLASSES); diff --git a/dspot/src/test/java/eu/stamp_project/dspot/selector/AbstractSelectorRemoveOverlapTest.java b/dspot/src/test/java/eu/stamp_project/dspot/selector/AbstractSelectorRemoveOverlapTest.java index c3dfc2904..27dcf02fb 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/selector/AbstractSelectorRemoveOverlapTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/selector/AbstractSelectorRemoveOverlapTest.java @@ -1,11 +1,11 @@ package eu.stamp_project.dspot.selector; -import eu.stamp_project.Main; import eu.stamp_project.automaticbuilder.AutomaticBuilder; import eu.stamp_project.automaticbuilder.maven.DSpotPOMCreator; import eu.stamp_project.dspot.DSpot; import eu.stamp_project.dspot.amplifier.StringLiteralAmplifier; import eu.stamp_project.dspot.amplifier.value.ValueCreator; +import eu.stamp_project.dspot.assertiongenerator.AssertionGenerator; import eu.stamp_project.dspot.assertiongenerator.assertiongenerator.AssertionGeneratorUtils; import eu.stamp_project.test_framework.TestFramework; import eu.stamp_project.utils.DSpotCache; @@ -13,7 +13,7 @@ import eu.stamp_project.utils.RandomHelper; import eu.stamp_project.utils.compilation.DSpotCompiler; import eu.stamp_project.utils.compilation.TestCompiler; -import eu.stamp_project.utils.execution.TestRunner; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.options.AutomaticBuilderEnum; import eu.stamp_project.utils.options.InputAmplDistributorEnum; import eu.stamp_project.utils.program.InputConfiguration; @@ -66,18 +66,19 @@ protected CtClass getTestClass() { protected DSpotCompiler compiler; - protected TestRunner testRunner; - protected TestCompiler testCompiler; + protected DSpotState dspotState; + @Before public void setUp() { - Main.verbose = true; + DSpotState.verbose = true; this.configuration = new InputConfiguration(); this.configuration.setAbsolutePathToProjectRoot(getPathToAbsoluteProjectRoot()); this.configuration.setOutputDirectory(outputDirectory); this.builder = AutomaticBuilderEnum.Maven.getAutomaticBuilder(configuration); - String dependencies = Main.completeDependencies(configuration, this.builder); + this.dspotState = new DSpotState(); + String dependencies = dspotState.completeDependencies(configuration, this.builder); DSpotUtils.init(false, outputDirectory, this.configuration.getFullClassPathWithExtraDependencies(), this.getPathToAbsoluteProjectRoot() @@ -111,19 +112,21 @@ public void setUp() { @Test public void testRemoveOverlappingTests() { this.testSelectorUnderTest.init(); - DSpot dspot = new DSpot( - 0.1d, - new TestFinder(Collections.emptyList(), Collections.emptyList()), - this.compiler, - this.testSelectorUnderTest, - InputAmplDistributorEnum.RandomInputAmplDistributor.getInputAmplDistributor(200, Collections.singletonList(new StringLiteralAmplifier())), - new Output(getPathToAbsoluteProjectRoot(), configuration.getOutputDirectory(), null), - 1, - false, - this.builder, - this.testCompiler - ); - dspot.amplify(getTestClass(), Collections.emptyList()); + DSpotState dspotState = new DSpotState(); + dspotState.setDelta(0.1f); + dspotState.setTestFinder(new TestFinder(Collections.emptyList(), Collections.emptyList())); + dspotState.setCompiler(compiler); + dspotState.setTestSelector(this.testSelectorUnderTest); + dspotState.setInputAmplDistributor(InputAmplDistributorEnum.RandomInputAmplDistributor.getInputAmplDistributor( + 200, Collections.singletonList(new StringLiteralAmplifier()))); + dspotState.setOutput(new Output(getPathToAbsoluteProjectRoot(), configuration.getOutputDirectory(), null)); + dspotState.setNbIteration(1); + dspotState.setAutomaticBuilder(builder); + dspotState.setTestCompiler(testCompiler); + dspotState.setTestClassesToBeAmplified(Collections.singletonList(getTestClass())); + dspotState.setAssertionGenerator(new AssertionGenerator(0.1f, compiler, testCompiler)); + DSpot dspot = new DSpot(dspotState); + dspot.run(); final File directory = new File(DSpotUtils.shouldAddSeparator.apply(this.configuration.getOutputDirectory())); if (!directory.exists()) { directory.mkdir(); diff --git a/dspot/src/test/java/eu/stamp_project/dspot/selector/AbstractSelectorTest.java b/dspot/src/test/java/eu/stamp_project/dspot/selector/AbstractSelectorTest.java index 56fe3875c..3a3161255 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/selector/AbstractSelectorTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/selector/AbstractSelectorTest.java @@ -1,6 +1,5 @@ package eu.stamp_project.dspot.selector; -import eu.stamp_project.Main; import eu.stamp_project.automaticbuilder.AutomaticBuilder; import eu.stamp_project.automaticbuilder.maven.DSpotPOMCreator; import eu.stamp_project.dspot.amplifier.value.ValueCreator; @@ -11,6 +10,7 @@ import eu.stamp_project.utils.RandomHelper; import eu.stamp_project.utils.compilation.DSpotCompiler; import eu.stamp_project.utils.compilation.TestCompiler; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.execution.TestRunner; import eu.stamp_project.utils.options.AutomaticBuilderEnum; import eu.stamp_project.utils.program.InputConfiguration; @@ -69,15 +69,18 @@ protected CtClass getTestClass() { protected TestCompiler testCompiler; + protected DSpotState dspotState; + @Before public void setUp() throws Exception { - Main.verbose = true; + DSpotState.verbose = true; this.configuration = new InputConfiguration(); this.configuration.setAbsolutePathToProjectRoot(getPathToAbsoluteProjectRoot()); this.configuration.setOutputDirectory(outputDirectory); this.configuration.setGregorMode(true); this.builder = AutomaticBuilderEnum.Maven.getAutomaticBuilder(configuration); - String dependencies = Main.completeDependencies(configuration, this.builder); + this.dspotState = new DSpotState(); + String dependencies = dspotState.completeDependencies(configuration, this.builder); DSpotUtils.init(false, outputDirectory, this.configuration.getFullClassPathWithExtraDependencies(), this.getPathToAbsoluteProjectRoot() diff --git a/dspot/src/test/java/eu/stamp_project/dspot/selector/OneTestClassPitScoreMutantSelectorTest.java b/dspot/src/test/java/eu/stamp_project/dspot/selector/OneTestClassPitScoreMutantSelectorTest.java index b923ae87a..c8e9c840e 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/selector/OneTestClassPitScoreMutantSelectorTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/selector/OneTestClassPitScoreMutantSelectorTest.java @@ -1,8 +1,8 @@ package eu.stamp_project.dspot.selector; -import eu.stamp_project.Main; import eu.stamp_project.automaticbuilder.AutomaticBuilder; import eu.stamp_project.utils.compilation.DSpotCompiler; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.execution.TestRunner; import eu.stamp_project.utils.options.AutomaticBuilderEnum; import eu.stamp_project.utils.pit.AbstractPitResult; @@ -33,15 +33,18 @@ public class OneTestClassPitScoreMutantSelectorTest { private TestRunner testRunner; + private DSpotState dspotState; + @Before public void setUp() { - Main.verbose = true; + DSpotState.verbose = true; this.configuration = new InputConfiguration(); this.configuration.setAbsolutePathToProjectRoot("src/test/resources/test-projects/"); this.builder = AutomaticBuilderEnum.Maven.getAutomaticBuilder(configuration); + this.dspotState = new DSpotState(); DSpotCompiler.createDSpotCompiler( configuration, - Main.completeDependencies(configuration, this.builder) + dspotState.completeDependencies(configuration, this.builder) ); Launcher launcher = new Launcher(); launcher.getEnvironment().setNoClasspath(true); diff --git a/dspot/src/test/java/eu/stamp_project/utils/collector/mongodb/MongodbCollectorTest.java b/dspot/src/test/java/eu/stamp_project/utils/collector/mongodb/MongodbCollectorTest.java index 8f1e8eca7..84947bb76 100644 --- a/dspot/src/test/java/eu/stamp_project/utils/collector/mongodb/MongodbCollectorTest.java +++ b/dspot/src/test/java/eu/stamp_project/utils/collector/mongodb/MongodbCollectorTest.java @@ -84,7 +84,7 @@ public void testInfoSubmissionToMongodbJacocoCoverageSelector() { MongoClient mongoClient = MongodbCollector.connectToMongo("mongodb://localhost:27017"); MongoCollection coll = MongodbCollector.getCollection("AmpTestRecords", MongodbCollector.getDatabase("Dspot", mongoClient)); - Document foundDoc = coll.find(eq("RepoSlug", "USER/Testing")).projection(fields(excludeId(), exclude("Date"), exclude("executeTestParallelWithNumberProcessors"))).first(); + Document foundDoc = coll.find(eq("RepoSlug", "USER/Testing")).projection(fields(excludeId(), exclude("Date"), exclude("executeTestParallelWithNumberProcessors"))).first(); coll.deleteOne(foundDoc); Document unwanted = foundDoc.get("AmpOptions", Document.class); unwanted.remove("executeTestParallelWithNumberProcessors"); @@ -147,4 +147,4 @@ public void testRestful() { assertEquals("Amplification succeeded", sendedMailSubject); } -} \ No newline at end of file +} diff --git a/dspot/src/test/java/eu/stamp_project/utils/compilation/TestCompilerTest.java b/dspot/src/test/java/eu/stamp_project/utils/compilation/TestCompilerTest.java index c5cd3a6a8..cd9fc58b8 100644 --- a/dspot/src/test/java/eu/stamp_project/utils/compilation/TestCompilerTest.java +++ b/dspot/src/test/java/eu/stamp_project/utils/compilation/TestCompilerTest.java @@ -1,9 +1,9 @@ package eu.stamp_project.utils.compilation; -import eu.stamp_project.Main; import eu.stamp_project.automaticbuilder.AutomaticBuilder; import eu.stamp_project.dspot.AbstractTestOnSample; import eu.stamp_project.utils.DSpotUtils; +import eu.stamp_project.utils.configuration.DSpotState; import eu.stamp_project.utils.options.AutomaticBuilderEnum; import eu.stamp_project.utils.program.InputConfiguration; import org.junit.Test; @@ -27,7 +27,8 @@ public void test() { final InputConfiguration configuration = new InputConfiguration(); configuration.setAbsolutePathToProjectRoot(getPathToProjectRoot()); final AutomaticBuilder builder = AutomaticBuilderEnum.Maven.getAutomaticBuilder(configuration); - String dependencies = Main.completeDependencies(configuration, builder); + DSpotState dspotState = new DSpotState(); + String dependencies = dspotState.completeDependencies(configuration, builder); configuration.setDependencies(dependencies); DSpotCompiler compiler = DSpotCompiler.createDSpotCompiler(configuration, dependencies); CtClass testClass = findClass("fr.inria.filter.failing.FailingTest");