Search in sources :

Example 86 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 87 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 88 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 89 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)

Example 90 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)

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