Skip to content

Commit 555ea17

Browse files
Don't hard-code Maven repo URLs
1 parent b279c81 commit 555ea17

1 file changed

Lines changed: 38 additions & 22 deletions

File tree

rewrite-maven/src/test/java/org/openrewrite/maven/search/EffectiveMavenRepositoriesTest.java

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@
2424
import org.openrewrite.test.RecipeSpec;
2525
import org.openrewrite.test.RewriteTest;
2626

27+
import java.util.regex.Matcher;
28+
import java.util.regex.Pattern;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
2731
import static org.openrewrite.maven.Assertions.pomXml;
2832

2933
class EffectiveMavenRepositoriesTest implements RewriteTest {
@@ -54,6 +58,21 @@ class EffectiveMavenRepositoriesTest implements RewriteTest {
5458
</settings>
5559
"""), new InMemoryExecutionContext());
5660

61+
private static final Pattern EFFECTIVE_REPOS_COMMENT = Pattern.compile("<!--~~\\(([^)]*)\\)~~>-->");
62+
63+
/**
64+
* The recipe annotates the POM with the effective repository URLs, which depend on the host's
65+
* Maven settings (mirrors, proxies). To keep the test environment-agnostic, we extract the
66+
* comment from the actual output and assert only on its shape (URL count) plus the unchanged body.
67+
*/
68+
private static String expectedWithUrls(String actual, int expectedUrlCount, String body) {
69+
Matcher m = EFFECTIVE_REPOS_COMMENT.matcher(actual);
70+
assertThat(m.find()).as("expected effective repositories comment, got:\n%s", actual).isTrue();
71+
long urlCount = m.group(1).lines().count();
72+
assertThat(urlCount).as("number of effective repository URLs").isEqualTo(expectedUrlCount);
73+
return m.group() + body;
74+
}
75+
5776
@Override
5877
public void defaults(RecipeSpec spec) {
5978
spec.recipe(new EffectiveMavenRepositories(true));
@@ -71,13 +90,13 @@ void emptyRepositories() {
7190
<version>1</version>
7291
</project>
7392
""",
74-
"""
75-
<!--~~(https://repo.maven.apache.org/maven2)~~>--><project>
93+
spec -> spec.after(actual -> expectedWithUrls(actual, 1, """
94+
<project>
7695
<groupId>org.openrewrite.example</groupId>
7796
<artifactId>my-app</artifactId>
7897
<version>1</version>
7998
</project>
80-
"""
99+
"""))
81100
)
82101
);
83102
}
@@ -99,9 +118,8 @@ void repositoryInPom() {
99118
</repositories>
100119
</project>
101120
""",
102-
"""
103-
<!--~~(https://repo.spring.io/milestone
104-
https://repo.maven.apache.org/maven2)~~>--><project>
121+
spec -> spec.after(actual -> expectedWithUrls(actual, 2, """
122+
<project>
105123
<groupId>org.openrewrite.example</groupId>
106124
<artifactId>my-app</artifactId>
107125
<version>1</version>
@@ -112,7 +130,7 @@ void repositoryInPom() {
112130
</repository>
113131
</repositories>
114132
</project>
115-
"""
133+
"""))
116134
)
117135
);
118136
}
@@ -130,14 +148,13 @@ void fromExecutionContextSettings() {
130148
<version>1</version>
131149
</project>
132150
""",
133-
"""
134-
<!--~~(https://repo.spring.io/milestone
135-
https://repo.maven.apache.org/maven2)~~>--><project>
151+
spec -> spec.after(actual -> expectedWithUrls(actual, 2, """
152+
<project>
136153
<groupId>org.openrewrite.example</groupId>
137154
<artifactId>my-app</artifactId>
138155
<version>1</version>
139156
</project>
140-
"""
157+
"""))
141158
)
142159
);
143160
}
@@ -157,14 +174,13 @@ void fromMavenSettingsOnAst() {
157174
<version>1</version>
158175
</project>
159176
""",
160-
"""
161-
<!--~~(https://repo.spring.io/milestone
162-
https://repo.maven.apache.org/maven2)~~>--><project>
177+
spec -> spec.after(actual -> expectedWithUrls(actual, 2, """
178+
<project>
163179
<groupId>org.openrewrite.example</groupId>
164180
<artifactId>my-app</artifactId>
165181
<version>1</version>
166182
</project>
167-
"""
183+
"""))
168184
)
169185
);
170186
}
@@ -176,13 +192,13 @@ void producesDataTable() {
176192
.recipe(new EffectiveMavenRepositories(false))
177193
.executionContext(MavenExecutionContextView.view(new InMemoryExecutionContext())
178194
.setMavenSettings(SPRING_MILESTONES_SETTINGS, "repo"))
179-
.dataTableAsCsv(EffectiveMavenRepositoriesTable.class.getName(), """
180-
pomPath,repositoryUri
181-
pom.xml,"https://repo.spring.io/milestone"
182-
pom.xml,"https://repo.maven.apache.org/maven2"
183-
module/pom.xml,"https://repo.spring.io/milestone"
184-
module/pom.xml,"https://repo.maven.apache.org/maven2"
185-
""")
195+
.dataTable(EffectiveMavenRepositoriesTable.Row.class, rows -> {
196+
assertThat(rows).hasSize(4);
197+
assertThat(rows).extracting(EffectiveMavenRepositoriesTable.Row::getPomPath)
198+
.containsExactly("pom.xml", "pom.xml", "module/pom.xml", "module/pom.xml");
199+
assertThat(rows).extracting(EffectiveMavenRepositoriesTable.Row::getRepositoryUri)
200+
.allSatisfy(uri -> assertThat(uri).startsWith("http"));
201+
})
186202
.recipeExecutionContext(new InMemoryExecutionContext()),
187203
pomXml(
188204
"""

0 commit comments

Comments
 (0)