|
| 1 | +/* |
| 2 | + * Copyright 2023 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 lombok.EqualsAndHashCode; |
| 19 | +import lombok.Value; |
| 20 | +import org.openrewrite.ExecutionContext; |
| 21 | +import org.openrewrite.Preconditions; |
| 22 | +import org.openrewrite.Recipe; |
| 23 | +import org.openrewrite.TreeVisitor; |
| 24 | +import org.openrewrite.java.AnnotationMatcher; |
| 25 | +import org.openrewrite.java.JavaIsoVisitor; |
| 26 | +import org.openrewrite.java.JavaParser; |
| 27 | +import org.openrewrite.java.JavaTemplate; |
| 28 | +import org.openrewrite.java.tree.J; |
| 29 | +import org.openrewrite.java.tree.JavaType.FullyQualified; |
| 30 | +import org.openrewrite.java.tree.JavaType.Method; |
| 31 | +import org.openrewrite.java.tree.TypeUtils; |
| 32 | +import org.openrewrite.marker.SearchResult; |
| 33 | + |
| 34 | +import java.util.Comparator; |
| 35 | +import java.util.Optional; |
| 36 | +import java.util.function.Predicate; |
| 37 | + |
| 38 | +@Value |
| 39 | +@EqualsAndHashCode(callSuper = false) |
| 40 | +public class AddMissingTestBeforeAfterAnnotations extends Recipe { |
| 41 | + @Override |
| 42 | + public String getDisplayName() { |
| 43 | + return "Add missing `@BeforeEach`, `@AfterEach`, `@Test` to overriding methods"; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public String getDescription() { |
| 48 | + return "Adds `@BeforeEach`, `@AfterEach`, `@Test` to methods overriding superclass methods if the annoations are present on the superclass method."; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 53 | + return Preconditions.check(new JavaIsoVisitor<ExecutionContext>() { |
| 54 | + @Override |
| 55 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 56 | + if (classDecl.getExtends() != null) { |
| 57 | + // Only classes that extend other classes can have override methods with missing annotations |
| 58 | + return SearchResult.found(classDecl); |
| 59 | + } |
| 60 | + return super.visitClassDeclaration(classDecl, ctx); |
| 61 | + } |
| 62 | + }, new AddMissingTestBeforeAfterAnnotationsVisitor()); |
| 63 | + } |
| 64 | + |
| 65 | + private static class AddMissingTestBeforeAfterAnnotationsVisitor extends JavaIsoVisitor<ExecutionContext> { |
| 66 | + @Override |
| 67 | + public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method, ExecutionContext ctx) { |
| 68 | + if (!method.hasModifier(J.Modifier.Type.Static) && !method.isConstructor()) { |
| 69 | + Optional<Method> superMethod = TypeUtils.findOverriddenMethod(method.getMethodType()); |
| 70 | + if (superMethod.isPresent()) { |
| 71 | + method = maybeAddMissingAnnotation(method, superMethod.get(), LifecyleAnnotation.BEFORE_EACH, ctx); |
| 72 | + method = maybeAddMissingAnnotation(method, superMethod.get(), LifecyleAnnotation.AFTER_EACH, ctx); |
| 73 | + method = maybeAddMissingAnnotation(method, superMethod.get(), LifecyleAnnotation.TEST, ctx); |
| 74 | + } |
| 75 | + } |
| 76 | + return super.visitMethodDeclaration(method, ctx); |
| 77 | + } |
| 78 | + |
| 79 | + private J.MethodDeclaration maybeAddMissingAnnotation(J.MethodDeclaration method, Method superMethod, LifecyleAnnotation la, ExecutionContext ctx) { |
| 80 | + if (la.hasOldAnnotation(superMethod) && !la.hasNewAnnotation(method)) { |
| 81 | + maybeAddImport(la.annotation); |
| 82 | + return JavaTemplate.builder(la.simpleAnnotation) |
| 83 | + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "junit-jupiter-api-5.9")) |
| 84 | + .imports(la.annotation) |
| 85 | + .build() |
| 86 | + .apply(getCursor(), method.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))); |
| 87 | + } |
| 88 | + return method; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + enum LifecyleAnnotation { |
| 93 | + BEFORE_EACH("org.junit.jupiter.api.BeforeEach", "org.junit.Before"), |
| 94 | + AFTER_EACH("org.junit.jupiter.api.AfterEach", "org.junit.After"), |
| 95 | + TEST("org.junit.jupiter.api.Test", "org.junit.Test"); |
| 96 | + |
| 97 | + String annotation; |
| 98 | + String simpleAnnotation; |
| 99 | + private Predicate<FullyQualified> oldAnnotationPredicate; |
| 100 | + private AnnotationMatcher annotationMatcher; |
| 101 | + |
| 102 | + LifecyleAnnotation(String annotation, String oldAnnotation) { |
| 103 | + this.annotation = annotation; |
| 104 | + this.simpleAnnotation = "@" + annotation.substring(annotation.lastIndexOf(".") + 1); |
| 105 | + this.oldAnnotationPredicate = n -> TypeUtils.isOfClassType(n, oldAnnotation); |
| 106 | + this.annotationMatcher = new AnnotationMatcher("@" + annotation); |
| 107 | + } |
| 108 | + |
| 109 | + boolean hasOldAnnotation(Method method) { |
| 110 | + return method.getAnnotations().stream().anyMatch(oldAnnotationPredicate); |
| 111 | + } |
| 112 | + |
| 113 | + boolean hasNewAnnotation(J.MethodDeclaration method) { |
| 114 | + return method.getAllAnnotations().stream().anyMatch(annotationMatcher::matches); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments