Search in sources :

Example 1 with TestListener

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;
}
Also used : TestCompiler(fr.inria.diversify.utils.compilation.TestCompiler) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) Logger(org.slf4j.Logger) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Description(org.junit.runner.Description) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Failure(org.junit.runner.notification.Failure) AssertGenerator(fr.inria.diversify.dspot.assertGenerator.AssertGenerator) ArrayList(java.util.ArrayList) TestSelector(fr.inria.diversify.dspot.selector.TestSelector) List(java.util.List) CtType(spoon.reflect.declaration.CtType) Amplifier(fr.inria.diversify.dspot.amplifier.Amplifier) AmplificationChecker(fr.inria.diversify.utils.AmplificationChecker) ModifierKind(spoon.reflect.declaration.ModifierKind) TestListener(fr.inria.stamp.test.listener.TestListener) Collections(java.util.Collections) CtMethod(spoon.reflect.declaration.CtMethod) Set(java.util.Set) Description(org.junit.runner.Description) ArrayList(java.util.ArrayList) TestListener(fr.inria.stamp.test.listener.TestListener) CtMethod(spoon.reflect.declaration.CtMethod)

Example 2 with TestListener

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;
    }
}
Also used : TestCompiler(fr.inria.diversify.utils.compilation.TestCompiler) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) Logger(org.slf4j.Logger) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Description(org.junit.runner.Description) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Failure(org.junit.runner.notification.Failure) AssertGenerator(fr.inria.diversify.dspot.assertGenerator.AssertGenerator) ArrayList(java.util.ArrayList) TestSelector(fr.inria.diversify.dspot.selector.TestSelector) List(java.util.List) CtType(spoon.reflect.declaration.CtType) Amplifier(fr.inria.diversify.dspot.amplifier.Amplifier) AmplificationChecker(fr.inria.diversify.utils.AmplificationChecker) ModifierKind(spoon.reflect.declaration.ModifierKind) TestListener(fr.inria.stamp.test.listener.TestListener) Collections(java.util.Collections) CtMethod(spoon.reflect.declaration.CtMethod) TestListener(fr.inria.stamp.test.listener.TestListener)

Example 3 with TestListener

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());
            }
        }
    }
}
Also used : TestCompiler(fr.inria.diversify.utils.compilation.TestCompiler) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) Logger(org.slf4j.Logger) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Description(org.junit.runner.Description) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Failure(org.junit.runner.notification.Failure) AssertGenerator(fr.inria.diversify.dspot.assertGenerator.AssertGenerator) ArrayList(java.util.ArrayList) TestSelector(fr.inria.diversify.dspot.selector.TestSelector) List(java.util.List) CtType(spoon.reflect.declaration.CtType) Amplifier(fr.inria.diversify.dspot.amplifier.Amplifier) AmplificationChecker(fr.inria.diversify.utils.AmplificationChecker) ModifierKind(spoon.reflect.declaration.ModifierKind) TestListener(fr.inria.stamp.test.listener.TestListener) Collections(java.util.Collections) CtMethod(spoon.reflect.declaration.CtMethod) TestListener(fr.inria.stamp.test.listener.TestListener) CtMethod(spoon.reflect.declaration.CtMethod)

Example 4 with TestListener

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());
}
Also used : TestListener(fr.inria.stamp.test.listener.TestListener) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) Test(org.junit.Test) AbstractTest(fr.inria.AbstractTest)

Example 5 with TestListener

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());
}
Also used : TestListener(fr.inria.stamp.test.listener.TestListener) File(java.io.File) Test(org.junit.Test)

Aggregations

TestListener (fr.inria.stamp.test.listener.TestListener)30 Test (org.junit.Test)20 IOException (java.io.IOException)8 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)7 File (java.io.File)7 Failure (org.junit.runner.notification.Failure)6 CtMethod (spoon.reflect.declaration.CtMethod)6 CtType (spoon.reflect.declaration.CtType)6 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)5 DSpotUtils (fr.inria.diversify.utils.DSpotUtils)5 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)5 TestCompiler (fr.inria.diversify.utils.compilation.TestCompiler)5 Collectors (java.util.stream.Collectors)5 Description (org.junit.runner.Description)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 CtClass (spoon.reflect.declaration.CtClass)5 Amplifier (fr.inria.diversify.dspot.amplifier.Amplifier)3 AssertGenerator (fr.inria.diversify.dspot.assertGenerator.AssertGenerator)3 TestSelector (fr.inria.diversify.dspot.selector.TestSelector)3