Skip to content

Commit

Permalink
chore: bump spoon version (#886)
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus authored Mar 5, 2020
1 parent 14cbb69 commit 81bd760
Show file tree
Hide file tree
Showing 15 changed files with 146 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public List<CtMethod<?>> assertionAmplification(CtType<?> testClass, List<CtMeth
return tests;
}
CtType cloneClass = testClass.clone();
cloneClass.setParent(testClass.getParent());
List<CtMethod<?>> testsWithoutAssertions = removeAssertions(tests,cloneClass);
testClass.getPackage().addType(cloneClass);
List<CtMethod<?>> testsWithoutAssertions = removeAssertions(tests, cloneClass);

// set up methodReconstructor for use in assertPassingAndFailingTests
this.methodReconstructor = new MethodReconstructor(
Expand All @@ -81,7 +81,7 @@ public List<CtMethod<?>> assertionAmplification(CtType<?> testClass, List<CtMeth
}

// remove existing assertions from cloned test methods
private List<CtMethod<?>> removeAssertions(List<CtMethod<?>> tests,CtType cloneClass){
private List<CtMethod<?>> removeAssertions(List<CtMethod<?>> tests, CtType cloneClass){
List<CtMethod<?>> testsWithoutAssertions = tests.stream()
.map(this.assertionRemover::removeAssertion)
.collect(Collectors.toList());
Expand Down Expand Up @@ -120,7 +120,8 @@ private List<CtMethod<?>> assertPassingAndFailingTests(CtType testClass, List<Ct

//Add parallel test execution support (JUnit4, JUnit5) for execution method (CMD, Maven)
CloneHelper.addParallelExecutionAnnotation(testClass, tests);
testResult = this.testCompiler.compileAndRun(testClass,
testResult = this.testCompiler.compileAndRun(
testClass,
this.compiler,
tests
);
Expand All @@ -135,7 +136,7 @@ private List<CtMethod<?>> assertPassingAndFailingTests(CtType testClass, List<Ct
return generatedTestWithAssertion;
}

private List<CtMethod<?>> addAssertionsOnPassingTests(TestResult testResult,List<CtMethod<?>> tests,CtType testClass){
private List<CtMethod<?>> addAssertionsOnPassingTests(TestResult testResult,List<CtMethod<?>> tests, CtType testClass){
final List<CtMethod<?>> generatedTestWithAssertion = new ArrayList<>();
final List<String> passingTestsName = testResult.getPassingTests();
if (!passingTestsName.isEmpty()) {
Expand All @@ -145,8 +146,7 @@ private List<CtMethod<?>> addAssertionsOnPassingTests(TestResult testResult,List
passingTestsName.stream()
.anyMatch(passingTestName -> checkMethodName(ctMethod.getSimpleName(), passingTestName))
).collect(Collectors.toList());
List<CtMethod<?>> passingTests = this.methodReconstructor.addAssertions(testClass,
passingTestMethods)
List<CtMethod<?>> passingTests = this.methodReconstructor.addAssertions(testClass, passingTestMethods)
.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,19 +164,20 @@ private static void addPrimitiveMap(List<CtStatement> invocations,CtMethod<?> te
// TODO we need maybe limit assertion on a limited number of elements
@SuppressWarnings("unchecked")
private static List<CtInvocation<?>> 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<CtInvocation<?>>) value.stream()
.limit(Math.min(value.size(), MAX_NUMBER_OF_CHECKED_ELEMENT_IN_LIST))
.map(factory::createLiteral)
.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
)
)
)
Expand All @@ -188,10 +188,7 @@ private static List<CtInvocation<?>> buildSnippetAssertCollection(Factory factor
// TODO we need maybe limit assertion on a limited number of elements
@SuppressWarnings("unchecked")
private static List<CtInvocation<?>> 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<CtInvocation<?>>) value.keySet().stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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<CtMethod<?>> compileAndDiscardUncompilableMethods(DSpotCompiler compiler,
CtType<?> testClassToBeCompiled,
List<CtMethod<?>> testsToRun) throws AmplificationException {
CtType<?> testClassToBeCompiled,
List<CtMethod<?>> testsToRun) throws AmplificationException {
final List<CtMethod<?>> uncompilableMethod = compileAndDiscardUncompilableMethods(compiler, testClassToBeCompiled, 0);
testsToRun.removeAll(uncompilableMethod);
uncompilableMethod.forEach(testClassToBeCompiled::removeMethod);
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -89,7 +86,6 @@ public static CtType<?> createAmplifiedTest(List<CtMethod<?>> ampTest, CtType<?>
if (!shouldKeepOriginalTestMethods) {
classTest.getMethods().stream()
.filter(TestFramework.get()::isTest)
//.filter(AmplificationChecker::isTest)
.forEach(currentTestClass::removeMethod);
}
return currentTestClass;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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<CtType> computeClassProvider(CtType testClass) {
List<CtType> types = Query.getElements(testClass.getParent(CtPackage.class), new TypeFilter(CtType.class));
Expand All @@ -179,35 +159,9 @@ public static Set<CtType> 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<CtType> getImport(CtType type) {
if (!AmplificationHelper.importByClass.containsKey(type)) {
ImportScanner importScanner = new ImportScannerImpl();
try {
importScanner.computeImports(type);
Set<CtType> 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<CtMethod<?>> getPassingTests(List<CtMethod<?>> newTests, TestResult result) {
final List<String> passingTests = result.getPassingTests();
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) +
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -83,20 +81,37 @@ public void generateAfterClassToSaveObservations(CtType<?> testClass, List<CtMet
suiteMethod.setType(
factory.createCtTypeReference(Test.class)
);

// init TestSetup class
final CtClass<?> 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))
Expand All @@ -105,7 +120,9 @@ public void generateAfterClassToSaveObservations(CtType<?> testClass, List<CtMet
tearDown.addThrownType(factory.createCtTypeReference(Exception.class));
tearDown.setType(factory.Type().VOID_PRIMITIVE);
createCallToSaveAndInsertAtTheEnd(factory, tearDown);
testSetupClass.addMethod(tearDown);
// add tearDown method to the anonymous TestSetup Class
testSetupNewClass.getAnonymousClass().addMethod(tearDown);

testClass.addMethod(suiteMethod);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public List<CtMethod<?>> selectToKeep(List<CtMethod<?>> 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
Expand Down
Loading

0 comments on commit 81bd760

Please sign in to comment.