Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ plugins {
group = "io.moderne.recipe"
description = "Rewrite DevCenter integration."

recipeDependencies {
testParserClasspath("org.junit.jupiter:junit-jupiter-api:6.0.0-RC3")
}

val rewriteVersion = rewriteRecipe.rewriteVersion.get()
dependencies {
implementation(platform("io.moderne.recipe:moderne-recipe-bom:latest.release"))
Expand Down
20 changes: 14 additions & 6 deletions src/main/java/io/moderne/devcenter/JUnitJupiterUpgrade.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
import org.openrewrite.Option;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.marker.JavaSourceSet;
import org.openrewrite.java.search.FindAnnotations;
import org.openrewrite.java.tree.J;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;

public class JUnitJupiterUpgrade extends UpgradeMigrationCard {
@Option(displayName = "Upgrade recipe",
Expand Down Expand Up @@ -57,17 +60,21 @@ public J preVisit(J tree, ExecutionContext ctx) {
J j2 = (J) new FindAnnotations("@org.junit.Test", true)
.getVisitor().visitNonNull(tree, ctx);
if (tree != j2) {
upgradesAndMigrations.insertRow(ctx, JUnitJupiterUpgrade.this,
Measure.JUnit4, "JUnit 4");
upgradesAndMigrations.insertRow(ctx, JUnitJupiterUpgrade.this, Measure.JUnit4, "JUnit 4");
}

J j3 = (J) new FindAnnotations("@org.junit.jupiter.api.Test", true)
.getVisitor().visitNonNull(j2, ctx);
if (tree != j3) {
upgradesAndMigrations.insertRow(ctx, JUnitJupiterUpgrade.this,
Measure.Completed, "JUnit 5");
Optional<JavaSourceSet> first = tree.getMarkers().findFirst(JavaSourceSet.class);
if (first.isPresent() && first.get().getGavToTypes().keySet().stream()
.anyMatch(gav -> gav.startsWith("org.junit.jupiter:junit-jupiter-api:6"))) {
Comment on lines +70 to +71
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In junit5 you can use either API + engine or a single dependency which represents both. If 6 works the same way then perhaps this might miss some Junit 6 usage?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's indeed a choice between two artifact ids; one for convenience, the other which contains the actual classes:

junit-jupiter
JUnit Jupiter aggregator artifact that transitively pulls in dependencies on junit-jupiter-api, junit-jupiter-params, and junit-jupiter-engine for simplified dependency management in build tools such as Gradle and Maven.

junit-jupiter-api
JUnit Jupiter API for writing tests and extensions.

The first one does not contain any classes though: https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter/6.0.0-RC3
Those still come from the junit-jupiter-api that we check here, so I think we should be good to detect JUnit 6 from here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With that I think we can merge, or revise as needed; Would be good to show JUnit 6 now that it's officially released:
https://bsky.app/profile/junit.org/post/3m225gwratc2k

upgradesAndMigrations.insertRow(ctx, JUnitJupiterUpgrade.this, Measure.Completed, "JUnit 6");
return j3;
}
// Can't tell what version of JUnit is on the classpath, but we found JUnit 5 annotations
upgradesAndMigrations.insertRow(ctx, JUnitJupiterUpgrade.this, Measure.JUnit5, "JUnit 5");
}

return j3;
}
};
Expand All @@ -89,7 +96,8 @@ public String getFixRecipeId() {
@Getter
public enum Measure implements DevCenterMeasure {
JUnit4("JUnit 4", "On JUnit 4 or less. Specifically looks for `@org.junit.Test`."),
Completed("Completed", "On JUnit Jupiter");
JUnit5("JUnit 5", "On JUnit Jupiter 5."),
Completed("Completed", "On JUnit Jupiter 6.");

private final @Language("markdown") String name;

Expand Down
37 changes: 34 additions & 3 deletions src/test/java/io/moderne/devcenter/JUnitJupiterUpgradeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@

import io.moderne.devcenter.table.UpgradesAndMigrations;
import org.junit.jupiter.api.Test;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.marker.JavaSourceSet;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.openrewrite.test.SourceSpecs;

import java.util.function.Consumer;

import static io.moderne.devcenter.JUnitJupiterUpgrade.Measure.Completed;
import static io.moderne.devcenter.JUnitJupiterUpgrade.Measure.JUnit4;
import static io.moderne.devcenter.JUnitJupiterUpgrade.Measure.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.java.Assertions.java;

Expand Down Expand Up @@ -70,6 +72,27 @@ void test() {
"""
);

//language=java
SourceSpecs junit6Source = java(
"""
import org.junit.jupiter.api.Test;
class TestWith6 {
@Test
void test() {
}
}
""",
"""
import org.junit.jupiter.api.Test;
class TestWith6 {
/*~~>*/@Test
void test() {
}
}
""",
spec -> spec.markers(JavaSourceSet.build("test",
JavaParser.dependenciesFromResources(new InMemoryExecutionContext(), "junit-jupiter-api"))));

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new JUnitJupiterUpgrade());
Expand All @@ -86,11 +109,19 @@ void junit4() {
@Test
void junit5() {
rewriteRun(
assertUpgradeStatus(Completed, "JUnit 5"),
assertUpgradeStatus(JUnit5, "JUnit 5"),
junit5Source
);
}

@Test
void junit6() {
rewriteRun(
assertUpgradeStatus(Completed, "JUnit 6"),
junit6Source
);
}

@Test
void bothJUnit4And5() {
rewriteRun(
Expand Down
Binary file not shown.