-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from MBoegers/5-add-recipe-to-add-testinstancel…
…ifecycleper_class-to-junit-test-classes 5 add recipe to add testinstancelifecycleper class to junit test classes
- Loading branch information
Showing
3 changed files
with
185 additions
and
1 deletion.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
...main/java/io/github/mboegers/openrewrite/testngtojupiter/AddTestLifecyleToJUnitTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package io.github.mboegers.openrewrite.testngtojupiter; | ||
|
||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.AnnotationMatcher; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import java.time.Duration; | ||
import java.util.Comparator; | ||
|
||
@Value | ||
@EqualsAndHashCode(callSuper = false) | ||
public class AddTestLifecyleToJUnitTests extends Recipe { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Add @TestInstance(TestInstance.Lifecycle.PER_CLASS) to Jupiter tests"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "To align JUnit Jupiter behavior with the expected behavior coming from TestNG the annotation @TestInstance(TestInstance.Lifecycle.PER_CLASS) is needed."; | ||
} | ||
|
||
@Override | ||
public Duration getEstimatedEffortPerOccurrence() { | ||
return Duration.ofMinutes(5); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new AddTestLifecyleToJUnitTests.AddLifecyleAnnotationVisitor(); | ||
} | ||
|
||
static class AddLifecyleAnnotationVisitor extends JavaIsoVisitor<ExecutionContext> { | ||
|
||
private static final AnnotationMatcher TEST_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.Test"); | ||
private static final AnnotationMatcher TEST_INSTANCE_MATCHER = new AnnotationMatcher("@org.junit.jupiter.api.TestInstance"); | ||
|
||
@Override | ||
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext executionContext) { | ||
classDecl = super.visitClassDeclaration(classDecl, executionContext); | ||
|
||
boolean hasTestInstanceAnnotation = classDecl.getLeadingAnnotations().stream().anyMatch(TEST_INSTANCE_MATCHER::matches); | ||
|
||
Boolean usesJUnit = getCursor().pollMessage("USES_JUNIT"); | ||
if (hasTestInstanceAnnotation || usesJUnit == null || !usesJUnit) { | ||
return classDecl; | ||
} | ||
|
||
// transform TestNG @Test to Jupiter | ||
var addAnnotationCoordinate = classDecl.getCoordinates().addAnnotation(Comparator.comparing(J.Annotation::getSimpleName)); | ||
var cursor = getCursor(); | ||
classDecl = JavaTemplate | ||
.builder("@TestInstance(TestInstance.Lifecycle.PER_CLASS)") | ||
.javaParser(JavaParser.fromJavaVersion().classpath("junit-jupiter-api")) | ||
.imports("org.junit.jupiter.api.TestInstance") | ||
.build() | ||
.apply(cursor, addAnnotationCoordinate); | ||
|
||
// update imports | ||
maybeAddImport("org.junit.jupiter.api.TestInstance", false); | ||
|
||
return classDecl; | ||
} | ||
|
||
@Override | ||
public J.Annotation visitAnnotation(J.Annotation annotation, ExecutionContext executionContext) { | ||
annotation = super.visitAnnotation(annotation, executionContext); | ||
|
||
if (TEST_MATCHER.matches(annotation)) { | ||
getCursor().dropParentUntil(J.ClassDeclaration.class::isInstance).putMessage("USES_JUNIT", true); | ||
} | ||
|
||
return annotation; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
...java/io/github/mboegers/openrewrite/testngtojupiter/AddTestLifecycleToJUnitTestsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package io.github.mboegers.openrewrite.testngtojupiter; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class AddTestLifecycleToJUnitTestsTest implements RewriteTest { | ||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.parser(JavaParser.fromJavaVersion() | ||
.logCompilationWarningsAndErrors(true) | ||
.classpath("junit-jupiter-api", "testng")) | ||
.recipe(new AddTestLifecyleToJUnitTests()); | ||
} | ||
|
||
@Test | ||
void addToJUnitTest() { | ||
//language=java | ||
rewriteRun(java( | ||
""" | ||
import org.junit.jupiter.api.Test; | ||
class MyTest { | ||
@Test | ||
void test() {} | ||
} | ||
""", """ | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class MyTest { | ||
@Test | ||
void test() {} | ||
} | ||
""")); | ||
} | ||
|
||
@Test | ||
void addToNestedJUnitTest() { | ||
//language=java | ||
rewriteRun(java( | ||
""" | ||
import org.junit.jupiter.api.Test; | ||
class MyTest { | ||
class InnerTest { | ||
@Test | ||
void test() {} | ||
} | ||
class OtherClass {} | ||
} | ||
""", """ | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
class MyTest { | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class InnerTest { | ||
@Test | ||
void test() {} | ||
} | ||
class OtherClass {} | ||
} | ||
""")); | ||
} | ||
|
||
@Test | ||
void doNetReaddToJUnitTest() { | ||
//language=java | ||
rewriteRun(java(""" | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInstance; | ||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class MyTest { | ||
@Test | ||
void test() {} | ||
} | ||
""")); | ||
} | ||
|
||
@Test | ||
void doNetReaddToTestNGTest() { | ||
//language=java | ||
rewriteRun(java(""" | ||
import org.testng.annotations.Test; | ||
class MyTest { | ||
@Test | ||
void test() {} | ||
} | ||
""")); | ||
} | ||
} |