|
| 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.junit5; |
| 17 | + |
| 18 | +import org.openrewrite.java.AddImport; |
| 19 | +import org.openrewrite.java.AutoFormat; |
| 20 | +import org.openrewrite.java.JavaIsoRefactorVisitor; |
| 21 | +import org.openrewrite.java.tree.Expression; |
| 22 | +import org.openrewrite.java.tree.J; |
| 23 | +import org.openrewrite.java.tree.JavaType; |
| 24 | +import org.openrewrite.java.tree.Statement; |
| 25 | +import org.openrewrite.java.tree.TypeUtils; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +import static java.util.Collections.singletonList; |
| 31 | + |
| 32 | +/** |
| 33 | + * Replace usages of JUnit 4's @Rule ExpectedException with JUnit 5 Assertions.assertThrows |
| 34 | + * |
| 35 | + * Currently only supports migration of this method from ExpectedException: void expect(Class<? extends Throwable> type) |
| 36 | + * Migrating the other methods of ExpectedException is not yet implemented. |
| 37 | + * |
| 38 | + */ |
| 39 | +public class ExpectedExceptionToAssertThrows extends JavaIsoRefactorVisitor { |
| 40 | + |
| 41 | + private static final String ExpectedExceptionFqn = "org.junit.rules.ExpectedException"; |
| 42 | + |
| 43 | + public ExpectedExceptionToAssertThrows() { |
| 44 | + setCursoringOn(); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public J.ClassDecl visitClassDecl(J.ClassDecl classDecl) { |
| 49 | + J.ClassDecl cd = super.visitClassDecl(classDecl); |
| 50 | + List<J.VariableDecls> expectedExceptionFields = classDecl.findFields(ExpectedExceptionFqn); |
| 51 | + if(expectedExceptionFields.size() > 0) { |
| 52 | + // Remove the ExpectedException fields |
| 53 | + List<J> statements = new ArrayList<>(classDecl.getBody().getStatements()); |
| 54 | + statements.removeAll(expectedExceptionFields); |
| 55 | + cd = cd.withBody(classDecl.getBody().withStatements(statements)); |
| 56 | + } |
| 57 | + return cd; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method) { |
| 62 | + if(method.getType() != null && method.getType().getDeclaringType().getFullyQualifiedName().equals(ExpectedExceptionFqn)) { |
| 63 | + J.MethodDecl enclosing = getCursor().firstEnclosing(J.MethodDecl.class); |
| 64 | + if(enclosing != null) { |
| 65 | + andThen(new Scoped(enclosing, method)); |
| 66 | + } |
| 67 | + } |
| 68 | + return super.visitMethodInvocation(method); |
| 69 | + } |
| 70 | + |
| 71 | + private static class Scoped extends JavaIsoRefactorVisitor { |
| 72 | + private final J.MethodDecl enclosingMethodDecl; |
| 73 | + private final J.MethodInvocation expectedExceptionMethod; |
| 74 | + |
| 75 | + private Scoped(J.MethodDecl enclosingMethodDecl, J.MethodInvocation expectedExceptionMethod) { |
| 76 | + this.enclosingMethodDecl = enclosingMethodDecl; |
| 77 | + this.expectedExceptionMethod = expectedExceptionMethod; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public J.MethodDecl visitMethod(J.MethodDecl m) { |
| 82 | + if(!(enclosingMethodDecl.isScope(m) && m.getBody() != null)) { |
| 83 | + return m; |
| 84 | + } |
| 85 | + |
| 86 | + List<Expression> args = expectedExceptionMethod.getArgs().getArgs(); |
| 87 | + if(args.size() != 1) { |
| 88 | + return m; |
| 89 | + } |
| 90 | + |
| 91 | + Expression expectedException = args.get(0); |
| 92 | + JavaType.FullyQualified argType = TypeUtils.asFullyQualified(expectedException.getType()); |
| 93 | + if(argType == null || !argType.getFullyQualifiedName().equals("java.lang.Class")) { |
| 94 | + return m; |
| 95 | + } |
| 96 | + |
| 97 | + // Remove the ExpectedException.expect() invocation, use the remaining statements as the lambda body for Assertions.assertThrows() |
| 98 | + List<Statement> statements = new ArrayList<>(m.getBody().getStatements()); |
| 99 | + statements.remove(expectedExceptionMethod); |
| 100 | + |
| 101 | + J.MethodInvocation assertThrows = AssertionsBuilder.assertThrows(expectedException, statements); |
| 102 | + |
| 103 | + AddImport addAssertThrows = new AddImport(); |
| 104 | + addAssertThrows.setType("org.junit.jupiter.api.Assertions"); |
| 105 | + addAssertThrows.setStaticMethod("assertThrows"); |
| 106 | + addAssertThrows.setOnlyIfReferenced(false); |
| 107 | + andThen(addAssertThrows); |
| 108 | + |
| 109 | + andThen(new AutoFormat(assertThrows)); |
| 110 | + maybeRemoveImport("org.junit.Rule"); |
| 111 | + maybeRemoveImport("org.junit.rules.ExpectedException"); |
| 112 | + |
| 113 | + m = m.withBody(m.getBody().withStatements(singletonList(assertThrows))); |
| 114 | + |
| 115 | + return m; |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments