Skip to content

Commit

Permalink
feat: Target one test class (#675)
Browse files Browse the repository at this point in the history
* feat: can now compute the baseline for a single test class

* feat: add options and parameters to target one single test class

* test: add a test targeting only one test class

* doc: add the new option
  • Loading branch information
danglotb authored Jan 29, 2019
1 parent 9808a3a commit 7a5a4be
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 7 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ Usage: java -jar target/dspot-<version>-jar-with-dependencies.jar
[optional, expert mode] specify the path to the .xml or .csv of the original
result of Pit Test. If you use this option the selector will be forced
to PitMutantScoreSelector
[--targetOneTestClass]
[optional, expert] enable this option will make DSpot computing the
mutation score of only one test class (the first pass through --test
command line option)
[--descartes]
Enable the descartes engine for Pit Mutant Score Selector.
Expand Down
9 changes: 8 additions & 1 deletion dspot-maven/src/main/java/eu/stamp_project/DSpotMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,12 @@ public class DSpotMojo extends AbstractMojo {
@Parameter(property = "path-pit-result")
private String pathPitResult;

/**
* [optional, expert] enable this option will make DSpot computing the mutation score of only one test class (the first pass through --test command line option)
*/
@Parameter(defaultValue = "false", property = "targetOneTestClass")
private Boolean targetOneTestClass;

/**
* Enable the descartes engine for Pit Mutant Score Selector.
*/
Expand Down Expand Up @@ -253,7 +259,8 @@ public void execute() {
.setGenerateAmplifiedTestClass(this.generateNewTestClass)
.setKeepOriginalTestMethods(this.keepOriginalTestMethods)
.setOutputDirectory(this.outputPath)
.setUseMavenToExecuteTest(this.useMavenToExeTest);
.setUseMavenToExecuteTest(this.useMavenToExeTest)
.setTargetOneTestClass(this.targetOneTestClass);

if (this.pathPitResult != null && !this.pathPitResult.isEmpty()) {
InputConfiguration.get().setSelector(new PitMutantScoreSelector(this.pathPitResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,13 @@ public void init(InputConfiguration configuration) {
super.init(configuration);
if (this.originalKilledMutants == null) {
final AutomaticBuilder automaticBuilder = InputConfiguration.get().getBuilder();
automaticBuilder.runPit();
if (InputConfiguration.get().shouldTargetOneTestClass()) {
automaticBuilder.runPit(
InputConfiguration.get().getFactory().Class().get(InputConfiguration.get().getTestClasses().get(0))
);
} else {
automaticBuilder.runPit();
}
initOriginalPitResult(parser.parseAndDelete(this.configuration.getAbsolutePathToProjectRoot() + automaticBuilder.getOutputDirectoryPit()) );
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class JSAPOptions {

/**
* parse the command line argument
*
* @param args command line arguments. Refer to the README on the github page or use --help command line option to display all the accepted arguments.
* @return true if --example is pass through the command line. It will run DSpot with a small example to make a demonstration of the usage of DSpot.
* Otherwise, it returns false and DSpot will run normally, using the properties and the command line options.
Expand Down Expand Up @@ -79,19 +80,19 @@ public static boolean parse(String[] args) {
LOGGER.warn("Forcing the Selector to PitMutantScoreSelector");
}
String pathToOriginalResultOfPit = jsapConfig.getString("mutant");
String extension = pathToOriginalResultOfPit.substring(pathToOriginalResultOfPit.length()-4);
String extension = pathToOriginalResultOfPit.substring(pathToOriginalResultOfPit.length() - 4);
PitMutantScoreSelector.OutputFormat originalFormat;
if(extension.toLowerCase().equals("xml")){
if (extension.toLowerCase().equals("xml")) {
originalFormat = PitMutantScoreSelector.OutputFormat.XML;
} else if (extension.toLowerCase().equals("csv")){
} else if (extension.toLowerCase().equals("csv")) {
originalFormat = PitMutantScoreSelector.OutputFormat.CSV;
} else {
LOGGER.warn("You specified the wrong Pit format. Skipping expert mode.");
originalFormat = PitMutantScoreSelector.OutputFormat.XML;
}
testCriterion = new PitMutantScoreSelector(jsapConfig.getString("mutant"), originalFormat, consecutiveFormat);

// default test selector mode
// default test selector mode
} else {
if (!(jsapConfig.getString("output-format") == null)) {
if (!"PitMutantScoreSelector".equals(jsapConfig.getString("test-criterion"))) {
Expand Down Expand Up @@ -141,7 +142,8 @@ public static boolean parse(String[] args) {
.setGenerateAmplifiedTestClass(jsapConfig.getBoolean("generate-new-test-class"))
.setKeepOriginalTestMethods(jsapConfig.getBoolean("keep-original-test-methods"))
.setDescartesMode(jsapConfig.getBoolean("descartes") && !jsapConfig.getBoolean("gregor"))
.setUseMavenToExecuteTest(jsapConfig.getBoolean("use-maven-to-exe-test"));
.setUseMavenToExecuteTest(jsapConfig.getBoolean("use-maven-to-exe-test"))
.setTargetOneTestClass(jsapConfig.getBoolean("targetOneTestClass"));
return false;
}

Expand Down Expand Up @@ -248,6 +250,12 @@ private static JSAP initJSAP() {
mutantScore.setUsageName("./path/to/mutations.csv");
mutantScore.setHelp("[optional, expert mode] specify the path to the .xml or .csv of the original result of Pit Test. If you use this option the selector will be forced to PitMutantScoreSelector");

Switch targetOneTestClass = new Switch("targetOneTestClass");
targetOneTestClass.setLongFlag("targetOneTestClass");
targetOneTestClass.setHelp("[optional, expert] enable this option will make DSpot computing the mutation score of only one test class (the first pass through --test command line option)");
targetOneTestClass.setDefault("false");


FlaggedOption testCases = new FlaggedOption("testCases");
testCases.setList(true);
testCases.setAllowMultipleDeclarations(false);
Expand Down Expand Up @@ -355,6 +363,7 @@ private static JSAP initJSAP() {
jsap.registerParameter(output);
jsap.registerParameter(cleanOutput);
jsap.registerParameter(mutantScore);
jsap.registerParameter(targetOneTestClass);
jsap.registerParameter(descartes);
jsap.registerParameter(gregor);
jsap.registerParameter(automaticBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -936,4 +936,15 @@ public boolean isJUnit5() {
public void setJUnit5(boolean JUnit5) {
isJUnit5 = JUnit5;
}

private boolean targetOneTestClass = false;

public boolean shouldTargetOneTestClass() {
return this.targetOneTestClass;
}

public InputConfiguration setTargetOneTestClass(boolean targetOneTestClass) {
this.targetOneTestClass = targetOneTestClass;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package eu.stamp_project.dspot.selector;

import eu.stamp_project.AbstractTest;
import eu.stamp_project.utils.pit.AbstractPitResult;
import eu.stamp_project.utils.program.InputConfiguration;
import org.junit.After;
import org.junit.Test;

import java.lang.reflect.Field;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* created by Benjamin DANGLOT
* [email protected]
* on 29/01/19
*/
public class OneTestClassPitScoreMutantSelectorTest extends AbstractTest {

private String FULL_QUALIFIED_NAME_TEST_CLASS = "example.TestSuiteExample";

@Override
public String getPathToPropertiesFile() {
return "src/test/resources/test-projects/test-projects.properties";
}

@Override
public void setUp() throws Exception {
super.setUp();
InputConfiguration.get().setTestClasses(Collections.singletonList(FULL_QUALIFIED_NAME_TEST_CLASS));
InputConfiguration.get().setTargetOneTestClass(true);
}

@After
public void tearDown() throws Exception {
InputConfiguration.get().setTargetOneTestClass(false);
InputConfiguration.get().setTestClasses(Collections.emptyList());
}

@Test
public void test() throws NoSuchFieldException, IllegalAccessException {
final PitMutantScoreSelector pitMutantScoreSelector = new PitMutantScoreSelector();
pitMutantScoreSelector.init(InputConfiguration.get());
final Field field = pitMutantScoreSelector.getClass().getDeclaredField("originalKilledMutants");
field.setAccessible(true);
List<AbstractPitResult> originalResult = (List<AbstractPitResult>) field.get(pitMutantScoreSelector);
assertTrue(originalResult.stream().allMatch(abstractPitResult -> abstractPitResult.getFullQualifiedNameOfKiller().equals(FULL_QUALIFIED_NAME_TEST_CLASS)));
}
}

0 comments on commit 7a5a4be

Please sign in to comment.