|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * <p> |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * <p> |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * <p> |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.java.testing.junitassertj; |
| 17 | + |
| 18 | +import org.openrewrite.AutoConfigure; |
| 19 | +import org.openrewrite.java.AutoFormat; |
| 20 | +import org.openrewrite.java.JavaIsoRefactorVisitor; |
| 21 | +import org.openrewrite.java.MethodMatcher; |
| 22 | +import org.openrewrite.java.tree.*; |
| 23 | + |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import static org.openrewrite.java.tree.MethodTypeBuilder.newMethodType; |
| 27 | + |
| 28 | +/** |
| 29 | + * This is a refactoring visitor that will convert JUnit-style assertArrayEquals() to assertJ's assertThat().containsExactly(). |
| 30 | + * |
| 31 | + * This visitor will handle the following JUnit 5 method signatures: |
| 32 | + * |
| 33 | + * <PRE> |
| 34 | + * Two parameter variants: |
| 35 | + * |
| 36 | + * assertArrayEquals(expected,actual) -> assertThat(actual).containsExactly(expected) |
| 37 | + * |
| 38 | + * Three parameter variant where the third argument is a String: |
| 39 | + * |
| 40 | + * assertArrayEquals(expected, actual, "message") -> assertThat(actual).as("message").containsExactly(expected) |
| 41 | + * |
| 42 | + * Three parameter variant where the third argument is a String supplier: |
| 43 | + * |
| 44 | + * assertArrayEquals(expected, actual, () -> "message") -> assertThat(actual).withFailureMessage("message").containsExactly(expected) |
| 45 | + * |
| 46 | + * Three parameter variant where args are all floating point numbers. |
| 47 | + * |
| 48 | + * assertArrayEquals(expected, actual, delta) -> assertThat(actual).containsExactly(expected, within(delta)); |
| 49 | + * |
| 50 | + * Four parameter variant when comparing floating point numbers with a delta and a message: |
| 51 | + * |
| 52 | + * assertArrayEquals(expected, actual, delta, "message") -> assertThat(actual).withFailureMessage("message").containsExactly(expected, within(delta)); |
| 53 | + * |
| 54 | + * </PRE> |
| 55 | + */ |
| 56 | +@AutoConfigure |
| 57 | +public class AssertArrayEqualsToAssertThat extends JavaIsoRefactorVisitor { |
| 58 | + |
| 59 | + private static final String JUNIT_QUALIFIED_ASSERTIONS_CLASS_NAME = "org.junit.jupiter.api.Assertions"; |
| 60 | + |
| 61 | + private static final String ASSERTJ_QUALIFIED_ASSERTIONS_CLASS_NAME = "org.assertj.core.api.Assertions"; |
| 62 | + private static final String ASSERTJ_ASSERT_THAT_METHOD_NAME = "assertThat"; |
| 63 | + private static final String ASSERTJ_WITHIN_METHOD_NAME = "within"; |
| 64 | + |
| 65 | + /** |
| 66 | + * This matcher finds the junit methods that will be migrated by this visitor. |
| 67 | + */ |
| 68 | + private static final MethodMatcher JUNIT_ASSERT_EQUALS_MATCHER = new MethodMatcher( |
| 69 | + JUNIT_QUALIFIED_ASSERTIONS_CLASS_NAME + " assertArrayEquals(..)" |
| 70 | + ); |
| 71 | + |
| 72 | + private static final JavaType ASSERTJ_ASSERTIONS_WILDCARD_STATIC_IMPORT = newMethodType() |
| 73 | + .declaringClass("org.assertj.core.api.Assertions") |
| 74 | + .flags(Flag.Public, Flag.Static) |
| 75 | + .name("*") |
| 76 | + .build(); |
| 77 | + |
| 78 | + public AssertArrayEqualsToAssertThat() { |
| 79 | + setCursoringOn(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method) { |
| 84 | + |
| 85 | + J.MethodInvocation original = super.visitMethodInvocation(method); |
| 86 | + |
| 87 | + if (!JUNIT_ASSERT_EQUALS_MATCHER.matches(method)) { |
| 88 | + return original; |
| 89 | + } |
| 90 | + |
| 91 | + List<Expression> originalArgs = original.getArgs().getArgs(); |
| 92 | + |
| 93 | + Expression expected = originalArgs.get(0); |
| 94 | + Expression actual = originalArgs.get(1); |
| 95 | + |
| 96 | + J.MethodInvocation replacement; |
| 97 | + if (originalArgs.size() == 2) { |
| 98 | + //assertThat(actual).isEqualTo(expected) |
| 99 | + replacement = assertSimple(actual, expected); |
| 100 | + } else if (originalArgs.size() == 3 && !isFloatingPointType(originalArgs.get(2))) { |
| 101 | + //assertThat(actual).as(message).isEqualTo(expected) |
| 102 | + replacement = assertWithMessage(actual, expected, originalArgs.get(2)); |
| 103 | + } else if (originalArgs.size() == 3) { |
| 104 | + //assert is using floating points with a delta and no message. |
| 105 | + replacement = assertFloatingPointDelta(actual, expected, originalArgs.get(2)); |
| 106 | + maybeAddImport(ASSERTJ_QUALIFIED_ASSERTIONS_CLASS_NAME, ASSERTJ_WITHIN_METHOD_NAME); |
| 107 | + |
| 108 | + } else { |
| 109 | + //The assertEquals is using a floating point with a delta argument and a message. |
| 110 | + replacement = assertFloatingPointDeltaWithMessage(actual, expected, originalArgs.get(2), originalArgs.get(3)); |
| 111 | + maybeAddImport(ASSERTJ_QUALIFIED_ASSERTIONS_CLASS_NAME, ASSERTJ_WITHIN_METHOD_NAME); |
| 112 | + } |
| 113 | + |
| 114 | + //Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" |
| 115 | + maybeAddImport(ASSERTJ_QUALIFIED_ASSERTIONS_CLASS_NAME, ASSERTJ_ASSERT_THAT_METHOD_NAME); |
| 116 | + //And if there are no longer references to the JUnit assertions class, we can remove the import. |
| 117 | + maybeRemoveImport(JUNIT_QUALIFIED_ASSERTIONS_CLASS_NAME); |
| 118 | + |
| 119 | + //Format the replacement method invocation in the context of where it is called. |
| 120 | + andThen(new AutoFormat(replacement)); |
| 121 | + return replacement; |
| 122 | + } |
| 123 | + |
| 124 | + private J.MethodInvocation assertSimple(Expression actual, Expression expected) { |
| 125 | + |
| 126 | + List<J.MethodInvocation> statements = treeBuilder.buildSnippet(getCursor(), |
| 127 | + String.format("assertThat(%s).containsExactly(%s);", actual.printTrimmed(), expected.printTrimmed()), |
| 128 | + ASSERTJ_ASSERTIONS_WILDCARD_STATIC_IMPORT |
| 129 | + ); |
| 130 | + return statements.get(0); |
| 131 | + } |
| 132 | + |
| 133 | + private J.MethodInvocation assertWithMessage(Expression actual, Expression expected, Expression message) { |
| 134 | + |
| 135 | + // In assertJ the "as" method has a more informative error message, but doesn't accept String suppliers |
| 136 | + // so we're using "as" if the message is a string and "withFailMessage" if it is a supplier. |
| 137 | + String messageAs = TypeUtils.isString(message.getType())?"as":"withFailMessage"; |
| 138 | + |
| 139 | + List<J.MethodInvocation> statements = treeBuilder.buildSnippet(getCursor(), |
| 140 | + String.format("assertThat(%s).%s(%s).containsExactly(%s);", |
| 141 | + actual.printTrimmed(), messageAs, message.printTrimmed(), expected.printTrimmed()), |
| 142 | + ASSERTJ_ASSERTIONS_WILDCARD_STATIC_IMPORT |
| 143 | + ); |
| 144 | + return statements.get(0); |
| 145 | + } |
| 146 | + |
| 147 | + private J.MethodInvocation assertFloatingPointDelta(Expression actual, Expression expected, Expression delta) { |
| 148 | + List<J.MethodInvocation> statements = treeBuilder.buildSnippet(getCursor(), |
| 149 | + String.format("assertThat(%s).containsExactly(%s, within(%s));", |
| 150 | + actual.printTrimmed(), expected.printTrimmed(), delta.printTrimmed()), |
| 151 | + ASSERTJ_ASSERTIONS_WILDCARD_STATIC_IMPORT |
| 152 | + ); |
| 153 | + return statements.get(0); |
| 154 | + } |
| 155 | + |
| 156 | + private J.MethodInvocation assertFloatingPointDeltaWithMessage(Expression actual, Expression expected, |
| 157 | + Expression delta, Expression message) { |
| 158 | + |
| 159 | + //If the message is a string use "as", if it is a supplier use "withFailMessage" |
| 160 | + String messageAs = TypeUtils.isString(message.getType())?"as":"withFailMessage"; |
| 161 | + |
| 162 | + List<J.MethodInvocation> statements = treeBuilder.buildSnippet(getCursor(), |
| 163 | + String.format("assertThat(%s).%s(%s).containsExactly(%s, within(%s));", |
| 164 | + actual.printTrimmed(), messageAs, message.printTrimmed(), expected.printTrimmed(), delta.printTrimmed()), |
| 165 | + ASSERTJ_ASSERTIONS_WILDCARD_STATIC_IMPORT |
| 166 | + ); |
| 167 | + return statements.get(0); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * Returns true if the expression's type is either a primitive float/double or their object forms Float/Double |
| 172 | + * |
| 173 | + * @param expression The expression parsed from the original AST. |
| 174 | + * @return true if the type is a floating point number. |
| 175 | + */ |
| 176 | + private boolean isFloatingPointType(Expression expression) { |
| 177 | + |
| 178 | + JavaType.FullyQualified fullyQualified = TypeUtils.asFullyQualified(expression.getType()); |
| 179 | + if (fullyQualified != null) { |
| 180 | + String typeName = fullyQualified.getFullyQualifiedName(); |
| 181 | + return (typeName.equals("java.lang.Double") || typeName.equals("java.lang.Float")); |
| 182 | + } |
| 183 | + |
| 184 | + JavaType.Primitive parameterType = TypeUtils.asPrimitive(expression.getType()); |
| 185 | + return parameterType == JavaType.Primitive.Double || parameterType == JavaType.Primitive.Float; |
| 186 | + } |
| 187 | +} |
0 commit comments