diff --git a/dspot/src/main/java/eu/stamp_project/dspot/amplifier/amplifiers/AbstractAmplifier.java b/dspot/src/main/java/eu/stamp_project/dspot/amplifier/amplifiers/AbstractAmplifier.java index 64753e2d5..e9ddcf04f 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/amplifier/amplifiers/AbstractAmplifier.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/amplifier/amplifiers/AbstractAmplifier.java @@ -1,7 +1,10 @@ package eu.stamp_project.dspot.amplifier.amplifiers; +import eu.stamp_project.dspot.common.configuration.options.CommentEnum; import eu.stamp_project.dspot.common.miscellaneous.CloneHelper; import eu.stamp_project.dspot.common.miscellaneous.Counter; +import eu.stamp_project.dspot.common.miscellaneous.DSpotUtils; +import spoon.reflect.code.CtComment; import spoon.reflect.declaration.CtElement; import spoon.reflect.declaration.CtMethod; @@ -71,8 +74,12 @@ protected List reduceAlreadyAmplifiedElements(List elementsToBeReduced) { protected CtMethod replace(T originalElement, T amplifiedElement, CtMethod testMethod) { originalElement.replace(amplifiedElement); amplifiedElement.putMetadata(this.METADATA_KEY, true); - CtMethod clone = CloneHelper.cloneTestMethodForAmp(testMethod, getSuffix()); + DSpotUtils.addComment(amplifiedElement, + getSuffix() + ": changed '" + originalElement + "' to '" + amplifiedElement + "'", + CtComment.CommentType.INLINE, CommentEnum.Amplifier); + CtMethod clone = CloneHelper.cloneTestMethodForAmp(testMethod, "_" + getSuffix()); amplifiedElement.replace(originalElement); + DSpotUtils.removeComments(originalElement, getSuffix()); Counter.updateInputOf(clone, 1); return clone; } diff --git a/dspot/src/main/java/eu/stamp_project/dspot/amplifier/amplifiers/TestMethodCallRemover.java b/dspot/src/main/java/eu/stamp_project/dspot/amplifier/amplifiers/TestMethodCallRemover.java index 0237ebddf..6a77fac5c 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/amplifier/amplifiers/TestMethodCallRemover.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/amplifier/amplifiers/TestMethodCallRemover.java @@ -1,15 +1,12 @@ package eu.stamp_project.dspot.amplifier.amplifiers; +import eu.stamp_project.dspot.common.configuration.options.CommentEnum; +import eu.stamp_project.dspot.common.miscellaneous.DSpotUtils; import eu.stamp_project.dspot.common.test_framework.TestFramework; import eu.stamp_project.dspot.common.miscellaneous.AmplificationHelper; import eu.stamp_project.dspot.common.miscellaneous.CloneHelper; import eu.stamp_project.dspot.common.miscellaneous.Counter; -import spoon.reflect.code.CtBlock; -import spoon.reflect.code.CtInvocation; -import spoon.reflect.code.CtStatement; -import spoon.reflect.code.CtStatementList; -import spoon.reflect.code.CtTry; -import spoon.reflect.code.CtWhile; +import spoon.reflect.code.*; import spoon.reflect.declaration.CtMethod; import spoon.reflect.declaration.CtType; import spoon.reflect.visitor.filter.TypeFilter; @@ -54,6 +51,8 @@ private CtMethod apply(CtMethod method, CtInvocation invocation) { ctStatementList.getStatements().get(indexOfInvocation).insertAfter(invocation); } Counter.updateInputOf(cloned, 1); + DSpotUtils.addComment(ctStatementList,"MethodCallRemover: removed call '" + invocation + "'", + CtComment.CommentType.INLINE, CommentEnum.Amplifier); return cloned; } diff --git a/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/AssertionGenerator.java b/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/AssertionGenerator.java index 20b2ef8c8..9149b6bdb 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/AssertionGenerator.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/AssertionGenerator.java @@ -106,36 +106,37 @@ public TestTuple removeAssertions(CtType testClass, List> tests) CtType cloneClass = testClass.clone(); testClass.getPackage().addType(cloneClass); if (devFriendlyAmplification) { - return new TestTuple(cloneClass, removeAssertionsAndTrailingInvocations(tests,cloneClass)); + return new TestTuple(cloneClass, removeAssertionsCompletely(tests,cloneClass)); } else { return new TestTuple(cloneClass, removeAssertions(tests, cloneClass)); } } /** - * Uses {@link AssertionRemover#removeAssertion(CtMethod)} to remove existing assertions from cloned test methods + * Uses {@link AssertionRemover#removeAssertions(CtMethod, boolean)} to remove existing assertions from cloned + * test methods, but leaves the arguments of the assertions * @param tests * @param cloneClass * @return */ private List> removeAssertions(List> tests, CtType cloneClass){ List> testsWithoutAssertions = tests.stream() - .map(this.assertionRemover::removeAssertion) + .map(test -> this.assertionRemover.removeAssertions(test, true)) .collect(Collectors.toList()); testsWithoutAssertions.forEach(cloneClass::addMethod); return testsWithoutAssertions; } /** - * Uses {@link AssertionRemover#removeAssertion(CtMethod)} to remove existing assertions from cloned test methods + * Uses {@link AssertionRemover#removeAssertions(CtMethod, boolean)} to remove existing assertions and their + * arguments from cloned test methods * @param tests * @param cloneClass * @return */ - private List> removeAssertionsAndTrailingInvocations(List> tests, CtType cloneClass){ + private List> removeAssertionsCompletely(List> tests, CtType cloneClass){ List> testsWithoutAssertions = tests.stream() - .map(this.assertionRemover::removeAssertion) - .map(this.assertionRemover::removeArgumentsOfTrailingAssertions) + .map(test -> this.assertionRemover.removeAssertions(test, false)) .collect(Collectors.toList()); testsWithoutAssertions.forEach(cloneClass::addMethod); return testsWithoutAssertions; diff --git a/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemover.java b/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemover.java index 096f35d8e..3d34309c8 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemover.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemover.java @@ -34,14 +34,14 @@ public Map, List>> getVariableAssertedPerTestMeth * @param testMethod Test method * @return Test's clone without any assertion */ - public CtMethod removeAssertion(CtMethod testMethod) { + public CtMethod removeAssertions(CtMethod testMethod, boolean leaveArguments) { CtMethod testWithoutAssertion = CloneHelper.cloneTestMethodNoAmp(testMethod); variableAssertedPerTestMethod.put(testWithoutAssertion, testWithoutAssertion .getElements(TestFramework.ASSERTIONS_FILTER) .stream() .filter(invocation -> !(invocation.getParent() instanceof CtRHSReceiver)) // it means that the return type is used in the test. - .flatMap(invocation -> this.removeAssertion(invocation).stream()) + .flatMap(invocation -> this.removeAssertion(invocation, leaveArguments).stream()) .collect(Collectors.toList()) ); @@ -64,34 +64,13 @@ public boolean matches(CtTry element) { } /** - * Can be called after {@link AssertionRemover#removeAssertion(CtMethod)} to remove the statements that - * previously were arguments of assertions all the way at - * @param testMethod - * @return - */ - public CtMethod removeArgumentsOfTrailingAssertions(CtMethod testMethod) { - List testStatements = testMethod.getElements(new TypeFilter<>(CtStatement.class)); - - for (int i = testStatements.size() - 1; i >= 0; i--) { - CtStatement statement = testStatements.get(i); - Object metadata = statement.getMetadata(AssertionGeneratorUtils.METADATA_WAS_IN_ASSERTION); - if (metadata != null && (Boolean) metadata) { - testMethod.getBody().removeStatement(statement); - } else { - break; - } - } - - return testMethod; - } - - /** - * Replaces an invocation with its arguments. + * Remove an invocation and optionally replace with its arguments * * @param invocation Invocation + * @param leaveArguments if true: replace the invocation with its arguments * @return the list of local variables extracted from assertions */ - public List> removeAssertion(CtInvocation invocation) { + public List> removeAssertion(CtInvocation invocation, boolean leaveArguments) { List> variableReadsAsserted = new ArrayList<>(); final Factory factory = invocation.getFactory(); final TypeFilter statementTypeFilter = new TypeFilter(CtStatement.class) { @@ -108,42 +87,40 @@ public boolean matches(CtStatement element) { if (clone instanceof CtUnaryOperator) { clone = ((CtUnaryOperator) clone).getOperand(); } - if (clone instanceof CtLambda) { - CtLambda lambda = ((CtLambda) clone); - if (lambda.getBody() != null) { - invocation.getParent(CtStatementList.class).insertBefore( - statementTypeFilter, - factory.createStatementList(lambda.getBody()) - ); - } else { - // in case of we have something like () -> "string" - if (lambda.getExpression() instanceof CtLiteral) { - continue; + if (leaveArguments) { + if (clone instanceof CtLambda) { + CtLambda lambda = ((CtLambda) clone); + if (lambda.getBody() != null) { + invocation.getParent(CtStatementList.class) + .insertBefore(statementTypeFilter, factory.createStatementList(lambda.getBody())); + } else { + // in case of we have something like () -> "string" + if (lambda.getExpression() instanceof CtLiteral) { + continue; + } + // TODO check that we support all cases by casting into CtInvocation + final CtBlock block = factory.createBlock(); + block.setStatements(Collections.singletonList((CtInvocation) lambda.getExpression().clone())); + invocation.getParent(CtStatementList.class).insertBefore(statementTypeFilter, + factory.createStatementList(block)); } - // TODO check that we support all cases by casting into CtInvocation - final CtBlock block = factory.createBlock(); - block.setStatements(Collections.singletonList((CtInvocation)lambda.getExpression().clone())); - invocation.getParent(CtStatementList.class).insertBefore( - statementTypeFilter, factory.createStatementList(block) - ); - } - } else if (clone instanceof CtStatement) { - invocation.getParent(CtStatementList.class).insertBefore(statementTypeFilter, (CtStatement) clone); - clone.putMetadata(AssertionGeneratorUtils.METADATA_WAS_IN_ASSERTION, true); - } else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) { - // TODO EXPLAIN - CtTypeReference typeOfParameter = clone.getType(); - if (clone.getType() == null || factory.Type().NULL_TYPE.equals(clone.getType())) { - typeOfParameter = factory.Type().createReference(Object.class); + } else if (clone instanceof CtStatement) { + invocation.getParent(CtStatementList.class).insertBefore(statementTypeFilter, + (CtStatement) clone); + clone.putMetadata(AssertionGeneratorUtils.METADATA_WAS_IN_ASSERTION, true); + } else if (!(clone instanceof CtLiteral || clone instanceof CtVariableRead)) { + // TODO EXPLAIN + CtTypeReference typeOfParameter = clone.getType(); + if (clone.getType() == null || factory.Type().NULL_TYPE.equals(clone.getType())) { + typeOfParameter = factory.Type().createReference(Object.class); + } + final CtLocalVariable localVariable = factory.createLocalVariable(typeOfParameter, + toCorrectJavaIdentifier(typeOfParameter.getSimpleName()) + "_" + counter[0]++, clone); + invocation.getParent(CtStatementList.class).insertBefore(statementTypeFilter, localVariable); + localVariable.putMetadata(AssertionGeneratorUtils.METADATA_WAS_IN_ASSERTION, true); } - final CtLocalVariable localVariable = factory.createLocalVariable( - typeOfParameter, - toCorrectJavaIdentifier(typeOfParameter.getSimpleName()) + "_" + counter[0]++, - clone - ); - invocation.getParent(CtStatementList.class).insertBefore(statementTypeFilter, localVariable); - localVariable.putMetadata(AssertionGeneratorUtils.METADATA_WAS_IN_ASSERTION, true); - } else if (clone instanceof CtVariableRead && !(clone instanceof CtFieldRead)) { + } + if (clone instanceof CtVariableRead && !(clone instanceof CtFieldRead)) { final CtVariableReference variable = ((CtVariableRead) clone).getVariable(); final List assertedVariables = invocation.getParent(CtBlock.class).getElements( localVariable -> localVariable.getSimpleName().equals(variable.getSimpleName()) diff --git a/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/InitializeDSpot.java b/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/InitializeDSpot.java index 9b9100ffd..2931710c6 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/InitializeDSpot.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/InitializeDSpot.java @@ -18,7 +18,6 @@ import eu.stamp_project.dspot.common.collector.smtp.EmailSender; import eu.stamp_project.dspot.common.configuration.test_finder.TestFinder; import org.apache.commons.io.FileUtils; -import org.junit.Test; import java.io.File; import java.io.IOException; @@ -149,15 +148,6 @@ public String completeDependencies(UserInput configuration, AutomaticBuilder aut dependencies = automaticBuilder.compileAndBuildClasspath(); configuration.setDependencies(dependencies); } - // TODO checks this. Since we support different Test Support, we may not need to add artificially junit in the classpath - if (!dependencies.contains("junit" + File.separator + "junit" + File.separator + "4")) { - dependencies = Test.class - .getProtectionDomain() - .getCodeSource() - .getLocation() - .getFile() + - AmplificationHelper.PATH_SEPARATOR + dependencies; - } if (!additionalClasspathElements.isEmpty()) { String pathToAdditionalClasspathElements = additionalClasspathElements; if (!Paths.get(additionalClasspathElements).isAbsolute()) { diff --git a/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java b/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java index 95d44b168..6b83d1979 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/common/configuration/UserInput.java @@ -62,7 +62,7 @@ public class UserInput { "that points to the folder that contains sources (.java)." + " Default value: ${DEFAULT-VALUE}" ) - private String pathToSourceCode = "src/main/java/"; + private String pathToSourceCode = "src" + File.separator + "main" + File.separator + "java" + File.separator; @CommandLine.Option( names = "--relative-path-to-test-code", @@ -71,7 +71,7 @@ public class UserInput { "that points to the folder that contains test sources (.java)." + " Default value: ${DEFAULT-VALUE}" ) - private String pathToTestSourceCode = "src/test/java/"; + private String pathToTestSourceCode = "src" + File.separator + "test" + File.separator + "java" + File.separator; @CommandLine.Option( names = "--relative-path-to-classes", @@ -80,7 +80,7 @@ public class UserInput { "that points to the folder that contains binaries of the source (.class)." + " Default value: ${DEFAULT-VALUE}" ) - private String pathToClasses = "target/classes/"; + private String pathToClasses = "target" + File.separator + "classes" + File.separator; @CommandLine.Option( names = "--relative-path-to-test-classes", @@ -89,7 +89,7 @@ public class UserInput { "that points to the folder that contains binaries of the test source (.class)." + " Default value: ${DEFAULT-VALUE}" ) - private String pathToTestClasses = "target/test-classes/"; + private String pathToTestClasses = "target" + File.separator + "test-classes" + File.separator; /* Amplification process configuration diff --git a/dspot/src/main/java/eu/stamp_project/dspot/common/miscellaneous/DSpotUtils.java b/dspot/src/main/java/eu/stamp_project/dspot/common/miscellaneous/DSpotUtils.java index 7f5fcb434..8db888a82 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/common/miscellaneous/DSpotUtils.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/common/miscellaneous/DSpotUtils.java @@ -20,6 +20,7 @@ import java.io.*; import java.util.Arrays; +import java.util.List; import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; @@ -197,6 +198,26 @@ public static void addComment(CtElement element, String content, CtComment.Comme } } } + public static void removeComments(CtElement element, String suffixOfComment) { + if (element instanceof CtLiteral) { + try { + CtElement parentLine = element.getParent(new LineFilter()); + if (parentLine != null) { + element = parentLine; + } + } catch (ParentNotInitializedException ignored) { + + } + } + + List ampComments = element.getComments().stream() + .filter(ctComment -> ctComment.getContent().startsWith(suffixOfComment)) + .collect(Collectors.toList()); + for (CtComment ampComment : ampComments) { + element.removeComment(ampComment); + } + + } public static final String PATH_TO_DSPOT_DEPENDENCIES = "target/dspot/dependencies/"; diff --git a/dspot/src/main/java/eu/stamp_project/dspot/common/test_framework/implementations/junit/JUnit5Support.java b/dspot/src/main/java/eu/stamp_project/dspot/common/test_framework/implementations/junit/JUnit5Support.java index dc98ddebb..0225d9927 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/common/test_framework/implementations/junit/JUnit5Support.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/common/test_framework/implementations/junit/JUnit5Support.java @@ -1,10 +1,9 @@ package eu.stamp_project.dspot.common.test_framework.implementations.junit; +import eu.stamp_project.dspot.common.configuration.options.CommentEnum; +import eu.stamp_project.dspot.common.miscellaneous.DSpotUtils; import eu.stamp_project.testrunner.runner.Failure; -import spoon.reflect.code.CtBlock; -import spoon.reflect.code.CtFieldRead; -import spoon.reflect.code.CtInvocation; -import spoon.reflect.code.CtLambda; +import spoon.reflect.code.*; import spoon.reflect.declaration.CtMethod; import spoon.reflect.factory.Factory; import spoon.reflect.reference.CtExecutableReference; @@ -72,6 +71,10 @@ public CtMethod generateExpectedExceptionsBlock(CtMethod test, Failure fai ); CtBlock body = factory.Core().createBlock(); body.addStatement(invocation); + DSpotUtils.addComment(invocation, + "AssertionGenerator generate try/catch block with fail statement", + CtComment.CommentType.INLINE, + CommentEnum.Amplifier); test.setBody(body); test.setSimpleName(test.getSimpleName() + "_failAssert" + (numberOfFail)); diff --git a/dspot/src/test/java/eu/stamp_project/MainTest.java b/dspot/src/test/java/eu/stamp_project/MainTest.java index 0fe5773a4..41368ed54 100644 --- a/dspot/src/test/java/eu/stamp_project/MainTest.java +++ b/dspot/src/test/java/eu/stamp_project/MainTest.java @@ -222,7 +222,7 @@ public void testDevFriendlyAmplification() throws Exception { Main.main(new String[]{ "--verbose", "--absolute-path-to-project-root", new File("src/test/resources/test-projects/").getAbsolutePath() + "/", - "--amplifiers", "FastLiteralAmplifier", + "--amplifiers", "MethodAdderOnExistingObjectsAmplifier", "--test-criterion", "ExtendedCoverageSelector", "--test", "example.TestSuiteExample2", "--dev-friendly", diff --git a/dspot/src/test/java/eu/stamp_project/dspot/AbstractTestOnSample.java b/dspot/src/test/java/eu/stamp_project/dspot/AbstractTestOnSample.java index b60dfdfff..fdbfe87fd 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/AbstractTestOnSample.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/AbstractTestOnSample.java @@ -1,6 +1,7 @@ package eu.stamp_project.dspot; import eu.stamp_project.dspot.assertiongenerator.assertiongenerator.AssertionGeneratorUtils; +import eu.stamp_project.dspot.common.configuration.options.CommentEnum; import eu.stamp_project.dspot.common.miscellaneous.DSpotUtils; import eu.stamp_project.dspot.common.test_framework.TestFramework; import eu.stamp_project.dspot.common.miscellaneous.CloneHelper; @@ -46,6 +47,7 @@ public void setUp() { TestFramework.init(launcher.getFactory()); DSpotCache.init(10000); RandomHelper.setSeedRandom(72L); + DSpotUtils.init(CommentEnum.None, null,null,null); this.testRunner = new TestRunner(getPathToProjectRoot(), "", false); } diff --git a/dspot/src/test/java/eu/stamp_project/dspot/amplifier/ArrayLiteralAmplifierTest.java b/dspot/src/test/java/eu/stamp_project/dspot/amplifier/ArrayLiteralAmplifierTest.java index 1496777b3..19c5cf5c8 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/amplifier/ArrayLiteralAmplifierTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/amplifier/ArrayLiteralAmplifierTest.java @@ -93,7 +93,7 @@ private void callAssertions(String methodName, List expectedValues){ assertEquals(expectedValues.size(), mutantMethods.size()); for (int i = 0; i < mutantMethods.size(); i++) { CtMethod mutantMethod = mutantMethods.get(i); - assertEquals(methodName + "litArray" + (i + 1), mutantMethod.getSimpleName()); + assertEquals(methodName + "_litArray" + (i + 1), mutantMethod.getSimpleName()); CtExpression mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtExpression.class)).get(0); assertTrue(mutantLiteral + " not in expected values", expectedValues.contains(mutantLiteral.toString())); diff --git a/dspot/src/test/java/eu/stamp_project/dspot/amplifier/NumberLiteralAmplifierTest.java b/dspot/src/test/java/eu/stamp_project/dspot/amplifier/NumberLiteralAmplifierTest.java index 1570473ac..aa2b158c5 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/amplifier/NumberLiteralAmplifierTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/amplifier/NumberLiteralAmplifierTest.java @@ -240,7 +240,7 @@ private void callAssertions(List mutantMethods, String nameMethod, Lis assertEquals(mutantMethodSize, mutantMethods.size()); for (int i = 0; i < mutantMethods.size(); i++) { CtMethod mutantMethod = mutantMethods.get(i); - assertEquals(nameMethod + "litNum" + (i + 1), mutantMethod.getSimpleName()); + assertEquals(nameMethod + "_litNum" + (i + 1), mutantMethod.getSimpleName()); CtExpression mutantLiteral = mutantMethod.getBody().getElements(new TypeFilter<>(CtExpression.class)).get(0); if (mutantLiteral instanceof CtLiteral) { assertNotEquals(originalValue, ((CtLiteral) mutantLiteral).getValue()); diff --git a/dspot/src/test/java/eu/stamp_project/dspot/amplifier/TestMethodCallAdder.java b/dspot/src/test/java/eu/stamp_project/dspot/amplifier/TestMethodCallAdder.java index a75144ed9..feebbb69a 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/amplifier/TestMethodCallAdder.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/amplifier/TestMethodCallAdder.java @@ -30,6 +30,7 @@ public class TestMethodCallAdder extends AbstractTestOnSample { @Before @Override public void setUp() { + super.setUp(); UserInput configuration = new UserInput(); configuration.setAbsolutePathToProjectRoot(new File("src/test/resources/sample/").getAbsolutePath()); @@ -37,7 +38,6 @@ public void setUp() { configuration.getFullClassPathWithExtraDependencies(), "src/test/resources/sample/" ); - super.setUp(); } @Test diff --git a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/AssertionGeneratorTest.java b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/AssertionGeneratorTest.java index 1f353be0f..9b3564aaa 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/AssertionGeneratorTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/AssertionGeneratorTest.java @@ -115,7 +115,7 @@ public void testCreateLogOnClassObject() throws Exception { final CtClass testClass = findClass("fr.inria.sample.TestClassWithoutAssert"); final CtMethod testOnClass = findMethod(testClass, "testOnClass"); final AssertionRemover assertionRemover = new AssertionRemover(); - final CtMethod testWithoutAssertions = assertionRemover.removeAssertion(testOnClass); + final CtMethod testWithoutAssertions = assertionRemover.removeAssertions(testOnClass, true); System.out.println( assertionGenerator.removeAndAmplifyAssertions(testClass, Collections.singletonList(testWithoutAssertions)) ); diff --git a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemoverTest.java b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemoverTest.java index 5f3d9edb6..ce5f25e3f 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemoverTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/AssertionRemoverTest.java @@ -43,7 +43,7 @@ public void testRemoveAssertionsOnLambdaWithNullBody() { CtClass testClass = findClass("fr.inria.sample.TestClassWithAssert"); final CtMethod test1 = (CtMethod) testClass.getMethodsByName("testWithALambdaWithNullBody").get(0); final AssertionRemover assertionRemover = new AssertionRemover(); - final CtMethod ctMethod = assertionRemover.removeAssertion(test1); + final CtMethod ctMethod = assertionRemover.removeAssertions(test1, true); final String expected = "@org.junit.Test" + AmplificationHelper.LINE_SEPARATOR + "public void testWithALambdaWithNullBody() throws java.lang.Exception {" + AmplificationHelper.LINE_SEPARATOR + " new fr.inria.sample.ClassThrowException().throwException();" + AmplificationHelper.LINE_SEPARATOR + @@ -65,7 +65,7 @@ e.g. assertEquals("aString", new MyObject().toString()) would give CtClass testClass = findClass("fr.inria.sample.TestClassWithAssert"); final CtMethod test1 = (CtMethod) testClass.getMethodsByName("testWithNewSomethingWithoutLocalVariables").get(0); final AssertionRemover assertionRemover = new AssertionRemover(); - final CtMethod ctMethod = assertionRemover.removeAssertion(test1); + final CtMethod ctMethod = assertionRemover.removeAssertions(test1, true); final CtMethod testWithLog = TestWithLogGenerator.createTestWithLog(ctMethod, "fr.inria.sample", Collections.emptyList()); assertEquals("@org.junit.Test(timeout = 10000)" + AmplificationHelper.LINE_SEPARATOR + @@ -88,7 +88,7 @@ public void testRemoveInvocationWhenReturnedValueIsUsed() { final CtClass testClass = findClass("fr.inria.sample.TestClassWithAssert"); final CtMethod test = findMethod(testClass, "testWithAMethodCallThatContainsAssertionsAndItsReturnedValueIsUsed"); final AssertionRemover assertionRemover = new AssertionRemover(); - final CtMethod ctMethod = assertionRemover.removeAssertion(test); + final CtMethod ctMethod = assertionRemover.removeAssertions(test, true); final String expectedMethodString = "@org.junit.Test(timeout = 10000)" + AmplificationHelper.LINE_SEPARATOR + "public void testWithAMethodCallThatContainsAssertionsAndItsReturnedValueIsUsed() throws java.lang.Exception {" + AmplificationHelper.LINE_SEPARATOR + " java.lang.String aString = verify(\"aString\");" + AmplificationHelper.LINE_SEPARATOR + @@ -104,7 +104,7 @@ public void testOnTestWithTryWithResource() { final CtClass testClass = findClass("fr.inria.sample.TestClassWithAssert"); final CtMethod testWithALambda = findMethod(testClass, "testWithTryWithResource"); final AssertionRemover assertionRemover = new AssertionRemover(); - final CtMethod ctMethod = assertionRemover.removeAssertion(testWithALambda); + final CtMethod ctMethod = assertionRemover.removeAssertions(testWithALambda, true); final String expectedBody = "{" + AmplificationHelper.LINE_SEPARATOR + " try (final java.io.FileInputStream fis = new java.io.FileInputStream(new java.io.File(\".\"))) {" + AmplificationHelper.LINE_SEPARATOR + " fis.toString();" + AmplificationHelper.LINE_SEPARATOR + @@ -121,7 +121,7 @@ public void testOnAssertionWithALambda() { final CtClass testClass = findClass("fr.inria.sample.TestClassWithAssert"); final CtMethod testWithALambda = findMethod(testClass, "testWithALambda"); final AssertionRemover assertionRemover = new AssertionRemover(); - final CtMethod ctMethod = assertionRemover.removeAssertion(testWithALambda); + final CtMethod ctMethod = assertionRemover.removeAssertions(testWithALambda, true); System.out.println(ctMethod); assertFalse(ctMethod.getBody().getStatements().isEmpty()); } @@ -136,7 +136,7 @@ public void testOnTestMethodWithNonJavaIdentifier() throws Exception { final CtClass testClass = findClass("fr.inria.sample.TestClassWithAssert"); final CtMethod testWithCatchVariable = findMethod(testClass, "testWithArray"); final AssertionRemover assertionRemover = new AssertionRemover(); - final CtMethod ctMethod = assertionRemover.removeAssertion(testWithCatchVariable); + final CtMethod ctMethod = assertionRemover.removeAssertions(testWithCatchVariable, true); assertTrue( ctMethod.getElements(new TypeFilter>(CtLocalVariable.class)) .stream() @@ -168,7 +168,7 @@ public void testOnCatchVariable() throws Exception { final CtClass testClass = findClass("fr.inria.sample.TestClassWithAssert"); final CtMethod testWithCatchVariable = findMethod(testClass, "test3_exceptionCatch"); final AssertionRemover assertionRemover = new AssertionRemover(); - final CtMethod ctMethod = assertionRemover.removeAssertion(testWithCatchVariable); + final CtMethod ctMethod = assertionRemover.removeAssertions(testWithCatchVariable, true); assertTrue(ctMethod.getElements(new TypeFilter<>(CtCatch.class)).isEmpty()); assertTrue(ctMethod.getElements(new TypeFilter<>(CtTry.class)).isEmpty()); assertTrue(ctMethod.getElements(new TypeFilter(CtInvocation.class) { @@ -188,7 +188,7 @@ public void testRemoveAssertionOnSimpleExample() throws Exception { public boolean matches(CtInvocation element) { return TestFramework.get().isAssert(element); } - }).forEach(assertionRemover::removeAssertion); + }).forEach(invocation -> assertionRemover.removeAssertion(invocation,true)); final String expectedMethod = "@org.junit.Test" + AmplificationHelper.LINE_SEPARATOR + "public void test1() {" + AmplificationHelper.LINE_SEPARATOR + @@ -212,7 +212,7 @@ public void testRemoveAssertionWithSwitchCase() throws Exception { public boolean matches(CtInvocation element) { return TestFramework.get().isAssert(element); } - }).forEach(assertionRemover::removeAssertion); + }).forEach(invocation -> assertionRemover.removeAssertion(invocation,true)); final String expectedMethod = "@org.junit.Test" + AmplificationHelper.LINE_SEPARATOR + "public void test1() {" + AmplificationHelper.LINE_SEPARATOR + @@ -242,7 +242,7 @@ public void testRemoveAssertionWithUnary() throws Exception { public boolean matches(CtInvocation element) { return TestFramework.get().isAssert(element); } - }).forEach(assertionRemover::removeAssertion); + }).forEach(invocation -> assertionRemover.removeAssertion(invocation,true)); final String expectedMethod = "@org.junit.Test" + AmplificationHelper.LINE_SEPARATOR + "public void test2() throws java.lang.Exception {" + AmplificationHelper.LINE_SEPARATOR + @@ -281,7 +281,7 @@ public void testOnDifferentKindOfAssertions() throws Exception { final CtClass testClass = findClass("fr.inria.helper.TestWithMultipleAsserts"); final AssertionRemover assertionRemover = new AssertionRemover(); final CtMethod testMethod = testClass.getMethodsByName("test").get(0); - final CtMethod removedAssertion = assertionRemover.removeAssertion(testMethod); + final CtMethod removedAssertion = assertionRemover.removeAssertions(testMethod, true); assertEquals(4, removedAssertion.getBody().getStatements().size()); } } diff --git a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/TryCatchFailGeneratorTest.java b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/TryCatchFailGeneratorTest.java index 9043d2a49..58c7cfbe4 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/TryCatchFailGeneratorTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/TryCatchFailGeneratorTest.java @@ -5,7 +5,9 @@ import org.junit.Test; import spoon.reflect.declaration.CtMethod; +import static org.hamcrest.CoreMatchers.containsString; import static org.junit.Assert.assertNull; +import static org.junit.Assert.*; /** * Created by Benjamin DANGLOT @@ -82,5 +84,7 @@ public void testSurroundWithTryCatchFail() throws Exception { new Failure(testName, testClassName, new ArrayIndexOutOfBoundsException(-100)) ); System.out.println(ctMethod); + assertThat(ctMethod.toString(), containsString("catch (java.lang.NullPointerException")); + assertThat(ctMethod.toString(), containsString("catch (java.lang.ArrayIndexOutOfBoundsException")); } } diff --git a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/methodreconstructor/observer/TestWithLogGeneratorTest.java b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/methodreconstructor/observer/TestWithLogGeneratorTest.java index 1c274187e..ddb72a965 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/methodreconstructor/observer/TestWithLogGeneratorTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/methodreconstructor/observer/TestWithLogGeneratorTest.java @@ -48,7 +48,7 @@ public void testOnLoops() throws Exception { " eu.stamp_project.dspot.assertiongenerator.assertiongenerator.methodreconstructor.observer.testwithloggenerator.objectlogsyntaxbuilder_constructs.ObjectLog.log(o_test2__7, \"o_test2__7\", \"test2__7___end\");" + AmplificationHelper.LINE_SEPARATOR + "}", eu.stamp_project.dspot.assertiongenerator.assertiongenerator.methodreconstructor.observer.TestWithLogGenerator.createTestWithLog( - new AssertionRemover().removeAssertion(findMethod("fr.inria.sample.TestClassWithLoop", "test2")), + new AssertionRemover().removeAssertions(findMethod("fr.inria.sample.TestClassWithLoop", "test2"), true), "fr.inria.sample", Collections.emptyList()).toString() ); diff --git a/dspot/src/test/java/eu/stamp_project/dspot/common/configuration/options/CommentTest.java b/dspot/src/test/java/eu/stamp_project/dspot/common/configuration/options/CommentTest.java index fe39cc776..abd15e341 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/common/configuration/options/CommentTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/common/configuration/options/CommentTest.java @@ -89,9 +89,10 @@ public void testDevFriendlyAllComments() throws Exception { Main.main(new String[]{ "--verbose", "--absolute-path-to-project-root", new File("src/test/resources/test-projects/").getAbsolutePath() + "/", - "--amplifiers", "FastLiteralAmplifier", + "--amplifiers", "MethodAdderOnExistingObjectsAmplifier", "--test-criterion", "ExtendedCoverageSelector", "--test", TEST_CASE, + "--cases", "test2", "--clean", "--with-comment=All", "--dev-friendly", @@ -100,7 +101,7 @@ public void testDevFriendlyAllComments() throws Exception { "/* Coverage improved at" + AmplificationHelper.LINE_SEPARATOR + " example.Example:" + AmplificationHelper.LINE_SEPARATOR + " charAt" + AmplificationHelper.LINE_SEPARATOR + - " L. 7 +7 instr." + AmplificationHelper.LINE_SEPARATOR + + " L. 2 +4 instr." + AmplificationHelper.LINE_SEPARATOR + " */"; String amplifierComment = " // AssertionGenerator: add assertion"; @@ -114,19 +115,17 @@ public void testAmplifierComments() throws Exception { Main.main(new String[]{ "--verbose", "--absolute-path-to-project-root", new File("src/test/resources/test-projects/").getAbsolutePath() + "/", - "--amplifiers", "FastLiteralAmplifier", + "--amplifiers", "MethodAdderOnExistingObjectsAmplifier", "--test-criterion", "ExtendedCoverageSelector", "--test", TEST_CASE, "--clean", "--with-comment=Amplifier", "--dev-friendly", }); - String commentAndExpression = - " // FastLiteralAmplifier: change number from '1' to '0.0'" + AmplificationHelper.LINE_SEPARATOR + - " // AssertionGenerator: create local variable with return value of invocation" + AmplificationHelper.LINE_SEPARATOR + - " char o_test3_literalMutationNumber41__4 = ex.charAt(s, s.length() - 0);"; + String comment = + " // MethodAdderOnExistingObjectsAmplifier: added method on existing object"; assertTrue(new File(PATH_OUTPUT_TEST_CLASS).exists()); - assertFileContains(commentAndExpression); + assertFileContains(comment); } @Test diff --git a/dspot/src/test/java/eu/stamp_project/dspot/common/test_framework/TestFrameworkTest.java b/dspot/src/test/java/eu/stamp_project/dspot/common/test_framework/TestFrameworkTest.java index 26ff31571..40ebf9813 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/common/test_framework/TestFrameworkTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/common/test_framework/TestFrameworkTest.java @@ -30,11 +30,11 @@ public class TestFrameworkTest extends AbstractTestOnSample { @Before @Override public void setUp() { + super.setUp(); UserInput configuration = new UserInput(); configuration.setAbsolutePathToProjectRoot(new File("src/test/resources/sample/").getAbsolutePath()); DSpotUtils.init(CommentEnum.Amplifier, "target/dspot/", configuration .getFullClassPathWithExtraDependencies(), "src/test/resources/sample/"); - super.setUp(); } private CtMethod findAndRegister(String ctClass, String methodName) { @@ -137,6 +137,7 @@ public void testGenerateExpectedExceptionsBlock() { private final static String JUnit5WithExceptingThrown = "@org.junit.jupiter.api.Test" + AmplificationHelper.LINE_SEPARATOR + "public void testExpectAnException_failAssert0() {" + AmplificationHelper.LINE_SEPARATOR + + " // AssertionGenerator generate try/catch block with fail statement" + AmplificationHelper.LINE_SEPARATOR + " org.junit.jupiter.api.Assertions.assertThrows(java.lang.RuntimeException.class, () -> {" + AmplificationHelper.LINE_SEPARATOR + " org.junit.jupiter.api.Assertions.assertTrue(true);" + AmplificationHelper.LINE_SEPARATOR + " throwAnException();" + AmplificationHelper.LINE_SEPARATOR + diff --git a/dspot/src/test/resources/test-projects/build.gradle b/dspot/src/test/resources/test-projects/build.gradle index 32ed9852a..579dd9a2c 100644 --- a/dspot/src/test/resources/test-projects/build.gradle +++ b/dspot/src/test/resources/test-projects/build.gradle @@ -17,4 +17,4 @@ dependencies { implementation 'org.apache.logging.log4j:log4j-api:2.8.2' implementation 'org.apache.logging.log4j:log4j-core:2.8.2' testImplementation 'junit:junit:4.12' -} \ No newline at end of file +} diff --git a/dspot/src/test/resources/test-projects/src/test/java/example/TestSuiteExample2.java b/dspot/src/test/resources/test-projects/src/test/java/example/TestSuiteExample2.java index f9d402357..c448db12c 100644 --- a/dspot/src/test/resources/test-projects/src/test/java/example/TestSuiteExample2.java +++ b/dspot/src/test/resources/test-projects/src/test/java/example/TestSuiteExample2.java @@ -18,7 +18,8 @@ public void test3() { public void test4() { example.Example ex = new example.Example(); java.lang.String s = "abcd"; - org.junit.Assert.assertEquals('d', ex.charAt(s, 12)); + char findChar = ex.charAt(s, 12); + org.junit.Assert.assertEquals('d', findChar); } @org.junit.Test @@ -42,7 +43,8 @@ public void test9() { @org.junit.Test public void test2() { example.Example ex = new example.Example(); - org.junit.Assert.assertEquals('d', ex.charAt("abcd", 3)); + char findChar = ex.charAt("abcd", 3); + org.junit.Assert.assertEquals('d', findChar); } }