From 9eabd197a0efeb261370aad45418329f6dcfd75c Mon Sep 17 00:00:00 2001 From: Amitoj Duggal Date: Sun, 23 Jun 2024 15:05:01 +0200 Subject: [PATCH] Add Kotlin support for JUnit recipes: UpdateBeforeAfterAnnotations (#533) * Adding Kotlin support to the recipes, fixing the recipes to work use preVisit instead of visitCompilationUnit. * Do a single recipe run per unit test * Stop after pre visit, since we're only updating imports * Adding Kotlin support for the UpdateBeforeAfterAnnotations, along with new tests for kotlin * Added kotlin tests for AddParameterizedTestAnnotation * Add Picnic AssertJ rules to AssertJ best practices (#527) * Add Picnic AssertJ rules to AssertJ best practices * Include Picnic's JUnitToAssertJRulesRecipes in migration * Exclude `jakarta.xml.bind-api` from TimeFold * Move the exclude to rewrite-third-party * refactor: Only publish build scans if authenticated Use this link to re-run the recipe: https://app.moderne.io/recipes/builder/kLJjXlflM?organizationId=T3BlblJld3JpdGU%3D Co-authored-by: Moderne * Drop Java 17 requirement through rewrite-third-party (#531) * Jmockit Expectations with no times or result should be transformed to Mockito verify statement (#530) * Ensure Jmockit expectations with no times or result transform to a mockito verify * Minor polish to text blocks * Make times, minTimes, maxTimes more flexible as it was originally so even if someone mistakenly puts times and minTimes together, it still migrates without issue --------- Co-authored-by: Tim te Beek * Rewrite both JMockit `@Mocked` and `@Injectable` annotated arguments (#532) * Ensure Jmockit expectations with no times or result transform to a mockito verify * Minor polish to text blocks * Make times, minTimes, maxTimes more flexible as it was originally so even if someone mistakenly puts times and minTimes together, it still migrates without issue * Add feature to enable migration of Jmockit Injectable annotation exactly as we are doing the Mocked annotation ie method parameter annotation as well as field annotation. Have moved all of the code from JMockitMockedVariableToMockito to JMockitAnnotationToMockito for code reuse. Also add the corresponding test cases and jmockit.yml file modification. * Use `@Option` instead of inheritance * Drop Option and just replace both variants * Update display name and description to match application --------- Co-authored-by: Tim te Beek * Adding Kotlin support for the UpdateBeforeAfterAnnotations, along with new tests for kotlin * Added kotlin tests for AddParameterizedTestAnnotation * Restoring changes to issue to retain the references * Remove trailing whitespace in text blocks * Update src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java Co-authored-by: Tim te Beek * Removing tests for kotlin and keep one in the Documentation example. We can tackle the issues and add tests separately if we see bug in workflows. * Removing extra tests for Kotlin, and keeping one, fixing issue with failing build. * Restored changes to the disabled test. * Minor touch up --------- Co-authored-by: Amitoj Duggal Co-authored-by: Tim te Beek Co-authored-by: Tim te Beek Co-authored-by: Moderne Co-authored-by: Shivani Sharma --- .../junit5/UpdateBeforeAfterAnnotations.java | 8 ++-- .../AddParameterizedTestAnnotationTest.java | 37 ++++++++++++++++++- .../UpdateBeforeAfterAnnotationsTest.java | 37 ++++++++++++++++--- 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotations.java b/src/main/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotations.java index 832634e32..1809e14ab 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotations.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotations.java @@ -49,15 +49,13 @@ public TreeVisitor getVisitor() { public static class UpdateBeforeAfterAnnotationsVisitor extends JavaIsoVisitor { @Override - public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) { - //This visitor handles changing the method visibility for any method annotated with one of the four before/after - //annotations. It registers visitors that will sweep behind it making the type changes. + public J preVisit(J tree, ExecutionContext ctx) { + stopAfterPreVisit(); doAfterVisit(new ChangeType("org.junit.Before", "org.junit.jupiter.api.BeforeEach", true).getVisitor()); doAfterVisit(new ChangeType("org.junit.After", "org.junit.jupiter.api.AfterEach", true).getVisitor()); doAfterVisit(new ChangeType("org.junit.BeforeClass", "org.junit.jupiter.api.BeforeAll", true).getVisitor()); doAfterVisit(new ChangeType("org.junit.AfterClass", "org.junit.jupiter.api.AfterAll", true).getVisitor()); - - return super.visitCompilationUnit(cu, ctx); + return tree; } } } diff --git a/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java b/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java index 5b2e87923..5103fea4d 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java @@ -20,10 +20,12 @@ import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.Issue; import org.openrewrite.java.JavaParser; +import org.openrewrite.kotlin.KotlinParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.kotlin.Assertions.kotlin; class AddParameterizedTestAnnotationTest implements RewriteTest { @Override @@ -31,6 +33,8 @@ public void defaults(RecipeSpec spec) { spec .parser(JavaParser.fromJavaVersion() .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9", "junit-jupiter-params-5.9")) + .parser(KotlinParser.builder() + .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9", "junit-jupiter-params-5.9")) .recipe(new AddParameterizedTestAnnotation()); } @@ -45,7 +49,7 @@ void replaceTestWithParameterizedTest() { import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.*; - + class NumbersTest { @Test @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) @@ -58,7 +62,7 @@ void testIsOdd(int number) { import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.*; - + class NumbersTest { @ParameterizedTest @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) @@ -67,6 +71,35 @@ void testIsOdd(int number) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.api.Test + import org.junit.jupiter.params.provider.ValueSource + import org.junit.jupiter.api.Assertions.assertTrue + + class NumbersTest { + @Test + @ValueSource(ints = [1, 3, 5, -3, 15, Int.MAX_VALUE]) + fun testIsOdd(number: Int) { + assertTrue(number % 2 != 0) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.ValueSource + import org.junit.jupiter.api.Assertions.assertTrue + + class NumbersTest { + @ParameterizedTest + @ValueSource(ints = [1, 3, 5, -3, 15, Int.MAX_VALUE]) + fun testIsOdd(number: Int) { + assertTrue(number % 2 != 0) + } + } + """ ) ); } diff --git a/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java index 47d723d7a..2c71b6107 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java @@ -21,10 +21,12 @@ import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.Issue; import org.openrewrite.java.JavaParser; +import org.openrewrite.kotlin.KotlinParser; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.java.Assertions.java; +import static org.openrewrite.kotlin.Assertions.kotlin; @SuppressWarnings("JUnitMalformedDeclaration") class UpdateBeforeAfterAnnotationsTest implements RewriteTest { @@ -34,6 +36,8 @@ public void defaults(RecipeSpec spec) { spec .parser(JavaParser.fromJavaVersion() .classpathFromResources(new InMemoryExecutionContext(), "junit-4.13")) + .parser(KotlinParser.builder() + .classpathFromResources(new InMemoryExecutionContext(), "junit-4.13")) .recipe(new UpdateBeforeAfterAnnotations()); } @@ -45,9 +49,8 @@ void beforeToBeforeEach() { java( """ import org.junit.Before; - + class Test { - @Before void before() { } @@ -55,14 +58,36 @@ void before() { """, """ import org.junit.jupiter.api.BeforeEach; - + class Test { - @BeforeEach void before() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.Before + + class Test { + + @Before + fun before() { + } + } + """, + """ + import org.junit.jupiter.api.BeforeEach + + class Test { + + @BeforeEach + fun before() { + } + } + """ ) ); } @@ -185,8 +210,8 @@ void before() { } @Test - @Disabled("Issue #59") - void beforeMethodOverridesPublicAbstract() { + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/59") + void retainPublicModifierOnOverriddenMethod() { //language=java rewriteRun(