|
| 1 | +/* |
| 2 | + * Copyright 2021 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.jetbrains.annotations.NotNull; |
| 21 | +import org.openrewrite.ExecutionContext; |
| 22 | +import org.openrewrite.Recipe; |
| 23 | +import org.openrewrite.TreeVisitor; |
| 24 | +import org.openrewrite.internal.lang.Nullable; |
| 25 | +import org.openrewrite.java.JavaIsoVisitor; |
| 26 | +import org.openrewrite.java.JavaParser; |
| 27 | +import org.openrewrite.java.JavaTemplate; |
| 28 | +import org.openrewrite.java.JavaVisitor; |
| 29 | +import org.openrewrite.java.search.FindAnnotations; |
| 30 | +import org.openrewrite.java.search.UsesType; |
| 31 | +import org.openrewrite.java.tree.J; |
| 32 | +import org.openrewrite.java.tree.JavaSourceFile; |
| 33 | +import org.openrewrite.java.tree.TypeUtils; |
| 34 | + |
| 35 | +import java.time.Duration; |
| 36 | +import java.util.*; |
| 37 | + |
| 38 | +import static org.openrewrite.Parser.Input.fromString; |
| 39 | + |
| 40 | +@Value |
| 41 | +@EqualsAndHashCode(callSuper = true) |
| 42 | +public class AddMissingNested extends Recipe { |
| 43 | + private static final String NESTED = "org.junit.jupiter.api.Nested"; |
| 44 | + private static final List<String> TEST_ANNOTATIONS = Arrays.asList( |
| 45 | + "org.junit.jupiter.api.Test", |
| 46 | + "org.junit.jupiter.api.TestTemplate", |
| 47 | + "org.junit.jupiter.api.RepeatedTest", |
| 48 | + "org.junit.jupiter.params.ParameterizedTest", |
| 49 | + "org.junit.jupiter.api.TestFactory"); |
| 50 | + |
| 51 | + @Override |
| 52 | + public String getDisplayName() { |
| 53 | + return "JUnit5 inner test classes should be annotated with `@Nested`."; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String getDescription() { |
| 58 | + return "Adds `@Nested` to inner classes that contain JUnit 5 tests."; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + protected @Nullable TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() { |
| 63 | + return new JavaVisitor<ExecutionContext>() { |
| 64 | + @Override |
| 65 | + public J visitJavaSourceFile(JavaSourceFile cu, ExecutionContext executionContext) { |
| 66 | + TEST_ANNOTATIONS.forEach(ann -> doAfterVisit(new UsesType<>(ann))); |
| 67 | + return cu; |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public Duration getEstimatedEffortPerOccurrence() { |
| 74 | + return Duration.ofMinutes(1); |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public Set<String> getTags() { |
| 79 | + return Collections.singleton("RSPEC-5790"); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public TreeVisitor<?, ExecutionContext> getVisitor() { |
| 84 | + return new JavaIsoVisitor<ExecutionContext>() { |
| 85 | + @Override |
| 86 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 87 | + J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx); |
| 88 | + cd = cd.withBody((J.Block) new AddNestedAnnotationVisitor().visit(cd.getBody(), ctx, getCursor())); |
| 89 | + maybeAddImport(NESTED); |
| 90 | + return cd; |
| 91 | + } |
| 92 | + }; |
| 93 | + } |
| 94 | + |
| 95 | + public static class AddNestedAnnotationVisitor extends JavaIsoVisitor<ExecutionContext> { |
| 96 | + @Override |
| 97 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) { |
| 98 | + J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, ctx); |
| 99 | + boolean alreadyNested = classDecl.getLeadingAnnotations().stream() |
| 100 | + .anyMatch(a -> TypeUtils.isOfClassType(a.getType(), NESTED)); |
| 101 | + if (!alreadyNested && hasTestMethods(cd)) { |
| 102 | + cd = cd.withTemplate(getNestedJavaTemplate(), cd.getCoordinates().addAnnotation(Comparator.comparing( |
| 103 | + J.Annotation::getSimpleName))); |
| 104 | + cd.getModifiers().removeIf(modifier -> modifier.getType().equals(J.Modifier.Type.Static)); |
| 105 | + } |
| 106 | + return cd; |
| 107 | + } |
| 108 | + |
| 109 | + @NotNull |
| 110 | + private JavaTemplate getNestedJavaTemplate() { |
| 111 | + return JavaTemplate.builder(this::getCursor, "@Nested") |
| 112 | + .javaParser(() -> JavaParser.fromJavaVersion().dependsOn(Collections.singletonList( |
| 113 | + fromString("package org.junit.jupiter.api;\npublic @interface Nested {}"))).build()) |
| 114 | + .imports(NESTED).build(); |
| 115 | + } |
| 116 | + |
| 117 | + private static boolean hasTestMethods(final J.ClassDeclaration cd) { |
| 118 | + return TEST_ANNOTATIONS.stream().anyMatch(ann -> !FindAnnotations.find(cd, "@" + ann).isEmpty()); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments