Search in sources :

Example 11 with CtMethod

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

the class JacocoCoverageSelector method selectToKeep.

@Override
public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> amplifiedTestToBeKept) {
    if (amplifiedTestToBeKept.isEmpty()) {
        return amplifiedTestToBeKept;
    }
    final List<String> methodNames = amplifiedTestToBeKept.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<?>> methodsKept = amplifiedTestToBeKept.stream().filter(ctMethod -> {
        final String simpleNameOfFirstParent = getFirstParentThatHasBeenRun(ctMethod).getSimpleName();
        return this.selectedToBeAmplifiedCoverageResultsMap.get(simpleNameOfFirstParent) == null || coverageResultsMap.get(ctMethod.getSimpleName()).isBetterThan(this.selectedToBeAmplifiedCoverageResultsMap.get(simpleNameOfFirstParent)) && !computePathExecuted.apply(coverageResultsMap.get(ctMethod.getSimpleName()).getCoverageBuilder()).equals(computePathExecuted.apply(this.selectedToBeAmplifiedCoverageResultsMap.get(simpleNameOfFirstParent).getCoverageBuilder()));
    }).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());
    this.selectedToBeAmplifiedCoverageResultsMap.putAll(methodsKept.stream().map(CtNamedElement::getSimpleName).collect(Collectors.toMap(Function.identity(), coverageResultsMap::get)));
    this.selectedAmplifiedTest.addAll(new ArrayList<>(methodsKept));
    return methodsKept;
}
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) CtNamedElement(spoon.reflect.declaration.CtNamedElement) CtMethod(spoon.reflect.declaration.CtMethod) CoverageResults(fr.inria.stamp.coverage.CoverageResults)

Example 12 with CtMethod

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

the class PitMutantScoreSelector 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_killed.json");
    if (file.exists()) {
        try {
            testClassJSON = gson.fromJson(new FileReader(file), TestClassJSON.class);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    } else {
        testClassJSON = new TestClassJSON(getNbMutantKilledOriginally(this.currentClassTestToBeAmplified.getQualifiedName()), this.currentClassTestToBeAmplified.getQualifiedName(), this.currentClassTestToBeAmplified.getMethods().stream().filter(AmplificationChecker::isTest).count());
    }
    List<CtMethod> keys = new ArrayList<>(this.testThatKilledMutants.keySet());
    keys.forEach(amplifiedTest -> {
        List<PitResult> pitResults = new ArrayList<>(this.testThatKilledMutants.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) Gson(com.google.gson.Gson) TestCaseJSON(fr.inria.diversify.dspot.selector.json.mutant.TestCaseJSON) PitResult(fr.inria.diversify.mutant.pit.PitResult) CtMethod(spoon.reflect.declaration.CtMethod) MutantJSON(fr.inria.diversify.dspot.selector.json.mutant.MutantJSON) TestClassJSON(fr.inria.diversify.dspot.selector.json.mutant.TestClassJSON)

Example 13 with CtMethod

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

the class PitMutantScoreSelector 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 AutomaticBuilder automaticBuilder = AutomaticBuilderFactory.getAutomaticBuilder(this.configuration);
    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> results = PitResultParser.parseAndDelete(program.getProgramDir() + automaticBuilder.getOutputDirectoryPit());
    Set<CtMethod<?>> selectedTests = new HashSet<>();
    if (results != null) {
        LOGGER.info("{} mutants has been generated ({})", results.size(), this.numberOfMutant);
        if (results.size() != this.numberOfMutant) {
            LOGGER.warn("Number of generated mutant is different than the original one.");
        }
        results.stream().filter(result -> result.getStateOfMutant() == PitResult.State.KILLED && !this.originalKilledMutants.contains(result) && !this.mutantNotTestedByOriginal.contains(result)).forEach(result -> {
            CtMethod method = result.getMethod(clone);
            if (killsMoreMutantThanParents(method, result)) {
                if (!testThatKilledMutants.containsKey(method)) {
                    testThatKilledMutants.put(method, new HashSet<>());
                }
                testThatKilledMutants.get(method).add(result);
                if (method == null) {
                    // output of pit test does not allow us to know which test case kill new mutants... we keep them all...
                    selectedTests.addAll(amplifiedTestToBeKept);
                } else {
                    selectedTests.add(method);
                }
            }
        });
    }
    this.selectedAmplifiedTest.addAll(selectedTests);
    selectedTests.forEach(selectedTest -> LOGGER.info("{} kills {} more mutants", selectedTest == null ? this.currentClassTestToBeAmplified.getSimpleName() : selectedTest.getSimpleName(), this.testThatKilledMutants.containsKey(selectedTest) ? this.testThatKilledMutants.get(selectedTest).size() : this.testThatKilledMutants.get(null)));
    return new ArrayList<>(selectedTests);
}
Also used : java.util(java.util) 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) Collectors(java.util.stream.Collectors) GsonBuilder(com.google.gson.GsonBuilder) AutomaticBuilder(fr.inria.diversify.automaticbuilder.AutomaticBuilder) CtType(spoon.reflect.declaration.CtType) java.io(java.io) Gson(com.google.gson.Gson) AutomaticBuilderFactory(fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory) AmplificationChecker(fr.inria.diversify.utils.AmplificationChecker) TestCaseJSON(fr.inria.diversify.dspot.selector.json.mutant.TestCaseJSON) Counter(fr.inria.diversify.utils.Counter) MutantJSON(fr.inria.diversify.dspot.selector.json.mutant.MutantJSON) TestClassJSON(fr.inria.diversify.dspot.selector.json.mutant.TestClassJSON) PitResult(fr.inria.diversify.mutant.pit.PitResult) PitMutantMinimizer(fr.inria.stamp.minimization.PitMutantMinimizer) PitResultParser(fr.inria.diversify.mutant.pit.PitResultParser) Minimizer(fr.inria.stamp.minimization.Minimizer) CtMethod(spoon.reflect.declaration.CtMethod) AutomaticBuilder(fr.inria.diversify.automaticbuilder.AutomaticBuilder) CtType(spoon.reflect.declaration.CtType) PitResult(fr.inria.diversify.mutant.pit.PitResult) CtMethod(spoon.reflect.declaration.CtMethod)

Example 14 with CtMethod

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

the class TestMethodCallRemover method apply.

public List<CtMethod> apply(CtMethod method) {
    List<CtMethod> methods = new ArrayList<>();
    if (method.getDeclaringType() != null) {
        // get the list of method calls
        List<CtInvocation> invocations = Query.getElements(method, new TypeFilter(CtInvocation.class));
        // this index serves to replace ith literal is replaced by zero in the ith clone of the method
        int invocation_index = 0;
        for (CtInvocation invocation : invocations) {
            try {
                if (toRemove(invocation) && !AmplificationChecker.isAssert(invocation) && !inWhileLoop(invocation) && !containsIteratorNext(invocation)) {
                    methods.add(apply(method, invocation_index));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            invocation_index++;
        }
    }
    return methods;
}
Also used : ArrayList(java.util.ArrayList) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtMethod(spoon.reflect.declaration.CtMethod)

Example 15 with CtMethod

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

the class CollectionCreator method generateEmptyCollection.

static CtExpression<?> generateEmptyCollection(CtTypeReference type, String nameMethod, Class<?> typeOfCollection) {
    final Factory factory = type.getFactory();
    final CtType<?> collectionsType = factory.Type().get(Collections.class);
    final CtTypeAccess<?> accessToCollections = factory.createTypeAccess(collectionsType.getReference());
    final CtMethod<?> singletonListMethod = collectionsType.getMethodsByName(nameMethod).get(0);
    final CtExecutableReference<?> executableReference = factory.Core().createExecutableReference();
    executableReference.setStatic(true);
    executableReference.setSimpleName(singletonListMethod.getSimpleName());
    executableReference.setDeclaringType(collectionsType.getReference());
    executableReference.setType(factory.createCtTypeReference(typeOfCollection));
    if (type.getActualTypeArguments().isEmpty()) {
        // supporting Collections.<type>emptyList()
        executableReference.addActualTypeArgument(type);
    } else if (type.getActualTypeArguments().stream().noneMatch(reference -> reference instanceof CtWildcardReference)) {
        // in case type is a list, we copy the Actual arguments
        executableReference.setActualTypeArguments(type.getActualTypeArguments());
    }
    return factory.createInvocation(accessToCollections, executableReference);
}
Also used : CtExecutableReference(spoon.reflect.reference.CtExecutableReference) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) CtType(spoon.reflect.declaration.CtType) CtExpression(spoon.reflect.code.CtExpression) CtTypeAccess(spoon.reflect.code.CtTypeAccess) Factory(spoon.reflect.factory.Factory) CtWildcardReference(spoon.reflect.reference.CtWildcardReference) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) CtMethod(spoon.reflect.declaration.CtMethod) CtWildcardReference(spoon.reflect.reference.CtWildcardReference) Factory(spoon.reflect.factory.Factory)

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