diff --git a/dspot/src/main/java/fr/inria/diversify/dspot/amplifier/AllLiteralAmplifiers.java b/dspot/src/main/java/fr/inria/diversify/dspot/amplifier/AllLiteralAmplifiers.java new file mode 100644 index 000000000..2871f0f4b --- /dev/null +++ b/dspot/src/main/java/fr/inria/diversify/dspot/amplifier/AllLiteralAmplifiers.java @@ -0,0 +1,39 @@ +package fr.inria.diversify.dspot.amplifier; + +import spoon.reflect.declaration.CtMethod; +import spoon.reflect.declaration.CtType; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Created by Benjamin DANGLOT + * benjamin.danglot@inria.fr + * on 07/03/18 + */ +public class AllLiteralAmplifiers implements Amplifier { + + private List literalAmplifiers; + + public AllLiteralAmplifiers() { + this.literalAmplifiers = Arrays.asList( + new StringLiteralAmplifier(), + new NumberLiteralAmplifier(), + new BooleanLiteralAmplifier(), + new CharLiteralAmplifier() + ); + } + + @Override + public List apply(CtMethod testMethod) { + return this.literalAmplifiers.stream() + .flatMap(amplifier -> amplifier.apply(testMethod).stream()) + .collect(Collectors.toList()); + } + + @Override + public void reset(CtType testClass) { + this.literalAmplifiers.forEach(amplifier -> amplifier.reset(testClass)); + } +} diff --git a/dspot/src/main/java/fr/inria/diversify/dspot/assertGenerator/AssertionRemover.java b/dspot/src/main/java/fr/inria/diversify/dspot/assertGenerator/AssertionRemover.java index cbc5216b6..617d63cc4 100644 --- a/dspot/src/main/java/fr/inria/diversify/dspot/assertGenerator/AssertionRemover.java +++ b/dspot/src/main/java/fr/inria/diversify/dspot/assertGenerator/AssertionRemover.java @@ -56,6 +56,7 @@ public void removeAssertion(CtInvocation invocation) { clone = ((CtUnaryOperator) clone).getOperand(); } if (clone instanceof CtStatement) { + clone.getTypeCasts().clear(); invocation.insertBefore((CtStatement) clone); } else if (! (clone instanceof CtLiteral || clone instanceof CtVariableRead)) { CtTypeReference typeOfParameter = clone.getType(); diff --git a/dspot/src/main/java/fr/inria/stamp/JSAPOptions.java b/dspot/src/main/java/fr/inria/stamp/JSAPOptions.java index 15f6723f1..fef14f50e 100644 --- a/dspot/src/main/java/fr/inria/stamp/JSAPOptions.java +++ b/dspot/src/main/java/fr/inria/stamp/JSAPOptions.java @@ -18,8 +18,8 @@ import java.util.Collections; import java.util.Iterator; import java.util.List; +import java.util.Objects; import java.util.stream.Collectors; -import java.util.stream.Stream; /** * Created by Benjamin DANGLOT @@ -72,24 +72,20 @@ public TestSelector buildSelector() { } public enum AmplifierEnum { - MethodAdd(Collections.singletonList(new TestMethodCallAdder())), - MethodRemove(Collections.singletonList(new TestMethodCallRemover())), - TestDataMutator(Collections.singletonList(new TestDataMutator())), - StatementAdd(Collections.singletonList(new StatementAdd())), - StringLiteralAmplifier(Collections.singletonList(new StringLiteralAmplifier())), - NumberLiteralAmplifier(Collections.singletonList(new NumberLiteralAmplifier())), - BooleanLiteralAmplifier(Collections.singletonList(new BooleanLiteralAmplifier())), - CharLiteralAmplifier(Collections.singletonList(new CharLiteralAmplifier())), - AllLiteralAmplifiers(Arrays.asList(new StringLiteralAmplifier(), - new NumberLiteralAmplifier(), - new BooleanLiteralAmplifier(), - new CharLiteralAmplifier()) - ), + MethodAdd(new TestMethodCallAdder()), + MethodRemove(new TestMethodCallRemover()), + TestDataMutator(new TestDataMutator()), + StatementAdd(new StatementAdd()), + StringLiteralAmplifier(new StringLiteralAmplifier()), + NumberLiteralAmplifier(new NumberLiteralAmplifier()), + BooleanLiteralAmplifier(new BooleanLiteralAmplifier()), + CharLiteralAmplifier(new CharLiteralAmplifier()), + AllLiteralAmplifiers(new AllLiteralAmplifiers()), None(null); - public final List amplifiers; + public final Amplifier amplifier; - private AmplifierEnum(List amplifiers) { - this.amplifiers = amplifiers; + private AmplifierEnum(Amplifier amplifier) { + this.amplifier = amplifier; } } @@ -141,14 +137,14 @@ public static Configuration parse(String[] args) { ); } - public static Stream stringToAmplifier(String amplifier) { + public static Amplifier stringToAmplifier(String amplifier) { try { - return AmplifierEnum.valueOf(amplifier).amplifiers.stream(); + return AmplifierEnum.valueOf(amplifier).amplifier; } catch (IllegalArgumentException e) { LOGGER.warn("Wrong values for amplifiers: {} is not recognized", amplifier); LOGGER.warn("Possible values are: StringLiteralAmplifier | NumberLiteralAmplifier | CharLiteralAmplifier | BooleanLiteralAmplifier | AllLiteralAmplifiers | MethodAdd | MethodRemove | TestDataMutator | StatementAdd | None"); LOGGER.warn("No amplifier has been added for {}", amplifier); - return Stream.of(); + return null; } } @@ -157,7 +153,8 @@ public static List buildAmplifiersFromString(String[] amplifiersAsStr return Collections.emptyList(); } else { return Arrays.stream(amplifiersAsString) - .flatMap(JSAPOptions::stringToAmplifier) + .map(JSAPOptions::stringToAmplifier) + .filter(Objects::nonNull) .collect(Collectors.toList()); } } @@ -218,15 +215,15 @@ private static JSAP initJSAP() { selector.setHelp("[optional] specify the test adequacy criterion to be maximized with amplification"); selector.setDefault("PitMutantScoreSelector"); - FlaggedOption specificTestCase = new FlaggedOption("test"); - specificTestCase.setStringParser(JSAP.STRING_PARSER); - specificTestCase.setShortFlag('t'); - specificTestCase.setList(true); - specificTestCase.setAllowMultipleDeclarations(false); - specificTestCase.setLongFlag("test"); - specificTestCase.setDefault("all"); - specificTestCase.setUsageName("my.package.MyClassTest"); - specificTestCase.setHelp("[optional] fully qualified names of test classes to be amplified. If the value is all, DSpot will amplify the whole test suite. You can also use regex to describe a set of test classes."); + FlaggedOption specificTestClass = new FlaggedOption("test"); + specificTestClass.setStringParser(JSAP.STRING_PARSER); + specificTestClass.setShortFlag('t'); + specificTestClass.setList(true); + specificTestClass.setAllowMultipleDeclarations(false); + specificTestClass.setLongFlag("test"); + specificTestClass.setDefault("all"); + specificTestClass.setUsageName("my.package.MyClassTest"); + specificTestClass.setHelp("[optional] fully qualified names of test classes to be amplified. If the value is all, DSpot will amplify the whole test suite. You can also use regex to describe a set of test classes."); FlaggedOption output = new FlaggedOption("output"); output.setStringParser(JSAP.STRING_PARSER); @@ -311,7 +308,7 @@ private static JSAP initJSAP() { jsap.registerParameter(iteration); jsap.registerParameter(selector); jsap.registerParameter(maxTestAmplified); - jsap.registerParameter(specificTestCase); + jsap.registerParameter(specificTestClass); jsap.registerParameter(testCases); jsap.registerParameter(output); jsap.registerParameter(cleanOutput); diff --git a/dspot/src/test/java/fr/inria/stamp/MainTest.java b/dspot/src/test/java/fr/inria/stamp/MainTest.java index 2413bd9ba..120456d89 100644 --- a/dspot/src/test/java/fr/inria/stamp/MainTest.java +++ b/dspot/src/test/java/fr/inria/stamp/MainTest.java @@ -210,6 +210,32 @@ public void testOneClassOneMethod() throws Throwable { } } + @Test + public void testOneClassTwoMethods() throws Throwable { + Main.main(new String[]{ + "--path-to-properties", "src/test/resources/test-projects/test-projects.properties", + "--test-criterion", "JacocoCoverageSelector", + "--amplifiers", "MethodAdd" + PATH_SEPARATOR + "TestDataMutator" + PATH_SEPARATOR + "StatementAdd", + "--iteration", "1", + "--randomSeed", "72", + "--maven-home", DSpotUtils.buildMavenHome(new InputConfiguration("src/test/resources/test-projects/test-projects.properties")), + "--test", "example.TestSuiteExample", + "--cases", "test2" + PATH_SEPARATOR + "test3", + "--output-path", "target/trash", + "--max-test-amplified", "200" + }); + final File reportFile = new File("target/trash/example.TestSuiteExample_jacoco_instr_coverage_report.txt"); + assertTrue(reportFile.exists()); + assertTrue(new File("target/trash/example.TestSuiteExample_jacoco_instr_coverage.json").exists()); + assertTrue(new File("target/trash/example/TestSuiteExampleAmpl.java").exists()); + try (BufferedReader reader = new BufferedReader(new FileReader(reportFile))) { + String content = reader.lines().reduce("", (acc, line) -> acc + line + nl); + assertEquals(expectedReportOneClassTwoMethods, content); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + private static final String expectedReportOneClassOneMethod = nl + "======= REPORT =======" + nl + "Initial instruction coverage: 33 / 37" + nl + @@ -218,6 +244,14 @@ public void testOneClassOneMethod() throws Throwable { "Amplified instruction coverage: 37 / 37" + nl + "100" + DECIMAL_SEPARATOR + "00%" + nl; + private static final String expectedReportOneClassTwoMethods = nl + + "======= REPORT =======" + nl + + "Initial instruction coverage: 33 / 37" + nl + + "89" + DECIMAL_SEPARATOR + "19%" + nl + + "Amplification results with 9 amplified tests." + nl + + "Amplified instruction coverage: 37 / 37" + nl + + "100" + DECIMAL_SEPARATOR + "00%" + nl; + @Test public void testRegexOnWholePackage() throws Throwable { Main.main(new String[]{