use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class AssertGeneratorHelper method createTestWithLog.
static CtMethod<?> createTestWithLog(CtMethod test, final String filter) {
CtMethod clone = AmplificationHelper.cloneTestMethodNoAmp(test);
clone.setSimpleName(test.getSimpleName() + "_withlog");
final List<CtStatement> allStatement = clone.getElements(new TypeFilter<>(CtStatement.class));
allStatement.stream().filter(statement -> isStmtToLog(filter, statement)).forEach(statement -> addLogStmt(statement, test.getSimpleName() + "__" + indexOfByRef(allStatement, statement)));
return clone;
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class Amplification method amplification.
/**
* Amplification of a single test.
*
* <p>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 classTest Test class
* @param test Method to amplify
* @param maxIteration Number of amplification iterations
* @return Valid amplified tests
* @throws IOException
* @throws InterruptedException
* @throws ClassNotFoundException
*/
private List<CtMethod<?>> amplification(CtType<?> classTest, CtMethod test, int maxIteration) throws IOException, InterruptedException, ClassNotFoundException {
List<CtMethod<?>> currentTestList = new ArrayList<>();
currentTestList.add(test);
List<CtMethod<?>> amplifiedTests = new ArrayList<>();
for (int i = 0; i < maxIteration; i++) {
LOGGER.info("iteration {}:", i);
List<CtMethod<?>> testsToBeAmplified = testSelector.selectToAmplify(currentTestList);
if (testsToBeAmplified.isEmpty()) {
LOGGER.info("No test could be generated from selected test");
continue;
}
LOGGER.info("{} tests selected to be amplified over {} available tests", testsToBeAmplified.size(), currentTestList.size());
currentTestList = AmplificationHelper.reduce(inputAmplifyTests(testsToBeAmplified));
List<CtMethod<?>> testsWithAssertions = assertGenerator.generateAsserts(classTest, currentTestList);
if (testsWithAssertions.isEmpty()) {
continue;
} else {
currentTestList = testsWithAssertions;
}
TestListener result = compileAndRunTests(classTest, currentTestList);
if (result == null) {
continue;
} else if (!result.getFailingTests().isEmpty()) {
LOGGER.warn("Discarding failing test cases");
final Set<String> failingTestCase = result.getFailingTests().stream().map(Failure::getDescription).map(Description::getMethodName).collect(Collectors.toSet());
currentTestList = currentTestList.stream().filter(ctMethod -> !failingTestCase.contains(ctMethod.getSimpleName())).collect(Collectors.toList());
}
currentTestList = AmplificationHelper.getPassingTests(currentTestList, result);
LOGGER.info("{} test method(s) has been successfully generated", currentTestList.size());
amplifiedTests.addAll(testSelector.selectToKeep(currentTestList));
}
return amplifiedTests;
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class Amplification method compileAndRunTestsNoFail.
/**
* Adds test methods to the test class and run them. Does not allow partially failing test classes.
*
* @param classTest Test class
* @param currentTestList New test methods to run
* @return Results of tests run or {@code null} if a test failed or could not be run (uncompilable)
*/
private TestListener compileAndRunTestsNoFail(CtType classTest, List<CtMethod<?>> currentTestList) {
final TestListener result = compileAndRunTests(classTest, currentTestList);
final long numberOfSubClasses = classTest.getFactory().Class().getAll().stream().filter(subClass -> classTest.getReference().equals(subClass.getSuperclass())).count();
if (result == null || !result.getFailingTests().isEmpty() || (!classTest.getModifiers().contains(ModifierKind.ABSTRACT) && result.getRunningTests().size() != currentTestList.size()) || (classTest.getModifiers().contains(ModifierKind.ABSTRACT) && numberOfSubClasses != result.getRunningTests().size())) {
return null;
} else {
return result;
}
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class Amplification method amplification.
/**
* Amplification of multiple methods.
*
* <p>See {@link #amplification(CtType, CtMethod, int)} for the details of amplification.
*
* @param classTest Test class
* @param methods Methods to amplify
* @param maxIteration Number of amplification iterations
* @throws IOException
* @throws InterruptedException
* @throws ClassNotFoundException
*/
public void amplification(CtType<?> classTest, List<CtMethod<?>> methods, int maxIteration) throws IOException, InterruptedException, ClassNotFoundException {
List<CtMethod<?>> tests = methods.stream().filter(mth -> AmplificationChecker.isTest(mth, this.configuration.getInputProgram().getRelativeTestSourceCodeDir())).collect(Collectors.toList());
if (tests.isEmpty()) {
LOGGER.warn("No test has been found into {}", classTest.getQualifiedName());
return;
}
LOGGER.info("amplification of {} ({} test(s))", classTest.getQualifiedName(), tests.size());
preAmplification(classTest, tests);
LOGGER.info("{} amplified test(s) has been selected, global: {}", this.testSelector.getAmplifiedTestCases().size() - ampTestCount, this.testSelector.getAmplifiedTestCases().size());
ampTestCount = this.testSelector.getAmplifiedTestCases().size();
resetAmplifiers(classTest);
for (int i = 0; i < tests.size(); i++) {
CtMethod test = tests.get(i);
LOGGER.info("amp {} ({}/{})", test.getSimpleName(), i + 1, tests.size());
TestListener result = compileAndRunTests(classTest, Collections.singletonList(tests.get(i)));
if (result != null) {
if (result.getFailingTests().isEmpty() && !result.getRunningTests().isEmpty()) {
amplification(classTest, test, maxIteration);
LOGGER.info("{} amplified test(s) has been selected, global: {}", this.testSelector.getAmplifiedTestCases().size() - ampTestCount, this.testSelector.getAmplifiedTestCases().size());
ampTestCount = this.testSelector.getAmplifiedTestCases().size();
} else {
LOGGER.info("{} / {} test cases failed!", result.getFailingTests().size(), result.getRunningTests().size());
}
}
}
}
use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.
the class DSpot method amplifyTest.
public CtType amplifyTest(CtType test, List<CtMethod<?>> methods) {
try {
Counter.reset();
Amplification testAmplification = new Amplification(this.inputConfiguration, this.amplifiers, this.testSelector, this.compiler);
final List<CtMethod<?>> filteredTestCases = this.filterTestCases(methods);
long time = System.currentTimeMillis();
testAmplification.amplification(test, filteredTestCases, numberOfIterations);
final long elapsedTime = System.currentTimeMillis() - time;
LOGGER.info("elapsedTime {}", elapsedTime);
this.projectTimeJSON.add(new ClassTimeJSON(test.getQualifiedName(), elapsedTime));
final CtType clone = test.clone();
test.getPackage().addType(clone);
CtType<?> amplification = AmplificationHelper.createAmplifiedTest(testSelector.getAmplifiedTestCases(), clone, testSelector.getMinimizer());
testSelector.report();
final File outputDirectory = new File(inputConfiguration.getOutputDirectory());
LOGGER.info("Print {} with {} amplified test cases in {}", amplification.getSimpleName(), testSelector.getAmplifiedTestCases().size(), this.inputConfiguration.getOutputDirectory());
DSpotUtils.printAmplifiedTestClass(amplification, outputDirectory);
FileUtils.cleanDirectory(compiler.getSourceOutputDirectory());
try {
String pathToDotClass = compiler.getBinaryOutputDirectory().getAbsolutePath() + "/" + test.getQualifiedName().replaceAll("\\.", "/") + ".class";
FileUtils.forceDelete(new File(pathToDotClass));
} catch (IOException ignored) {
// ignored
}
writeTimeJson();
return amplification;
} catch (IOException | InterruptedException | ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
Aggregations