From 474d8068c19514ccf401005055c49c91d5d721d8 Mon Sep 17 00:00:00 2001 From: Bartosz Spyrko-Smietanko Date: Fri, 21 Oct 2022 13:01:36 +0100 Subject: [PATCH] Add repositoryFactory to VersionResolverFactory --- .../channel/maven/VersionResolverFactory.java | 15 +++++++-- .../maven/VersionResolverFactoryTest.java | 31 +++++++++++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/maven-resolver/src/main/java/org/wildfly/channel/maven/VersionResolverFactory.java b/maven-resolver/src/main/java/org/wildfly/channel/maven/VersionResolverFactory.java index c2b5a759..97bd57e4 100644 --- a/maven-resolver/src/main/java/org/wildfly/channel/maven/VersionResolverFactory.java +++ b/maven-resolver/src/main/java/org/wildfly/channel/maven/VersionResolverFactory.java @@ -29,6 +29,7 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.function.Function; import java.util.stream.Collectors; import org.eclipse.aether.RepositorySystem; @@ -59,14 +60,24 @@ public class VersionResolverFactory implements MavenVersionsResolver.Factory { private static final Logger LOG = Logger.getLogger(VersionResolverFactory.class); public static final RepositoryPolicy DEFAULT_REPOSITORY_POLICY = new RepositoryPolicy(true, RepositoryPolicy.UPDATE_POLICY_ALWAYS, RepositoryPolicy.CHECKSUM_POLICY_FAIL); + private static final Function DEFAULT_REPOSITORY_MAPPER = r -> new RemoteRepository.Builder(r.getId(), "default", r.getUrl()) + .setPolicy(DEFAULT_REPOSITORY_POLICY) + .build(); private final RepositorySystem system; private final RepositorySystemSession session; + private final Function repositoryFactory; public VersionResolverFactory(RepositorySystem system, RepositorySystemSession session) { + this(system, session, DEFAULT_REPOSITORY_MAPPER); + } + public VersionResolverFactory(RepositorySystem system, + RepositorySystemSession session, + Function repositoryFactory) { this.system = system; this.session = session; + this.repositoryFactory = repositoryFactory; } @Override @@ -74,9 +85,7 @@ public MavenVersionsResolver create(Collection repositories) { Objects.requireNonNull(repositories); final List mvnRepositories = repositories.stream() - .map(r -> new RemoteRepository.Builder(r.getId(), "default", r.getUrl()) - .setPolicy(DEFAULT_REPOSITORY_POLICY) - .build()) + .map(repositoryFactory::apply) .collect(Collectors.toList()); return create(mvnRepositories); } diff --git a/maven-resolver/src/test/java/org/wildfly/channel/maven/VersionResolverFactoryTest.java b/maven-resolver/src/test/java/org/wildfly/channel/maven/VersionResolverFactoryTest.java index cd406062..09b355c6 100644 --- a/maven-resolver/src/test/java/org/wildfly/channel/maven/VersionResolverFactoryTest.java +++ b/maven-resolver/src/test/java/org/wildfly/channel/maven/VersionResolverFactoryTest.java @@ -28,6 +28,7 @@ import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -37,6 +38,7 @@ import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; import org.eclipse.aether.artifact.DefaultArtifact; +import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.resolution.ArtifactRequest; import org.eclipse.aether.resolution.ArtifactResolutionException; import org.eclipse.aether.resolution.ArtifactResult; @@ -49,6 +51,7 @@ import org.mockito.ArgumentCaptor; import org.wildfly.channel.ArtifactCoordinate; import org.wildfly.channel.Channel; +import org.wildfly.channel.Repository; import org.wildfly.channel.UnresolvedMavenArtifactException; import org.wildfly.channel.spi.MavenVersionsResolver; @@ -228,8 +231,6 @@ public void testResolverResolveMetadataUsingGav() throws ArtifactResolutionExcep Artifact artifact = mock(Artifact.class); artifactResult.setArtifact(artifact); when (artifact.getFile()).thenReturn(artifactFile); - VersionRangeResult versionRangeResult = new VersionRangeResult(new VersionRangeRequest()); - Version v100 = mock(Version.class); final ArgumentCaptor artifactRequestArgumentCaptor = ArgumentCaptor.forClass(ArtifactRequest.class); when(system.resolveArtifact(eq(session), artifactRequestArgumentCaptor.capture())).thenReturn(artifactResult); @@ -240,5 +241,31 @@ public void testResolverResolveMetadataUsingGav() throws ArtifactResolutionExcep assertEquals(artifactFile.toURI().toURL(), resolvedURL.get(0)); assertEquals("1.0.0", artifactRequestArgumentCaptor.getAllValues().get(0).getArtifact().getVersion()); } + + @Test + public void testRepositoryFactory() throws Exception { + RepositorySystem system = mock(RepositorySystem.class); + RepositorySystemSession session = mock(RepositorySystemSession.class); + + VersionResolverFactory factory = new VersionResolverFactory(system, session, + r->new RemoteRepository.Builder(r.getId(), "default", r.getUrl() + ".new").build()); + MavenVersionsResolver resolver = factory.create(List.of(new Repository("test_1", "http://test_1"))); + + File artifactFile = new File("test"); + ArtifactResult artifactResult = new ArtifactResult(new ArtifactRequest()); + Artifact artifact = mock(Artifact.class); + artifactResult.setArtifact(artifact); + when (artifact.getFile()).thenReturn(artifactFile); + + ArgumentCaptor captor = ArgumentCaptor.forClass(ArtifactRequest.class); + + when(system.resolveArtifact(eq(session), captor.capture())).thenReturn(artifactResult); + + resolver.resolveArtifact("group", "artifact", "ext", null, "1.0.0"); + + final List actualRepos = captor.getAllValues().get(0).getRepositories(); + assertEquals(1, actualRepos.size()); + assertEquals("http://test_1.new", actualRepos.get(0).getUrl()); + } }