use of fr.inria.stamp.coverage.CoverageResults 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;
}
use of fr.inria.stamp.coverage.CoverageResults 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;
}
use of fr.inria.stamp.coverage.CoverageResults 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;
}
Aggregations