Skip to content

Commit

Permalink
Add repositoryFactory to VersionResolverFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
spyrkob committed Nov 24, 2022
1 parent ebc2fa7 commit 474d806
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -59,24 +60,32 @@ 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<Repository, RemoteRepository> 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<Repository, RemoteRepository> repositoryFactory;

public VersionResolverFactory(RepositorySystem system,
RepositorySystemSession session) {
this(system, session, DEFAULT_REPOSITORY_MAPPER);
}
public VersionResolverFactory(RepositorySystem system,
RepositorySystemSession session,
Function<Repository, RemoteRepository> repositoryFactory) {
this.system = system;
this.session = session;
this.repositoryFactory = repositoryFactory;
}

@Override
public MavenVersionsResolver create(Collection<Repository> repositories) {
Objects.requireNonNull(repositories);

final List<RemoteRepository> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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<ArtifactRequest> artifactRequestArgumentCaptor = ArgumentCaptor.forClass(ArtifactRequest.class);
when(system.resolveArtifact(eq(session), artifactRequestArgumentCaptor.capture())).thenReturn(artifactResult);

Expand All @@ -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<ArtifactRequest> 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<RemoteRepository> actualRepos = captor.getAllValues().get(0).getRepositories();
assertEquals(1, actualRepos.size());
assertEquals("http://test_1.new", actualRepos.get(0).getUrl());
}
}

0 comments on commit 474d806

Please sign in to comment.