|
| 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.mockito; |
| 17 | + |
| 18 | +import org.openrewrite.ExecutionContext; |
| 19 | +import org.openrewrite.Recipe; |
| 20 | +import org.openrewrite.java.AnnotationMatcher; |
| 21 | +import org.openrewrite.java.JavaIsoVisitor; |
| 22 | +import org.openrewrite.java.JavaParser; |
| 23 | +import org.openrewrite.java.JavaTemplate; |
| 24 | +import org.openrewrite.java.search.UsesType; |
| 25 | +import org.openrewrite.java.testing.junit5.RunnerToExtension; |
| 26 | +import org.openrewrite.java.tree.J; |
| 27 | + |
| 28 | +import java.util.Collections; |
| 29 | +import java.util.Comparator; |
| 30 | + |
| 31 | +public class MockitoJUnitRunnerSilentToExtension extends Recipe { |
| 32 | + @Override |
| 33 | + public String getDisplayName() { |
| 34 | + return "JUnit 4 MockitoJUnitRunner.Silent to JUnit Jupiter MockitoExtension with LENIENT settings"; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public String getDescription() { |
| 39 | + return "Replace `@RunWith(MockitoJUnitRunner.Silent.class)` with `@ExtendWith(MockitoExtension.class)` and `@MockitoSettings(strictness = Strictness.LENIENT)`."; |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + protected UsesType<ExecutionContext> getSingleSourceApplicableTest() { |
| 44 | + return new UsesType<>("org.mockito.junit.MockitoJUnitRunner$Silent"); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + protected JavaIsoVisitor<ExecutionContext> getVisitor() { |
| 49 | + |
| 50 | + return new JavaIsoVisitor<ExecutionContext>() { |
| 51 | + final JavaParser parser = JavaParser.fromJavaVersion().dependsOn( |
| 52 | + "package org.mockito.quality;" + |
| 53 | + "public enum Strictness {" + |
| 54 | + " LENIENT,WARN,STRICT_STUBS;" + |
| 55 | + " private Strictness() {}" + |
| 56 | + "}", |
| 57 | + "package org.mockito.junit.jupiter;" + |
| 58 | + "import org.mockito.quality.Strictness;" + |
| 59 | + "public @interface MockitoSettings {" + |
| 60 | + " Strictness strictness() default Strictness.STRICT_STUBS;" + |
| 61 | + "}").build(); |
| 62 | + final AnnotationMatcher silentRunnerMatcher = new AnnotationMatcher("org.junit.runner.RunWith @RunWith(org.mockito.junit.MockitoJUnitRunner.MockitoJUnitRunner.Silent.class)"); |
| 63 | + |
| 64 | + @Override |
| 65 | + public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) { |
| 66 | + J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext); |
| 67 | + if (cd.getLeadingAnnotations().stream().anyMatch(silentRunnerMatcher::matches)) { |
| 68 | + JavaTemplate template = JavaTemplate.builder(this::getCursor, "@MockitoSettings(strictness = Strictness.LENIENT)") |
| 69 | + .imports("org.mockito.quality.Strictness", "org.mockito.junit.jupiter.MockitoSettings") |
| 70 | + .javaParser(() -> parser) |
| 71 | + .build(); |
| 72 | + cd = maybeAutoFormat(cd, cd.withTemplate(template, cd.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName))), executionContext); |
| 73 | + doAfterVisit(new RunnerToExtension(Collections.singletonList("org.mockito.junit.MockitoJUnitRunner$Silent"), |
| 74 | + "org.mockito.junit.jupiter.MockitoExtension")); |
| 75 | + maybeRemoveImport("org.mockito.junit.MockitoJUnitRunner"); |
| 76 | + maybeAddImport("org.mockito.quality.Strictness"); |
| 77 | + maybeAddImport("org.mockito.junit.jupiter.MockitoSettings"); |
| 78 | + } |
| 79 | + return cd; |
| 80 | + } |
| 81 | + }; |
| 82 | + } |
| 83 | +} |
0 commit comments