diff --git a/rewrite-maven/src/test/java/org/openrewrite/maven/UpgradeTransitiveDependencyVersionTest.java b/rewrite-maven/src/test/java/org/openrewrite/maven/UpgradeTransitiveDependencyVersionTest.java index 811feaa8267..f523aebbfc0 100644 --- a/rewrite-maven/src/test/java/org/openrewrite/maven/UpgradeTransitiveDependencyVersionTest.java +++ b/rewrite-maven/src/test/java/org/openrewrite/maven/UpgradeTransitiveDependencyVersionTest.java @@ -17,11 +17,13 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; +import org.openrewrite.Issue; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; import static org.openrewrite.maven.Assertions.pomXml; +// Bonus: If there's a way to modify these assertions to do an XML-based comparison instead of a string compare please point that out. These are unnecessarily brittle as-is due to caring about whitespace. class UpgradeTransitiveDependencyVersionTest implements RewriteTest { @Override @@ -35,67 +37,316 @@ public void defaults(RecipeSpec spec) { void singleProject() { rewriteRun( pomXml( - """ - - 4.0.0 - org.openrewrite - core - 0.1.0-SNAPSHOT - - - org.openrewrite - rewrite-java - 7.0.0 - - - - """, """ - - 4.0.0 - org.openrewrite - core - 0.1.0-SNAPSHOT + + 4.0.0 + org.openrewrite + core + 0.1.0-SNAPSHOT + + + org.openrewrite + rewrite-java + 7.0.0 + + + + """, + """ + + 4.0.0 + org.openrewrite + core + 0.1.0-SNAPSHOT + + + + com.fasterxml.jackson.core + jackson-core + 2.12.5 + + + + + + org.openrewrite + rewrite-java + 7.0.0 + + + + """ + ) + ); + } + + @Test + void leavesDirectDependencyUntouched() { + rewriteRun( + pomXml( + """ + + 4.0.0 + org.openrewrite + core + 0.1.0-SNAPSHOT + + + com.fasterxml.jackson.core + jackson-core + 2.12.0 + + + + """ + ) + ); + } + + /* + Demonstrates that this recipe currently takes no "upgrade dependency" action when it observes that a BOM manages the dependency version and that BOM appears to already set it to the desired version number. + The problem with this seems to be that it is looking at the BOM in isolation and doesn't realize that a maven property set in the master POM is overriding a property value in the BOM + and thus causing Maven to use the version number from the parent POM's property value instead of the one seen in the BOM. + + See the XML comments in the "before" XML below. + */ + @Test + @Issue("https://github.com/openrewrite/rewrite/pull/4274") + void upgradeTransitiveDependencyVersion_NewIssue_NotYetFixed() { + rewriteRun( + spec -> spec.recipe(new UpgradeTransitiveDependencyVersion("commons-codec", "commons-codec", "1.15", null, + null, null, null, null, null, null)), + pomXml( + """ + + + 4.0.0 + + + com.ijson.common + ijson-parent-pom + 1.0.8 + + + com.mycompany.app + my-app + 1.0.0 + + + + + + org.springframework.boot + spring-boot-dependencies + 2.5.15 + pom + import + + + + + + + + org.apache.httpcomponents + httpclient + 4.5.6 + + + + """, + """ + + + 4.0.0 + + + com.ijson.common + ijson-parent-pom + 1.0.8 + + + com.mycompany.app + my-app + 1.0.0 + + + + + commons-codec + commons-codec + 1.15 + + + + org.springframework.boot + spring-boot-dependencies + 2.5.15 + pom + import + + + + + + + + org.apache.httpcomponents + httpclient + 4.5.6 + + + + """ + ) + ); + } + + // Demonstrates that transitive dependency versions can be upgraded when their version is managed by a master POM. + @Test + void upgradeTransitiveDependencyVersion_WorksForMasterPomManagedDeps() { + rewriteRun( + spec -> spec.recipe(new UpgradeTransitiveDependencyVersion("org.apache.commons", "commons-lang3", "3.14.0", null, + null, null, null, null, null, null)), + pomXml( + """ + + + 4.0.0 + + + org.apache.logging.log4j + log4j + 2.13.3 + + + com.mycompany.app + my-app + + + + + org.apache.commons + commons-text + 1.12.0 + + + + """, + """ + + + 4.0.0 + + + org.apache.logging.log4j + log4j + 2.13.3 + + + com.mycompany.app + my-app + + + + org.apache.commons + commons-lang3 + 3.14.0 + + + + + + + + org.apache.commons + commons-text + 1.12.0 + + + + """ + ) + ); + } + + // Demonstrates that transitive dependency versions can be upgraded when their version is managed by a BOM specified in the dependencyManagement section. + @Test + void upgradeTransitiveDependencyVersion_WorksForBillOfMaterialsManagedDeps() { + rewriteRun( + spec -> spec.recipe(new UpgradeTransitiveDependencyVersion("org.springframework.boot", "spring-boot-actuator", "2.7.0", null, + null, null, null, null, null, null)), + pomXml( + """ + + + 4.0.0 + + com.mycompany.app + my-app + 1.0.0 + - com.fasterxml.jackson.core - jackson-core - 2.12.5 + org.springframework.boot + spring-boot-dependencies + 2.5.15 + pom + import + + - org.openrewrite - rewrite-java - 7.0.0 + org.springframework.boot + spring-boot-starter-actuator + 2.7.0 - - """) - ); - } + + """, + """ + + + 4.0.0 + + com.mycompany.app + my-app + 1.0.0 + + + + + org.springframework.boot + spring-boot-actuator + 2.7.0 + + + org.springframework.boot + spring-boot-dependencies + 2.5.15 + pom + import + + + - @Test - void leavesDirectDependencyUntouched() { - rewriteRun( - pomXml( - """ - - 4.0.0 - org.openrewrite - core - 0.1.0-SNAPSHOT + - com.fasterxml.jackson.core - jackson-core - 2.12.0 + org.springframework.boot + spring-boot-starter-actuator + 2.7.0 - - """) + + """ + ) ); } }