|
45 | 45 |
|
46 | 46 | import static org.assertj.core.api.Assertions.assertThat; |
47 | 47 | import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 48 | +import static org.openrewrite.maven.Assertions.pomXml; |
48 | 49 | import static org.openrewrite.maven.utilities.MavenWrapper.*; |
49 | 50 | import static org.openrewrite.properties.Assertions.properties; |
50 | 51 | import static org.openrewrite.test.SourceSpecs.*; |
@@ -92,6 +93,15 @@ void addMavenWrapper() { |
92 | 93 | assertThat(mavenWrapperJar.getUri()).isEqualTo(URI.create("https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar")); |
93 | 94 | assertThat(isValidWrapperJar(mavenWrapperJar)).as("Wrapper jar is not valid").isTrue(); |
94 | 95 | }), |
| 96 | + pomXml( |
| 97 | + """ |
| 98 | + <project> |
| 99 | + <groupId>com.example</groupId> |
| 100 | + <artifactId>demo</artifactId> |
| 101 | + <version>1.0.0</version> |
| 102 | + </project> |
| 103 | + """ |
| 104 | + ), |
95 | 105 | properties( |
96 | 106 | doesNotExist(), |
97 | 107 | withLicenseHeader(""" |
@@ -126,6 +136,15 @@ void addMavenWrapperWithWrapperJarChecksumEnabled() { |
126 | 136 | assertThat(mavenWrapperJar.getUri()).isEqualTo(URI.create("https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar")); |
127 | 137 | assertThat(isValidWrapperJar(mavenWrapperJar)).as("Wrapper jar is not valid").isTrue(); |
128 | 138 | }), |
| 139 | + pomXml( |
| 140 | + """ |
| 141 | + <project> |
| 142 | + <groupId>com.example</groupId> |
| 143 | + <artifactId>demo</artifactId> |
| 144 | + <version>1.0.0</version> |
| 145 | + </project> |
| 146 | + """ |
| 147 | + ), |
129 | 148 | properties( |
130 | 149 | doesNotExist(), |
131 | 150 | withLicenseHeader(""" |
@@ -276,6 +295,109 @@ void updateWrapperWithWrapperJarChecksumEnabled() { |
276 | 295 | ); |
277 | 296 | } |
278 | 297 |
|
| 298 | + @Test |
| 299 | + void doesNotAddWrapperToNonMavenProject() { |
| 300 | + rewriteRun( |
| 301 | + spec -> spec.recipe(new UpdateMavenWrapper("3.1.x", null, "3.8.x", null, null, null)) |
| 302 | + .afterRecipe(run -> |
| 303 | + assertThat(run.getChangeset().getAllResults()).isEmpty() |
| 304 | + ), |
| 305 | + text( |
| 306 | + """ |
| 307 | + Some random file content |
| 308 | + """ |
| 309 | + ) |
| 310 | + ); |
| 311 | + } |
| 312 | + |
| 313 | + /** |
| 314 | + * When multiple independent Maven projects exist in subdirectories without a root {@code pom.xml}, |
| 315 | + * the recipe adds wrapper files at the root level. This allows all subprojects to use |
| 316 | + * the same wrapper from the repository root. |
| 317 | + */ |
| 318 | + @Test |
| 319 | + void addsWrapperToRootWhenMultipleIndependentMavenProjectsExist() { |
| 320 | + rewriteRun( |
| 321 | + spec -> spec.recipe(new UpdateMavenWrapper("3.1.x", null, "3.8.x", null, null, null)) |
| 322 | + .expectedCyclesThatMakeChanges(1) |
| 323 | + .afterRecipe(run -> { |
| 324 | + assertThat(run.getChangeset().getAllResults()).hasSize(4); |
| 325 | + |
| 326 | + // Verify wrapper files are added at root, not in subdirectories |
| 327 | + var mvnw = run.getChangeset().getAllResults().stream() |
| 328 | + .map(Result::getAfter) |
| 329 | + .filter(Objects::nonNull) |
| 330 | + .filter(r -> r.getSourcePath().endsWith("mvnw")) |
| 331 | + .findFirst(); |
| 332 | + assertThat(mvnw).isPresent(); |
| 333 | + assertThat(mvnw.get().getSourcePath().toString()).isEqualTo("mvnw"); |
| 334 | + |
| 335 | + var mavenWrapperJar = run.getChangeset().getAllResults().stream() |
| 336 | + .map(Result::getAfter) |
| 337 | + .filter(Objects::nonNull) |
| 338 | + .filter(r -> r.getSourcePath().endsWith("maven-wrapper.jar")) |
| 339 | + .findFirst(); |
| 340 | + assertThat(mavenWrapperJar).isPresent(); |
| 341 | + assertThat(mavenWrapperJar.get().getSourcePath().toString()).isEqualTo(".mvn/wrapper/maven-wrapper.jar"); |
| 342 | + |
| 343 | + var mvnwCmd = run.getChangeset().getAllResults().stream() |
| 344 | + .map(Result::getAfter) |
| 345 | + .filter(Objects::nonNull) |
| 346 | + .filter(r -> r.getSourcePath().endsWith("mvnw.cmd")) |
| 347 | + .findFirst(); |
| 348 | + assertThat(mvnwCmd).isPresent(); |
| 349 | + |
| 350 | + var properties = run.getChangeset().getAllResults().stream() |
| 351 | + .map(Result::getAfter) |
| 352 | + .filter(Objects::nonNull) |
| 353 | + .filter(r -> r.getSourcePath().endsWith("maven-wrapper.properties")) |
| 354 | + .findFirst(); |
| 355 | + assertThat(properties).isPresent(); |
| 356 | + |
| 357 | + // Verify subdirectories do NOT contain wrapper files |
| 358 | + var filesInProjectA = run.getChangeset().getAllResults().stream() |
| 359 | + .map(Result::getAfter) |
| 360 | + .filter(Objects::nonNull) |
| 361 | + .filter(r -> r.getSourcePath().toString().startsWith("project-a/") && |
| 362 | + (r.getSourcePath().toString().contains("mvnw") || |
| 363 | + r.getSourcePath().toString().contains("maven-wrapper"))) |
| 364 | + .toList(); |
| 365 | + assertThat(filesInProjectA).isEmpty(); |
| 366 | + |
| 367 | + var filesInProjectB = run.getChangeset().getAllResults().stream() |
| 368 | + .map(Result::getAfter) |
| 369 | + .filter(Objects::nonNull) |
| 370 | + .filter(r -> r.getSourcePath().toString().startsWith("project-b/") && |
| 371 | + (r.getSourcePath().toString().contains("mvnw") || |
| 372 | + r.getSourcePath().toString().contains("maven-wrapper"))) |
| 373 | + .toList(); |
| 374 | + assertThat(filesInProjectB).isEmpty(); |
| 375 | + }), |
| 376 | + dir("project-a", |
| 377 | + pomXml( |
| 378 | + """ |
| 379 | + <project> |
| 380 | + <groupId>com.example</groupId> |
| 381 | + <artifactId>project-a</artifactId> |
| 382 | + <version>1.0.0</version> |
| 383 | + </project> |
| 384 | + """ |
| 385 | + ) |
| 386 | + ), |
| 387 | + dir("project-b", |
| 388 | + pomXml( |
| 389 | + """ |
| 390 | + <project> |
| 391 | + <groupId>com.example</groupId> |
| 392 | + <artifactId>project-b</artifactId> |
| 393 | + <version>1.0.0</version> |
| 394 | + </project> |
| 395 | + """ |
| 396 | + ) |
| 397 | + ) |
| 398 | + ); |
| 399 | + } |
| 400 | + |
279 | 401 | @Test |
280 | 402 | void updateWrapperInSubDirectory() { |
281 | 403 | rewriteRun( |
@@ -327,6 +449,27 @@ void updateVersionUsingSourceDistribution() { |
327 | 449 | assertThat(mvnwDownloaderJava.getSourcePath()).isEqualTo(WRAPPER_DOWNLOADER_LOCATION); |
328 | 450 | assertThat(mvnwDownloaderJava.getUri()).isEqualTo(URI.create("https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper-distribution/3.1.1/maven-wrapper-distribution-3.1.1-source.zip")); |
329 | 451 | }), |
| 452 | + pomXml( |
| 453 | + """ |
| 454 | + <project> |
| 455 | + <groupId>com.example</groupId> |
| 456 | + <artifactId>demo</artifactId> |
| 457 | + <version>1.0.0</version> |
| 458 | + </project> |
| 459 | + """, |
| 460 | + """ |
| 461 | + <project> |
| 462 | + <groupId>com.example</groupId> |
| 463 | + <artifactId>demo</artifactId> |
| 464 | + <version>1.0.0</version> |
| 465 | + </project> |
| 466 | + """, |
| 467 | + spec -> spec.afterRecipe(pom -> |
| 468 | + assertThat(pom.getMarkers().findFirst(BuildTool.class)).hasValueSatisfying(buildTool -> { |
| 469 | + assertThat(buildTool.getType()).isEqualTo(BuildTool.Type.Maven); |
| 470 | + assertThat(buildTool.getVersion()).isEqualTo("3.8.9"); |
| 471 | + })) |
| 472 | + ), |
330 | 473 | properties( |
331 | 474 | withLicenseHeader(""" |
332 | 475 | distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.0/apache-maven-3.8.0-bin.zip |
|
0 commit comments