use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class MethodsAssertGenerator method makeFailureTest.
/**
* Adds surrounding try/catch/fail in a failing test.
*
* @param test Failing test method to amplify
* @param failure Test's failure description
* @return New amplified test
*/
protected CtMethod<?> makeFailureTest(CtMethod<?> test, Failure failure) {
CtMethod cloneMethodTest = AmplificationHelper.cloneTestMethodForAmp(test, "");
cloneMethodTest.setSimpleName(test.getSimpleName());
Factory factory = cloneMethodTest.getFactory();
Throwable exception = failure.getException();
if (// TestTimedOutException means infinite loop
exception instanceof TestTimedOutException || exception instanceof AssertionError) {
// AssertionError means that some assertion remained in the test: TODO
return null;
}
Class exceptionClass;
if (exception == null) {
exceptionClass = Exception.class;
} else {
exceptionClass = exception.getClass();
}
CtTry tryBlock = factory.Core().createTry();
tryBlock.setBody(cloneMethodTest.getBody());
String snippet = "org.junit.Assert.fail(\"" + test.getSimpleName() + " should have thrown " + exceptionClass.getSimpleName() + "\")";
tryBlock.getBody().addStatement(factory.Code().createCodeSnippetStatement(snippet));
DSpotUtils.addComment(tryBlock, "AssertGenerator generate try/catch block with fail statement", CtComment.CommentType.INLINE);
CtCatch ctCatch = factory.Core().createCatch();
CtTypeReference exceptionType = factory.Type().createReference(exceptionClass);
ctCatch.setParameter(factory.Code().createCatchVariable(exceptionType, "eee"));
ctCatch.setBody(factory.Core().createBlock());
List<CtCatch> catchers = new ArrayList<>(1);
catchers.add(ctCatch);
tryBlock.setCatchers(catchers);
CtBlock body = factory.Core().createBlock();
body.addStatement(tryBlock);
cloneMethodTest.setBody(body);
cloneMethodTest.setSimpleName(cloneMethodTest.getSimpleName() + "_failAssert" + (numberOfFail++));
Counter.updateAssertionOf(cloneMethodTest, 1);
return cloneMethodTest;
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class CloverCoverageSelector method selectToAmplify.
@Override
public List<CtMethod<?>> selectToAmplify(List<CtMethod<?>> testsToBeAmplified) {
if (this.currentClassTestToBeAmplified == null && !testsToBeAmplified.isEmpty()) {
this.currentClassTestToBeAmplified = testsToBeAmplified.get(0).getDeclaringType();
final Map<String, Map<String, List<Integer>>> lineCoveragePerTestMethods = CloverExecutor.executeAll(this.configuration, PATH_TO_COPIED_FILES);
this.originalLineCoveragePerClass = new HashMap<>();
final InputProgram program = this.configuration.getInputProgram();
lineCoveragePerTestMethods.keySet().stream().map(lineCoveragePerTestMethods::get).forEach(lineCoveragePerTestMethod -> lineCoveragePerTestMethod.keySet().forEach(className -> {
final CtType<?> key = program.getFactory().Type().get(className);
if (!this.originalLineCoveragePerClass.containsKey(key)) {
this.originalLineCoveragePerClass.put(key, new HashSet<>());
}
this.originalLineCoveragePerClass.get(key).addAll(lineCoveragePerTestMethod.get(className));
}));
final String classesOfProject = program.getProgramDir() + program.getClassesDir() + AmplificationHelper.PATH_SEPARATOR + program.getProgramDir() + program.getTestClassesDir();
final String classpath = AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).buildClasspath(program.getProgramDir()) + AmplificationHelper.PATH_SEPARATOR + classesOfProject;
this.initialCoverage = EntryPoint.runCoverageOnTestClasses(classpath, classesOfProject, DSpotUtils.getAllTestClasses(configuration));
}
if (testsToBeAmplified.size() > 1) {
final List<CtMethod<?>> collect = testsToBeAmplified.stream().filter(this.selectedAmplifiedTest::contains).collect(Collectors.toList());
if (collect.isEmpty()) {
return testsToBeAmplified;
} else {
return collect;
}
} else {
return testsToBeAmplified;
}
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class ExecutedMutantSelector method selectToKeep.
@Override
public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) {
if (amplifiedTestToBeKept.isEmpty()) {
return amplifiedTestToBeKept;
}
// construct a test classes with only amplified tests
CtType clone = this.currentClassTestToBeAmplified.clone();
clone.setParent(this.currentClassTestToBeAmplified.getParent());
this.currentClassTestToBeAmplified.getMethods().stream().filter(AmplificationChecker::isTest).forEach(clone::removeMethod);
amplifiedTestToBeKept.forEach(clone::addMethod);
// pretty print it
DSpotUtils.printCtTypeToGivenDirectory(clone, new File(DSpotCompiler.pathToTmpTestSources));
// then compile
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> pitResults = PitResultParser.parseAndDelete(program.getProgramDir() + AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).getOutputDirectoryPit());
final int numberOfSelectedAmplifiedTest = pitResults.stream().filter(pitResult -> pitResult.getStateOfMutant() == PitResult.State.KILLED || pitResult.getStateOfMutant() == PitResult.State.SURVIVED).filter(pitResult -> !this.originalMutantExecuted.contains(pitResult)).map(pitResult -> {
final CtMethod amplifiedTestThatExecuteMoreMutants = pitResult.getMethod(clone);
if (!this.mutantExecutedPerAmplifiedTestMethod.containsKey(amplifiedTestThatExecuteMoreMutants)) {
this.mutantExecutedPerAmplifiedTestMethod.put(amplifiedTestThatExecuteMoreMutants, new HashSet<>());
}
this.mutantExecutedPerAmplifiedTestMethod.get(amplifiedTestThatExecuteMoreMutants).add(pitResult);
this.selectedAmplifiedTest.add(amplifiedTestThatExecuteMoreMutants);
return amplifiedTestThatExecuteMoreMutants;
}).collect(Collectors.toSet()).size();
LOGGER.info("{} has been selected to amplify the test suite", numberOfSelectedAmplifiedTest);
return amplifiedTestToBeKept;
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class ExecutedMutantSelector 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_executed.json");
if (file.exists()) {
try {
testClassJSON = gson.fromJson(new FileReader(file), TestClassJSON.class);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
} else {
testClassJSON = new TestClassJSON(getNbMutantExecutedOriginally(this.currentClassTestToBeAmplified.getQualifiedName()), this.currentClassTestToBeAmplified.getQualifiedName(), this.currentClassTestToBeAmplified.getMethods().stream().filter(AmplificationChecker::isTest).count());
}
List<CtMethod> keys = new ArrayList<>(this.mutantExecutedPerAmplifiedTestMethod.keySet());
keys.forEach(amplifiedTest -> {
List<PitResult> pitResults = new ArrayList<>(this.mutantExecutedPerAmplifiedTestMethod.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 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;
}
Aggregations