use of fr.inria.stamp.test.listener.TestListener 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 fr.inria.stamp.test.listener.TestListener 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 fr.inria.stamp.test.listener.TestListener 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 fr.inria.stamp.test.listener.TestListener in project dspot by STAMP-project.
the class DSpotAndResourcesTest method test.
@Test
public void test() throws Exception {
/*
Contract: DSpot is able to run tests that are using resources.
This does not mean that referenced resources by relative-path is supported.
Developers should not point directly resources by relative path inside their test, but rather use API
such as getResourceAsStream()
*/
final CtClass<?> classUsingResources = Utils.findClass("fr.inria.testresources.TestResources");
final InputProgram program = Utils.getInputProgram();
final String classpath = program.getProgramDir() + program.getClassesDir() + PATH_SEPARATOR + program.getProgramDir() + program.getTestClassesDir() + PATH_SEPARATOR + Utils.getBuilder().buildClasspath(program.getProgramDir());
final TestListener result = TestLauncher.runFromSpoonNodes(Utils.getInputConfiguration(), classpath, classUsingResources, classUsingResources.getMethodsByName("testResources"));
assertTrue(result.getFailingTests().isEmpty());
assertEquals(1, result.getRunningTests().size());
}
use of fr.inria.stamp.test.listener.TestListener in project dspot by STAMP-project.
the class TestLauncherTest method testOnJMockit.
@Test
public void testOnJMockit() throws Exception {
try {
FileUtils.deleteDirectory(new File("src/test/resources/jmockit/target/"));
} catch (Exception ignored) {
}
Utils.init("src/test/resources/jmockit/mock.properties");
final String classpath = Utils.getBuilder().buildClasspath(Utils.getInputProgram().getProgramDir()) + System.getProperty("path.separator") + Utils.getInputProgram().getProgramDir() + "/" + Utils.getInputProgram().getClassesDir() + System.getProperty("path.separator") + Utils.getInputProgram().getProgramDir() + "/" + Utils.getInputProgram().getTestClassesDir();
final CtClass<?> jmockitTest = Utils.findClass("org.baeldung.mocks.jmockit.LoginControllerIntegrationTest");
TestListener run = TestLauncher.run(Utils.getInputConfiguration(), classpath, jmockitTest);
assertEquals(7, run.getRunningTests().size());
assertEquals(7, run.getPassingTests().size());
assertEquals(0, run.getFailingTests().size());
}
Aggregations