From 0d5447106ea8d944bc11e738b0bcc4e187e57b95 Mon Sep 17 00:00:00 2001 From: Amitoj Duggal Date: Mon, 17 Jun 2024 10:21:33 +0200 Subject: [PATCH 01/19] Adding Kotlin support to the recipes, fixing the recipes to work use preVisit instead of visitCompilationUnit. --- build.gradle.kts | 1 + .../testing/junit5/CleanupJUnitImports.java | 16 ++++--- .../junit5/CleanupJUnitImportsTest.java | 47 +++++++++++++++++++ 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 6e03b6a63..4f3662b82 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -43,6 +43,7 @@ dependencies { testImplementation("org.openrewrite:rewrite-java-17") testImplementation("org.openrewrite:rewrite-groovy") + testImplementation("org.openrewrite:rewrite-kotlin:$rewriteVersion") testImplementation("org.openrewrite.gradle.tooling:model:$rewriteVersion") testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release") diff --git a/src/main/java/org/openrewrite/java/testing/junit5/CleanupJUnitImports.java b/src/main/java/org/openrewrite/java/testing/junit5/CleanupJUnitImports.java index a411e92b2..0f168ca3a 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/CleanupJUnitImports.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/CleanupJUnitImports.java @@ -22,6 +22,7 @@ import org.openrewrite.java.JavaIsoVisitor; import org.openrewrite.java.search.UsesType; import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.JavaSourceFile; public class CleanupJUnitImports extends Recipe { @Override @@ -44,14 +45,17 @@ public TreeVisitor getVisitor() { public static class CleanupJUnitImportsVisitor extends JavaIsoVisitor { @Override - public J.CompilationUnit visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) { - for (J.Import im : cu.getImports()) { - String packageName = im.getPackageName(); - if (packageName.startsWith("junit") || (packageName.startsWith("org.junit") && !packageName.contains("jupiter"))) { - maybeRemoveImport(im.getTypeName()); + public J preVisit(J tree, ExecutionContext ctx) { + if (tree instanceof JavaSourceFile) { + JavaSourceFile c = (JavaSourceFile) tree; + for (J.Import imp : c.getImports()) { + String packageName = imp.getPackageName(); + if (packageName.startsWith("junit") || (packageName.startsWith("org.junit") && !packageName.contains("jupiter"))) { + maybeRemoveImport(imp.getTypeName()); + } } } - return cu; + return super.preVisit(tree, ctx); } } } diff --git a/src/test/java/org/openrewrite/java/testing/junit5/CleanupJUnitImportsTest.java b/src/test/java/org/openrewrite/java/testing/junit5/CleanupJUnitImportsTest.java index f59a1cbea..305519b32 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/CleanupJUnitImportsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/CleanupJUnitImportsTest.java @@ -19,10 +19,12 @@ import org.openrewrite.DocumentExample; import org.openrewrite.InMemoryExecutionContext; 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 CleanupJUnitImportsTest implements RewriteTest { @@ -31,6 +33,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 CleanupJUnitImports()); } @@ -50,6 +54,20 @@ public class MyTest {} """ ) ); + + //language=kotlin + rewriteRun( + kotlin( + """ + import org.junit.Test + + class MyTest {} + """, + """ + class MyTest {} + """ + ) + ); } @Test @@ -67,6 +85,20 @@ public class MyTest { """ ) ); + + //language=kotlin + rewriteRun( + kotlin( + """ + import java.util.Arrays + import java.util.Collections + import java.util.HashSet + + class MyTest { + } + """ + ) + ); } @Test @@ -84,5 +116,20 @@ public void foo() {} """ ) ); + + //language=kotlin + rewriteRun( + kotlin( + """ + import org.junit.Test + + class MyTest { + @Test + fun foo() {} + } + """ + ) + ); + } } From 30960c1c428f45ca9322e52b677e677010865aa3 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 17 Jun 2024 14:01:58 +0200 Subject: [PATCH 02/19] Do a single recipe run per unit test --- .../junit5/CleanupJUnitImportsTest.java | 27 +++++++------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/test/java/org/openrewrite/java/testing/junit5/CleanupJUnitImportsTest.java b/src/test/java/org/openrewrite/java/testing/junit5/CleanupJUnitImportsTest.java index 305519b32..30ec0e109 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/CleanupJUnitImportsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/CleanupJUnitImportsTest.java @@ -41,8 +41,8 @@ public void defaults(RecipeSpec spec) { @DocumentExample @Test void removesUnusedImport() { - //language=java rewriteRun( + //language=java java( """ import org.junit.Test; @@ -52,11 +52,8 @@ public class MyTest {} """ public class MyTest {} """ - ) - ); - - //language=kotlin - rewriteRun( + ), + //language=kotlin kotlin( """ import org.junit.Test @@ -72,8 +69,8 @@ class MyTest {} @Test void leavesOtherImportsAlone() { - //language=java rewriteRun( + //language=java java( """ import java.util.Arrays; @@ -83,11 +80,8 @@ void leavesOtherImportsAlone() { public class MyTest { } """ - ) - ); - - //language=kotlin - rewriteRun( + ), + //language=kotlin kotlin( """ import java.util.Arrays @@ -103,8 +97,8 @@ class MyTest { @Test void leavesUsedJUnitImportAlone() { - //language=java rewriteRun( + //language=java java( """ import org.junit.Test; @@ -114,11 +108,8 @@ public class MyTest { public void foo() {} } """ - ) - ); - - //language=kotlin - rewriteRun( + ), + //language=kotlin kotlin( """ import org.junit.Test From e07c52376b4da5a669a05d02850c0a9c91c7daf5 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 17 Jun 2024 14:02:16 +0200 Subject: [PATCH 03/19] Stop after pre visit, since we're only updating imports --- .../openrewrite/java/testing/junit5/CleanupJUnitImports.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/openrewrite/java/testing/junit5/CleanupJUnitImports.java b/src/main/java/org/openrewrite/java/testing/junit5/CleanupJUnitImports.java index 0f168ca3a..77a408c1b 100644 --- a/src/main/java/org/openrewrite/java/testing/junit5/CleanupJUnitImports.java +++ b/src/main/java/org/openrewrite/java/testing/junit5/CleanupJUnitImports.java @@ -46,6 +46,7 @@ public TreeVisitor getVisitor() { public static class CleanupJUnitImportsVisitor extends JavaIsoVisitor { @Override public J preVisit(J tree, ExecutionContext ctx) { + stopAfterPreVisit(); if (tree instanceof JavaSourceFile) { JavaSourceFile c = (JavaSourceFile) tree; for (J.Import imp : c.getImports()) { @@ -55,7 +56,7 @@ public J preVisit(J tree, ExecutionContext ctx) { } } } - return super.preVisit(tree, ctx); + return tree; } } } From 15545f10496069acc62f94ed2ddb99811fee24b7 Mon Sep 17 00:00:00 2001 From: Amitoj Duggal Date: Mon, 17 Jun 2024 22:35:25 +0200 Subject: [PATCH 04/19] Adding Kotlin support for the UpdateBeforeAfterAnnotations, along with new tests for kotlin --- .../junit5/UpdateBeforeAfterAnnotations.java | 8 +- .../UpdateBeforeAfterAnnotationsTest.java | 207 +++++++++++++++--- 2 files changed, 180 insertions(+), 35 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/UpdateBeforeAfterAnnotationsTest.java b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java index 47d723d7a..4e8f0c244 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java @@ -15,16 +15,16 @@ */ package org.openrewrite.java.testing.junit5; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; 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,9 +34,12 @@ 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()); } + @DocumentExample @Test void beforeToBeforeEach() { @@ -45,9 +48,9 @@ void beforeToBeforeEach() { java( """ import org.junit.Before; - + class Test { - + @Before void before() { } @@ -55,18 +58,42 @@ 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() { + } + } + """ ) ); } + @Test void afterToAfterEach() { //language=java @@ -74,9 +101,9 @@ void afterToAfterEach() { java( """ import org.junit.After; - + class Test { - + @After void after() { } @@ -84,18 +111,42 @@ void after() { """, """ import org.junit.jupiter.api.AfterEach; - + class Test { - + @AfterEach void after() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.After + + class Test { + + @After + fun after() { + } + } + """, + """ + import org.junit.jupiter.api.AfterEach + + class Test { + + @AfterEach + fun after() { + } + } + """ ) ); } + @Test void beforeClassToBeforeAll() { //language=java @@ -103,9 +154,9 @@ void beforeClassToBeforeAll() { java( """ import org.junit.BeforeClass; - + class Test { - + @BeforeClass void beforeClass() { } @@ -113,18 +164,42 @@ void beforeClass() { """, """ import org.junit.jupiter.api.BeforeAll; - + class Test { - + @BeforeAll void beforeClass() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.BeforeClass + + class Test { + + @BeforeClass + fun beforeClass() { + } + } + """, + """ + import org.junit.jupiter.api.BeforeAll + + class Test { + + @BeforeAll + fun beforeClass() { + } + } + """ ) ); } + @Test void afterClassToAfterAll() { //language=java @@ -132,7 +207,7 @@ void afterClassToAfterAll() { java( """ import org.junit.AfterClass; - + class Test { @AfterClass void afterClass() { @@ -141,19 +216,38 @@ void afterClass() { """, """ import org.junit.jupiter.api.AfterAll; - + class Test { @AfterAll void afterClass() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.AfterClass + + class Test { + @AfterClass + fun afterClass() { + } + } + """, + """ + import org.junit.jupiter.api.AfterAll + + class Test { + @AfterAll + fun afterClass() { + } + } + """ ) ); } - @Issue("https://github.com/openrewrite/rewrite/issues/150") - @Disabled @Test void convertsToPackageVisibility() { //language=java @@ -161,9 +255,9 @@ void convertsToPackageVisibility() { java( """ import org.junit.Before; - + class Test { - + @Before // comments public void before() { } @@ -171,12 +265,34 @@ public void before() { """, """ import org.junit.jupiter.api.BeforeEach; - + class Test { - - // comments - @BeforeEach - void before() { + + @BeforeEach // comments + public void before() { + } + } + """ + ), + //language=kotlin + kotlin( + """ + import org.junit.Before + + class Test { + + @Before // comments + fun before() { + } + } + """, + """ + import org.junit.jupiter.api.BeforeEach + + class Test { + + @BeforeEach // comments + fun before() { } } """ @@ -184,8 +300,8 @@ void before() { ); } + @Test - @Disabled("Issue #59") void beforeMethodOverridesPublicAbstract() { //language=java rewriteRun( @@ -200,9 +316,9 @@ public class AbstractTest { java( """ import org.junit.Before; - + public class A extends AbstractTest { - + @Before public void setup() { } @@ -210,14 +326,45 @@ public void setup() { """, """ import org.junit.jupiter.api.BeforeEach; - + public class A extends AbstractTest { - + @BeforeEach public void setup() { } } """ + ), + //language=kotlin + kotlin( + """ + abstract class AbstractTest { + abstract fun setup() + } + """ + ), + //language=kotlin + kotlin( + """ + import org.junit.Before + + class A : AbstractTest() { + + @Before + fun setup() { + } + } + """, + """ + import org.junit.jupiter.api.BeforeEach + + class A : AbstractTest() { + + @BeforeEach + fun setup() { + } + } + """ ) ); } From fa1372270b6430f97c34df04fb51cc42cee99eed Mon Sep 17 00:00:00 2001 From: Amitoj Duggal Date: Tue, 18 Jun 2024 23:41:32 +0200 Subject: [PATCH 05/19] Added kotlin tests for AddParameterizedTestAnnotation --- .../AddParameterizedTestAnnotationTest.java | 361 +++++++++++++++++- 1 file changed, 359 insertions(+), 2 deletions(-) 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..801ef1fed 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java @@ -18,12 +18,13 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; 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,10 +32,11 @@ 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()); } - @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/314") @Test @DocumentExample void replaceTestWithParameterizedTest() { @@ -67,6 +69,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) + } + } + """ ) ); } @@ -102,6 +133,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) + } + } + """ ) ); } @@ -125,6 +185,19 @@ void printMessage() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.api.Test + + class NumbersTest { + @Test + fun printMessage() { + System.out.println("message") + } + } + """ ) ); } @@ -158,6 +231,33 @@ void processUserData(String email) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.CsvSource + import org.junit.jupiter.api.Test + + class TestClass { + @Test + @CsvSource(["test@test.com"]) + fun processUserData(email: String) { + System.out.println(email) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.CsvSource + + class TestClass { + @ParameterizedTest + @CsvSource(["test@test.com"]) + fun processUserData(email: String) { + System.out.println(email) + } + } + """ ) ); } @@ -191,6 +291,33 @@ void foo() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.api.Test + import org.junit.jupiter.params.provider.MethodSource + + class TestClass { + @Test + @MethodSource + fun foo() { + System.out.println("bar") + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.MethodSource + + class TestClass { + @ParameterizedTest + @MethodSource + fun foo() { + System.out.println("bar") + } + } + """ ) ); } @@ -226,6 +353,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 TestClass { + @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 TestClass { + @ParameterizedTest + @ValueSource(ints = [1, 3, 5, -3, 15, Int.MAX_VALUE]) + fun testIsOdd(number: Int) { + assertTrue(number % 2 != 0) + } + } + """ ) ); } @@ -259,6 +415,33 @@ void processUserData(String email) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.NullSource + import org.junit.jupiter.api.Test + + class TestClass { + @Test + @NullSource + fun processUserData(email: String?) { + System.out.println(email) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.NullSource + + class TestClass { + @ParameterizedTest + @NullSource + fun processUserData(email: String?) { + System.out.println(email) + } + } + """ ) ); } @@ -292,6 +475,33 @@ void processUserData(String email) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.EmptySource + import org.junit.jupiter.api.Test + + class TestClass { + @Test + @EmptySource + fun processUserData(email: String) { + System.out.println(email) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.EmptySource + + class TestClass { + @ParameterizedTest + @EmptySource + fun processUserData(email: String) { + System.out.println(email) + } + } + """ ) ); } @@ -325,6 +535,33 @@ void processUserData(String email) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.NullAndEmptySource + import org.junit.jupiter.api.Test + + class TestClass { + @Test + @NullAndEmptySource + fun processUserData(email: String?) { + System.out.println(email) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.NullAndEmptySource + + class TestClass { + @ParameterizedTest + @NullAndEmptySource + fun processUserData(email: String?) { + System.out.println(email) + } + } + """ ) ); } @@ -372,6 +609,47 @@ void processTime(time timeOfDay) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.EnumSource + import org.junit.jupiter.api.Test + + class TestClass { + enum class time { + MORNING, + NOON, + AFTERNOON, + MIDNIGHT + } + + @Test + @EnumSource + fun processTime(timeOfDay: time) { + System.out.println("Its " + timeOfDay) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.EnumSource + + class TestClass { + enum class time { + MORNING, + NOON, + AFTERNOON, + MIDNIGHT + } + + @ParameterizedTest + @EnumSource + fun processTime(timeOfDay: time) { + System.out.println("Its " + timeOfDay) + } + } + """ ) ); } @@ -409,6 +687,37 @@ void testWithCsvFileSourceFromFile(String country, int reference) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.CsvFileSource + import org.junit.jupiter.api.Test + import org.junit.jupiter.api.Assertions.* + + class TestClass { + @Test + @CsvFileSource(files = ["src/test/resources/two-column.csv"], numLinesToSkip = 1) + fun testWithCsvFileSourceFromFile(country: String, reference: Int) { + assertNotNull(country) + assertNotEquals(0, reference) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.CsvFileSource + import org.junit.jupiter.api.Assertions.* + + class TestClass { + @ParameterizedTest + @CsvFileSource(files = ["src/test/resources/two-column.csv"], numLinesToSkip = 1) + fun testWithCsvFileSourceFromFile(country: String, reference: Int) { + assertNotNull(country) + assertNotEquals(0, reference) + } + } + """ ) ); } @@ -464,6 +773,54 @@ public Stream provideArguments(ExtensionContext context) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.api.extension.ExtensionContext + import org.junit.jupiter.params.provider.Arguments + import org.junit.jupiter.params.provider.ArgumentsProvider + import org.junit.jupiter.params.provider.ArgumentsSource + import org.junit.jupiter.api.Test + import java.util.stream.Stream + import org.junit.jupiter.api.Assertions.* + + class TestClass { + @Test + @ArgumentsSource(MyArgumentsProvider::class) + fun testWithArgumentsSource(argument: String) { + assertNotNull(argument) + } + class MyArgumentsProvider : ArgumentsProvider { + override fun provideArguments(context: ExtensionContext): Stream { + return Stream.of("apple", "banana").map { Arguments.of(it) } + } + } + } + """, + + """ + import org.junit.jupiter.api.extension.ExtensionContext + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.Arguments + import org.junit.jupiter.params.provider.ArgumentsProvider + import org.junit.jupiter.params.provider.ArgumentsSource + import java.util.stream.Stream + import org.junit.jupiter.api.Assertions.* + + class TestClass { + @ParameterizedTest + @ArgumentsSource(MyArgumentsProvider::class) + fun testWithArgumentsSource(argument: String) { + assertNotNull(argument) + } + class MyArgumentsProvider : ArgumentsProvider { + override fun provideArguments(context: ExtensionContext): Stream { + return Stream.of("apple", "banana").map { Arguments.of(it) } + } + } + } + """ ) ); } From f6fd0089fc73b07344b5cee0c9a9c58817238614 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Mon, 17 Jun 2024 19:20:19 +0200 Subject: [PATCH 06/19] 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 --- build.gradle.kts | 1 + .../testing/assertj/IsEqualToEmptyString.java | 7 +++++++ src/main/resources/META-INF/rewrite/assertj.yml | 17 +++++++++++++++++ ...gTest.java => AssertJBestPracticesTest.java} | 4 ++-- 4 files changed, 27 insertions(+), 2 deletions(-) rename src/test/java/org/openrewrite/java/testing/assertj/{IsEqualToEmptyStringTest.java => AssertJBestPracticesTest.java} (93%) diff --git a/build.gradle.kts b/build.gradle.kts index 4f3662b82..4efd2fa0e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -34,6 +34,7 @@ dependencies { implementation("org.openrewrite:rewrite-maven") implementation("org.openrewrite.recipe:rewrite-java-dependencies:$rewriteVersion") implementation("org.openrewrite.recipe:rewrite-static-analysis:$rewriteVersion") + implementation("org.openrewrite.recipe:rewrite-third-party:$rewriteVersion") runtimeOnly("org.openrewrite:rewrite-java-17") compileOnly("org.projectlombok:lombok:latest.release") diff --git a/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java index 168c14324..184ef6dee 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java @@ -27,6 +27,13 @@ import java.util.Collections; +/** + * AssertJ has a more idiomatic way of asserting that a String is empty. + * This recipe will find instances of `assertThat(String).isEqualTo("")` and replace them with `isEmpty()`. + * + * @deprecated Use {@link tech.picnic.errorprone.refasterrules.AssertJStringRulesRecipes.AbstractStringAssertStringIsEmptyRecipe} instead. + */ +@Deprecated public class IsEqualToEmptyString extends Recipe { private static final MethodMatcher IS_EQUAL_TO = new MethodMatcher("org.assertj.core.api.AbstractStringAssert isEqualTo(java.lang.String)"); diff --git a/src/main/resources/META-INF/rewrite/assertj.yml b/src/main/resources/META-INF/rewrite/assertj.yml index 2695a5dd0..f928d283a 100644 --- a/src/main/resources/META-INF/rewrite/assertj.yml +++ b/src/main/resources/META-INF/rewrite/assertj.yml @@ -28,6 +28,22 @@ recipeList: - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions - org.openrewrite.java.testing.assertj.IsEqualToEmptyString + - tech.picnic.errorprone.refasterrules.AssertJBigDecimalRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJBigIntegerRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJBooleanRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJByteRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJCharSequenceRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJDoubleRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJFloatRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJIntegerRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJLongRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJNumberRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJPrimitiveRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJShortRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJStringRulesRecipes + - tech.picnic.errorprone.refasterrules.AssertJThrowingCallableRulesRecipes + --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.testing.assertj.StaticImports @@ -365,3 +381,4 @@ recipeList: version: 3.x onlyIfUsing: org.assertj.core.api.Assertions acceptTransitive: true + - tech.picnic.errorprone.refasterrules.JUnitToAssertJRulesRecipes diff --git a/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java b/src/test/java/org/openrewrite/java/testing/assertj/AssertJBestPracticesTest.java similarity index 93% rename from src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java rename to src/test/java/org/openrewrite/java/testing/assertj/AssertJBestPracticesTest.java index 9edf572d6..4252b33ff 100644 --- a/src/test/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyStringTest.java +++ b/src/test/java/org/openrewrite/java/testing/assertj/AssertJBestPracticesTest.java @@ -24,13 +24,13 @@ import static org.openrewrite.java.Assertions.java; -class IsEqualToEmptyStringTest implements RewriteTest { +class AssertJBestPracticesTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec .parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "assertj-core-3.24")) - .recipe(new IsEqualToEmptyString()); + .recipeFromResources("org.openrewrite.java.testing.assertj.Assertj"); } @DocumentExample From 64dfb10c90bf19c0b4f0ec6411560818487319d3 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Tue, 18 Jun 2024 20:32:30 +0000 Subject: [PATCH 07/19] 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 --- settings.gradle.kts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/settings.gradle.kts b/settings.gradle.kts index 19421c709..991d06956 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -30,6 +30,12 @@ develocity { fileFingerprints = true } + publishing { + onlyIf { + authenticated + } + } + uploadInBackground = !isCiServer } } From e579694c064a62c1d8656f3bb2ed5b56f17f850d Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 19 Jun 2024 15:52:52 +0200 Subject: [PATCH 08/19] Drop Java 17 requirement through rewrite-third-party (#531) --- build.gradle.kts | 1 - .../testing/assertj/IsEqualToEmptyString.java | 3 --- src/main/resources/META-INF/rewrite/assertj.yml | 17 ----------------- 3 files changed, 21 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 4efd2fa0e..4f3662b82 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -34,7 +34,6 @@ dependencies { implementation("org.openrewrite:rewrite-maven") implementation("org.openrewrite.recipe:rewrite-java-dependencies:$rewriteVersion") implementation("org.openrewrite.recipe:rewrite-static-analysis:$rewriteVersion") - implementation("org.openrewrite.recipe:rewrite-third-party:$rewriteVersion") runtimeOnly("org.openrewrite:rewrite-java-17") compileOnly("org.projectlombok:lombok:latest.release") diff --git a/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java index 184ef6dee..fb014fd94 100644 --- a/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java +++ b/src/main/java/org/openrewrite/java/testing/assertj/IsEqualToEmptyString.java @@ -30,10 +30,7 @@ /** * AssertJ has a more idiomatic way of asserting that a String is empty. * This recipe will find instances of `assertThat(String).isEqualTo("")` and replace them with `isEmpty()`. - * - * @deprecated Use {@link tech.picnic.errorprone.refasterrules.AssertJStringRulesRecipes.AbstractStringAssertStringIsEmptyRecipe} instead. */ -@Deprecated public class IsEqualToEmptyString extends Recipe { private static final MethodMatcher IS_EQUAL_TO = new MethodMatcher("org.assertj.core.api.AbstractStringAssert isEqualTo(java.lang.String)"); diff --git a/src/main/resources/META-INF/rewrite/assertj.yml b/src/main/resources/META-INF/rewrite/assertj.yml index f928d283a..2695a5dd0 100644 --- a/src/main/resources/META-INF/rewrite/assertj.yml +++ b/src/main/resources/META-INF/rewrite/assertj.yml @@ -28,22 +28,6 @@ recipeList: - org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions - org.openrewrite.java.testing.assertj.IsEqualToEmptyString - - tech.picnic.errorprone.refasterrules.AssertJBigDecimalRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJBigIntegerRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJBooleanRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJByteRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJCharSequenceRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJDoubleRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJFloatRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJIntegerRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJLongRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJNumberRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJPrimitiveRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJShortRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJStringRulesRecipes - - tech.picnic.errorprone.refasterrules.AssertJThrowingCallableRulesRecipes - --- type: specs.openrewrite.org/v1beta/recipe name: org.openrewrite.java.testing.assertj.StaticImports @@ -381,4 +365,3 @@ recipeList: version: 3.x onlyIfUsing: org.assertj.core.api.Assertions acceptTransitive: true - - tech.picnic.errorprone.refasterrules.JUnitToAssertJRulesRecipes From 1d22c17a1959455b9f4ec5376e5486a25c8bfa45 Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Thu, 20 Jun 2024 19:28:43 +1000 Subject: [PATCH 09/19] 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 --- .../jmockit/ExpectationsBlockRewriter.java | 43 +- .../JMockitExpectationsToMockitoTest.java | 383 +++++++++++++++--- 2 files changed, 352 insertions(+), 74 deletions(-) diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java b/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java index c45b0b798..ae2dce1ee 100644 --- a/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java +++ b/src/main/java/org/openrewrite/java/testing/jmockit/ExpectationsBlockRewriter.java @@ -17,6 +17,7 @@ import org.openrewrite.Cursor; import org.openrewrite.ExecutionContext; +import org.openrewrite.internal.lang.Nullable; import org.openrewrite.java.JavaParser; import org.openrewrite.java.JavaTemplate; import org.openrewrite.java.JavaVisitor; @@ -118,22 +119,31 @@ private void rewriteMethodBody(List expectationStatements) { return; } J.MethodInvocation invocation = (J.MethodInvocation) expectationStatements.get(0); - if (!mockInvocationResults.getResults().isEmpty()) { + boolean hasExpectationsResults = !mockInvocationResults.getResults().isEmpty(); + if (hasExpectationsResults) { // rewrite the statement to mockito if there are results rewriteExpectationResult(mockInvocationResults.getResults(), invocation); } else if (nextStatementCoordinates.isReplacement()) { // if there are no results and the Expectations block is not yet replaced, remove it removeExpectationsStatement(); } + + boolean hasTimes = false; if (mockInvocationResults.getTimes() != null) { + hasTimes = true; writeMethodVerification(invocation, mockInvocationResults.getTimes(), "times"); } if (mockInvocationResults.getMinTimes() != null) { + hasTimes = true; writeMethodVerification(invocation, mockInvocationResults.getMinTimes(), "atLeast"); } if (mockInvocationResults.getMaxTimes() != null) { + hasTimes = true; writeMethodVerification(invocation, mockInvocationResults.getMaxTimes(), "atMost"); } + if (!hasExpectationsResults && !hasTimes) { + writeMethodVerification(invocation, null, null); + } } private void rewriteExpectationResult(List results, J.MethodInvocation invocation) { @@ -182,18 +192,22 @@ private void removeExpectationsStatement() { methodBody.getStatements().get(bodyStatementIndex + numStatementsAdded).getCoordinates().after(); } - private void writeMethodVerification(J.MethodInvocation invocation, Expression times, String verificationMode) { + private void writeMethodVerification(J.MethodInvocation invocation, @Nullable Expression times, @Nullable String verificationMode) { String fqn = getInvocationSelectFullyQualifiedClassName(invocation); if (fqn == null) { // cannot write a verification statement for an invocation without a select field return; } visitor.maybeAddImport("org.mockito.Mockito", "verify"); - visitor.maybeAddImport("org.mockito.Mockito", verificationMode); + if (verificationMode != null) { + visitor.maybeAddImport("org.mockito.Mockito", verificationMode); + } List templateParams = new ArrayList<>(); templateParams.add(invocation.getSelect()); - templateParams.add(times); + if (times != null) { + templateParams.add(times); + } templateParams.add(invocation.getName().getSimpleName()); String verifyTemplate = getVerifyTemplate(invocation.getArguments(), fqn, verificationMode, templateParams); @@ -251,17 +265,20 @@ private static void appendToTemplate(StringBuilder templateBuilder, boolean buil templateBuilder.append(templateField); } - private static String getVerifyTemplate(List arguments, String fqn, String verificationMode, List templateParams) { + private static String getVerifyTemplate(List arguments, String fqn, @Nullable String verificationMode, List templateParams) { + StringBuilder templateBuilder = new StringBuilder("verify(#{any(" + fqn + ")}"); // verify(object + if (verificationMode != null) { + templateBuilder.append(", ").append(verificationMode).append("(#{any(int)})"); // verify(object, times(2) + } + templateBuilder.append(").#{}("); // verify(object, times(2)).method( + if (arguments.isEmpty()) { - return "verify(#{any(" + fqn + ")}, " - + verificationMode - + "(#{any(int)})).#{}();"; + templateBuilder.append(");"); // verify(object, times(2)).method(); + return templateBuilder.toString(); } - StringBuilder templateBuilder = new StringBuilder("verify(#{any(" + fqn + ")}, " - + verificationMode - + "(#{any(int)})).#{}("); + boolean hasArgument = false; - for (Expression argument : arguments) { + for (Expression argument : arguments) { // verify(object, times(2).method(anyLong(), any Int() if (argument instanceof J.Empty) { continue; } else if (argument instanceof J.Literal) { @@ -276,7 +293,7 @@ private static String getVerifyTemplate(List arguments, String fqn, if (hasArgument) { templateBuilder.delete(templateBuilder.length() - 2, templateBuilder.length()); } - templateBuilder.append(");"); + templateBuilder.append(");"); // verify(object, times(2).method(anyLong(), any Int()); return templateBuilder.toString(); } diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java index 72d8cfe7d..69f4627fe 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitExpectationsToMockitoTest.java @@ -44,7 +44,7 @@ public void defaults(RecipeSpec spec) { @DocumentExample @Test - void voidResult() { + void whenNoResultNoTimes() { //language=java rewriteRun( java( @@ -71,6 +71,55 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; + + import static org.mockito.Mockito.*; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + void test() { + myObject.wait(10L, 10); + verify(myObject).wait(anyLong(), anyInt()); + } + } + """ + ) + ); + } + + @DocumentExample + @Test + void whenNoResultNoTimesNoArgs() { + //language=java + rewriteRun( + java( + """ + import mockit.Expectations; + import mockit.Mocked; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + Object myObject; + + void test() { + new Expectations() {{ + myObject.wait(); + }}; + myObject.wait(10L, 10); + } + } + """, + """ + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + import static org.mockito.Mockito.verify; @ExtendWith(MockitoExtension.class) class MyTest { @@ -79,6 +128,7 @@ class MyTest { void test() { myObject.wait(10L, 10); + verify(myObject).wait(); } } """ @@ -159,19 +209,19 @@ public int getSomeField() { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + void test() { new Expectations() {{ myObject.getSomeField(); @@ -190,7 +240,7 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; @@ -225,19 +275,19 @@ public String getSomeField(String s) { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + void test() { new Expectations() {{ myObject.getSomeField(anyString); @@ -251,7 +301,7 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.when; @@ -285,21 +335,21 @@ public String getSomeField() { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + String expected = "expected"; - + void test() { new Expectations() {{ myObject.getSomeField(); @@ -313,17 +363,17 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; - + @ExtendWith(MockitoExtension.class) class MyTest { @Mock MyObject myObject; - + String expected = "expected"; - + void test() { when(myObject.getSomeField()).thenReturn(expected); assertEquals(expected, myObject.getSomeField()); @@ -348,19 +398,19 @@ public Object getSomeField() { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertNotNull; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + void test() { new Expectations() {{ myObject.getSomeField(); @@ -374,7 +424,7 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.mockito.Mockito.when; @@ -529,7 +579,7 @@ public String getSomeOtherField(Object input) { """ ), java( - """ + """ import java.util.ArrayList; import java.util.List; @@ -544,7 +594,7 @@ public String getSomeOtherField(Object input) { class MyTest { @Mocked MyObject myObject; - + void test() { new Expectations() {{ myObject.getSomeField((List) any); @@ -572,7 +622,7 @@ void test() { class MyTest { @Mock MyObject myObject; - + void test() { when(myObject.getSomeField(anyList())).thenReturn(null); when(myObject.getSomeOtherField(any(Object.class))).thenReturn(null); @@ -585,6 +635,73 @@ void test() { ); } + @Test + void whenNoArguments() { + //language=java + rewriteRun( + java( + """ + import java.util.List; + + class MyObject { + public String getSomeField() { + return "X"; + } + } + """ + ), + java( + """ + import java.util.ArrayList; + import java.util.List; + + import mockit.Expectations; + import mockit.Mocked; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + import static org.junit.jupiter.api.Assertions.assertNull; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + MyObject myObject; + + void test() { + new Expectations() {{ + myObject.getSomeField(); + result = null; + }}; + assertNull(myObject.getSomeField()); + } + } + """, + """ + import java.util.ArrayList; + import java.util.List; + + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + import static org.junit.jupiter.api.Assertions.assertNull; + import static org.mockito.Mockito.when; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + MyObject myObject; + + void test() { + when(myObject.getSomeField()).thenReturn(null); + assertNull(myObject.getSomeField()); + } + } + """ + ) + ); + } + @Test void whenMixedArgumentMatcher() { //language=java @@ -601,7 +718,7 @@ public String getSomeField(String s, String s2, String s3, long l1) { """ ), java( - """ + """ import java.util.ArrayList; import java.util.List; @@ -616,7 +733,7 @@ public String getSomeField(String s, String s2, String s3, long l1) { class MyTest { @Mocked MyObject myObject; - + void test() { String bazz = "bazz"; new Expectations() {{ @@ -642,7 +759,7 @@ void test() { class MyTest { @Mock MyObject myObject; - + void test() { String bazz = "bazz"; when(myObject.getSomeField(eq("foo"), anyString(), eq(bazz), eq(10L))).thenReturn(null); @@ -672,31 +789,31 @@ public String getString() { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + void test() { String a = "a"; String s = "s"; - + new Expectations() {{ myObject.getSomeField(anyString); result = s; - + myObject.getString(); result = a; }}; - + assertEquals("s", myObject.getSomeField("foo")); assertEquals("a", myObject.getString()); } @@ -706,7 +823,7 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.when; @@ -719,11 +836,11 @@ class MyTest { void test() { String a = "a"; String s = "s"; - + when(myObject.getSomeField(anyString())).thenReturn(s); - + when(myObject.getString()).thenReturn(a); - + assertEquals("s", myObject.getSomeField("foo")); assertEquals("a", myObject.getString()); } @@ -747,19 +864,19 @@ public String getSomeField(String s) { """ ), java( - """ + """ import mockit.Expectations; import mockit.Mocked; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Mocked MyObject myObject; - + void test() { String a = "a"; new Expectations() {{ @@ -768,7 +885,7 @@ void test() { String b = "b"; result = s; }}; - + assertEquals("s", myObject.getSomeField("foo")); } } @@ -777,7 +894,7 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.anyString; import static org.mockito.Mockito.when; @@ -792,7 +909,7 @@ void test() { String s = "s"; String b = "b"; when(myObject.getSomeField(anyString())).thenReturn(s); - + assertEquals("s", myObject.getSomeField("foo")); } } @@ -803,6 +920,55 @@ void test() { @Test void whenTimes() { + //language=java + rewriteRun( + java( + """ + import mockit.Expectations; + import mockit.Mocked; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + Object myObject; + + void test() { + new Expectations() {{ + myObject.wait(anyLong, anyInt); + times = 2; + }}; + myObject.wait(10L, 10); + myObject.wait(10L, 10); + } + } + """, + """ + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + import static org.mockito.Mockito.*; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + void test() { + myObject.wait(10L, 10); + myObject.wait(10L, 10); + verify(myObject, times(2)).wait(anyLong(), anyInt()); + } + } + """ + ) + ); + } + + @Test + void whenMinTimes() { //language=java rewriteRun( java( @@ -820,9 +986,55 @@ class MyTest { void test() { new Expectations() {{ myObject.wait(anyLong, anyInt); - times = 2; + minTimes = 2; }}; myObject.wait(10L, 10); + } + } + """, + """ + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + import static org.mockito.Mockito.*; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + void test() { + myObject.wait(10L, 10); + verify(myObject, atLeast(2)).wait(anyLong(), anyInt()); + } + } + """ + ) + ); + } + + @Test + void whenMaxTimes() { + //language=java + rewriteRun( + java( + """ + import mockit.Expectations; + import mockit.Mocked; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + Object myObject; + + void test() { + new Expectations() {{ + myObject.wait(anyLong, anyInt); + maxTimes = 5; + }}; myObject.wait(10L, 10); } } @@ -831,9 +1043,9 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.mockito.Mockito.*; - + @ExtendWith(MockitoExtension.class) class MyTest { @Mock @@ -841,8 +1053,56 @@ class MyTest { void test() { myObject.wait(10L, 10); + verify(myObject, atMost(5)).wait(anyLong(), anyInt()); + } + } + """ + ) + ); + } + + @Test + void whenMinTimesMaxTimes() { + //language=java + rewriteRun( + java( + """ + import mockit.Expectations; + import mockit.Mocked; + import mockit.integration.junit5.JMockitExtension; + import org.junit.jupiter.api.extension.ExtendWith; + + @ExtendWith(JMockitExtension.class) + class MyTest { + @Mocked + Object myObject; + + void test() { + new Expectations() {{ + myObject.wait(anyLong, anyInt); + minTimes = 1; + maxTimes = 3; + }}; + myObject.wait(10L, 10); + } + } + """, + """ + import org.junit.jupiter.api.extension.ExtendWith; + import org.mockito.Mock; + import org.mockito.junit.jupiter.MockitoExtension; + + import static org.mockito.Mockito.*; + + @ExtendWith(MockitoExtension.class) + class MyTest { + @Mock + Object myObject; + + void test() { myObject.wait(10L, 10); - verify(myObject, times(2)).wait(anyLong(), anyInt()); + verify(myObject, atLeast(1)).wait(anyLong(), anyInt()); + verify(myObject, atMost(3)).wait(anyLong(), anyInt()); } } """ @@ -869,14 +1129,14 @@ public String getSomeField() { import mockit.Tested; import mockit.integration.junit5.JMockitExtension; import org.junit.jupiter.api.extension.ExtendWith; - + import static org.junit.jupiter.api.Assertions.assertEquals; - + @ExtendWith(JMockitExtension.class) class MyTest { @Tested MyObject myObject; - + void test() { new Expectations(myObject) {{ myObject.getSomeField(); @@ -890,15 +1150,15 @@ void test() { import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.junit.jupiter.MockitoExtension; - + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.when; - + @ExtendWith(MockitoExtension.class) class MyTest { @InjectMocks MyObject myObject; - + void test() { when(myObject.getSomeField()).thenReturn("foo"); assertEquals("foo", myObject.getSomeField()); @@ -943,10 +1203,10 @@ public void doSomething() {} class MyTest { @Mocked Object myObject; - + @Mocked MyObject myOtherObject; - + void test() { new Expectations() {{ myObject.hashCode(); @@ -989,6 +1249,7 @@ void test() { assertNull(myOtherObject.getSomeObjectField()); myObject.wait(10L, 10); assertEquals("foo", myOtherObject.getSomeStringField("bar", 10L)); + verify(myObject).wait(anyLong(), anyInt()); } } """ From 77af97e03fc3768c003b0dc182b730ba22934cb9 Mon Sep 17 00:00:00 2001 From: Shivani Sharma Date: Thu, 20 Jun 2024 20:59:50 +1000 Subject: [PATCH 10/19] 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 --- .../JMockitAnnotatedArgumentToMockito.java | 108 ++++++++++++++++++ .../JMockitMockedVariableToMockito.java | 106 ----------------- .../java/testing/jmockit/package-info.java | 19 +++ .../resources/META-INF/rewrite/jmockit.yml | 2 +- ...MockitAnnotatedArgumentToMockitoTest.java} | 95 +++++++++++++-- 5 files changed, 214 insertions(+), 116 deletions(-) create mode 100644 src/main/java/org/openrewrite/java/testing/jmockit/JMockitAnnotatedArgumentToMockito.java delete mode 100644 src/main/java/org/openrewrite/java/testing/jmockit/JMockitMockedVariableToMockito.java create mode 100644 src/main/java/org/openrewrite/java/testing/jmockit/package-info.java rename src/test/java/org/openrewrite/java/testing/jmockit/{JMockitMockedVariableToMockitoTest.java => JMockitAnnotatedArgumentToMockitoTest.java} (59%) diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitAnnotatedArgumentToMockito.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitAnnotatedArgumentToMockito.java new file mode 100644 index 000000000..a89e16e89 --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitAnnotatedArgumentToMockito.java @@ -0,0 +1,108 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.testing.jmockit; + +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; +import org.openrewrite.ExecutionContext; +import org.openrewrite.Preconditions; +import org.openrewrite.Recipe; +import org.openrewrite.TreeVisitor; +import org.openrewrite.internal.ListUtils; +import org.openrewrite.java.JavaIsoVisitor; +import org.openrewrite.java.JavaParser; +import org.openrewrite.java.JavaTemplate; +import org.openrewrite.java.search.FindAnnotations; +import org.openrewrite.java.search.UsesType; +import org.openrewrite.java.tree.J; +import org.openrewrite.java.tree.Statement; + +import java.util.ArrayList; +import java.util.List; + +@EqualsAndHashCode(callSuper = false) +public class JMockitAnnotatedArgumentToMockito extends Recipe { + @Override + public String getDisplayName() { + return "Convert JMockit `@Mocked` and `@Injectable` annotated arguments"; + } + + @Override + public String getDescription() { + return "Convert JMockit `@Mocked` and `@Injectable` annotated arguments into Mockito statements."; + } + + @Override + public TreeVisitor getVisitor() { + return Preconditions.check( + Preconditions.or( + new UsesType<>("mockit.Mocked", false), + new UsesType<>("mockit.Injectable", false) + ), + new JavaIsoVisitor() { + @Override + public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDeclaration, ExecutionContext ctx) { + J.MethodDeclaration md = super.visitMethodDeclaration(methodDeclaration, ctx); + + List parameters = md.getParameters(); + if (!parameters.isEmpty() && !(parameters.get(0) instanceof J.Empty)) { + maybeRemoveImport("mockit.Injectable"); + maybeRemoveImport("mockit.Mocked"); + maybeAddImport("org.mockito.Mockito"); + + // Create lists to store the mocked parameters and the new type parameters + List mockedParameter = new ArrayList<>(); + + // Remove any mocked parameters from the method declaration + md = md.withParameters(ListUtils.map(parameters, parameter -> { + if (parameter instanceof J.VariableDeclarations) { + J.VariableDeclarations variableDeclarations = (J.VariableDeclarations) parameter; + // Check if the parameter has the annotation "mockit.Mocked or mockit.Injectable" + if (!FindAnnotations.find(variableDeclarations, "mockit.Injectable").isEmpty() || + !FindAnnotations.find(variableDeclarations, "mockit.Mocked").isEmpty() ) { + mockedParameter.add(variableDeclarations); + return null; + } + } + return parameter; + })); + + // Add mocked parameters as statements to the method declaration + if (!mockedParameter.isEmpty()) { + JavaTemplate addStatementsTemplate = JavaTemplate.builder("#{} #{} = Mockito.mock(#{}.class);\n") + .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12")) + .imports("org.mockito.Mockito") + .contextSensitive() + .build(); + // Retain argument order by iterating in reverse + for (int i = mockedParameter.size() - 1; i >= 0; i--) { + J.VariableDeclarations variableDeclarations = mockedParameter.get(i); + // Apply the template and update the method declaration + md = addStatementsTemplate.apply(updateCursor(md), + md.getBody().getCoordinates().firstStatement(), + variableDeclarations.getTypeExpression().toString(), + variableDeclarations.getVariables().get(0).getSimpleName(), + variableDeclarations.getTypeExpression().toString()); + } + } + } + return md; + } + } + ); + } +} diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitMockedVariableToMockito.java b/src/main/java/org/openrewrite/java/testing/jmockit/JMockitMockedVariableToMockito.java deleted file mode 100644 index 80e7ce019..000000000 --- a/src/main/java/org/openrewrite/java/testing/jmockit/JMockitMockedVariableToMockito.java +++ /dev/null @@ -1,106 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - *

- * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * https://www.apache.org/licenses/LICENSE-2.0 - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.java.testing.jmockit; - -import lombok.EqualsAndHashCode; -import lombok.Value; -import org.openrewrite.ExecutionContext; -import org.openrewrite.Preconditions; -import org.openrewrite.Recipe; -import org.openrewrite.TreeVisitor; -import org.openrewrite.internal.ListUtils; -import org.openrewrite.internal.lang.NonNullApi; -import org.openrewrite.java.JavaIsoVisitor; -import org.openrewrite.java.JavaParser; -import org.openrewrite.java.JavaTemplate; -import org.openrewrite.java.search.FindAnnotations; -import org.openrewrite.java.search.UsesType; -import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.Statement; - -import java.util.ArrayList; -import java.util.List; - -@Value -@EqualsAndHashCode(callSuper = false) -@NonNullApi -public class JMockitMockedVariableToMockito extends Recipe { - @Override - public String getDisplayName() { - return "Rewrite JMockit Mocked Variable"; - } - - @Override - public String getDescription() { - return "Rewrites JMockit `Mocked Variable` to Mockito statements."; - } - - @Override - public TreeVisitor getVisitor() { - return Preconditions.check(new UsesType<>("mockit.Mocked", false), - new RewriteMockedVariableVisitor()); - } - - private static class RewriteMockedVariableVisitor extends JavaIsoVisitor { - @Override - public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDeclaration, ExecutionContext ctx) { - J.MethodDeclaration md = super.visitMethodDeclaration(methodDeclaration, ctx); - - List parameters = md.getParameters(); - if (!parameters.isEmpty() && !(parameters.get(0) instanceof J.Empty)) { - maybeRemoveImport("mockit.Mocked"); - maybeAddImport("org.mockito.Mockito"); - - // Create lists to store the mocked parameters and the new type parameters - List mockedParameter = new ArrayList<>(); - - // Remove any mocked parameters from the method declaration - md = md.withParameters(ListUtils.map(parameters, parameter -> { - if (parameter instanceof J.VariableDeclarations) { - J.VariableDeclarations variableDeclarations = (J.VariableDeclarations) parameter; - // Check if the parameter has the annotation "mockit.Mocked" - if (!FindAnnotations.find(variableDeclarations, "mockit.Mocked").isEmpty()) { - mockedParameter.add(variableDeclarations); - return null; - } - } - return parameter; - })); - - // Add mocked parameters as statements to the method declaration - if (!mockedParameter.isEmpty()) { - JavaTemplate addStatementsTemplate = JavaTemplate.builder("#{} #{} = Mockito.mock(#{}.class);\n") - .javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12")) - .imports("org.mockito.Mockito") - .contextSensitive() - .build(); - // Retain argument order by iterating in reverse - for (int i = mockedParameter.size() - 1; i >= 0; i--) { - J.VariableDeclarations variableDeclarations = mockedParameter.get(i); - // Apply the template and update the method declaration - md = addStatementsTemplate.apply(updateCursor(md), - md.getBody().getCoordinates().firstStatement(), - variableDeclarations.getTypeExpression().toString(), - variableDeclarations.getVariables().get(0).getSimpleName(), - variableDeclarations.getTypeExpression().toString()); - } - } - } - - return md; - } - } -} diff --git a/src/main/java/org/openrewrite/java/testing/jmockit/package-info.java b/src/main/java/org/openrewrite/java/testing/jmockit/package-info.java new file mode 100644 index 000000000..85365fe33 --- /dev/null +++ b/src/main/java/org/openrewrite/java/testing/jmockit/package-info.java @@ -0,0 +1,19 @@ +/* + * Copyright 2021 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +@NonNullApi +package org.openrewrite.java.testing.jmockit; + +import org.openrewrite.internal.lang.NonNullApi; diff --git a/src/main/resources/META-INF/rewrite/jmockit.yml b/src/main/resources/META-INF/rewrite/jmockit.yml index dd7d1367a..1b4651ae2 100644 --- a/src/main/resources/META-INF/rewrite/jmockit.yml +++ b/src/main/resources/META-INF/rewrite/jmockit.yml @@ -23,7 +23,7 @@ tags: - jmockit recipeList: - org.openrewrite.java.testing.jmockit.JMockitExpectationsToMockito - - org.openrewrite.java.testing.jmockit.JMockitMockedVariableToMockito + - org.openrewrite.java.testing.jmockit.JMockitAnnotatedArgumentToMockito - org.openrewrite.java.ChangeType: oldFullyQualifiedTypeName: mockit.Mocked newFullyQualifiedTypeName: org.mockito.Mock diff --git a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitMockedVariableToMockitoTest.java b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitAnnotatedArgumentToMockitoTest.java similarity index 59% rename from src/test/java/org/openrewrite/java/testing/jmockit/JMockitMockedVariableToMockitoTest.java rename to src/test/java/org/openrewrite/java/testing/jmockit/JMockitAnnotatedArgumentToMockitoTest.java index 89293f3ef..aba68599f 100644 --- a/src/test/java/org/openrewrite/java/testing/jmockit/JMockitMockedVariableToMockitoTest.java +++ b/src/test/java/org/openrewrite/java/testing/jmockit/JMockitAnnotatedArgumentToMockitoTest.java @@ -24,7 +24,7 @@ import static org.openrewrite.java.Assertions.java; -class JMockitMockedVariableToMockitoTest implements RewriteTest { +class JMockitAnnotatedArgumentToMockitoTest implements RewriteTest { @Override public void defaults(RecipeSpec spec) { spec @@ -32,9 +32,7 @@ public void defaults(RecipeSpec spec) { .logCompilationWarningsAndErrors(true) .classpathFromResources(new InMemoryExecutionContext(), "junit-jupiter-api-5.9", - "jmockit-1.49", - "mockito-core-3.12", - "mockito-junit-jupiter-3.12" + "jmockit-1.49" )) .recipeFromResource( "/META-INF/rewrite/jmockit.yml", @@ -52,7 +50,7 @@ void mockedVariableTest() { import mockit.Mocked; import static org.junit.jupiter.api.Assertions.assertNotNull; - + class A { @Mocked Object mockedObject; @@ -68,7 +66,7 @@ void test(@Mocked Object o, @Mocked Object o2) { import org.mockito.Mockito; import static org.junit.jupiter.api.Assertions.assertNotNull; - + class A { @Mock Object mockedObject; @@ -86,7 +84,7 @@ void test() { } @Test - void noVariableTest() { + void mockedNoVariableTest() { rewriteRun( //language=java java( @@ -94,7 +92,7 @@ void noVariableTest() { import mockit.Mocked; import static org.junit.jupiter.api.Assertions.assertNotNull; - + class A { @Mocked Object mockedObject; @@ -108,7 +106,86 @@ void test() { import org.mockito.Mock; import static org.junit.jupiter.api.Assertions.assertNotNull; - + + class A { + @Mock + Object mockedObject; + + void test() { + assertNotNull(mockedObject); + } + } + """ + ) + ); + } + + @Test + void injectableVariableTest() { + //language=java + rewriteRun( + java( + """ + import mockit.Injectable; + + import static org.junit.jupiter.api.Assertions.assertNotNull; + + class A { + @Injectable + Object mockedObject; + + void test(@Injectable Object o, @Injectable Object o2) { + assertNotNull(o); + assertNotNull(o2); + } + } + """, + """ + import org.mockito.Mock; + import org.mockito.Mockito; + + import static org.junit.jupiter.api.Assertions.assertNotNull; + + class A { + @Mock + Object mockedObject; + + void test() { + Object o = Mockito.mock(Object.class); + Object o2 = Mockito.mock(Object.class); + assertNotNull(o); + assertNotNull(o2); + } + } + """ + ) + ); + } + + @Test + void injectableNoVariableTest() { + rewriteRun( + //language=java + java( + """ + import mockit.Injectable; + + import static org.junit.jupiter.api.Assertions.assertNotNull; + + class A { + @Injectable + Object mockedObject; + + void test() { + assertNotNull(mockedObject); + } + } + """, + """ + import org.mockito.Mock; + + import static org.junit.jupiter.api.Assertions.assertNotNull; + class A { @Mock Object mockedObject; From c88a886537b7267e81d9436000345a814608d13e Mon Sep 17 00:00:00 2001 From: Amitoj Duggal Date: Mon, 17 Jun 2024 22:35:25 +0200 Subject: [PATCH 11/19] Adding Kotlin support for the UpdateBeforeAfterAnnotations, along with new tests for kotlin --- .../junit5/UpdateBeforeAfterAnnotations.java | 8 +- .../UpdateBeforeAfterAnnotationsTest.java | 207 +++++++++++++++--- 2 files changed, 180 insertions(+), 35 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/UpdateBeforeAfterAnnotationsTest.java b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java index 47d723d7a..4e8f0c244 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java @@ -15,16 +15,16 @@ */ package org.openrewrite.java.testing.junit5; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; 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,9 +34,12 @@ 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()); } + @DocumentExample @Test void beforeToBeforeEach() { @@ -45,9 +48,9 @@ void beforeToBeforeEach() { java( """ import org.junit.Before; - + class Test { - + @Before void before() { } @@ -55,18 +58,42 @@ 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() { + } + } + """ ) ); } + @Test void afterToAfterEach() { //language=java @@ -74,9 +101,9 @@ void afterToAfterEach() { java( """ import org.junit.After; - + class Test { - + @After void after() { } @@ -84,18 +111,42 @@ void after() { """, """ import org.junit.jupiter.api.AfterEach; - + class Test { - + @AfterEach void after() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.After + + class Test { + + @After + fun after() { + } + } + """, + """ + import org.junit.jupiter.api.AfterEach + + class Test { + + @AfterEach + fun after() { + } + } + """ ) ); } + @Test void beforeClassToBeforeAll() { //language=java @@ -103,9 +154,9 @@ void beforeClassToBeforeAll() { java( """ import org.junit.BeforeClass; - + class Test { - + @BeforeClass void beforeClass() { } @@ -113,18 +164,42 @@ void beforeClass() { """, """ import org.junit.jupiter.api.BeforeAll; - + class Test { - + @BeforeAll void beforeClass() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.BeforeClass + + class Test { + + @BeforeClass + fun beforeClass() { + } + } + """, + """ + import org.junit.jupiter.api.BeforeAll + + class Test { + + @BeforeAll + fun beforeClass() { + } + } + """ ) ); } + @Test void afterClassToAfterAll() { //language=java @@ -132,7 +207,7 @@ void afterClassToAfterAll() { java( """ import org.junit.AfterClass; - + class Test { @AfterClass void afterClass() { @@ -141,19 +216,38 @@ void afterClass() { """, """ import org.junit.jupiter.api.AfterAll; - + class Test { @AfterAll void afterClass() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.AfterClass + + class Test { + @AfterClass + fun afterClass() { + } + } + """, + """ + import org.junit.jupiter.api.AfterAll + + class Test { + @AfterAll + fun afterClass() { + } + } + """ ) ); } - @Issue("https://github.com/openrewrite/rewrite/issues/150") - @Disabled @Test void convertsToPackageVisibility() { //language=java @@ -161,9 +255,9 @@ void convertsToPackageVisibility() { java( """ import org.junit.Before; - + class Test { - + @Before // comments public void before() { } @@ -171,12 +265,34 @@ public void before() { """, """ import org.junit.jupiter.api.BeforeEach; - + class Test { - - // comments - @BeforeEach - void before() { + + @BeforeEach // comments + public void before() { + } + } + """ + ), + //language=kotlin + kotlin( + """ + import org.junit.Before + + class Test { + + @Before // comments + fun before() { + } + } + """, + """ + import org.junit.jupiter.api.BeforeEach + + class Test { + + @BeforeEach // comments + fun before() { } } """ @@ -184,8 +300,8 @@ void before() { ); } + @Test - @Disabled("Issue #59") void beforeMethodOverridesPublicAbstract() { //language=java rewriteRun( @@ -200,9 +316,9 @@ public class AbstractTest { java( """ import org.junit.Before; - + public class A extends AbstractTest { - + @Before public void setup() { } @@ -210,14 +326,45 @@ public void setup() { """, """ import org.junit.jupiter.api.BeforeEach; - + public class A extends AbstractTest { - + @BeforeEach public void setup() { } } """ + ), + //language=kotlin + kotlin( + """ + abstract class AbstractTest { + abstract fun setup() + } + """ + ), + //language=kotlin + kotlin( + """ + import org.junit.Before + + class A : AbstractTest() { + + @Before + fun setup() { + } + } + """, + """ + import org.junit.jupiter.api.BeforeEach + + class A : AbstractTest() { + + @BeforeEach + fun setup() { + } + } + """ ) ); } From df099f0154185eb03a9eef2bddaba6be0296a75c Mon Sep 17 00:00:00 2001 From: Amitoj Duggal Date: Tue, 18 Jun 2024 23:41:32 +0200 Subject: [PATCH 12/19] Added kotlin tests for AddParameterizedTestAnnotation --- .../AddParameterizedTestAnnotationTest.java | 361 +++++++++++++++++- 1 file changed, 359 insertions(+), 2 deletions(-) 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..801ef1fed 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java @@ -18,12 +18,13 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; 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,10 +32,11 @@ 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()); } - @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/314") @Test @DocumentExample void replaceTestWithParameterizedTest() { @@ -67,6 +69,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) + } + } + """ ) ); } @@ -102,6 +133,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) + } + } + """ ) ); } @@ -125,6 +185,19 @@ void printMessage() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.api.Test + + class NumbersTest { + @Test + fun printMessage() { + System.out.println("message") + } + } + """ ) ); } @@ -158,6 +231,33 @@ void processUserData(String email) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.CsvSource + import org.junit.jupiter.api.Test + + class TestClass { + @Test + @CsvSource(["test@test.com"]) + fun processUserData(email: String) { + System.out.println(email) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.CsvSource + + class TestClass { + @ParameterizedTest + @CsvSource(["test@test.com"]) + fun processUserData(email: String) { + System.out.println(email) + } + } + """ ) ); } @@ -191,6 +291,33 @@ void foo() { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.api.Test + import org.junit.jupiter.params.provider.MethodSource + + class TestClass { + @Test + @MethodSource + fun foo() { + System.out.println("bar") + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.MethodSource + + class TestClass { + @ParameterizedTest + @MethodSource + fun foo() { + System.out.println("bar") + } + } + """ ) ); } @@ -226,6 +353,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 TestClass { + @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 TestClass { + @ParameterizedTest + @ValueSource(ints = [1, 3, 5, -3, 15, Int.MAX_VALUE]) + fun testIsOdd(number: Int) { + assertTrue(number % 2 != 0) + } + } + """ ) ); } @@ -259,6 +415,33 @@ void processUserData(String email) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.NullSource + import org.junit.jupiter.api.Test + + class TestClass { + @Test + @NullSource + fun processUserData(email: String?) { + System.out.println(email) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.NullSource + + class TestClass { + @ParameterizedTest + @NullSource + fun processUserData(email: String?) { + System.out.println(email) + } + } + """ ) ); } @@ -292,6 +475,33 @@ void processUserData(String email) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.EmptySource + import org.junit.jupiter.api.Test + + class TestClass { + @Test + @EmptySource + fun processUserData(email: String) { + System.out.println(email) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.EmptySource + + class TestClass { + @ParameterizedTest + @EmptySource + fun processUserData(email: String) { + System.out.println(email) + } + } + """ ) ); } @@ -325,6 +535,33 @@ void processUserData(String email) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.NullAndEmptySource + import org.junit.jupiter.api.Test + + class TestClass { + @Test + @NullAndEmptySource + fun processUserData(email: String?) { + System.out.println(email) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.NullAndEmptySource + + class TestClass { + @ParameterizedTest + @NullAndEmptySource + fun processUserData(email: String?) { + System.out.println(email) + } + } + """ ) ); } @@ -372,6 +609,47 @@ void processTime(time timeOfDay) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.EnumSource + import org.junit.jupiter.api.Test + + class TestClass { + enum class time { + MORNING, + NOON, + AFTERNOON, + MIDNIGHT + } + + @Test + @EnumSource + fun processTime(timeOfDay: time) { + System.out.println("Its " + timeOfDay) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.EnumSource + + class TestClass { + enum class time { + MORNING, + NOON, + AFTERNOON, + MIDNIGHT + } + + @ParameterizedTest + @EnumSource + fun processTime(timeOfDay: time) { + System.out.println("Its " + timeOfDay) + } + } + """ ) ); } @@ -409,6 +687,37 @@ void testWithCsvFileSourceFromFile(String country, int reference) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.params.provider.CsvFileSource + import org.junit.jupiter.api.Test + import org.junit.jupiter.api.Assertions.* + + class TestClass { + @Test + @CsvFileSource(files = ["src/test/resources/two-column.csv"], numLinesToSkip = 1) + fun testWithCsvFileSourceFromFile(country: String, reference: Int) { + assertNotNull(country) + assertNotEquals(0, reference) + } + } + """, + """ + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.CsvFileSource + import org.junit.jupiter.api.Assertions.* + + class TestClass { + @ParameterizedTest + @CsvFileSource(files = ["src/test/resources/two-column.csv"], numLinesToSkip = 1) + fun testWithCsvFileSourceFromFile(country: String, reference: Int) { + assertNotNull(country) + assertNotEquals(0, reference) + } + } + """ ) ); } @@ -464,6 +773,54 @@ public Stream provideArguments(ExtensionContext context) { } } """ + ), + //language=kotlin + kotlin( + """ + import org.junit.jupiter.api.extension.ExtensionContext + import org.junit.jupiter.params.provider.Arguments + import org.junit.jupiter.params.provider.ArgumentsProvider + import org.junit.jupiter.params.provider.ArgumentsSource + import org.junit.jupiter.api.Test + import java.util.stream.Stream + import org.junit.jupiter.api.Assertions.* + + class TestClass { + @Test + @ArgumentsSource(MyArgumentsProvider::class) + fun testWithArgumentsSource(argument: String) { + assertNotNull(argument) + } + class MyArgumentsProvider : ArgumentsProvider { + override fun provideArguments(context: ExtensionContext): Stream { + return Stream.of("apple", "banana").map { Arguments.of(it) } + } + } + } + """, + + """ + import org.junit.jupiter.api.extension.ExtensionContext + import org.junit.jupiter.params.ParameterizedTest + import org.junit.jupiter.params.provider.Arguments + import org.junit.jupiter.params.provider.ArgumentsProvider + import org.junit.jupiter.params.provider.ArgumentsSource + import java.util.stream.Stream + import org.junit.jupiter.api.Assertions.* + + class TestClass { + @ParameterizedTest + @ArgumentsSource(MyArgumentsProvider::class) + fun testWithArgumentsSource(argument: String) { + assertNotNull(argument) + } + class MyArgumentsProvider : ArgumentsProvider { + override fun provideArguments(context: ExtensionContext): Stream { + return Stream.of("apple", "banana").map { Arguments.of(it) } + } + } + } + """ ) ); } From ddd9a1e5a5a2f9114e926a4c6d0097aea48e77b4 Mon Sep 17 00:00:00 2001 From: Amitoj Duggal Date: Thu, 20 Jun 2024 22:59:29 +0200 Subject: [PATCH 13/19] Restoring changes to issue to retain the references --- .../java/testing/junit5/AddParameterizedTestAnnotationTest.java | 2 ++ 1 file changed, 2 insertions(+) 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 801ef1fed..3f40a26ee 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.Issue; import org.openrewrite.java.JavaParser; import org.openrewrite.kotlin.KotlinParser; import org.openrewrite.test.RecipeSpec; @@ -37,6 +38,7 @@ public void defaults(RecipeSpec spec) { .recipe(new AddParameterizedTestAnnotation()); } + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/314") @Test @DocumentExample void replaceTestWithParameterizedTest() { From a1eba6b4e25cc176d9034a32631b8e564f6a97d8 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Fri, 21 Jun 2024 00:20:24 +0200 Subject: [PATCH 14/19] Remove trailing whitespace in text blocks --- .../AddParameterizedTestAnnotationTest.java | 46 +++++++++---------- .../UpdateBeforeAfterAnnotationsTest.java | 45 +++++++++--------- 2 files changed, 45 insertions(+), 46 deletions(-) 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 3f40a26ee..16211dcab 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java @@ -49,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}) @@ -62,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}) @@ -113,7 +113,7 @@ void replaceTestWithParameterizedTestRegardlessOfOrder() { import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.*; - + class NumbersTest { @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) @Test @@ -126,7 +126,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}) @@ -179,7 +179,7 @@ void onlyReplacesWithValueSourceAnnotation() { java( """ import org.junit.jupiter.api.Test; - + class NumbersTest { @Test void printMessage() { @@ -212,7 +212,7 @@ void replacesCsvSource() { """ import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.api.Test; - + class TestClass { @Test @CsvSource({"test@test.com"}) @@ -224,7 +224,7 @@ void processUserData(String email) { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; - + class TestClass { @ParameterizedTest @CsvSource({"test@test.com"}) @@ -272,7 +272,7 @@ void replacesMethodSource() { """ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.MethodSource; - + class TestClass { @Test @MethodSource() @@ -284,7 +284,7 @@ void foo() { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; - + class TestClass { @ParameterizedTest @MethodSource() @@ -333,7 +333,7 @@ void addMissingAnnotation() { import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.*; - + class TestClass { @Test @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) @@ -346,7 +346,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 TestClass { @ParameterizedTest @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) @@ -396,7 +396,7 @@ void replacesNullSource() { """ import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.api.Test; - + class TestClass { @Test @NullSource @@ -408,7 +408,7 @@ void processUserData(String email) { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullSource; - + class TestClass { @ParameterizedTest @NullSource @@ -456,7 +456,7 @@ void replacesEmptySource() { """ import org.junit.jupiter.params.provider.EmptySource; import org.junit.jupiter.api.Test; - + class TestClass { @Test @EmptySource @@ -468,7 +468,7 @@ void processUserData(String email) { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EmptySource; - + class TestClass { @ParameterizedTest @EmptySource @@ -516,7 +516,7 @@ void replacesNullAndEmptySource() { """ import org.junit.jupiter.params.provider.NullAndEmptySource; import org.junit.jupiter.api.Test; - + class TestClass { @Test @NullAndEmptySource @@ -528,7 +528,7 @@ void processUserData(String email) { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullAndEmptySource; - + class TestClass { @ParameterizedTest @NullAndEmptySource @@ -576,7 +576,7 @@ void replacesEnumSource() { """ import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.api.Test; - + class TestClass { enum time { MORNING, @@ -584,7 +584,7 @@ enum time { AFTERNOON, MIDNIGHT } - + @Test @EnumSource void processTime(time timeOfDay) { @@ -595,7 +595,7 @@ void processTime(time timeOfDay) { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; - + class TestClass { enum time { MORNING, @@ -603,7 +603,7 @@ enum time { AFTERNOON, MIDNIGHT } - + @ParameterizedTest @EnumSource void processTime(time timeOfDay) { @@ -665,7 +665,7 @@ void replacesCsvFileSource() { import org.junit.jupiter.params.provider.CsvFileSource; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; - + class TestClass { @Test @CsvFileSource(files = "src/test/resources/two-column.csv", numLinesToSkip = 1) @@ -679,7 +679,7 @@ void testWithCsvFileSourceFromFile(String country, int reference) { import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; import static org.junit.jupiter.api.Assertions.*; - + class TestClass { @ParameterizedTest @CsvFileSource(files = "src/test/resources/two-column.csv", numLinesToSkip = 1) 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 4e8f0c244..c0afb91ed 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java @@ -48,9 +48,9 @@ void beforeToBeforeEach() { java( """ import org.junit.Before; - + class Test { - + @Before void before() { } @@ -58,9 +58,9 @@ void before() { """, """ import org.junit.jupiter.api.BeforeEach; - + class Test { - + @BeforeEach void before() { } @@ -101,9 +101,9 @@ void afterToAfterEach() { java( """ import org.junit.After; - + class Test { - + @After void after() { } @@ -111,9 +111,9 @@ void after() { """, """ import org.junit.jupiter.api.AfterEach; - + class Test { - + @AfterEach void after() { } @@ -154,9 +154,9 @@ void beforeClassToBeforeAll() { java( """ import org.junit.BeforeClass; - + class Test { - + @BeforeClass void beforeClass() { } @@ -164,9 +164,9 @@ void beforeClass() { """, """ import org.junit.jupiter.api.BeforeAll; - + class Test { - + @BeforeAll void beforeClass() { } @@ -207,7 +207,7 @@ void afterClassToAfterAll() { java( """ import org.junit.AfterClass; - + class Test { @AfterClass void afterClass() { @@ -216,7 +216,7 @@ void afterClass() { """, """ import org.junit.jupiter.api.AfterAll; - + class Test { @AfterAll void afterClass() { @@ -255,9 +255,9 @@ void convertsToPackageVisibility() { java( """ import org.junit.Before; - + class Test { - + @Before // comments public void before() { } @@ -265,9 +265,9 @@ public void before() { """, """ import org.junit.jupiter.api.BeforeEach; - + class Test { - + @BeforeEach // comments public void before() { } @@ -300,7 +300,6 @@ fun before() { ); } - @Test void beforeMethodOverridesPublicAbstract() { //language=java @@ -316,9 +315,9 @@ public class AbstractTest { java( """ import org.junit.Before; - + public class A extends AbstractTest { - + @Before public void setup() { } @@ -326,9 +325,9 @@ public void setup() { """, """ import org.junit.jupiter.api.BeforeEach; - + public class A extends AbstractTest { - + @BeforeEach public void setup() { } From b42b321651be532a9252fd0e41e359434cc1213a Mon Sep 17 00:00:00 2001 From: Amitoj Duggal Date: Fri, 21 Jun 2024 09:55:36 +0200 Subject: [PATCH 15/19] Update src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java Co-authored-by: Tim te Beek --- .../java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java | 1 + 1 file changed, 1 insertion(+) 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 c0afb91ed..c99863cf8 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java @@ -248,6 +248,7 @@ fun afterClass() { ); } + @Issue("https://github.com/openrewrite/rewrite/issues/150") @Test void convertsToPackageVisibility() { //language=java From 8423915f7a5d684e4bb47c5327629a3d7581ac31 Mon Sep 17 00:00:00 2001 From: Amitoj Duggal Date: Fri, 21 Jun 2024 09:59:34 +0200 Subject: [PATCH 16/19] 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. --- .../AddParameterizedTestAnnotationTest.java | 368 +----------------- 1 file changed, 21 insertions(+), 347 deletions(-) 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 16211dcab..5103fea4d 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/AddParameterizedTestAnnotationTest.java @@ -113,7 +113,7 @@ void replaceTestWithParameterizedTestRegardlessOfOrder() { import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.*; - + class NumbersTest { @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) @Test @@ -126,7 +126,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}) @@ -135,35 +135,6 @@ 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) - } - } - """ ) ); } @@ -179,7 +150,7 @@ void onlyReplacesWithValueSourceAnnotation() { java( """ import org.junit.jupiter.api.Test; - + class NumbersTest { @Test void printMessage() { @@ -187,19 +158,6 @@ void printMessage() { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.jupiter.api.Test - - class NumbersTest { - @Test - fun printMessage() { - System.out.println("message") - } - } - """ ) ); } @@ -212,7 +170,7 @@ void replacesCsvSource() { """ import org.junit.jupiter.params.provider.CsvSource; import org.junit.jupiter.api.Test; - + class TestClass { @Test @CsvSource({"test@test.com"}) @@ -224,7 +182,7 @@ void processUserData(String email) { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; - + class TestClass { @ParameterizedTest @CsvSource({"test@test.com"}) @@ -233,33 +191,6 @@ void processUserData(String email) { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.jupiter.params.provider.CsvSource - import org.junit.jupiter.api.Test - - class TestClass { - @Test - @CsvSource(["test@test.com"]) - fun processUserData(email: String) { - System.out.println(email) - } - } - """, - """ - import org.junit.jupiter.params.ParameterizedTest - import org.junit.jupiter.params.provider.CsvSource - - class TestClass { - @ParameterizedTest - @CsvSource(["test@test.com"]) - fun processUserData(email: String) { - System.out.println(email) - } - } - """ ) ); } @@ -272,7 +203,7 @@ void replacesMethodSource() { """ import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.MethodSource; - + class TestClass { @Test @MethodSource() @@ -284,7 +215,7 @@ void foo() { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; - + class TestClass { @ParameterizedTest @MethodSource() @@ -293,33 +224,6 @@ void foo() { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.jupiter.api.Test - import org.junit.jupiter.params.provider.MethodSource - - class TestClass { - @Test - @MethodSource - fun foo() { - System.out.println("bar") - } - } - """, - """ - import org.junit.jupiter.params.ParameterizedTest - import org.junit.jupiter.params.provider.MethodSource - - class TestClass { - @ParameterizedTest - @MethodSource - fun foo() { - System.out.println("bar") - } - } - """ ) ); } @@ -333,7 +237,7 @@ void addMissingAnnotation() { import org.junit.jupiter.api.Test; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.*; - + class TestClass { @Test @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) @@ -346,7 +250,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 TestClass { @ParameterizedTest @ValueSource(ints = {1, 3, 5, -3, 15, Integer.MAX_VALUE}) @@ -355,35 +259,6 @@ 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 TestClass { - @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 TestClass { - @ParameterizedTest - @ValueSource(ints = [1, 3, 5, -3, 15, Int.MAX_VALUE]) - fun testIsOdd(number: Int) { - assertTrue(number % 2 != 0) - } - } - """ ) ); } @@ -396,7 +271,7 @@ void replacesNullSource() { """ import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.api.Test; - + class TestClass { @Test @NullSource @@ -408,7 +283,7 @@ void processUserData(String email) { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullSource; - + class TestClass { @ParameterizedTest @NullSource @@ -417,33 +292,6 @@ void processUserData(String email) { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.jupiter.params.provider.NullSource - import org.junit.jupiter.api.Test - - class TestClass { - @Test - @NullSource - fun processUserData(email: String?) { - System.out.println(email) - } - } - """, - """ - import org.junit.jupiter.params.ParameterizedTest - import org.junit.jupiter.params.provider.NullSource - - class TestClass { - @ParameterizedTest - @NullSource - fun processUserData(email: String?) { - System.out.println(email) - } - } - """ ) ); } @@ -456,7 +304,7 @@ void replacesEmptySource() { """ import org.junit.jupiter.params.provider.EmptySource; import org.junit.jupiter.api.Test; - + class TestClass { @Test @EmptySource @@ -468,7 +316,7 @@ void processUserData(String email) { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EmptySource; - + class TestClass { @ParameterizedTest @EmptySource @@ -477,33 +325,6 @@ void processUserData(String email) { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.jupiter.params.provider.EmptySource - import org.junit.jupiter.api.Test - - class TestClass { - @Test - @EmptySource - fun processUserData(email: String) { - System.out.println(email) - } - } - """, - """ - import org.junit.jupiter.params.ParameterizedTest - import org.junit.jupiter.params.provider.EmptySource - - class TestClass { - @ParameterizedTest - @EmptySource - fun processUserData(email: String) { - System.out.println(email) - } - } - """ ) ); } @@ -516,7 +337,7 @@ void replacesNullAndEmptySource() { """ import org.junit.jupiter.params.provider.NullAndEmptySource; import org.junit.jupiter.api.Test; - + class TestClass { @Test @NullAndEmptySource @@ -528,7 +349,7 @@ void processUserData(String email) { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.NullAndEmptySource; - + class TestClass { @ParameterizedTest @NullAndEmptySource @@ -537,33 +358,6 @@ void processUserData(String email) { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.jupiter.params.provider.NullAndEmptySource - import org.junit.jupiter.api.Test - - class TestClass { - @Test - @NullAndEmptySource - fun processUserData(email: String?) { - System.out.println(email) - } - } - """, - """ - import org.junit.jupiter.params.ParameterizedTest - import org.junit.jupiter.params.provider.NullAndEmptySource - - class TestClass { - @ParameterizedTest - @NullAndEmptySource - fun processUserData(email: String?) { - System.out.println(email) - } - } - """ ) ); } @@ -576,7 +370,7 @@ void replacesEnumSource() { """ import org.junit.jupiter.params.provider.EnumSource; import org.junit.jupiter.api.Test; - + class TestClass { enum time { MORNING, @@ -584,7 +378,7 @@ enum time { AFTERNOON, MIDNIGHT } - + @Test @EnumSource void processTime(time timeOfDay) { @@ -595,7 +389,7 @@ void processTime(time timeOfDay) { """ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; - + class TestClass { enum time { MORNING, @@ -603,7 +397,7 @@ enum time { AFTERNOON, MIDNIGHT } - + @ParameterizedTest @EnumSource void processTime(time timeOfDay) { @@ -611,47 +405,6 @@ void processTime(time timeOfDay) { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.jupiter.params.provider.EnumSource - import org.junit.jupiter.api.Test - - class TestClass { - enum class time { - MORNING, - NOON, - AFTERNOON, - MIDNIGHT - } - - @Test - @EnumSource - fun processTime(timeOfDay: time) { - System.out.println("Its " + timeOfDay) - } - } - """, - """ - import org.junit.jupiter.params.ParameterizedTest - import org.junit.jupiter.params.provider.EnumSource - - class TestClass { - enum class time { - MORNING, - NOON, - AFTERNOON, - MIDNIGHT - } - - @ParameterizedTest - @EnumSource - fun processTime(timeOfDay: time) { - System.out.println("Its " + timeOfDay) - } - } - """ ) ); } @@ -665,7 +418,7 @@ void replacesCsvFileSource() { import org.junit.jupiter.params.provider.CsvFileSource; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; - + class TestClass { @Test @CsvFileSource(files = "src/test/resources/two-column.csv", numLinesToSkip = 1) @@ -679,7 +432,7 @@ void testWithCsvFileSourceFromFile(String country, int reference) { import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvFileSource; import static org.junit.jupiter.api.Assertions.*; - + class TestClass { @ParameterizedTest @CsvFileSource(files = "src/test/resources/two-column.csv", numLinesToSkip = 1) @@ -689,37 +442,6 @@ void testWithCsvFileSourceFromFile(String country, int reference) { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.jupiter.params.provider.CsvFileSource - import org.junit.jupiter.api.Test - import org.junit.jupiter.api.Assertions.* - - class TestClass { - @Test - @CsvFileSource(files = ["src/test/resources/two-column.csv"], numLinesToSkip = 1) - fun testWithCsvFileSourceFromFile(country: String, reference: Int) { - assertNotNull(country) - assertNotEquals(0, reference) - } - } - """, - """ - import org.junit.jupiter.params.ParameterizedTest - import org.junit.jupiter.params.provider.CsvFileSource - import org.junit.jupiter.api.Assertions.* - - class TestClass { - @ParameterizedTest - @CsvFileSource(files = ["src/test/resources/two-column.csv"], numLinesToSkip = 1) - fun testWithCsvFileSourceFromFile(country: String, reference: Int) { - assertNotNull(country) - assertNotEquals(0, reference) - } - } - """ ) ); } @@ -775,54 +497,6 @@ public Stream provideArguments(ExtensionContext context) { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.jupiter.api.extension.ExtensionContext - import org.junit.jupiter.params.provider.Arguments - import org.junit.jupiter.params.provider.ArgumentsProvider - import org.junit.jupiter.params.provider.ArgumentsSource - import org.junit.jupiter.api.Test - import java.util.stream.Stream - import org.junit.jupiter.api.Assertions.* - - class TestClass { - @Test - @ArgumentsSource(MyArgumentsProvider::class) - fun testWithArgumentsSource(argument: String) { - assertNotNull(argument) - } - class MyArgumentsProvider : ArgumentsProvider { - override fun provideArguments(context: ExtensionContext): Stream { - return Stream.of("apple", "banana").map { Arguments.of(it) } - } - } - } - """, - - """ - import org.junit.jupiter.api.extension.ExtensionContext - import org.junit.jupiter.params.ParameterizedTest - import org.junit.jupiter.params.provider.Arguments - import org.junit.jupiter.params.provider.ArgumentsProvider - import org.junit.jupiter.params.provider.ArgumentsSource - import java.util.stream.Stream - import org.junit.jupiter.api.Assertions.* - - class TestClass { - @ParameterizedTest - @ArgumentsSource(MyArgumentsProvider::class) - fun testWithArgumentsSource(argument: String) { - assertNotNull(argument) - } - class MyArgumentsProvider : ArgumentsProvider { - override fun provideArguments(context: ExtensionContext): Stream { - return Stream.of("apple", "banana").map { Arguments.of(it) } - } - } - } - """ ) ); } From 2799ad2648d196e4ec87033eb1bfe114601b0417 Mon Sep 17 00:00:00 2001 From: Amitoj Duggal Date: Fri, 21 Jun 2024 10:02:31 +0200 Subject: [PATCH 17/19] Removing extra tests for Kotlin, and keeping one, fixing issue with failing build. --- .../UpdateBeforeAfterAnnotationsTest.java | 130 +----------------- 1 file changed, 4 insertions(+), 126 deletions(-) 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 c99863cf8..9a63a6bd6 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java @@ -18,6 +18,7 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.Issue; import org.openrewrite.java.JavaParser; import org.openrewrite.kotlin.KotlinParser; import org.openrewrite.test.RecipeSpec; @@ -93,7 +94,6 @@ fun before() { ); } - @Test void afterToAfterEach() { //language=java @@ -119,34 +119,10 @@ void after() { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.After - - class Test { - - @After - fun after() { - } - } - """, - """ - import org.junit.jupiter.api.AfterEach - - class Test { - - @AfterEach - fun after() { - } - } - """ ) ); } - @Test void beforeClassToBeforeAll() { //language=java @@ -172,34 +148,10 @@ void beforeClass() { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.BeforeClass - - class Test { - - @BeforeClass - fun beforeClass() { - } - } - """, - """ - import org.junit.jupiter.api.BeforeAll - - class Test { - - @BeforeAll - fun beforeClass() { - } - } - """ ) ); } - @Test void afterClassToAfterAll() { //language=java @@ -223,27 +175,6 @@ void afterClass() { } } """ - ), - //language=kotlin - kotlin( - """ - import org.junit.AfterClass - - class Test { - @AfterClass - fun afterClass() { - } - } - """, - """ - import org.junit.jupiter.api.AfterAll - - class Test { - @AfterAll - fun afterClass() { - } - } - """ ) ); } @@ -269,31 +200,9 @@ public void before() { class Test { - @BeforeEach // comments - public void before() { - } - } - """ - ), - //language=kotlin - kotlin( - """ - import org.junit.Before - - class Test { - - @Before // comments - fun before() { - } - } - """, - """ - import org.junit.jupiter.api.BeforeEach - - class Test { - - @BeforeEach // comments - fun before() { + // comments + @BeforeEach + void before() { } } """ @@ -334,37 +243,6 @@ public void setup() { } } """ - ), - //language=kotlin - kotlin( - """ - abstract class AbstractTest { - abstract fun setup() - } - """ - ), - //language=kotlin - kotlin( - """ - import org.junit.Before - - class A : AbstractTest() { - - @Before - fun setup() { - } - } - """, - """ - import org.junit.jupiter.api.BeforeEach - - class A : AbstractTest() { - - @BeforeEach - fun setup() { - } - } - """ ) ); } From 6049913a8e136bbc0d730dc7dbfba379fc1d839c Mon Sep 17 00:00:00 2001 From: Amitoj Duggal Date: Fri, 21 Jun 2024 11:07:47 +0200 Subject: [PATCH 18/19] Restored changes to the disabled test. --- .../java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java | 2 ++ 1 file changed, 2 insertions(+) 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 9a63a6bd6..75e6f13c5 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java @@ -15,6 +15,7 @@ */ package org.openrewrite.java.testing.junit5; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; import org.openrewrite.InMemoryExecutionContext; @@ -180,6 +181,7 @@ void afterClass() { } @Issue("https://github.com/openrewrite/rewrite/issues/150") + @Disabled @Test void convertsToPackageVisibility() { //language=java From 41e97e6bd45d24babf79c0bdf5e11e7473e041cf Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 23 Jun 2024 14:57:11 +0200 Subject: [PATCH 19/19] Minor touch up --- .../junit5/UpdateBeforeAfterAnnotationsTest.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) 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 75e6f13c5..2c71b6107 100644 --- a/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java +++ b/src/test/java/org/openrewrite/java/testing/junit5/UpdateBeforeAfterAnnotationsTest.java @@ -41,7 +41,6 @@ public void defaults(RecipeSpec spec) { .recipe(new UpdateBeforeAfterAnnotations()); } - @DocumentExample @Test void beforeToBeforeEach() { @@ -50,9 +49,8 @@ void beforeToBeforeEach() { java( """ import org.junit.Before; - + class Test { - @Before void before() { } @@ -60,9 +58,8 @@ void before() { """, """ import org.junit.jupiter.api.BeforeEach; - + class Test { - @BeforeEach void before() { } @@ -213,7 +210,8 @@ void before() { } @Test - void beforeMethodOverridesPublicAbstract() { + @Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/59") + void retainPublicModifierOnOverriddenMethod() { //language=java rewriteRun(