From 10f7c41566fbce52f60e8bf1312a0f57e14e194e Mon Sep 17 00:00:00 2001 From: Jonathan Schneider Date: Sat, 24 Oct 2026 20:00:31 -0400 Subject: [PATCH] Keep DependencyListTest and ModuleHasDependencyTest from asking Maven Central directly 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. --- .../java/dependencies/DependencyListTest.java | 7 +++ .../DependencyResolutionDiagnosticTest.java | 32 +--------- .../java/dependencies/MavenCentralMirror.java | 59 +++++++++++++++++++ .../search/ModuleHasDependencyTest.java | 15 +---- 4 files changed, 69 insertions(+), 44 deletions(-) create mode 100644 src/test/java/org/openrewrite/java/dependencies/MavenCentralMirror.java diff --git a/src/test/java/org/openrewrite/java/dependencies/DependencyListTest.java b/src/test/java/org/openrewrite/java/dependencies/DependencyListTest.java index ef3d9151..5d9237c1 100644 --- a/src/test/java/org/openrewrite/java/dependencies/DependencyListTest.java +++ b/src/test/java/org/openrewrite/java/dependencies/DependencyListTest.java @@ -16,7 +16,9 @@ package org.openrewrite.java.dependencies; import org.junit.jupiter.api.Test; +import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.java.dependencies.table.DependencyListReport; +import org.openrewrite.maven.MavenExecutionContextView; import org.openrewrite.maven.tree.*; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; @@ -138,6 +140,11 @@ void directOnly() { void validateResolvable() { rewriteRun( spec -> spec.recipe(new DependencyList(DependencyList.Scope.Compile, false, true)) + // Only the pretend repository below, which can't be reached, is asked: Maven Central and the local + // repository, which the recipe would ask too, are left out + .executionContext(MavenExecutionContextView.view(new InMemoryExecutionContext()) + .setAddCentralRepository(false) + .setAddLocalRepository(false)) .dataTable(DependencyListReport.Row.class, rows -> assertThat(rows) .singleElement() .extracting(DependencyListReport.Row::getResolutionFailure) diff --git a/src/test/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnosticTest.java b/src/test/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnosticTest.java index f57bb3e1..6bccfeea 100644 --- a/src/test/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnosticTest.java +++ b/src/test/java/org/openrewrite/java/dependencies/DependencyResolutionDiagnosticTest.java @@ -17,7 +17,6 @@ import org.junit.jupiter.api.Test; import org.openrewrite.DocumentExample; -import org.openrewrite.ExecutionContext; import org.openrewrite.InMemoryExecutionContext; import org.openrewrite.Parser; import org.openrewrite.java.dependencies.table.RepositoryAccessibilityReport; @@ -33,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.gradle.Assertions.buildGradle; import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; +import static org.openrewrite.java.dependencies.MavenCentralMirror.settingsMirroringOnlyCentral; import static org.openrewrite.maven.Assertions.pomXml; /** @@ -262,34 +262,4 @@ private static void reachable(RepositoryAccessibilityReport.Row row) { assertThat(row.getPingHttpCode()).as("what %s answered", row.getUri()).isEqualTo(200); assertThat(row.getPingExceptionType()).as("what reaching %s threw", row.getUri()).isEmpty(); } - - /** - * Settings with no mirror but one of Maven Central, the mirror CI names in MAVEN_CENTRAL_MIRROR_URL, _USERNAME and - * _PASSWORD, so that Central is reached as the rest of the build reaches it. With no mirror named, none at all. - */ - private static MavenSettings settingsMirroringOnlyCentral(ExecutionContext ctx) { - if (System.getenv("MAVEN_CENTRAL_MIRROR_URL") == null) { - return new MavenSettings(null, null, null, null, null, null); - } - return MavenSettings.parse(new Parser.Input(Path.of("settings.xml"), () -> new ByteArrayInputStream( - //language=xml - """ - - - - central-mirror - ${env.MAVEN_CENTRAL_MIRROR_USERNAME} - ${env.MAVEN_CENTRAL_MIRROR_PASSWORD} - - - - - central-mirror - central - ${env.MAVEN_CENTRAL_MIRROR_URL} - - - - """.getBytes())), ctx); - } } diff --git a/src/test/java/org/openrewrite/java/dependencies/MavenCentralMirror.java b/src/test/java/org/openrewrite/java/dependencies/MavenCentralMirror.java new file mode 100644 index 00000000..2774ef79 --- /dev/null +++ b/src/test/java/org/openrewrite/java/dependencies/MavenCentralMirror.java @@ -0,0 +1,59 @@ +/* + * Copyright 2026 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.java.dependencies; + +import org.openrewrite.ExecutionContext; +import org.openrewrite.Parser; +import org.openrewrite.maven.MavenSettings; + +import java.io.ByteArrayInputStream; +import java.nio.file.Path; + +public final class MavenCentralMirror { + + private MavenCentralMirror() { + } + + /** + * Settings with no mirror but one of Maven Central, the mirror CI names in MAVEN_CENTRAL_MIRROR_URL, _USERNAME and + * _PASSWORD, so that Central is reached as the rest of the build reaches it. With no mirror named, none at all. + */ + public static MavenSettings settingsMirroringOnlyCentral(ExecutionContext ctx) { + if (System.getenv("MAVEN_CENTRAL_MIRROR_URL") == null) { + return new MavenSettings(null, null, null, null, null, null); + } + return MavenSettings.parse(new Parser.Input(Path.of("settings.xml"), () -> new ByteArrayInputStream( + //language=xml + """ + + + + central-mirror + ${env.MAVEN_CENTRAL_MIRROR_USERNAME} + ${env.MAVEN_CENTRAL_MIRROR_PASSWORD} + + + + + central-mirror + central + ${env.MAVEN_CENTRAL_MIRROR_URL} + + + + """.getBytes())), ctx); + } +} diff --git a/src/test/java/org/openrewrite/java/dependencies/search/ModuleHasDependencyTest.java b/src/test/java/org/openrewrite/java/dependencies/search/ModuleHasDependencyTest.java index 68924dc1..0337746c 100644 --- a/src/test/java/org/openrewrite/java/dependencies/search/ModuleHasDependencyTest.java +++ b/src/test/java/org/openrewrite/java/dependencies/search/ModuleHasDependencyTest.java @@ -22,20 +22,16 @@ import org.junit.jupiter.params.provider.NullSource; import org.junit.jupiter.params.provider.ValueSource; import org.openrewrite.InMemoryExecutionContext; -import org.openrewrite.Parser; import org.openrewrite.maven.MavenExecutionContextView; -import org.openrewrite.maven.MavenSettings; import org.openrewrite.test.RecipeSpec; import org.openrewrite.test.RewriteTest; -import java.io.ByteArrayInputStream; -import java.nio.file.Path; - import static org.assertj.core.api.Assertions.assertThat; import static org.openrewrite.gradle.Assertions.buildGradle; import static org.openrewrite.gradle.toolingapi.Assertions.withToolingApi; import static org.openrewrite.java.Assertions.java; import static org.openrewrite.java.Assertions.mavenProject; +import static org.openrewrite.java.dependencies.MavenCentralMirror.settingsMirroringOnlyCentral; import static org.openrewrite.maven.Assertions.pomXml; class ModuleHasDependencyTest implements RewriteTest { @@ -412,14 +408,7 @@ void mavenMatchesOnRequested() { rewriteRun( spec -> { MavenExecutionContextView ctx = MavenExecutionContextView.view(new InMemoryExecutionContext()); - MavenSettings emptySettings = MavenSettings.parse(new Parser.Input(Path.of("settings.xml"), () -> new ByteArrayInputStream( - //language=xml - """ - - """.getBytes())), ctx); - ctx.setMavenSettings(emptySettings); + ctx.setMavenSettings(settingsMirroringOnlyCentral(ctx)); spec.recipe(new ModuleHasDependency(GroupId, ArtifactId, null, null, null, null)) .executionContext(ctx); },