Search in sources :

Example 1 with JacocoExecutor

use of fr.inria.stamp.coverage.JacocoExecutor in project dspot by STAMP-project.

the class JacocoCoverageSelector method selectToAmplify.

@Override
public List<CtMethod<?>> selectToAmplify(List<CtMethod<?>> testsToBeAmplified) {
    if (this.currentClassTestToBeAmplified == null && !testsToBeAmplified.isEmpty()) {
        this.currentClassTestToBeAmplified = testsToBeAmplified.get(0).getDeclaringType();
        this.initialCoverage = new JacocoExecutor(this.program, this.configuration, this.currentClassTestToBeAmplified).executeJacoco(this.currentClassTestToBeAmplified);
        this.selectedToBeAmplifiedCoverageResultsMap = null;
        this.selectedAmplifiedTest.clear();
    }
    final List<String> methodNames = testsToBeAmplified.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList());
    final Map<String, CoverageResults> coverageResultsMap = new JacocoExecutor(this.program, this.configuration, this.currentClassTestToBeAmplified).executeJacoco(this.currentClassTestToBeAmplified, methodNames);
    final List<String> pathExecuted = new ArrayList<>();
    final List<CtMethod<?>> filteredTests = testsToBeAmplified.stream().filter(ctMethod -> ctMethod != null && coverageResultsMap.get(ctMethod.getSimpleName()) != null).filter(ctMethod -> {
        final String pathByExecInstructions = computePathExecuted.apply(coverageResultsMap.get(ctMethod.getSimpleName()).getCoverageBuilder());
        if (pathExecuted.contains(pathByExecInstructions)) {
            return false;
        } else {
            pathExecuted.add(pathByExecInstructions);
            return true;
        }
    }).collect(Collectors.toList());
    if (this.selectedToBeAmplifiedCoverageResultsMap == null) {
        final List<String> filteredMethodNames = filteredTests.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList());
        this.selectedToBeAmplifiedCoverageResultsMap = coverageResultsMap.keySet().stream().filter(filteredMethodNames::contains).collect(Collectors.toMap(Function.identity(), coverageResultsMap::get));
    }
    return filteredTests;
}
Also used : IntStream(java.util.stream.IntStream) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) JacocoExecutor(fr.inria.stamp.coverage.JacocoExecutor) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) ILine(org.jacoco.core.analysis.ILine) CtNamedElement(spoon.reflect.declaration.CtNamedElement) Function(java.util.function.Function) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) TestClassJSON(fr.inria.diversify.dspot.selector.json.coverage.TestClassJSON) ICounter(org.jacoco.core.analysis.ICounter) CtType(spoon.reflect.declaration.CtType) Gson(com.google.gson.Gson) AutomaticBuilderFactory(fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory) TestCaseJSON(fr.inria.diversify.dspot.selector.json.coverage.TestCaseJSON) Map(java.util.Map) Counter(fr.inria.diversify.utils.Counter) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) FileWriter(java.io.FileWriter) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) CoverageResults(fr.inria.stamp.coverage.CoverageResults) FileReader(java.io.FileReader) CtMethod(spoon.reflect.declaration.CtMethod) JacocoExecutor(fr.inria.stamp.coverage.JacocoExecutor) ArrayList(java.util.ArrayList) CtMethod(spoon.reflect.declaration.CtMethod) CoverageResults(fr.inria.stamp.coverage.CoverageResults)

Example 2 with JacocoExecutor

use of fr.inria.stamp.coverage.JacocoExecutor in project dspot by STAMP-project.

the class JacocoCoverageSelector method selectToKeep.

@Override
public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) {
    if (amplifiedTestToBeKept.isEmpty()) {
        return amplifiedTestToBeKept;
    }
    final List<String> methodNames = amplifiedTestToBeKept.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList());
    final Map<String, CoverageResults> coverageResultsMap = new JacocoExecutor(this.program, this.configuration, this.currentClassTestToBeAmplified).executeJacoco(this.currentClassTestToBeAmplified, methodNames);
    final List<String> pathExecuted = new ArrayList<>();
    final List<CtMethod<?>> methodsKept = amplifiedTestToBeKept.stream().filter(ctMethod -> {
        final String simpleNameOfFirstParent = getFirstParentThatHasBeenRun(ctMethod).getSimpleName();
        return this.selectedToBeAmplifiedCoverageResultsMap.get(simpleNameOfFirstParent) == null || coverageResultsMap.get(ctMethod.getSimpleName()).isBetterThan(this.selectedToBeAmplifiedCoverageResultsMap.get(simpleNameOfFirstParent)) && !computePathExecuted.apply(coverageResultsMap.get(ctMethod.getSimpleName()).getCoverageBuilder()).equals(computePathExecuted.apply(this.selectedToBeAmplifiedCoverageResultsMap.get(simpleNameOfFirstParent).getCoverageBuilder()));
    }).filter(ctMethod -> {
        final String pathByExecInstructions = computePathExecuted.apply(coverageResultsMap.get(ctMethod.getSimpleName()).getCoverageBuilder());
        if (pathExecuted.contains(pathByExecInstructions)) {
            return false;
        } else {
            pathExecuted.add(pathByExecInstructions);
            return true;
        }
    }).collect(Collectors.toList());
    this.selectedToBeAmplifiedCoverageResultsMap.putAll(methodsKept.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toMap(Function.identity(), coverageResultsMap::get)));
    this.selectedAmplifiedTest.addAll(new ArrayList<>(methodsKept));
    return methodsKept;
}
Also used : IntStream(java.util.stream.IntStream) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) JacocoExecutor(fr.inria.stamp.coverage.JacocoExecutor) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) ILine(org.jacoco.core.analysis.ILine) CtNamedElement(spoon.reflect.declaration.CtNamedElement) Function(java.util.function.Function) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) TestClassJSON(fr.inria.diversify.dspot.selector.json.coverage.TestClassJSON) ICounter(org.jacoco.core.analysis.ICounter) CtType(spoon.reflect.declaration.CtType) Gson(com.google.gson.Gson) AutomaticBuilderFactory(fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory) TestCaseJSON(fr.inria.diversify.dspot.selector.json.coverage.TestCaseJSON) Map(java.util.Map) Counter(fr.inria.diversify.utils.Counter) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) FileWriter(java.io.FileWriter) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) CoverageResults(fr.inria.stamp.coverage.CoverageResults) FileReader(java.io.FileReader) CtMethod(spoon.reflect.declaration.CtMethod) JacocoExecutor(fr.inria.stamp.coverage.JacocoExecutor) ArrayList(java.util.ArrayList) CtNamedElement(spoon.reflect.declaration.CtNamedElement) CtMethod(spoon.reflect.declaration.CtMethod) CoverageResults(fr.inria.stamp.coverage.CoverageResults)

Example 3 with JacocoExecutor

use of fr.inria.stamp.coverage.JacocoExecutor in project dspot by STAMP-project.

the class JacocoCoverageSelector method report.

@Override
public void report() {
    final String nl = System.getProperty("line.separator");
    StringBuilder report = new StringBuilder();
    report.append(nl).append("======= REPORT =======").append(nl);
    report.append("Initial instruction coverage: ").append(this.initialCoverage.instructionsCovered).append(" / ").append(this.initialCoverage.instructionsTotal).append(nl).append(String.format("%.2f", 100.0D * ((double) this.initialCoverage.instructionsCovered / (double) this.initialCoverage.instructionsTotal))).append("%").append(nl);
    report.append("Amplification results with ").append(this.selectedAmplifiedTest.size()).append(" amplified tests.").append(nl);
    final CtType<?> clone = this.currentClassTestToBeAmplified.clone();
    this.currentClassTestToBeAmplified.getPackage().addType(clone);
    this.selectedAmplifiedTest.forEach(clone::addMethod);
    try {
        FileUtils.deleteDirectory(new File(DSpotCompiler.pathToTmpTestSources));
    } catch (IOException ignored) {
    // ignored
    }
    DSpotUtils.printCtTypeToGivenDirectory(clone, new File(DSpotCompiler.pathToTmpTestSources));
    this.currentClassTestToBeAmplified.getPackage().removeType(clone);
    final String fileSeparator = System.getProperty("file.separator");
    final String classpath = AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).buildClasspath(this.program.getProgramDir()) + System.getProperty("path.separator") + this.program.getProgramDir() + fileSeparator + this.program.getClassesDir() + System.getProperty("path.separator") + this.program.getProgramDir() + fileSeparator + this.program.getTestClassesDir();
    DSpotCompiler.compile(DSpotCompiler.pathToTmpTestSources, classpath, new File(this.program.getProgramDir() + fileSeparator + this.program.getTestClassesDir()));
    final CoverageResults coverageResults = new JacocoExecutor(this.program, this.configuration, this.currentClassTestToBeAmplified).executeJacoco(this.currentClassTestToBeAmplified);
    report.append("Amplified instruction coverage: ").append(coverageResults.instructionsCovered).append(" / ").append(coverageResults.instructionsTotal).append(nl).append(String.format("%.2f", 100.0D * ((double) coverageResults.instructionsCovered / (double) coverageResults.instructionsTotal))).append("%").append(nl);
    System.out.println(report.toString());
    File reportDir = new File(this.configuration.getOutputDirectory());
    if (!reportDir.exists()) {
        reportDir.mkdir();
    }
    try (FileWriter writer = new FileWriter(this.configuration.getOutputDirectory() + fileSeparator + this.currentClassTestToBeAmplified.getQualifiedName() + "_jacoco_instr_coverage_report.txt", false)) {
        writer.write(report.toString());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    jsonReport(coverageResults);
    this.currentClassTestToBeAmplified = null;
}
Also used : JacocoExecutor(fr.inria.stamp.coverage.JacocoExecutor) FileWriter(java.io.FileWriter) IOException(java.io.IOException) File(java.io.File) CoverageResults(fr.inria.stamp.coverage.CoverageResults)

Aggregations

CoverageResults (fr.inria.stamp.coverage.CoverageResults)3 JacocoExecutor (fr.inria.stamp.coverage.JacocoExecutor)3 File (java.io.File)3 FileWriter (java.io.FileWriter)3 IOException (java.io.IOException)3 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2 AutomaticBuilderFactory (fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory)2 TestCaseJSON (fr.inria.diversify.dspot.selector.json.coverage.TestCaseJSON)2 TestClassJSON (fr.inria.diversify.dspot.selector.json.coverage.TestClassJSON)2 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)2 Counter (fr.inria.diversify.utils.Counter)2 DSpotUtils (fr.inria.diversify.utils.DSpotUtils)2 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)2 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)2 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2