Search in sources :

Example 16 with CtMethod

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;
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) CtTypedElement(spoon.reflect.declaration.CtTypedElement) ObjectLog(fr.inria.diversify.compare.ObjectLog) Predicate(java.util.function.Predicate) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) CtNamedElement(spoon.reflect.declaration.CtNamedElement) spoon.reflect.code(spoon.reflect.code) CtMethod(spoon.reflect.declaration.CtMethod) CtMethod(spoon.reflect.declaration.CtMethod)

Example 17 with CtMethod

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;
}
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 18 with CtMethod

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;
    }
}
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 19 with CtMethod

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());
            }
        }
    }
}
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 20 with CtMethod

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);
    }
}
Also used : ClassTimeJSON(fr.inria.diversify.utils.json.ClassTimeJSON) CtType(spoon.reflect.declaration.CtType) CtMethod(spoon.reflect.declaration.CtMethod)

Aggregations

CtMethod (spoon.reflect.declaration.CtMethod)240 Test (org.junit.Test)163 Factory (spoon.reflect.factory.Factory)77 Launcher (spoon.Launcher)73 CtClass (spoon.reflect.declaration.CtClass)47 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)47 CtType (spoon.reflect.declaration.CtType)45 AbstractTest (fr.inria.AbstractTest)36 ArrayList (java.util.ArrayList)35 List (java.util.List)33 CtTypeReference (spoon.reflect.reference.CtTypeReference)31 CtInvocation (spoon.reflect.code.CtInvocation)26 CtStatement (spoon.reflect.code.CtStatement)26 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)19 Collectors (java.util.stream.Collectors)19 CtLiteral (spoon.reflect.code.CtLiteral)18 CtElement (spoon.reflect.declaration.CtElement)18 CtIf (spoon.reflect.code.CtIf)16 CtAnnotation (spoon.reflect.declaration.CtAnnotation)16 CtField (spoon.reflect.declaration.CtField)16