Search in sources :

Example 6 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class MethodsAssertGenerator method makeFailureTest.

/**
 * Adds surrounding try/catch/fail in a failing test.
 *
 * @param test Failing test method to amplify
 * @param failure Test's failure description
 * @return New amplified test
 */
protected CtMethod<?> makeFailureTest(CtMethod<?> test, Failure failure) {
    CtMethod cloneMethodTest = AmplificationHelper.cloneTestMethodForAmp(test, "");
    cloneMethodTest.setSimpleName(test.getSimpleName());
    Factory factory = cloneMethodTest.getFactory();
    Throwable exception = failure.getException();
    if (// TestTimedOutException means infinite loop
    exception instanceof TestTimedOutException || exception instanceof AssertionError) {
        // AssertionError means that some assertion remained in the test: TODO
        return null;
    }
    Class exceptionClass;
    if (exception == null) {
        exceptionClass = Exception.class;
    } else {
        exceptionClass = exception.getClass();
    }
    CtTry tryBlock = factory.Core().createTry();
    tryBlock.setBody(cloneMethodTest.getBody());
    String snippet = "org.junit.Assert.fail(\"" + test.getSimpleName() + " should have thrown " + exceptionClass.getSimpleName() + "\")";
    tryBlock.getBody().addStatement(factory.Code().createCodeSnippetStatement(snippet));
    DSpotUtils.addComment(tryBlock, "AssertGenerator generate try/catch block with fail statement", CtComment.CommentType.INLINE);
    CtCatch ctCatch = factory.Core().createCatch();
    CtTypeReference exceptionType = factory.Type().createReference(exceptionClass);
    ctCatch.setParameter(factory.Code().createCatchVariable(exceptionType, "eee"));
    ctCatch.setBody(factory.Core().createBlock());
    List<CtCatch> catchers = new ArrayList<>(1);
    catchers.add(ctCatch);
    tryBlock.setCatchers(catchers);
    CtBlock body = factory.Core().createBlock();
    body.addStatement(tryBlock);
    cloneMethodTest.setBody(body);
    cloneMethodTest.setSimpleName(cloneMethodTest.getSimpleName() + "_failAssert" + (numberOfFail++));
    Counter.updateAssertionOf(cloneMethodTest, 1);
    return cloneMethodTest;
}
Also used : LoggerFactory(org.slf4j.LoggerFactory) Factory(spoon.reflect.factory.Factory) CtTypeReference(spoon.reflect.reference.CtTypeReference) TestTimedOutException(org.junit.runners.model.TestTimedOutException) CtMethod(spoon.reflect.declaration.CtMethod)

Example 7 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class CloverCoverageSelector method selectToAmplify.

@Override
public List<CtMethod<?>> selectToAmplify(List<CtMethod<?>> testsToBeAmplified) {
    if (this.currentClassTestToBeAmplified == null && !testsToBeAmplified.isEmpty()) {
        this.currentClassTestToBeAmplified = testsToBeAmplified.get(0).getDeclaringType();
        final Map<String, Map<String, List<Integer>>> lineCoveragePerTestMethods = CloverExecutor.executeAll(this.configuration, PATH_TO_COPIED_FILES);
        this.originalLineCoveragePerClass = new HashMap<>();
        final InputProgram program = this.configuration.getInputProgram();
        lineCoveragePerTestMethods.keySet().stream().map(lineCoveragePerTestMethods::get).forEach(lineCoveragePerTestMethod -> lineCoveragePerTestMethod.keySet().forEach(className -> {
            final CtType<?> key = program.getFactory().Type().get(className);
            if (!this.originalLineCoveragePerClass.containsKey(key)) {
                this.originalLineCoveragePerClass.put(key, new HashSet<>());
            }
            this.originalLineCoveragePerClass.get(key).addAll(lineCoveragePerTestMethod.get(className));
        }));
        final String classesOfProject = program.getProgramDir() + program.getClassesDir() + AmplificationHelper.PATH_SEPARATOR + program.getProgramDir() + program.getTestClassesDir();
        final String classpath = AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).buildClasspath(program.getProgramDir()) + AmplificationHelper.PATH_SEPARATOR + classesOfProject;
        this.initialCoverage = EntryPoint.runCoverageOnTestClasses(classpath, classesOfProject, DSpotUtils.getAllTestClasses(configuration));
    }
    if (testsToBeAmplified.size() > 1) {
        final List<CtMethod<?>> collect = testsToBeAmplified.stream().filter(this.selectedAmplifiedTest::contains).collect(Collectors.toList());
        if (collect.isEmpty()) {
            return testsToBeAmplified;
        } else {
            return collect;
        }
    } else {
        return testsToBeAmplified;
    }
}
Also used : AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) FileWriter(java.io.FileWriter) Set(java.util.Set) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) File(java.io.File) HashSet(java.util.HashSet) CloverExecutor(fr.inria.stamp.coverage.clover.CloverExecutor) List(java.util.List) CtType(spoon.reflect.declaration.CtType) AutomaticBuilderFactory(fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory) Map(java.util.Map) AmplificationChecker(fr.inria.diversify.utils.AmplificationChecker) EntryPoint(eu.stamp.project.testrunner.EntryPoint) Coverage(eu.stamp.project.testrunner.runner.coverage.Coverage) CtMethod(spoon.reflect.declaration.CtMethod) CtType(spoon.reflect.declaration.CtType) HashMap(java.util.HashMap) Map(java.util.Map) CtMethod(spoon.reflect.declaration.CtMethod) InputProgram(fr.inria.diversify.utils.sosiefier.InputProgram) HashSet(java.util.HashSet)

Example 8 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class ExecutedMutantSelector method selectToKeep.

@Override
public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) {
    if (amplifiedTestToBeKept.isEmpty()) {
        return amplifiedTestToBeKept;
    }
    // construct a test classes with only amplified tests
    CtType clone = this.currentClassTestToBeAmplified.clone();
    clone.setParent(this.currentClassTestToBeAmplified.getParent());
    this.currentClassTestToBeAmplified.getMethods().stream().filter(AmplificationChecker::isTest).forEach(clone::removeMethod);
    amplifiedTestToBeKept.forEach(clone::addMethod);
    // pretty print it
    DSpotUtils.printCtTypeToGivenDirectory(clone, new File(DSpotCompiler.pathToTmpTestSources));
    // then compile
    final String classpath = AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).buildClasspath(this.program.getProgramDir()) + AmplificationHelper.PATH_SEPARATOR + this.program.getProgramDir() + "/" + this.program.getClassesDir() + AmplificationHelper.PATH_SEPARATOR + "target/dspot/dependencies/" + AmplificationHelper.PATH_SEPARATOR + this.program.getProgramDir() + "/" + this.program.getTestClassesDir();
    DSpotCompiler.compile(DSpotCompiler.pathToTmpTestSources, classpath, new File(this.program.getProgramDir() + "/" + this.program.getTestClassesDir()));
    AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).runPit(this.program.getProgramDir(), clone);
    final List<PitResult> pitResults = PitResultParser.parseAndDelete(program.getProgramDir() + AutomaticBuilderFactory.getAutomaticBuilder(this.configuration).getOutputDirectoryPit());
    final int numberOfSelectedAmplifiedTest = pitResults.stream().filter(pitResult -> pitResult.getStateOfMutant() == PitResult.State.KILLED || pitResult.getStateOfMutant() == PitResult.State.SURVIVED).filter(pitResult -> !this.originalMutantExecuted.contains(pitResult)).map(pitResult -> {
        final CtMethod amplifiedTestThatExecuteMoreMutants = pitResult.getMethod(clone);
        if (!this.mutantExecutedPerAmplifiedTestMethod.containsKey(amplifiedTestThatExecuteMoreMutants)) {
            this.mutantExecutedPerAmplifiedTestMethod.put(amplifiedTestThatExecuteMoreMutants, new HashSet<>());
        }
        this.mutantExecutedPerAmplifiedTestMethod.get(amplifiedTestThatExecuteMoreMutants).add(pitResult);
        this.selectedAmplifiedTest.add(amplifiedTestThatExecuteMoreMutants);
        return amplifiedTestThatExecuteMoreMutants;
    }).collect(Collectors.toSet()).size();
    LOGGER.info("{} has been selected to amplify the test suite", numberOfSelectedAmplifiedTest);
    return amplifiedTestToBeKept;
}
Also used : DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AutomaticBuilder(fr.inria.diversify.automaticbuilder.AutomaticBuilder) CtType(spoon.reflect.declaration.CtType) Gson(com.google.gson.Gson) AutomaticBuilderFactory(fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory) Map(java.util.Map) TestCaseJSON(fr.inria.diversify.dspot.selector.json.mutant.TestCaseJSON) Counter(fr.inria.diversify.utils.Counter) PitResult(fr.inria.diversify.mutant.pit.PitResult) PitResultParser(fr.inria.diversify.mutant.pit.PitResultParser) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) Logger(org.slf4j.Logger) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) FileWriter(java.io.FileWriter) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) AmplificationChecker(fr.inria.diversify.utils.AmplificationChecker) MutantJSON(fr.inria.diversify.dspot.selector.json.mutant.MutantJSON) FileReader(java.io.FileReader) TestClassJSON(fr.inria.diversify.dspot.selector.json.mutant.TestClassJSON) CtMethod(spoon.reflect.declaration.CtMethod) CtType(spoon.reflect.declaration.CtType) PitResult(fr.inria.diversify.mutant.pit.PitResult) File(java.io.File) CtMethod(spoon.reflect.declaration.CtMethod)

Example 9 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class ExecutedMutantSelector method reportJSONMutants.

private void reportJSONMutants() {
    if (this.currentClassTestToBeAmplified == null) {
        return;
    }
    TestClassJSON testClassJSON;
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    final File file = new File(this.configuration.getOutputDirectory() + "/" + this.currentClassTestToBeAmplified.getQualifiedName() + "_mutants_executed.json");
    if (file.exists()) {
        try {
            testClassJSON = gson.fromJson(new FileReader(file), TestClassJSON.class);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    } else {
        testClassJSON = new TestClassJSON(getNbMutantExecutedOriginally(this.currentClassTestToBeAmplified.getQualifiedName()), this.currentClassTestToBeAmplified.getQualifiedName(), this.currentClassTestToBeAmplified.getMethods().stream().filter(AmplificationChecker::isTest).count());
    }
    List<CtMethod> keys = new ArrayList<>(this.mutantExecutedPerAmplifiedTestMethod.keySet());
    keys.forEach(amplifiedTest -> {
        List<PitResult> pitResults = new ArrayList<>(this.mutantExecutedPerAmplifiedTestMethod.get(amplifiedTest));
        final List<MutantJSON> mutantsJson = new ArrayList<>();
        pitResults.forEach(pitResult -> mutantsJson.add(new MutantJSON(pitResult.getFullQualifiedNameMutantOperator(), pitResult.getLineNumber(), pitResult.getNameOfMutatedMethod())));
        if (amplifiedTest == null) {
            testClassJSON.addTestCase(new TestCaseJSON(this.currentClassTestToBeAmplified.getSimpleName(), Counter.getAllAssertions(), Counter.getAllInput(), mutantsJson));
        } else {
            testClassJSON.addTestCase(new TestCaseJSON(amplifiedTest.getSimpleName(), Counter.getAssertionOfSinceOrigin(amplifiedTest), Counter.getInputOfSinceOrigin(amplifiedTest), mutantsJson));
        }
    });
    try (FileWriter writer = new FileWriter(file, false)) {
        writer.write(gson.toJson(testClassJSON));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) FileWriter(java.io.FileWriter) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) IOException(java.io.IOException) TestCaseJSON(fr.inria.diversify.dspot.selector.json.mutant.TestCaseJSON) FileReader(java.io.FileReader) PitResult(fr.inria.diversify.mutant.pit.PitResult) File(java.io.File) CtMethod(spoon.reflect.declaration.CtMethod) MutantJSON(fr.inria.diversify.dspot.selector.json.mutant.MutantJSON) TestClassJSON(fr.inria.diversify.dspot.selector.json.mutant.TestClassJSON)

Example 10 with CtMethod

use of spoon.reflect.declaration.CtMethod in project dspot by STAMP-project.

the class JacocoCoverageSelector method selectToAmplify.

@Override
public List<CtMethod<?>> selectToAmplify(List<CtMethod<?>> testsToBeAmplified) {
    if (this.currentClassTestToBeAmplified == null && !testsToBeAmplified.isEmpty()) {
        this.currentClassTestToBeAmplified = testsToBeAmplified.get(0).getDeclaringType();
        this.initialCoverage = new JacocoExecutor(this.program, this.configuration, this.currentClassTestToBeAmplified).executeJacoco(this.currentClassTestToBeAmplified);
        this.selectedToBeAmplifiedCoverageResultsMap = null;
        this.selectedAmplifiedTest.clear();
    }
    final List<String> methodNames = testsToBeAmplified.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList());
    final Map<String, CoverageResults> coverageResultsMap = new JacocoExecutor(this.program, this.configuration, this.currentClassTestToBeAmplified).executeJacoco(this.currentClassTestToBeAmplified, methodNames);
    final List<String> pathExecuted = new ArrayList<>();
    final List<CtMethod<?>> filteredTests = testsToBeAmplified.stream().filter(ctMethod -> ctMethod != null && coverageResultsMap.get(ctMethod.getSimpleName()) != null).filter(ctMethod -> {
        final String pathByExecInstructions = computePathExecuted.apply(coverageResultsMap.get(ctMethod.getSimpleName()).getCoverageBuilder());
        if (pathExecuted.contains(pathByExecInstructions)) {
            return false;
        } else {
            pathExecuted.add(pathByExecInstructions);
            return true;
        }
    }).collect(Collectors.toList());
    if (this.selectedToBeAmplifiedCoverageResultsMap == null) {
        final List<String> filteredMethodNames = filteredTests.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toList());
        this.selectedToBeAmplifiedCoverageResultsMap = coverageResultsMap.keySet().stream().filter(filteredMethodNames::contains).collect(Collectors.toMap(Function.identity(), coverageResultsMap::get));
    }
    return filteredTests;
}
Also used : IntStream(java.util.stream.IntStream) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) JacocoExecutor(fr.inria.stamp.coverage.JacocoExecutor) CoverageBuilder(org.jacoco.core.analysis.CoverageBuilder) ILine(org.jacoco.core.analysis.ILine) CtNamedElement(spoon.reflect.declaration.CtNamedElement) Function(java.util.function.Function) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) TestClassJSON(fr.inria.diversify.dspot.selector.json.coverage.TestClassJSON) ICounter(org.jacoco.core.analysis.ICounter) CtType(spoon.reflect.declaration.CtType) Gson(com.google.gson.Gson) AutomaticBuilderFactory(fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory) TestCaseJSON(fr.inria.diversify.dspot.selector.json.coverage.TestCaseJSON) Map(java.util.Map) Counter(fr.inria.diversify.utils.Counter) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) InputConfiguration(fr.inria.diversify.utils.sosiefier.InputConfiguration) FileWriter(java.io.FileWriter) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) CoverageResults(fr.inria.stamp.coverage.CoverageResults) FileReader(java.io.FileReader) CtMethod(spoon.reflect.declaration.CtMethod) JacocoExecutor(fr.inria.stamp.coverage.JacocoExecutor) ArrayList(java.util.ArrayList) CtMethod(spoon.reflect.declaration.CtMethod) CoverageResults(fr.inria.stamp.coverage.CoverageResults)

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