use of spoon.reflect.declaration.CtMethod 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 spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class PitMutantScoreSelector method reportJSONMutants.
private void reportJSONMutants() {
if (this.currentClassTestToBeAmplified == null) {
return;
}
TestClassJSON testClassJSON;
Gson gson = new GsonBuilder().setPrettyPrinting().create();
final File file = new File(this.configuration.getOutputDirectory() + "/" + this.currentClassTestToBeAmplified.getQualifiedName() + "_mutants_killed.json");
if (file.exists()) {
try {
testClassJSON = gson.fromJson(new FileReader(file), TestClassJSON.class);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
} else {
testClassJSON = new TestClassJSON(getNbMutantKilledOriginally(this.currentClassTestToBeAmplified.getQualifiedName()), this.currentClassTestToBeAmplified.getQualifiedName(), this.currentClassTestToBeAmplified.getMethods().stream().filter(AmplificationChecker::isTest).count());
}
List<CtMethod> keys = new ArrayList<>(this.testThatKilledMutants.keySet());
keys.forEach(amplifiedTest -> {
List<PitResult> pitResults = new ArrayList<>(this.testThatKilledMutants.get(amplifiedTest));
final List<MutantJSON> mutantsJson = new ArrayList<>();
pitResults.forEach(pitResult -> mutantsJson.add(new MutantJSON(pitResult.getFullQualifiedNameMutantOperator(), pitResult.getLineNumber(), pitResult.getNameOfMutatedMethod())));
if (amplifiedTest == null) {
testClassJSON.addTestCase(new TestCaseJSON(this.currentClassTestToBeAmplified.getSimpleName(), Counter.getAllAssertions(), Counter.getAllInput(), mutantsJson));
} else {
testClassJSON.addTestCase(new TestCaseJSON(amplifiedTest.getSimpleName(), Counter.getAssertionOfSinceOrigin(amplifiedTest), Counter.getInputOfSinceOrigin(amplifiedTest), mutantsJson));
}
});
try (FileWriter writer = new FileWriter(file, false)) {
writer.write(gson.toJson(testClassJSON));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class PitMutantScoreSelector method selectToKeep.
@Override
public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) {
if (amplifiedTestToBeKept.isEmpty()) {
return amplifiedTestToBeKept;
}
CtType clone = this.currentClassTestToBeAmplified.clone();
clone.setParent(this.currentClassTestToBeAmplified.getParent());
this.currentClassTestToBeAmplified.getMethods().stream().filter(AmplificationChecker::isTest).forEach(clone::removeMethod);
amplifiedTestToBeKept.forEach(clone::addMethod);
DSpotUtils.printCtTypeToGivenDirectory(clone, new File(DSpotCompiler.pathToTmpTestSources));
final AutomaticBuilder automaticBuilder = AutomaticBuilderFactory.getAutomaticBuilder(this.configuration);
final String classpath = AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).buildClasspath(this.program.getProgramDir()) + AmplificationHelper.PATH_SEPARATOR + this.program.getProgramDir() + "/" + this.program.getClassesDir() + AmplificationHelper.PATH_SEPARATOR + "target/dspot/dependencies/" + AmplificationHelper.PATH_SEPARATOR + this.program.getProgramDir() + "/" + this.program.getTestClassesDir();
DSpotCompiler.compile(DSpotCompiler.pathToTmpTestSources, classpath, new File(this.program.getProgramDir() + "/" + this.program.getTestClassesDir()));
AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).runPit(this.program.getProgramDir(), clone);
final List<PitResult> results = PitResultParser.parseAndDelete(program.getProgramDir() + automaticBuilder.getOutputDirectoryPit());
Set<CtMethod<?>> selectedTests = new HashSet<>();
if (results != null) {
LOGGER.info("{} mutants has been generated ({})", results.size(), this.numberOfMutant);
if (results.size() != this.numberOfMutant) {
LOGGER.warn("Number of generated mutant is different than the original one.");
}
results.stream().filter(result -> result.getStateOfMutant() == PitResult.State.KILLED && !this.originalKilledMutants.contains(result) && !this.mutantNotTestedByOriginal.contains(result)).forEach(result -> {
CtMethod method = result.getMethod(clone);
if (killsMoreMutantThanParents(method, result)) {
if (!testThatKilledMutants.containsKey(method)) {
testThatKilledMutants.put(method, new HashSet<>());
}
testThatKilledMutants.get(method).add(result);
if (method == null) {
// output of pit test does not allow us to know which test case kill new mutants... we keep them all...
selectedTests.addAll(amplifiedTestToBeKept);
} else {
selectedTests.add(method);
}
}
});
}
this.selectedAmplifiedTest.addAll(selectedTests);
selectedTests.forEach(selectedTest -> LOGGER.info("{} kills {} more mutants", selectedTest == null ? this.currentClassTestToBeAmplified.getSimpleName() : selectedTest.getSimpleName(), this.testThatKilledMutants.containsKey(selectedTest) ? this.testThatKilledMutants.get(selectedTest).size() : this.testThatKilledMutants.get(null)));
return new ArrayList<>(selectedTests);
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class TestMethodCallRemover method apply.
public List<CtMethod> apply(CtMethod method) {
List<CtMethod> methods = new ArrayList<>();
if (method.getDeclaringType() != null) {
// get the list of method calls
List<CtInvocation> invocations = Query.getElements(method, new TypeFilter(CtInvocation.class));
// this index serves to replace ith literal is replaced by zero in the ith clone of the method
int invocation_index = 0;
for (CtInvocation invocation : invocations) {
try {
if (toRemove(invocation) && !AmplificationChecker.isAssert(invocation) && !inWhileLoop(invocation) && !containsIteratorNext(invocation)) {
methods.add(apply(method, invocation_index));
}
} catch (Exception e) {
throw new RuntimeException(e);
}
invocation_index++;
}
}
return methods;
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class CollectionCreator method generateEmptyCollection.
static CtExpression<?> generateEmptyCollection(CtTypeReference type, String nameMethod, Class<?> typeOfCollection) {
final Factory factory = type.getFactory();
final CtType<?> collectionsType = factory.Type().get(Collections.class);
final CtTypeAccess<?> accessToCollections = factory.createTypeAccess(collectionsType.getReference());
final CtMethod<?> singletonListMethod = collectionsType.getMethodsByName(nameMethod).get(0);
final CtExecutableReference<?> executableReference = factory.Core().createExecutableReference();
executableReference.setStatic(true);
executableReference.setSimpleName(singletonListMethod.getSimpleName());
executableReference.setDeclaringType(collectionsType.getReference());
executableReference.setType(factory.createCtTypeReference(typeOfCollection));
if (type.getActualTypeArguments().isEmpty()) {
// supporting Collections.<type>emptyList()
executableReference.addActualTypeArgument(type);
} else if (type.getActualTypeArguments().stream().noneMatch(reference -> reference instanceof CtWildcardReference)) {
// in case type is a list, we copy the Actual arguments
executableReference.setActualTypeArguments(type.getActualTypeArguments());
}
return factory.createInvocation(accessToCollections, executableReference);
}
Aggregations