Search in sources :

Example 1 with PitResult

use of fr.inria.diversify.mutant.pit.PitResult 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 2 with PitResult

use of fr.inria.diversify.mutant.pit.PitResult 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 3 with PitResult

use of fr.inria.diversify.mutant.pit.PitResult 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 4 with PitResult

use of fr.inria.diversify.mutant.pit.PitResult 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 5 with PitResult

use of fr.inria.diversify.mutant.pit.PitResult in project dspot by STAMP-project.

the class ExecutedMutantSelectorTest method test.

@Ignore
@Test
public void test() throws Exception {
    // pre computing the number of executed mutants...
    Main.verbose = true;
    AutomaticBuilderFactory.getAutomaticBuilder(Utils.getInputConfiguration()).runPit(Utils.getInputProgram().getProgramDir());
    final List<PitResult> pitResults = PitResultParser.parseAndDelete(Utils.getInputProgram().getProgramDir() + "target/pit-reports/");
    final ExecutedMutantSelector testSelector = new ExecutedMutantSelector();
    DSpot dspot = new DSpot(Utils.getInputConfiguration(), 1, Collections.singletonList(new TestDataMutator()), testSelector);
    final CtType amplifyTest = dspot.amplifyTest(Utils.findClass("example.TestSuiteExample"), Collections.singletonList(Utils.findMethod("example.TestSuiteExample", "test8")));
    // pretty print it
    DSpotUtils.printCtTypeToGivenDirectory(amplifyTest, new File(DSpotCompiler.pathToTmpTestSources));
    // then compile
    final String classpath = AutomaticBuilderFactory.getAutomaticBuilder(Utils.getInputConfiguration()).buildClasspath(Utils.getInputProgram().getProgramDir()) + AmplificationHelper.PATH_SEPARATOR + Utils.getInputProgram().getProgramDir() + "/" + Utils.getInputProgram().getClassesDir() + AmplificationHelper.PATH_SEPARATOR + "target/dspot/dependencies/" + AmplificationHelper.PATH_SEPARATOR + Utils.getInputProgram().getProgramDir() + "/" + Utils.getInputProgram().getTestClassesDir();
    DSpotCompiler.compile(DSpotCompiler.pathToTmpTestSources, classpath, new File(Utils.getInputProgram().getProgramDir() + "/" + Utils.getInputProgram().getTestClassesDir()));
    AutomaticBuilderFactory.getAutomaticBuilder(Utils.getInputConfiguration()).runPit(Utils.getInputProgram().getProgramDir());
    final List<PitResult> amplifiedPitResults = PitResultParser.parseAndDelete(Utils.getInputProgram().getProgramDir() + "target/pit-reports/");
    assertTrue(pitResults.stream().filter(pitResult -> pitResult.getStateOfMutant() == PitResult.State.KILLED || pitResult.getStateOfMutant() == PitResult.State.SURVIVED).count() < amplifiedPitResults.stream().filter(pitResult -> pitResult.getStateOfMutant() == PitResult.State.KILLED || pitResult.getStateOfMutant() == PitResult.State.SURVIVED).count());
    Main.verbose = false;
}
Also used : TestDataMutator(fr.inria.diversify.dspot.amplifier.TestDataMutator) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) DSpotCompiler(fr.inria.diversify.utils.compilation.DSpotCompiler) DSpotUtils(fr.inria.diversify.utils.DSpotUtils) Main(fr.inria.stamp.Main) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) DSpot(fr.inria.diversify.dspot.DSpot) File(java.io.File) List(java.util.List) CtType(spoon.reflect.declaration.CtType) Ignore(org.junit.Ignore) Utils(fr.inria.Utils) AutomaticBuilderFactory(fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory) PitResult(fr.inria.diversify.mutant.pit.PitResult) Collections(java.util.Collections) PitResultParser(fr.inria.diversify.mutant.pit.PitResultParser) Before(org.junit.Before) DSpot(fr.inria.diversify.dspot.DSpot) CtType(spoon.reflect.declaration.CtType) PitResult(fr.inria.diversify.mutant.pit.PitResult) File(java.io.File) TestDataMutator(fr.inria.diversify.dspot.amplifier.TestDataMutator) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

PitResult (fr.inria.diversify.mutant.pit.PitResult)6 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 MutantJSON (fr.inria.diversify.dspot.selector.json.mutant.MutantJSON)4 TestCaseJSON (fr.inria.diversify.dspot.selector.json.mutant.TestCaseJSON)4 TestClassJSON (fr.inria.diversify.dspot.selector.json.mutant.TestClassJSON)4 File (java.io.File)4 CtMethod (spoon.reflect.declaration.CtMethod)4 AutomaticBuilderFactory (fr.inria.diversify.automaticbuilder.AutomaticBuilderFactory)3 PitResultParser (fr.inria.diversify.mutant.pit.PitResultParser)3 AmplificationHelper (fr.inria.diversify.utils.AmplificationHelper)3 DSpotUtils (fr.inria.diversify.utils.DSpotUtils)3 DSpotCompiler (fr.inria.diversify.utils.compilation.DSpotCompiler)3 CtType (spoon.reflect.declaration.CtType)3 AutomaticBuilder (fr.inria.diversify.automaticbuilder.AutomaticBuilder)2 AmplificationChecker (fr.inria.diversify.utils.AmplificationChecker)2 Counter (fr.inria.diversify.utils.Counter)2 InputConfiguration (fr.inria.diversify.utils.sosiefier.InputConfiguration)2 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2