|
33 | 33 | import java.util.List; |
34 | 34 |
|
35 | 35 | import static org.assertj.core.api.Assertions.assertThat; |
| 36 | +import static org.openrewrite.java.Assertions.mavenProject; |
36 | 37 | import static org.openrewrite.maven.Assertions.pomXml; |
37 | 38 |
|
38 | 39 | class ResolvedPomTest implements RewriteTest { |
@@ -106,6 +107,53 @@ void resolveDependencyWithPlaceholderClassifier() { |
106 | 107 | ); |
107 | 108 | } |
108 | 109 |
|
| 110 | + @Test |
| 111 | + void projectVersion() { |
| 112 | + rewriteRun( |
| 113 | + pomXml( |
| 114 | + """ |
| 115 | + <project> |
| 116 | + <groupId>org.example</groupId> |
| 117 | + <artifactId>parent</artifactId> |
| 118 | + <version>3.17.0</version> |
| 119 | + </project> |
| 120 | + """ |
| 121 | + ), |
| 122 | + mavenProject( |
| 123 | + "app", |
| 124 | + pomXml( |
| 125 | + """ |
| 126 | + <project> |
| 127 | + <groupId>org.example</groupId> |
| 128 | + <artifactId>app</artifactId> |
| 129 | + <version>${project.version}</version> |
| 130 | + <parent> |
| 131 | + <groupId>org.example</groupId> |
| 132 | + <artifactId>parent</artifactId> |
| 133 | + <version>3.17.0</version> |
| 134 | + <relativeParent>../pom.xml</relativeParent> |
| 135 | + </parent> |
| 136 | + <dependencies> |
| 137 | + <dependency> |
| 138 | + <groupId>org.apache.commons</groupId> |
| 139 | + <artifactId>commons-lang3</artifactId> |
| 140 | + <version>${project.version}</version> |
| 141 | + </dependency> |
| 142 | + </dependencies> |
| 143 | + </project> |
| 144 | + """, |
| 145 | + spec -> spec |
| 146 | + .afterRecipe(doc -> { |
| 147 | + List<ResolvedDependency> deps = doc.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow().getDependencies() |
| 148 | + .get(Scope.Compile); |
| 149 | + assertThat(deps).hasSize(1); |
| 150 | + assertThat(deps.getFirst().getVersion()).isEqualTo("3.17.0"); |
| 151 | + }) |
| 152 | + ) |
| 153 | + ) |
| 154 | + ); |
| 155 | + } |
| 156 | + |
109 | 157 | @Test |
110 | 158 | void resolveExecutionsFromDifferentParents() { |
111 | 159 | String grandFather = """ |
@@ -226,7 +274,7 @@ void resolveExecutionsFromDifferentParents() { |
226 | 274 | pomXml(father, spec -> spec.path("father/pom.xml")), |
227 | 275 | pomXml(child, spec -> spec.path("father/child/pom.xml").afterRecipe(doc -> { |
228 | 276 | List<Plugin> pluginManagement = doc.getMarkers().findFirst(MavenResolutionResult.class) |
229 | | - .get().getPom().getPluginManagement(); |
| 277 | + .orElseThrow().getPom().getPluginManagement(); |
230 | 278 | assertThat(pluginManagement).hasSize(1); |
231 | 279 | Plugin plugin = pluginManagement.getFirst(); |
232 | 280 | assertThat(plugin).extracting(Plugin::getArtifactId).isEqualTo("maven-enforcer-plugin"); |
@@ -469,7 +517,7 @@ public void downloadError(GroupArtifactVersion gav, List<String> attemptedUris, |
469 | 517 | pomXml(father, spec -> spec.path("pom.xml")), |
470 | 518 | pomXml(childA, spec -> spec.path("childA/pom.xml")), |
471 | 519 | pomXml(childB, spec -> spec.path("childB/pom.xml").afterRecipe(doc -> { |
472 | | - ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).get().getPom(); |
| 520 | + ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow().getPom(); |
473 | 521 | String version = pom.getManagedVersion("com.some", "some-artifact", null, null); |
474 | 522 | // Assert that version is not null! |
475 | 523 | assertThat(version).isEqualTo("1"); |
@@ -540,7 +588,7 @@ void ignoreScopeInDependencyManagement(@TempDir Path localRepository) throws IOE |
540 | 588 | """, |
541 | 589 | spec -> spec.path("child/pom.xml") |
542 | 590 | .afterRecipe(doc -> { |
543 | | - ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).get().getPom(); |
| 591 | + ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow().getPom(); |
544 | 592 | assertThat(pom.getDependencyManagement()).hasSize(1); |
545 | 593 | }) |
546 | 594 | ) |
@@ -586,7 +634,7 @@ void firstUniqueManagedDependencyWins() { |
586 | 634 | """, |
587 | 635 | spec -> spec |
588 | 636 | .afterRecipe(doc -> { |
589 | | - ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).get().getPom(); |
| 637 | + ResolvedPom pom = doc.getMarkers().findFirst(MavenResolutionResult.class).orElseThrow().getPom(); |
590 | 638 | assertThat(pom.getManagedVersion("com.fasterxml.jackson.core", "jackson-core", null, null)).isEqualTo("2.16.1"); |
591 | 639 | }) |
592 | 640 | ) |
|
0 commit comments