Skip to content

Commit

Permalink
Merge branch 'fix/ModuleInModule' of github.com:Gepardec/rewrite into…
Browse files Browse the repository at this point in the history
… Gepardec-fix/ModuleInModule
  • Loading branch information
sambsnyd committed Oct 11, 2024
2 parents a68563a + eb8b048 commit bd51eaf
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,24 @@ public Pom download(GroupArtifactVersion gav,
// The requested gav might itself have an unresolved placeholder in the version, so also check raw values
for (Pom projectPom : projectPoms.values()) {
if (gav.getGroupId().equals(projectPom.getGroupId()) &&
gav.getArtifactId().equals(projectPom.getArtifactId()) &&
(gav.getVersion().equals(projectPom.getVersion()) || projectPom.getVersion().equals(projectPom.getValue(gav.getVersion())))) {
return projectPom;
gav.getArtifactId().equals(projectPom.getArtifactId())){
if (gav.getVersion().equals(projectPom.getVersion()) || projectPom.getVersion().equals(projectPom.getValue(gav.getVersion()))) {
return projectPom;
}
Parent parent = projectPom.getParent();
while (parent != null){
for (Pom project : projectPoms.values()) {
if (parent.getGroupId().equals(project.getGroupId()) && parent.getArtifactId().equals(project.getArtifactId())){
if (projectPom.getVersion().equals(project.getValue(gav.getVersion()))){
return projectPom;
}
parent = project.getParent();
if (parent == null){
break;
}
}
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ public MockResponse dispatch(RecordedRequest recordedRequest) {
@Test
void ossSonatype() {
InMemoryExecutionContext ctx = new InMemoryExecutionContext();
MavenRepository ossSonatype = new MavenRepository("oss", "https://oss.sonatype.org/content/repositories/snapshots/",
null, "true", false, null, null, null);
MavenRepository ossSonatype = MavenRepository.builder()
.id("oss")
.uri("https://oss.sonatype.org/content/repositories/snapshots/")
.snapshots(true)
.build();
MavenRepository repo = new MavenPomDownloader(ctx).normalizeRepository(ossSonatype,
MavenExecutionContextView.view(ctx), null);
assertThat(repo).isNotNull().extracting((MavenRepository::getUri)).isEqualTo(ossSonatype.getUri());
Expand Down Expand Up @@ -749,6 +752,7 @@ void doNotRenameRepoForCustomMavenLocal(@TempDir Path tempDir) throws MavenDownl
var downloader = new MavenPomDownloader(emptyMap(), ctx);

var result = downloader.download(gav, null, null, List.of());
//noinspection DataFlowIssue
assertThat(result.getRepository()).isNotNull();
assertThat(result.getRepository().getUri()).startsWith(tempDir.toUri().toString());
}
Expand Down Expand Up @@ -806,4 +810,113 @@ private static GroupArtifactVersion createArtifact(Path repository) throws IOExc
Files.write(jar, "I'm a jar".getBytes()); // empty jars get ignored
return new GroupArtifactVersion("org.openrewrite", "rewrite", "1.0.0");
}

@Test
void shouldNotThrowExceptionForModulesInModulesWithRightProperty() {
var gav = new GroupArtifactVersion("test", "test2", "${test}");

Path pomPath = Paths.get("test/test2/pom.xml");
Pom pom = Pom.builder()
.sourcePath(pomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.parent(new Parent(new GroupArtifactVersion("test", "test", "${test}"), "../pom.xml"))
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "test2", "7.0.0", null))
.build();

ResolvedPom resolvedPom = ResolvedPom.builder()
.requested(pom)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.repositories(singletonList(MAVEN_CENTRAL))
.build();

Path pomPath2 = Paths.get("test/pom.xml");
Pom pom2 = Pom.builder()
.sourcePath(pomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.parent(new Parent(new GroupArtifactVersion("test", "root-test", "${test}"), "../pom.xml"))
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "test", "7.0.0", null))
.build();


Path parentPomPath = Paths.get("pom.xml");
Pom parentPom = Pom.builder()
.sourcePath(parentPomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("test", "7.0.0"))
.parent(null)
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "root-test", "7.0.0", null))
.build();

Map<Path, Pom> pomsByPath = new HashMap<>();
pomsByPath.put(parentPomPath, parentPom);
pomsByPath.put(pomPath, pom);
pomsByPath.put(pomPath2, pom2);

String httpUrl = "http://%s.com".formatted(UUID.randomUUID());
MavenRepository nonexistentRepo = new MavenRepository("repo", httpUrl, null, null, false, null, null, null, null);

MavenPomDownloader downloader = new MavenPomDownloader(pomsByPath, ctx);

assertDoesNotThrow(() -> downloader.download(gav, Objects.requireNonNull(pom.getParent()).getRelativePath(), resolvedPom, singletonList(nonexistentRepo)));
}

@Test
void shouldThrowExceptionForModulesInModulesWithNoRightProperty() {
var gav = new GroupArtifactVersion("test", "test2", "${test}");

Path pomPath = Paths.get("test/test2/pom.xml");
Pom pom = Pom.builder()
.sourcePath(pomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.parent(new Parent(new GroupArtifactVersion("test", "test", "${test}"), "../pom.xml"))
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "test2", "7.0.0", null))
.build();

ResolvedPom resolvedPom = ResolvedPom.builder()
.requested(pom)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.repositories(singletonList(MAVEN_CENTRAL))
.build();

Path pomPath2 = Paths.get("test/pom.xml");
Pom pom2 = Pom.builder()
.sourcePath(pomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("REPO_URL", MAVEN_CENTRAL.getUri()))
.parent(new Parent(new GroupArtifactVersion("test", "root-test", "${test}"), "../pom.xml"))
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "test", "7.0.0", null))
.build();


Path parentPomPath = Paths.get("pom.xml");
Pom parentPom = Pom.builder()
.sourcePath(parentPomPath)
.repository(MAVEN_CENTRAL)
.properties(singletonMap("tt", "7.0.0"))
.parent(null)
.gav(new ResolvedGroupArtifactVersion(
"${REPO_URL}", "test", "root-test", "7.0.0", null))
.build();

Map<Path, Pom> pomsByPath = new HashMap<>();
pomsByPath.put(parentPomPath, parentPom);
pomsByPath.put(pomPath, pom);
pomsByPath.put(pomPath2, pom2);

String httpUrl = "http://%s.com".formatted(UUID.randomUUID());
MavenRepository nonexistentRepo = new MavenRepository("repo", httpUrl, null, null, false, null, null, null, null);

MavenPomDownloader downloader = new MavenPomDownloader(pomsByPath, ctx);

assertThrows(IllegalArgumentException.class, () -> downloader.download(gav, Objects.requireNonNull(pom.getParent()).getRelativePath(), resolvedPom, singletonList(nonexistentRepo)));
}

}

0 comments on commit bd51eaf

Please sign in to comment.