Skip to content

Commit c51907b

Browse files
authored
Keep DependencyListTest and ModuleHasDependencyTest from asking Maven Central directly (#207)
Where CI names a mirror, as the Code Genome Project's builds do, two tests still reached repo.maven.apache.org directly. DependencyListTest#validateResolvable gives the recipe an LST whose only repository can't be reached. Its xml() source loads no Maven settings, and the downloader adds Maven Central behind that repository, so the test asked Central for com/test/test/maven-metadata.xml and its directory listing, and got 404s. Its context now leaves out Central and the local repository, so the pretend repository is the only one asked. The recipe still reports the MavenDownloadingException the test checks for. ModuleHasDependencyTest.WhenDependencyIsRequestedButNotResolved#mavenMatchesOnRequested forced empty settings, which keep the machine's own out but name no mirror, so spring-beans 6.0.0 and the POMs it needs came from Central directly. Like DependencyResolutionDiagnosticTest#maven since #206, it now uses settings that mirror only Central through MAVEN_CENTRAL_MIRROR_URL, _USERNAME and _PASSWORD when they are set, and the same empty settings when they are not. settingsMirroringOnlyCentral moves from DependencyResolutionDiagnosticTest to MavenCentralMirror so that both tests can use it.
1 parent 8be85c5 commit c51907b

4 files changed

Lines changed: 69 additions & 44 deletions

File tree

‎src/test/java/org/openrewrite/java/dependencies/DependencyListTest.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package org.openrewrite.java.dependencies;
1717

1818
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.InMemoryExecutionContext;
1920
import org.openrewrite.java.dependencies.table.DependencyListReport;
21+
import org.openrewrite.maven.MavenExecutionContextView;
2022
import org.openrewrite.maven.tree.*;
2123
import org.openrewrite.test.RecipeSpec;
2224
import org.openrewrite.test.RewriteTest;
@@ -138,6 +140,11 @@ void directOnly() {
138140
void validateResolvable() {
139141
rewriteRun(
140142
spec -> spec.recipe(new DependencyList(DependencyList.Scope.Compile, false, true))
143+
// Only the pretend repository below, which can't be reached, is asked: Maven Central and the local
144+
// repository, which the recipe would ask too, are left out
145+
.executionContext(MavenExecutionContextView.view(new InMemoryExecutionContext())
146+
.setAddCentralRepository(false)
147+
.setAddLocalRepository(false))
141148
.dataTable(DependencyListReport.Row.class, rows -> assertThat(rows)
142149
.singleElement()
143150
.extracting(DependencyListReport.Row::getResolutionFailure)

‎src/test/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnosticTest.java‎

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
20-
import org.openrewrite.ExecutionContext;
2120
import org.openrewrite.InMemoryExecutionContext;
2221
import org.openrewrite.Parser;
2322
import org.openrewrite.java.dependencies.table.RepositoryAccessibilityReport;
@@ -33,6 +32,7 @@
3332
import static org.assertj.core.api.Assertions.assertThat;
3433
import static org.openrewrite.gradle.Assertions.buildGradle;
3534
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
35+
import static org.openrewrite.java.dependencies.MavenCentralMirror.settingsMirroringOnlyCentral;
3636
import static org.openrewrite.maven.Assertions.pomXml;
3737

3838
/**
@@ -262,34 +262,4 @@ private static void reachable(RepositoryAccessibilityReport.Row row) {
262262
assertThat(row.getPingHttpCode()).as("what %s answered", row.getUri()).isEqualTo(200);
263263
assertThat(row.getPingExceptionType()).as("what reaching %s threw", row.getUri()).isEmpty();
264264
}
265-
266-
/**
267-
* Settings with no mirror but one of Maven Central, the mirror CI names in MAVEN_CENTRAL_MIRROR_URL, _USERNAME and
268-
* _PASSWORD, so that Central is reached as the rest of the build reaches it. With no mirror named, none at all.
269-
*/
270-
private static MavenSettings settingsMirroringOnlyCentral(ExecutionContext ctx) {
271-
if (System.getenv("MAVEN_CENTRAL_MIRROR_URL") == null) {
272-
return new MavenSettings(null, null, null, null, null, null);
273-
}
274-
return MavenSettings.parse(new Parser.Input(Path.of("settings.xml"), () -> new ByteArrayInputStream(
275-
//language=xml
276-
"""
277-
<settings>
278-
<servers>
279-
<server>
280-
<id>central-mirror</id>
281-
<username>${env.MAVEN_CENTRAL_MIRROR_USERNAME}</username>
282-
<password>${env.MAVEN_CENTRAL_MIRROR_PASSWORD}</password>
283-
</server>
284-
</servers>
285-
<mirrors>
286-
<mirror>
287-
<id>central-mirror</id>
288-
<mirrorOf>central</mirrorOf>
289-
<url>${env.MAVEN_CENTRAL_MIRROR_URL}</url>
290-
</mirror>
291-
</mirrors>
292-
</settings>
293-
""".getBytes())), ctx);
294-
}
295265
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2026 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.dependencies;
17+
18+
import org.openrewrite.ExecutionContext;
19+
import org.openrewrite.Parser;
20+
import org.openrewrite.maven.MavenSettings;
21+
22+
import java.io.ByteArrayInputStream;
23+
import java.nio.file.Path;
24+
25+
public final class MavenCentralMirror {
26+
27+
private MavenCentralMirror() {
28+
}
29+
30+
/**
31+
* Settings with no mirror but one of Maven Central, the mirror CI names in MAVEN_CENTRAL_MIRROR_URL, _USERNAME and
32+
* _PASSWORD, so that Central is reached as the rest of the build reaches it. With no mirror named, none at all.
33+
*/
34+
public static MavenSettings settingsMirroringOnlyCentral(ExecutionContext ctx) {
35+
if (System.getenv("MAVEN_CENTRAL_MIRROR_URL") == null) {
36+
return new MavenSettings(null, null, null, null, null, null);
37+
}
38+
return MavenSettings.parse(new Parser.Input(Path.of("settings.xml"), () -> new ByteArrayInputStream(
39+
//language=xml
40+
"""
41+
<settings>
42+
<servers>
43+
<server>
44+
<id>central-mirror</id>
45+
<username>${env.MAVEN_CENTRAL_MIRROR_USERNAME}</username>
46+
<password>${env.MAVEN_CENTRAL_MIRROR_PASSWORD}</password>
47+
</server>
48+
</servers>
49+
<mirrors>
50+
<mirror>
51+
<id>central-mirror</id>
52+
<mirrorOf>central</mirrorOf>
53+
<url>${env.MAVEN_CENTRAL_MIRROR_URL}</url>
54+
</mirror>
55+
</mirrors>
56+
</settings>
57+
""".getBytes())), ctx);
58+
}
59+
}

‎src/test/java/org/openrewrite/java/dependencies/search/ModuleHasDependencyTest.java‎

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,16 @@
2222
import org.junit.jupiter.params.provider.NullSource;
2323
import org.junit.jupiter.params.provider.ValueSource;
2424
import org.openrewrite.InMemoryExecutionContext;
25-
import org.openrewrite.Parser;
2625
import org.openrewrite.maven.MavenExecutionContextView;
27-
import org.openrewrite.maven.MavenSettings;
2826
import org.openrewrite.test.RecipeSpec;
2927
import org.openrewrite.test.RewriteTest;
3028

31-
import java.io.ByteArrayInputStream;
32-
import java.nio.file.Path;
33-
3429
import static org.assertj.core.api.Assertions.assertThat;
3530
import static org.openrewrite.gradle.Assertions.buildGradle;
3631
import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi;
3732
import static org.openrewrite.java.Assertions.java;
3833
import static org.openrewrite.java.Assertions.mavenProject;
34+
import static org.openrewrite.java.dependencies.MavenCentralMirror.settingsMirroringOnlyCentral;
3935
import static org.openrewrite.maven.Assertions.pomXml;
4036

4137
class ModuleHasDependencyTest implements RewriteTest {
@@ -412,14 +408,7 @@ void mavenMatchesOnRequested() {
412408
rewriteRun(
413409
spec -> {
414410
MavenExecutionContextView ctx = MavenExecutionContextView.view(new InMemoryExecutionContext());
415-
MavenSettings emptySettings = MavenSettings.parse(new Parser.Input(Path.of("settings.xml"), () -> new ByteArrayInputStream(
416-
//language=xml
417-
"""
418-
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
419-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
420-
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd"/>
421-
""".getBytes())), ctx);
422-
ctx.setMavenSettings(emptySettings);
411+
ctx.setMavenSettings(settingsMirroringOnlyCentral(ctx));
423412
spec.recipe(new ModuleHasDependency(GroupId, ArtifactId, null, null, null, null))
424413
.executionContext(ctx);
425414
},

0 commit comments

Comments
 (0)