use of spoon.reflect.declaration.CtType in project dspot by STAMP-project.
the class MethodsAssertGenerator method addAssertions.
/**
* Adds new assertions in multiple tests.
*
* <p>Instruments the tests to have observation points.
* Details in {@link AssertGeneratorHelper#createTestWithLog(CtMethod, String)}.
*
* <p>Details of the assertion generation in {@link #buildTestWithAssert(CtMethod, Map)}.
*
* @param testClass Test class
* @param testCases Passing test methods
* @return New tests with new assertions generated from observation points values
* @throws IOException
* @throws ClassNotFoundException
*/
private List<CtMethod<?>> addAssertions(CtType<?> testClass, List<CtMethod<?>> testCases) throws IOException, ClassNotFoundException {
CtType clone = testClass.clone();
testClass.getPackage().addType(clone);
LOGGER.info("Add observations points in passing tests.");
LOGGER.info("Instrumentation...");
final List<CtMethod<?>> testCasesWithLogs = testCases.stream().map(ctMethod -> {
DSpotUtils.printProgress(testCases.indexOf(ctMethod), testCases.size());
return AssertGeneratorHelper.createTestWithLog(ctMethod, this.originalClass.getPackage().getQualifiedName());
}).collect(Collectors.toList());
final List<CtMethod<?>> testsToRun = new ArrayList<>();
IntStream.range(0, 3).forEach(i -> testsToRun.addAll(testCasesWithLogs.stream().map(CtMethod::clone).map(ctMethod -> {
ctMethod.setSimpleName(ctMethod.getSimpleName() + i);
return ctMethod;
}).map(ctMethod -> {
clone.addMethod(ctMethod);
return ctMethod;
}).collect(Collectors.toList())));
ObjectLog.reset();
LOGGER.info("Run instrumented tests. ({})", testsToRun.size());
final TestListener result = TestCompiler.compileAndRun(clone, this.compiler, testsToRun, this.configuration);
if (result == null || !result.getFailingTests().isEmpty()) {
return Collections.emptyList();
} else {
Map<String, Observation> observations = ObjectLog.getObservations();
LOGGER.info("Generating assertions...");
return testCases.stream().map(ctMethod -> this.buildTestWithAssert(ctMethod, observations)).collect(Collectors.toList());
}
}
use of spoon.reflect.declaration.CtType in project dspot by STAMP-project.
the class ChangeDetectorSelector 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 String classpath = AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).buildClasspath(this.program.getProgramDir()) + AmplificationHelper.PATH_SEPARATOR + "target/dspot/dependencies/";
DSpotCompiler.compile(DSpotCompiler.pathToTmpTestSources, classpath + AmplificationHelper.PATH_SEPARATOR + this.program.getProgramDir() + "/" + this.program.getClassesDir() + AmplificationHelper.PATH_SEPARATOR + this.program.getProgramDir() + "/" + this.program.getTestClassesDir(), new File(this.pathToChangedVersionOfProgram + "/" + this.program.getTestClassesDir()));
final TestListener results = TestLauncher.run(this.configuration, classpath + AmplificationHelper.PATH_SEPARATOR + this.pathToChangedVersionOfProgram + "/" + this.program.getClassesDir() + AmplificationHelper.PATH_SEPARATOR + this.pathToChangedVersionOfProgram + "/" + this.program.getTestClassesDir(), clone, amplifiedTestToBeKept.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList()));
if (!results.getFailingTests().isEmpty()) {
results.getFailingTests().forEach(failure -> this.failurePerAmplifiedTest.put(amplifiedTestToBeKept.stream().filter(ctMethod -> ctMethod.getSimpleName().equals(failure.getDescription().getMethodName())).findFirst().get(), failure));
}
return amplifiedTestToBeKept;
}
use of spoon.reflect.declaration.CtType in project dspot by STAMP-project.
the class Main method run.
public static void run(Configuration configuration, InputConfiguration inputConfiguration) throws Exception {
AmplificationHelper.setSeedRandom(23L);
AmplificationHelper.minimize = configuration.minimize;
InputProgram program = new InputProgram();
inputConfiguration.setInputProgram(program);
inputConfiguration.getProperties().setProperty("automaticBuilderName", configuration.automaticBuilderName);
AmplificationHelper.MAX_NUMBER_OF_TESTS = configuration.maxTestAmplified;
if (configuration.mavenHome != null) {
inputConfiguration.getProperties().put("maven.home", configuration.mavenHome);
}
if (configuration.pathToOutput != null) {
inputConfiguration.getProperties().setProperty("outputDirectory", configuration.pathToOutput);
}
DSpot dspot = new DSpot(inputConfiguration, configuration.nbIteration, configuration.amplifiers, configuration.selector);
AmplificationHelper.setSeedRandom(configuration.seed);
AmplificationHelper.setTimeOutInMs(configuration.timeOutInMs);
createOutputDirectories(inputConfiguration, configuration.clean);
final long startTime = System.currentTimeMillis();
final List<CtType> amplifiedTestClasses;
if ("all".equals(configuration.testClasses.get(0))) {
amplifiedTestClasses = dspot.amplifyAllTests();
} else if ("diff".equals(configuration.testClasses.get(0))) {
amplifiedTestClasses = dspot.amplifyAllTests(SelectorOnDiff.findTestClassesAccordingToADiff(inputConfiguration));
} else {
if (configuration.testCases.isEmpty()) {
amplifiedTestClasses = dspot.amplifyAllTestsNames(configuration.testClasses);
} else {
amplifiedTestClasses = configuration.testClasses.stream().map(testClasses -> dspot.amplifyTest(testClasses, configuration.testCases)).collect(Collectors.toList());
}
}
LOGGER.info("Amplification {}.", amplifiedTestClasses.isEmpty() ? "failed" : "succeed");
final long elapsedTime = System.currentTimeMillis() - startTime;
LOGGER.info("Elapsed time {} ms", elapsedTime);
}
use of spoon.reflect.declaration.CtType in project dspot by STAMP-project.
the class AssertGenerator method generateAsserts.
/**
* Adds new assertions in multiple tests.
*
* <p>Details of the assertions generation in {@link MethodsAssertGenerator#generateAsserts(CtType, List)}.
*
* @param testClass Test class
* @param tests Test methods to amplify
* @return New amplified tests
* @throws IOException
* @throws ClassNotFoundException
*/
public List<CtMethod<?>> generateAsserts(CtType<?> testClass, List<CtMethod<?>> tests) throws IOException, ClassNotFoundException {
if (tests.isEmpty()) {
return tests;
}
CtType cloneClass = testClass.clone();
cloneClass.setParent(testClass.getParent());
List<CtMethod<?>> testsWithoutAssertions = tests.stream().map(this.assertionRemover::removeAssertion).collect(Collectors.toList());
testsWithoutAssertions.forEach(cloneClass::addMethod);
MethodsAssertGenerator ags = new MethodsAssertGenerator(testClass, this.configuration, compiler);
final List<CtMethod<?>> amplifiedTestsWithAssertions = ags.generateAsserts(cloneClass, testsWithoutAssertions);
if (amplifiedTestsWithAssertions.isEmpty()) {
LOGGER.info("Could not generate any test with assertions");
} else {
LOGGER.info("{} new tests with assertions generated", amplifiedTestsWithAssertions.size());
}
return amplifiedTestsWithAssertions;
}
use of spoon.reflect.declaration.CtType in project dspot by STAMP-project.
the class AmplificationHelper method cloneTestClassAndAddGivenTest.
/**
* Clones the test class and adds the test methods.
*
* @param original Test class
* @param methods Test methods
* @return Test class with new methods
*/
public static CtType cloneTestClassAndAddGivenTest(CtType original, List<CtMethod<?>> methods) {
CtType clone = original.clone();
original.getPackage().addType(clone);
methods.forEach(clone::addMethod);
return clone;
}
Aggregations