From 7a3429edb5d8aaa9459999a9c8d940c35dadb12c Mon Sep 17 00:00:00 2001 From: Martin Wittlinger Date: Sun, 2 Jan 2022 21:57:33 +0100 Subject: [PATCH] feat(assertions): add reorder for changed parameter order --- .../junit/AssertionsTransformation.java | 35 ++++++++++++++++--- .../junit/ExpectedExceptionRemoval.java | 12 +++---- .../Junit4AnnotationsTransformation.java | 12 +++++++ 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/AssertionsTransformation.java b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/AssertionsTransformation.java index 64684ac7a..0a427666d 100644 --- a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/AssertionsTransformation.java +++ b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/AssertionsTransformation.java @@ -5,10 +5,12 @@ import java.util.List; import spoon.experimental.CtUnresolvedImport; +import spoon.reflect.code.CtExpression; import spoon.reflect.code.CtInvocation; import spoon.reflect.code.CtTypeAccess; import spoon.reflect.declaration.CtImport; import spoon.reflect.declaration.CtMethod; +import spoon.reflect.declaration.CtParameter; import spoon.reflect.declaration.CtType; import spoon.reflect.reference.CtExecutableReference; import spoon.reflect.reference.CtReference; @@ -56,6 +58,24 @@ private void convertToJunit5(List> junit4Asserts) { junit4Assert.setTarget(null); junit4Assert.getExecutable() .setDeclaringType(getFactory().Type().createReference("org.junit.jupiter.api.Assertions")); + List> parameters = junit4Assert.getArguments(); + if (parameters.size() == 3) { + if (parameters.get(0).getType().getSimpleName().equals("String")) { + List> newParameters = new ArrayList<>(); + newParameters.add(parameters.get(1)); + newParameters.add(parameters.get(2)); + newParameters.add(parameters.get(0)); + junit4Assert.setArguments(newParameters); + } + } + if (parameters.size() == 2 && junit4Assert.getExecutable().getSimpleName().equals("assertTrue")) { + if (parameters.get(0).getType().getSimpleName().equals("String")) { + List> newParameters = new ArrayList<>(); + newParameters.add(parameters.get(1)); + newParameters.add(parameters.get(0)); + junit4Assert.setArguments(newParameters); + } + } } } @@ -68,6 +88,7 @@ private List> getJunit4Asserts(CtMethod method) { .filter(v -> ((CtTypeAccess) v.getTarget()).getAccessedType() .getQualifiedName() .equals("org.junit.Assert")) + .filter(v -> !v.getExecutable().getSimpleName().equals("assertThat")) .toList(); } @@ -81,9 +102,11 @@ private void adjustImports(CtMethod method) { public void visitUnresolvedImport(CtUnresolvedImport ctUnresolvedImport) { if (ctUnresolvedImport.getUnresolvedReference().startsWith("org.junit.Assert.")) { imports.add(ctUnresolvedImport); - newImports.add(getFactory().createUnresolvedImport(ctUnresolvedImport.getUnresolvedReference() - .replace("org.junit.Assert.", "org.junit.jupiter.api.Assertions."), - true)); + if (!ctUnresolvedImport.getUnresolvedReference().endsWith("assertThat")) { + newImports.add(getFactory().createUnresolvedImport(ctUnresolvedImport.getUnresolvedReference() + .replace("org.junit.Assert.", "org.junit.jupiter.api.Assertions."), + true)); + } } } @@ -91,8 +114,10 @@ public void visitUnresolvedImport(CtUnresolvedImport ctUnresolvedImport) { public void visitMethodImport(CtExecutableReference executableReference) { if (executableReference.getDeclaringType().getQualifiedName().equals("org.junit.Assert")) { references.add(executableReference); - newImports.add(getFactory().createUnresolvedImport( - "org.junit.jupiter.api.Assertions." + executableReference.getSimpleName(), true)); + if (!executableReference.getSimpleName().equals("assertThat")) { + newImports.add(getFactory().createUnresolvedImport( + "org.junit.jupiter.api.Assertions." + executableReference.getSimpleName(), true)); + } } } })); diff --git a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/ExpectedExceptionRemoval.java b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/ExpectedExceptionRemoval.java index be416f0f6..3eb920b5d 100644 --- a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/ExpectedExceptionRemoval.java +++ b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/ExpectedExceptionRemoval.java @@ -51,13 +51,11 @@ public void process(CtMethod method) { } private void removeExpectedValue(CtAnnotation testAnnotation) { - testAnnotation - .setValues(testAnnotation - .getValues() - .entrySet() - .stream() - .filter(v -> !v.getKey().equals("expected")) - .collect(Collectors.toMap(Entry::getKey, Entry::getValue))); + testAnnotation.setValues(testAnnotation.getValues() + .entrySet() + .stream() + .filter(v -> !v.getKey().equals("expected")) + .collect(Collectors.toMap(Entry::getKey, Entry::getValue))); } private CtInvocation createAssertThrows(CtExpression exceptionClass, CtStatement body) { diff --git a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/Junit4AnnotationsTransformation.java b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/Junit4AnnotationsTransformation.java index 476566f9c..9c16c6356 100644 --- a/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/Junit4AnnotationsTransformation.java +++ b/code-transformation/src/main/java/xyz/keksdose/spoon/code_solver/transformations/junit/Junit4AnnotationsTransformation.java @@ -7,6 +7,7 @@ import spoon.reflect.declaration.CtType; import xyz.keksdose.spoon.code_solver.history.Change; import xyz.keksdose.spoon.code_solver.history.ChangeListener; +import xyz.keksdose.spoon.code_solver.transformations.ImportHelper; import xyz.keksdose.spoon.code_solver.transformations.TransformationProcessor; public class Junit4AnnotationsTransformation extends TransformationProcessor> { @@ -27,6 +28,8 @@ public void process(CtMethod method) { private void refactorBeforeClass(CtMethod method) { Optional> beforeClassAnnotation = JunitHelper.getJunit4BeforeClassAnnotation(method); if (beforeClassAnnotation.isPresent()) { + ImportHelper.removeImport("org.junit.BeforeClass", method.getPosition().getCompilationUnit()); + ImportHelper.addImport("org.junit.jupiter.api.BeforeAll", false, method.getPosition().getCompilationUnit()); method.removeAnnotation(beforeClassAnnotation.get()); method.addAnnotation(JunitHelper.createBeforeAllAnnotation(getFactory())); setChanged(method.getParent(CtType.class), @@ -38,6 +41,9 @@ private void refactorBeforeClass(CtMethod method) { private void refactorBefore(CtMethod method) { Optional> beforeAnnotation = JunitHelper.getJunit4BeforeAnnotation(method); if (beforeAnnotation.isPresent()) { + ImportHelper.removeImport("org.junit.Before", method.getPosition().getCompilationUnit()); + ImportHelper.addImport("org.junit.jupiter.api.BeforeEach", false, + method.getPosition().getCompilationUnit()); method.removeAnnotation(beforeAnnotation.get()); method.addAnnotation(JunitHelper.createBeforeEachAnnotation(getFactory())); setChanged(method.getParent(CtType.class), new Change( @@ -49,6 +55,8 @@ private void refactorBefore(CtMethod method) { private void refactorAfter(CtMethod method) { Optional> afterAnnotation = JunitHelper.getJunit4AfterAnnotation(method); if (afterAnnotation.isPresent()) { + ImportHelper.removeImport("org.junit.After", method.getPosition().getCompilationUnit()); + ImportHelper.addImport("org.junit.jupiter.api.AfterEach", false, method.getPosition().getCompilationUnit()); method.removeAnnotation(afterAnnotation.get()); method.addAnnotation(JunitHelper.createAfterEachAnnotation(getFactory())); setChanged(method.getParent(CtType.class), @@ -61,6 +69,8 @@ private void refactorAfter(CtMethod method) { private void refactorAfterClass(CtMethod method) { Optional> afterAnnotation = JunitHelper.getJunit4AfterClassAnnotation(method); if (afterAnnotation.isPresent()) { + ImportHelper.removeImport("org.junit.AfterClass", method.getPosition().getCompilationUnit()); + ImportHelper.addImport("org.junit.jupiter.api.AfterAll", false, method.getPosition().getCompilationUnit()); method.removeAnnotation(afterAnnotation.get()); method.addAnnotation(JunitHelper.createAfterAllAnnotation(getFactory())); setChanged(method.getParent(CtType.class), new Change( @@ -72,6 +82,8 @@ private void refactorAfterClass(CtMethod method) { private void refactorIgnore(CtMethod method) { Optional> ignoreAnnotation = JunitHelper.getIgnoreAnnotation(method); if (ignoreAnnotation.isPresent()) { + ImportHelper.removeImport("org.junit.Ignore", method.getPosition().getCompilationUnit()); + ImportHelper.addImport("org.junit.jupiter.api.Disabled", false, method.getPosition().getCompilationUnit()); method.removeAnnotation(ignoreAnnotation.get()); method.addAnnotation(JunitHelper.createDisableAnnotation(getFactory())); setChanged(method.getParent(CtType.class),