From 82066543a2b4a17619483778a763f39399718c04 Mon Sep 17 00:00:00 2001
From: Pavel Vojtechovsky
Date: Sun, 21 May 2017 18:34:56 +0200
Subject: [PATCH] test CtParameterRemoveRefactoring
---
.../refactoring/MethodsRefactoringTest.java | 74 +++++++++++++++++--
.../parameter/testclasses/TypeB.java | 9 +++
.../parameter/testclasses/TypeS.java | 12 +++
3 files changed, 90 insertions(+), 5 deletions(-)
diff --git a/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java b/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java
index a8b8f227839..9e011efd45b 100644
--- a/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java
+++ b/src/test/java/spoon/test/refactoring/MethodsRefactoringTest.java
@@ -3,6 +3,7 @@
import static org.junit.Assert.*;
import java.io.File;
+import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@@ -12,9 +13,14 @@
import org.junit.Test;
+import spoon.Launcher;
+import spoon.OutputType;
+import spoon.SpoonModelBuilder;
+import spoon.compiler.SpoonResourceHelper;
+import spoon.refactoring.CtParameterRemoveRefactoring;
+import spoon.refactoring.RefactoringException;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtStatement;
-import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtExecutable;
@@ -24,7 +30,6 @@
import spoon.reflect.reference.CtExecutableReference;
import spoon.reflect.visitor.filter.AllMethodsSameSignatureFunction;
import spoon.reflect.visitor.filter.ExecutableReferenceFilter;
-import spoon.reflect.visitor.filter.NameFilter;
import spoon.reflect.visitor.filter.SubInheritanceHierarchyFunction;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.refactoring.parameter.testclasses.IFaceB;
@@ -34,9 +39,7 @@
import spoon.test.refactoring.parameter.testclasses.TypeA;
import spoon.test.refactoring.parameter.testclasses.TypeB;
import spoon.test.refactoring.parameter.testclasses.TypeC;
-import spoon.test.refactoring.parameter.testclasses.TypeL;
import spoon.test.refactoring.parameter.testclasses.TypeR;
-import spoon.test.refactoring.parameter.testclasses.TypeS;
import spoon.testing.utils.ModelUtils;
public class MethodsRefactoringTest {
@@ -64,6 +67,7 @@ public void testSubInheritanceHierarchyFunction() {
"spoon.test.refactoring.parameter.testclasses.TypeB",
"spoon.test.refactoring.parameter.testclasses.TypeB$1",
"spoon.test.refactoring.parameter.testclasses.TypeB$1Local",
+ "spoon.test.refactoring.parameter.testclasses.TypeB$2",
"spoon.test.refactoring.parameter.testclasses.TypeC",
"spoon.test.refactoring.parameter.testclasses.IFaceL",
"spoon.test.refactoring.parameter.testclasses.TypeL",
@@ -300,4 +304,64 @@ private boolean removeSame(Collection list, Object item) {
}
return false;
}
-}
\ No newline at end of file
+
+ @Test
+ public void testCtParameterRemoveRefactoring() throws FileNotFoundException {
+ String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
+ final Launcher launcher = new Launcher();
+ launcher.getEnvironment().setNoClasspath(true);
+ SpoonModelBuilder comp = launcher.createCompiler();
+ comp.addInputSource(SpoonResourceHelper.createResource(new File("./src/test/java/"+testPackagePath)));
+ comp.build();
+ Factory factory = comp.getFactory();
+
+ CtType> typeA = factory.Class().get(TypeA.class);
+
+ CtMethod> methodTypeA_method1 = typeA.getMethodsByName("method1").get(0);
+ CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring();
+ refactor.setTarget(methodTypeA_method1.getParameters().get(0));
+ //check that expected methods are targets of refactoring
+ List> execs = refactor.getTargetExecutables();
+ execs.forEach(exec->{
+ //check that each to be modified method has one parameter
+ assertEquals(1, exec.getParameters().size());
+ });
+ refactor.refactor();
+ execs.forEach(exec->{
+ //check that each to be modified method has no parameter after refactoring
+ assertEquals(0, exec.getParameters().size());
+ });
+ launcher.setSourceOutputDirectory(new File("./target/spooned/"));
+ launcher.getModelBuilder().generateProcessedSourceFiles(OutputType.CLASSES);
+ ModelUtils.canBeBuilt("./target/spooned/"+testPackagePath, 8);
+ }
+ @Test
+ public void testCtParameterRemoveRefactoringValidationCheck() throws FileNotFoundException {
+ String testPackagePath = "spoon/test/refactoring/parameter/testclasses";
+ final Launcher launcher = new Launcher();
+ launcher.getEnvironment().setNoClasspath(true);
+ SpoonModelBuilder comp = launcher.createCompiler();
+ comp.addInputSource(SpoonResourceHelper.createResource(new File("./src/test/java/"+testPackagePath)));
+ comp.build();
+ Factory factory = comp.getFactory();
+
+ CtType> typeR = factory.Class().get(TypeR.class);
+
+ CtMethod> methodTypeR_method1 = typeR.getMethodsByName("method1").get(0);
+ CtParameterRemoveRefactoring refactor = new CtParameterRemoveRefactoring().setTarget(methodTypeR_method1.getParameters().get(0));
+ refactor.setTarget(methodTypeR_method1.getParameters().get(0));
+ //check that each to be refactored method has one parameter
+ List> execs = refactor.getTargetExecutables();
+ execs.forEach(exec->{
+ //check that each to be modified method has one parameter
+ assertEquals(1, exec.getParameters().size());
+ });
+ //try refactor
+ try {
+ refactor.refactor();
+ fail();
+ } catch (RefactoringException e) {
+ this.getClass();
+ }
+ }
+}
diff --git a/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeB.java b/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeB.java
index c6650cc779a..9ce22799e09 100644
--- a/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeB.java
+++ b/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeB.java
@@ -39,4 +39,13 @@ public void method1(Double p1) {
}
}
}
+ private void anMethodWithNullParameterValue() {
+ IFaceB ifaceB = new IFaceB() {
+ @Override
+ @TestHierarchy("A_method1")
+ public void method1(String p1) {
+ }
+ };
+ ifaceB.method1(null);
+ }
}
diff --git a/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeS.java b/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeS.java
index d0bd3321156..ab225c9c900 100644
--- a/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeS.java
+++ b/src/test/java/spoon/test/refactoring/parameter/testclasses/TypeS.java
@@ -21,4 +21,16 @@ private void methodWithLambdaOf_R() {
};
ifaceT.method1(1.0);
}
+
+ private void methodWithComplexExpression_R() {
+ IFaceT ifaceT = p->{
+ @TestHierarchy("R_method1")
+ int x;
+ };
+ /*
+ * the refactoring should by default report calling of method as problem, because it cannot know,
+ * whether that method call has side effects. Such method call remove might cause malfunction.
+ */
+ ifaceT.method1(Math.abs(1.0));
+ }
}