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 d10e8dfb5..2cb1d6e91 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 @@ -63,8 +63,8 @@ public List> assertionAmplification(CtType testClass, List> testsWithoutAssertions = removeAssertions(tests,cloneClass); + testClass.getPackage().addType(cloneClass); + List> testsWithoutAssertions = removeAssertions(tests, cloneClass); // set up methodReconstructor for use in assertPassingAndFailingTests this.methodReconstructor = new MethodReconstructor( @@ -81,7 +81,7 @@ public List> assertionAmplification(CtType testClass, List> removeAssertions(List> tests,CtType cloneClass){ + private List> removeAssertions(List> tests, CtType cloneClass){ List> testsWithoutAssertions = tests.stream() .map(this.assertionRemover::removeAssertion) .collect(Collectors.toList()); @@ -120,7 +120,8 @@ private List> assertPassingAndFailingTests(CtType testClass, List> assertPassingAndFailingTests(CtType testClass, List> addAssertionsOnPassingTests(TestResult testResult,List> tests,CtType testClass){ + private List> addAssertionsOnPassingTests(TestResult testResult,List> tests, CtType testClass){ final List> generatedTestWithAssertion = new ArrayList<>(); final List passingTestsName = testResult.getPassingTests(); if (!passingTestsName.isEmpty()) { @@ -145,8 +146,7 @@ private List> addAssertionsOnPassingTests(TestResult testResult,List passingTestsName.stream() .anyMatch(passingTestName -> checkMethodName(ctMethod.getSimpleName(), passingTestName)) ).collect(Collectors.toList()); - List> passingTests = this.methodReconstructor.addAssertions(testClass, - passingTestMethods) + List> passingTests = this.methodReconstructor.addAssertions(testClass, passingTestMethods) .stream() .filter(Objects::nonNull) .collect(Collectors.toList()); diff --git a/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/methodreconstructor/AssertionSyntaxBuilder.java b/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/methodreconstructor/AssertionSyntaxBuilder.java index 285786c77..0eafe798a 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/methodreconstructor/AssertionSyntaxBuilder.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/assertiongenerator/assertiongenerator/methodreconstructor/AssertionSyntaxBuilder.java @@ -10,7 +10,6 @@ import spoon.reflect.code.CtInvocation; import spoon.reflect.code.CtLiteral; import spoon.reflect.code.CtStatement; -import spoon.reflect.code.CtVariableAccess; import spoon.reflect.declaration.*; import spoon.reflect.factory.Factory; import spoon.reflect.reference.CtExecutableReference; @@ -165,10 +164,8 @@ private static void addPrimitiveMap(List invocations,CtMethod te // TODO we need maybe limit assertion on a limited number of elements @SuppressWarnings("unchecked") private static List> buildSnippetAssertCollection(Factory factory, CtMethod testMethod, String expression, Collection value) { - final CtVariableAccess variableRead = factory.createVariableRead( - factory.createLocalVariableReference().setSimpleName(expression), - false - ); + final CtExpression variableRead = new Translator(factory).translate(expression); + final CtExecutableReference contains = factory.Type().get(Collection.class).getMethodsByName("contains").get(0).getReference(); return (List>) value.stream() .limit(Math.min(value.size(), MAX_NUMBER_OF_CHECKED_ELEMENT_IN_LIST)) @@ -176,8 +173,11 @@ private static List> buildSnippetAssertCollection(Factory factor .map(o -> TestFramework.get().buildInvocationToAssertion( testMethod, AssertEnum.ASSERT_TRUE, - Collections.singletonList(factory.createInvocation(variableRead, - contains, (CtLiteral) o + Collections.singletonList( + factory.createInvocation( + variableRead, + contains, + (CtLiteral) o ) ) ) @@ -188,10 +188,7 @@ private static List> buildSnippetAssertCollection(Factory factor // TODO we need maybe limit assertion on a limited number of elements @SuppressWarnings("unchecked") private static List> buildSnippetAssertMap(Factory factory, CtMethod testMethod, String expression, Map value) { - final CtVariableAccess variableRead = factory.createVariableRead( - factory.createLocalVariableReference().setSimpleName(expression), - false - ); + final CtExpression variableRead = new Translator(factory).translate(expression); final CtExecutableReference containsKey = factory.Type().get(Map.class).getMethodsByName("containsKey").get(0).getReference(); final CtExecutableReference get = factory.Type().get(Map.class).getMethodsByName("get").get(0).getReference(); return (List>) value.keySet().stream() diff --git a/dspot/src/main/java/eu/stamp_project/dspot/common/compilation/TestCompiler.java b/dspot/src/main/java/eu/stamp_project/dspot/common/compilation/TestCompiler.java index 73a758592..5ab5f357d 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/common/compilation/TestCompiler.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/common/compilation/TestCompiler.java @@ -138,7 +138,7 @@ public TestResult compileAndRun(CtType testClass, throw new AmplificationException(e); } } - }else { + } else { //Delete junit-platform.properties if exits if (TestFramework.isJUnit5(testsToRun.get(0))) { String rootPath = classPath.split(":")[0]; @@ -173,8 +173,8 @@ public TestResult compileAndRun(CtType testClass, * This Exception is not thrown when the compilation fails but rather when the arguments are wrong. */ public List> compileAndDiscardUncompilableMethods(DSpotCompiler compiler, - CtType testClassToBeCompiled, - List> testsToRun) throws AmplificationException { + CtType testClassToBeCompiled, + List> testsToRun) throws AmplificationException { final List> uncompilableMethod = compileAndDiscardUncompilableMethods(compiler, testClassToBeCompiled, 0); testsToRun.removeAll(uncompilableMethod); uncompilableMethod.forEach(testClassToBeCompiled::removeMethod); @@ -260,13 +260,17 @@ private static CtClass getNewModelCtClass(String pathToSrcFolder, String full // output the .java of the test class to be compiled // this method delete also the old .class, i.e. the old compiled file of the same test class, if exists - private static void printJavaFileAndDeleteClassFile(DSpotCompiler compiler, CtType classTest) { + private static void printJavaFileAndDeleteClassFile(DSpotCompiler compiler, CtType classTest) { try { +// final String pathToJavaFile = +// compiler.getSourceOutputDirectory().getAbsolutePath() + "/" +// + classTest.getQualifiedName().replaceAll("\\.", "/") + ".java"; +// forceDelete(pathToJavaFile); DSpotUtils.printCtTypeToGivenDirectory(classTest, compiler.getSourceOutputDirectory()); } catch (Exception e) { throw new RuntimeException(e); } - String pathToDotClass = + final String pathToDotClass = compiler.getBinaryOutputDirectory().getAbsolutePath() + "/" + classTest.getQualifiedName().replaceAll("\\.", "/") + ".class"; try { diff --git a/dspot/src/main/java/eu/stamp_project/dspot/common/miscellaneous/AmplificationHelper.java b/dspot/src/main/java/eu/stamp_project/dspot/common/miscellaneous/AmplificationHelper.java index ae8fbf5e8..f43a4c39b 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/common/miscellaneous/AmplificationHelper.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/common/miscellaneous/AmplificationHelper.java @@ -8,13 +8,10 @@ import org.slf4j.LoggerFactory; import spoon.reflect.code.CtComment; import spoon.reflect.code.CtLiteral; -import spoon.reflect.declaration.CtImport; import spoon.reflect.declaration.CtMethod; import spoon.reflect.declaration.CtPackage; import spoon.reflect.declaration.CtType; import spoon.reflect.reference.CtTypeReference; -import spoon.reflect.visitor.ImportScanner; -import spoon.reflect.visitor.ImportScannerImpl; import spoon.reflect.visitor.Query; import spoon.reflect.visitor.filter.TypeFilter; @@ -89,7 +86,6 @@ public static CtType createAmplifiedTest(List> ampTest, CtType if (!shouldKeepOriginalTestMethods) { classTest.getMethods().stream() .filter(TestFramework.get()::isTest) - //.filter(AmplificationChecker::isTest) .forEach(currentTestClass::removeMethod); } return currentTestClass; @@ -138,10 +134,6 @@ public static CtMethod removeAmpTestParent(CtMethod amplifiedTest) { return ampTestToParent.remove(amplifiedTest); } - public static int getAmpTestParentSize() { - return ampTestToParent.size(); - } - public static void addTestBindingToOriginal(CtMethod clonedTest, CtMethod fromTest) { CtMethod originalTest = fromTest; if (originalTestBindings.containsKey(fromTest)) { @@ -150,22 +142,10 @@ public static void addTestBindingToOriginal(CtMethod clonedTest, CtMethod fromTe originalTestBindings.put(clonedTest, originalTest); } - public static void removeTestBindingToOriginal(CtMethod clonedTest) { - originalTestBindings.remove(clonedTest); - } - public static CtMethod getOriginalTestMethod(CtMethod clonedTest) { return originalTestBindings.get(clonedTest); } - public static int getTestBindingToOriginalSize() { - return originalTestBindings.size(); - } - - public static void resetTestBindingToOriginal() { - originalTestBindings.clear(); - } - @Deprecated public static Set computeClassProvider(CtType testClass) { List types = Query.getElements(testClass.getParent(CtPackage.class), new TypeFilter(CtType.class)); @@ -179,35 +159,9 @@ public static Set computeClassProvider(CtType testClass) { types.add(testClass.getParent(CtType.class)); } - types.addAll(types.stream() - .flatMap(type -> getImport(type).stream()) - .collect(Collectors.toSet())); - - return new HashSet<>(types); } - @Deprecated - public static Set getImport(CtType type) { - if (!AmplificationHelper.importByClass.containsKey(type)) { - ImportScanner importScanner = new ImportScannerImpl(); - try { - importScanner.computeImports(type); - Set set = importScanner.getAllImports() - .stream() - .map(CtImport::getReference) - .filter(Objects::nonNull) - .filter(ctElement -> ctElement instanceof CtType) - .map(ctElement -> (CtType) ctElement) - .collect(Collectors.toSet()); - AmplificationHelper.importByClass.put(type, set); - } catch (Exception e) { - AmplificationHelper.importByClass.put(type, new HashSet<>(0)); - } - } - return AmplificationHelper.importByClass.get(type); - } - @Deprecated public static List> getPassingTests(List> newTests, TestResult result) { final List passingTests = result.getPassingTests(); @@ -228,13 +182,4 @@ public static CtMethod addOriginInComment(CtMethod amplifiedTest, CtMethod return amplifiedTest; } - @Deprecated - public static CtMethod getTopParent(CtMethod test) { - CtMethod topParent; - CtMethod currentTest = test; - while ((topParent = getAmpTestParent(currentTest)) != null) { - currentTest = topParent; - } - return currentTest; - } } 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 78737305d..9ef3058d2 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 @@ -11,14 +11,8 @@ import spoon.Launcher; import spoon.compiler.Environment; import spoon.reflect.code.CtComment; -import spoon.reflect.declaration.CtClass; -import spoon.reflect.declaration.CtElement; -import spoon.reflect.declaration.CtMethod; -import spoon.reflect.declaration.CtType; -import spoon.reflect.declaration.ModifierKind; -import spoon.reflect.factory.Factory; +import spoon.reflect.declaration.*; import spoon.reflect.reference.CtTypeReference; -import spoon.reflect.visitor.DefaultJavaPrettyPrinter; import spoon.support.JavaOutputProcessor; import java.io.*; @@ -80,14 +74,13 @@ public static void printCtTypeToGivenDirectory(CtType type, File directory) { } public static void printCtTypeToGivenDirectory(CtType type, File directory, boolean autoImports) { + Environment env = type.getFactory().getEnvironment(); try { - Factory factory = type.getFactory(); - Environment env = factory.getEnvironment(); env.setAutoImports(autoImports); env.setNoClasspath(true); env.setCommentEnabled(DSpotUtils.withComment); - JavaOutputProcessor processor = new JavaOutputProcessor(new DefaultJavaPrettyPrinter(env)); - processor.setFactory(factory); + JavaOutputProcessor processor = new JavaOutputProcessor(env.createPrettyPrinterAutoImport()); + processor.setFactory(type.getFactory()); processor.getEnvironment().setSourceOutputDirectory(directory); processor.createJavaFile(type); env.setAutoImports(false); diff --git a/dspot/src/main/java/eu/stamp_project/dspot/common/report/output/selector/TestSelectorElementReportImpl.java b/dspot/src/main/java/eu/stamp_project/dspot/common/report/output/selector/TestSelectorElementReportImpl.java index 76c136433..8ac917b1a 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/common/report/output/selector/TestSelectorElementReportImpl.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/common/report/output/selector/TestSelectorElementReportImpl.java @@ -44,6 +44,10 @@ public String getReportForCollector() { @Override public String output(CtType testClass, String outputDirectory) { + final File outputDirectoryFile = new File(DSpotUtils.shouldAddSeparator.apply(outputDirectory)); + if (!outputDirectoryFile.exists()) { + outputDirectoryFile.mkdirs(); + } // 1 output the specific JSON file for the test class final File outputJsonFile = new File( DSpotUtils.shouldAddSeparator.apply(outputDirectory) + diff --git a/dspot/src/main/java/eu/stamp_project/dspot/common/test_framework/implementations/junit/JUnit3Support.java b/dspot/src/main/java/eu/stamp_project/dspot/common/test_framework/implementations/junit/JUnit3Support.java index fe2c7962c..a97a92b98 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/common/test_framework/implementations/junit/JUnit3Support.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/common/test_framework/implementations/junit/JUnit3Support.java @@ -1,14 +1,12 @@ package eu.stamp_project.dspot.common.test_framework.implementations.junit; -import junit.extensions.TestSetup; import junit.framework.Test; import junit.framework.TestSuite; +import spoon.reflect.code.CtConstructorCall; +import spoon.reflect.code.CtExpression; +import spoon.reflect.code.CtNewClass; import spoon.reflect.code.CtReturn; -import spoon.reflect.declaration.CtClass; -import spoon.reflect.declaration.CtElement; -import spoon.reflect.declaration.CtMethod; -import spoon.reflect.declaration.CtType; -import spoon.reflect.declaration.ModifierKind; +import spoon.reflect.declaration.*; import spoon.reflect.factory.Factory; import spoon.reflect.reference.CtTypeReference; @@ -83,20 +81,37 @@ public void generateAfterClassToSaveObservations(CtType testClass, List testSetupClass = factory.Class().create("junit.extensions.TestSetup"); - final CtReturn returnStatement = factory.createReturn(); - returnStatement.setReturnedExpression( - factory.Code().createNewClass( - factory.createCtTypeReference(TestSetup.class), + + CtParameter parameter = factory.createParameter(); + parameter.setType(factory.Type().createReference(Class.class)); + parameter.setSimpleName("test"); + + testSetupClass.addConstructor( + factory.createConstructor( testSetupClass, - factory.Code(). - createConstructorCall( - factory.createCtTypeReference(TestSuite.class), - factory.createCodeSnippetExpression(testClass.getQualifiedName() + ".class") - ) + Collections.emptySet(), + Collections.singletonList(parameter), + Collections.emptySet() ) ); + final CtExpression parameterValue = factory.createCodeSnippetExpression(testClass.getQualifiedName() + ".class"); + parameterValue.setType(factory.Type().createReference(Class.class)); + + final CtConstructorCall constructorCall = factory.createConstructorCall( + factory.createCtTypeReference(TestSuite.class), parameterValue + ); + + final CtNewClass testSetupNewClass = factory.Code().createNewClass(testSetupClass, parameterValue); + //testSetupNewClass.addArgument(constructorCall); + + final CtReturn returnStatement = factory.createReturn(); + returnStatement.setReturnedExpression(testSetupNewClass); suiteMethod.setBody(returnStatement); + + // create tearDown method final CtMethod tearDown = factory.createMethod(); tearDown.setModifiers( new HashSet<>(Collections.singletonList(ModifierKind.PROTECTED)) @@ -105,7 +120,9 @@ public void generateAfterClassToSaveObservations(CtType testClass, List> selectToKeep(List> amplifiedTestToBeKept) { return amplifiedTestToBeKept; } CtType clone = this.currentClassTestToBeAmplified.clone(); - clone.setParent(this.currentClassTestToBeAmplified.getParent()); + this.currentClassTestToBeAmplified.getPackage().addType(clone); this.currentClassTestToBeAmplified.getMethods().stream() .filter(TestFramework.get()::isTest) .forEach(clone::removeMethod); diff --git a/dspot/src/main/java/eu/stamp_project/dspot/selector/JacocoCoverageSelector.java b/dspot/src/main/java/eu/stamp_project/dspot/selector/JacocoCoverageSelector.java index 148d3d3b1..9e617825b 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/selector/JacocoCoverageSelector.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/selector/JacocoCoverageSelector.java @@ -192,8 +192,9 @@ public TestSelectorElementReport report() { //ignored } DSpotUtils.printCtTypeToGivenDirectory(clone, new File(DSpotCompiler.getPathToAmplifiedTestSrc())); - DSpotCompiler.compile(DSpotCompiler.getPathToAmplifiedTestSrc(), - this.classpath, + DSpotCompiler.compile( + DSpotCompiler.getPathToAmplifiedTestSrc(), + this.classpath + AmplificationHelper.PATH_SEPARATOR + this.targetClasses, new File(this.pathToTestClasses) ); try { diff --git a/dspot/src/main/java/eu/stamp_project/dspot/selector/PitMutantScoreSelector.java b/dspot/src/main/java/eu/stamp_project/dspot/selector/PitMutantScoreSelector.java index f1dc5acfc..0c62a44cd 100644 --- a/dspot/src/main/java/eu/stamp_project/dspot/selector/PitMutantScoreSelector.java +++ b/dspot/src/main/java/eu/stamp_project/dspot/selector/PitMutantScoreSelector.java @@ -162,6 +162,7 @@ public List> selectToKeep(List> amplifiedTestToBeKept) { // prepare clone of the test class CtType clone = this.currentClassTestToBeAmplified.clone(); + this.currentClassTestToBeAmplified.getPackage().addType(clone); clone.setParent(this.currentClassTestToBeAmplified.getParent()); // remove test methods from clone that are in original test class and add all amplified methods diff --git a/dspot/src/test/java/eu/stamp_project/MainTest.java b/dspot/src/test/java/eu/stamp_project/MainTest.java index 31a21c2e1..b90fdd806 100644 --- a/dspot/src/test/java/eu/stamp_project/MainTest.java +++ b/dspot/src/test/java/eu/stamp_project/MainTest.java @@ -226,7 +226,9 @@ public void testExample() throws Exception { try (BufferedReader reader = new BufferedReader(new FileReader(amplifiedTestClass))) { String content = reader.lines().collect(Collectors.joining(AmplificationHelper.LINE_SEPARATOR)); System.out.println(content); - assertTrue(content.startsWith(expectedAmplifiedTestClass)); + assertTrue( + content.startsWith(expectedAmplifiedTestClass) + ); assertTrue(new File("target/dspot/output/original/example/TestSuiteExample.java").exists()); } catch (IOException e) { throw new RuntimeException(e); @@ -235,19 +237,15 @@ public void testExample() throws Exception { //we don't test the whole file, but only the begin of it. It is sufficient to detect the auto import. private static final String expectedAmplifiedTestClass = "package example;" + AmplificationHelper.LINE_SEPARATOR + - AmplificationHelper.LINE_SEPARATOR + - AmplificationHelper.LINE_SEPARATOR + "import org.junit.Assert;" + AmplificationHelper.LINE_SEPARATOR + "import org.junit.Test;" + AmplificationHelper.LINE_SEPARATOR + - AmplificationHelper.LINE_SEPARATOR + - AmplificationHelper.LINE_SEPARATOR + "public class TestSuiteExample {" + AmplificationHelper.LINE_SEPARATOR + " @Test(timeout = 10000)" + AmplificationHelper.LINE_SEPARATOR + " public void test3_literalMutationString50_failAssert0() throws Exception {" + AmplificationHelper.LINE_SEPARATOR + " try {" + AmplificationHelper.LINE_SEPARATOR + " Example ex = new Example();" + AmplificationHelper.LINE_SEPARATOR + " String s = \"\";" + AmplificationHelper.LINE_SEPARATOR + - " ex.charAt(s, ((s.length()) - 1));" + AmplificationHelper.LINE_SEPARATOR + + " ex.charAt(s, s.length() - 1);" + AmplificationHelper.LINE_SEPARATOR + " Assert.fail(\"test3_literalMutationString50 should have thrown StringIndexOutOfBoundsException\");" + AmplificationHelper.LINE_SEPARATOR + " } catch (StringIndexOutOfBoundsException expected) {" + AmplificationHelper.LINE_SEPARATOR + " Assert.assertEquals(\"String index out of range: 0\", expected.getMessage());" + AmplificationHelper.LINE_SEPARATOR + diff --git a/dspot/src/test/java/eu/stamp_project/dspot/common/miscellaneous/AmplificationHelperTest.java b/dspot/src/test/java/eu/stamp_project/dspot/common/miscellaneous/AmplificationHelperTest.java index 28de8adf0..06cb86e7f 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/common/miscellaneous/AmplificationHelperTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/common/miscellaneous/AmplificationHelperTest.java @@ -64,26 +64,28 @@ public void testCreateAmplifiedTestClass() throws Exception { .map(CtMethod::clone) .peek(ctMethod -> ctMethod.setSimpleName("ampl" + ctMethod.getSimpleName())) .collect(Collectors.toList()); - CtType amplifiedTest = AmplificationHelper.renameTestClassUnderAmplification(AmplificationHelper.createAmplifiedTest(fakeAmplifiedMethod, classTest)); + CtType amplifiedTest = AmplificationHelper.createAmplifiedTest(fakeAmplifiedMethod, classTest); + CtType renamedAmplifiedTest = AmplificationHelper.renameTestClassUnderAmplification(amplifiedTest); - assertTrue(amplifiedTest.getSimpleName().contains("Ampl")); // (1) + assertTrue(renamedAmplifiedTest.getSimpleName().contains("Ampl")); // (1) - assertEquals(11, amplifiedTest.getMethods().size()); // (2) + assertEquals(11, renamedAmplifiedTest.getMethods().size()); // (2) assertEquals(11, classTest.getMethods().size());// (2) // (3) - assertFalse(classTest.getElements(new TypeFilter(CtTypeReference.class) { + assertTrue(classTest.getElements(new TypeFilter(CtTypeReference.class) { @Override public boolean matches(CtTypeReference element) { - return classTest.equals(element.getDeclaration()) && + return element.getDeclaration() != null && + renamedAmplifiedTest.toString().equals(element.getDeclaration().toString()) && super.matches(element); } }).isEmpty()); // (3) - assertTrue(amplifiedTest.getElements(new TypeFilter(CtTypeReference.class) { + assertFalse(renamedAmplifiedTest.getElements(new TypeFilter(CtTypeReference.class) { @Override public boolean matches(CtTypeReference element) { - return classTest.equals(element.getDeclaration()) && + return renamedAmplifiedTest.equals(element.getDeclaration()) && super.matches(element); } }).isEmpty()); @@ -117,7 +119,8 @@ public void testCreateAmplifiedTestClassWithOriginalTestMethod() { assertFalse(classTest.getElements(new TypeFilter(CtTypeReference.class) { @Override public boolean matches(CtTypeReference element) { - return classTest.equals(element.getDeclaration()) && + return element.getDeclaration() != null && + classTest.getQualifiedName().equals(element.getDeclaration().getQualifiedName()) && super.matches(element); } }).isEmpty()); diff --git a/dspot/src/test/java/eu/stamp_project/dspot/common/miscellaneous/DSpotUtilsTest.java b/dspot/src/test/java/eu/stamp_project/dspot/common/miscellaneous/DSpotUtilsTest.java index 2e420901e..d7c8b5ae2 100644 --- a/dspot/src/test/java/eu/stamp_project/dspot/common/miscellaneous/DSpotUtilsTest.java +++ b/dspot/src/test/java/eu/stamp_project/dspot/common/miscellaneous/DSpotUtilsTest.java @@ -15,9 +15,13 @@ import org.junit.Before; import org.junit.Test; import spoon.Launcher; +import spoon.compiler.Environment; +import spoon.reflect.declaration.CtClass; import spoon.reflect.declaration.CtMethod; import spoon.reflect.declaration.CtType; import spoon.reflect.factory.Factory; +import spoon.reflect.visitor.DefaultJavaPrettyPrinter; +import spoon.support.JavaOutputProcessor; import java.io.BufferedReader; import java.io.File; @@ -106,14 +110,13 @@ public void testWithLombokAnnotation() throws Exception { new BufferedReader(new FileReader(outputDirectory + "/fr/inria/lombok/LombokClassThatUseBuilderTest.java"))) { assertEquals( "package fr.inria.lombok;" + AmplificationHelper.LINE_SEPARATOR + - "" + AmplificationHelper.LINE_SEPARATOR + - "" + AmplificationHelper.LINE_SEPARATOR + + "import org.junit.Test;" + AmplificationHelper.LINE_SEPARATOR + "public class LombokClassThatUseBuilderTest {" + AmplificationHelper.LINE_SEPARATOR + - " @org.junit.Test" + AmplificationHelper.LINE_SEPARATOR + + " @Test" + AmplificationHelper.LINE_SEPARATOR + " public void test() {" + AmplificationHelper.LINE_SEPARATOR + - " fr.inria.lombok.LombokClassThatUseBuilder.builder().build();" + AmplificationHelper.LINE_SEPARATOR + + " LombokClassThatUseBuilder.builder().build();" + AmplificationHelper.LINE_SEPARATOR + " }" + AmplificationHelper.LINE_SEPARATOR + - "}" + AmplificationHelper.LINE_SEPARATOR, + "}", reader.lines() .collect(Collectors.joining(AmplificationHelper.LINE_SEPARATOR)) ); @@ -167,5 +170,55 @@ public void testPrintAmplifiedTestClass() throws Exception { assertEquals(nbMethodStart + 2, launcher.getFactory().Class().get("example.TestSuiteExample").getMethods().size()); } + @Test + public void test() { + + /* Testing scenario: + Build a model with a class + Clone this class + Modify the clone by adding a method + Print out the clone + Build a model using the printed clone + Compare the number of number between the outputted class and the cloned one + */ + + // build original class + Launcher launcher = new Launcher(); + launcher.addInputResource("src/test/java/eu/stamp_project/dspot/common/miscellaneous/DSpotUtilsTest.java"); + launcher.getEnvironment().setNoClasspath(true); + launcher.buildModel(); + final Factory factory = launcher.getFactory(); + + // clone + final CtClass compilationUnitPrintTest = factory.Class().get("eu.stamp_project.dspot.common.miscellaneous.DSpotUtilsTest"); + final CtClass clone = compilationUnitPrintTest.clone(); + compilationUnitPrintTest.getPackage().addType(clone); + assertEquals(5 , clone.getMethods().size()); + assertEquals(5 , compilationUnitPrintTest.getMethods().size()); + + // modification + CtMethod cloneMethod = ((CtMethod) clone.getMethodsByName("test").get(0)).clone(); + cloneMethod.setSimpleName("cloneTest"); + clone.addMethod(cloneMethod); + assertEquals(6 , clone.getMethods().size()); + assertEquals(5 , compilationUnitPrintTest.getMethods().size()); + + DSpotUtils.printCtTypeToGivenDirectory(clone, new File("target")); + + assertEquals(6 , clone.getMethods().size()); + assertEquals(5 , compilationUnitPrintTest.getMethods().size()); + // building now a new model from the java file outputted just before + launcher = new Launcher(); + launcher.addInputResource("target/eu/stamp_project/dspot/common/miscellaneous/DSpotUtilsTest.java"); + launcher.getEnvironment().setNoClasspath(true); + launcher.buildModel(); + + // compare the number of methods in the printed class and the clone, should be the same (2) + assertEquals( + clone.getMethods().size(), + launcher.getFactory().Class().get("eu.stamp_project.dspot.common.miscellaneous.DSpotUtilsTest").getMethods().size() + ); + + } } 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 2fc98f64f..6181a615c 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 @@ -59,7 +59,8 @@ public void testGenerateAfterClassToSaveObservations() { final CtType type = this.launcher.getFactory().Type().get("fr.inria.helper.SecondClassJUnit3"); TestFramework.get().generateAfterClassToSaveObservations(type, Collections.singletonList(testJUnit3)); final String expectedToString = "public static junit.framework.Test suite() {" + AmplificationHelper.LINE_SEPARATOR + - " return new junit.extensions.TestSetup(new junit.framework.TestSuite(fr.inria.helper.SecondClassJUnit3.class)) {" + AmplificationHelper.LINE_SEPARATOR + +// " return new junit.extensions.TestSetup(new junit.framework.TestSuite(fr.inria.helper.SecondClassJUnit3.class)) {" + AmplificationHelper.LINE_SEPARATOR + + " return new junit.extensions.TestSetup(fr.inria.helper.SecondClassJUnit3.class) {" + AmplificationHelper.LINE_SEPARATOR + " protected void tearDown() throws java.lang.Exception {" + AmplificationHelper.LINE_SEPARATOR + " eu.stamp_project.dspot.assertiongenerator.assertiongenerator.methodreconstructor.observer.testwithloggenerator.objectlogsyntaxbuilder_constructs.ObjectLog.save();" + AmplificationHelper.LINE_SEPARATOR + " }" + AmplificationHelper.LINE_SEPARATOR + diff --git a/pom.xml b/pom.xml index c70fce336..0492539f2 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ 1.8 - 7.3.0 + 8.1.0-SNAPSHOT 4.12 1.7.25 3.0.0