Search in sources :

Example 66 with RemoteRepository

use of org.sonatype.aether.repository.RemoteRepository in project sonatype-aether by sonatype.

the class DefaultArtifactResolverTest method testArtifactNotFoundCache.

@Test
public void testArtifactNotFoundCache() throws Exception {
    RecordingRepositoryConnector connector = new RecordingRepositoryConnector() {

        @Override
        public void get(Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads) {
            super.get(artifactDownloads, metadataDownloads);
            for (ArtifactDownload download : artifactDownloads) {
                download.getFile().delete();
                ArtifactTransferException exception = new ArtifactNotFoundException(download.getArtifact(), null, "not found");
                download.setException(exception);
            }
        }
    };
    remoteRepositoryManager.setConnector(connector);
    resolver.setUpdateCheckManager(new DefaultUpdateCheckManager());
    session.setNotFoundCachingEnabled(true);
    session.setUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_NEVER);
    RemoteRepository remoteRepo = new RemoteRepository("id", "default", "file:///");
    Artifact artifact1 = artifact;
    Artifact artifact2 = artifact.setVersion("ver2");
    ArtifactRequest request1 = new ArtifactRequest(artifact1, Arrays.asList(remoteRepo), "");
    ArtifactRequest request2 = new ArtifactRequest(artifact2, Arrays.asList(remoteRepo), "");
    connector.setExpectGet(new Artifact[] { artifact1, artifact2 });
    try {
        resolver.resolveArtifacts(session, Arrays.asList(request1, request2));
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
        connector.assertSeenExpected();
    }
    TestFileUtils.write("artifact", new File(lrm.getRepository().getBasedir(), lrm.getPathForLocalArtifact(artifact2)));
    lrm.setArtifactAvailability(artifact2, false);
    DefaultUpdateCheckManagerTest.resetSessionData(session);
    connector.resetActual();
    connector.setExpectGet(new Artifact[0]);
    try {
        resolver.resolveArtifacts(session, Arrays.asList(request1, request2));
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
        connector.assertSeenExpected();
        for (ArtifactResult result : e.getResults()) {
            Throwable t = result.getExceptions().get(0);
            assertEquals(t.toString(), true, t instanceof ArtifactNotFoundException);
            assertEquals(t.toString(), true, t.getMessage().contains("cached"));
        }
    }
}
Also used : RemoteRepository(org.sonatype.aether.repository.RemoteRepository) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) Artifact(org.sonatype.aether.artifact.Artifact) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) Collection(java.util.Collection) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) File(java.io.File) Test(org.junit.Test)

Example 67 with RemoteRepository

use of org.sonatype.aether.repository.RemoteRepository in project sonatype-aether by sonatype.

the class DefaultDependencyCollectorTest method testArtifactDescriptorResolutionNotRestrictedToRepoHostingSelectedVersion.

@Test
public void testArtifactDescriptorResolutionNotRestrictedToRepoHostingSelectedVersion() throws Exception {
    RemoteRepository repo2 = new RemoteRepository("test", "default", "file:///");
    final List<RemoteRepository> repos = new ArrayList<RemoteRepository>();
    collector.setArtifactDescriptorReader(new ArtifactDescriptorReader() {

        public ArtifactDescriptorResult readArtifactDescriptor(RepositorySystemSession session, ArtifactDescriptorRequest request) throws ArtifactDescriptorException {
            repos.addAll(request.getRepositories());
            return new ArtifactDescriptorResult(request);
        }
    });
    DependencyNode root = parser.parseLiteral("verrange:parent:jar:[1,):compile");
    List<Dependency> dependencies = Arrays.asList(root.getDependency());
    CollectRequest request = new CollectRequest(dependencies, null, Arrays.asList(repository, repo2));
    CollectResult result = collector.collectDependencies(session, request);
    assertEquals(0, result.getExceptions().size());
    assertEquals(2, repos.size());
    assertEquals("id", repos.get(0).getId());
    assertEquals("test", repos.get(1).getId());
}
Also used : RepositorySystemSession(org.sonatype.aether.RepositorySystemSession) TestRepositorySystemSession(org.sonatype.aether.test.impl.TestRepositorySystemSession) CollectResult(org.sonatype.aether.collection.CollectResult) ArrayList(java.util.ArrayList) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) Dependency(org.sonatype.aether.graph.Dependency) CollectRequest(org.sonatype.aether.collection.CollectRequest) DependencyNode(org.sonatype.aether.graph.DependencyNode) ArtifactDescriptorException(org.sonatype.aether.resolution.ArtifactDescriptorException) ArtifactDescriptorResult(org.sonatype.aether.resolution.ArtifactDescriptorResult) ArtifactDescriptorRequest(org.sonatype.aether.resolution.ArtifactDescriptorRequest) ArtifactDescriptorReader(org.sonatype.aether.impl.ArtifactDescriptorReader) Test(org.junit.Test)

Example 68 with RemoteRepository

use of org.sonatype.aether.repository.RemoteRepository in project sonatype-aether by sonatype.

the class SimpleLocalRepositoryManager method find.

public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) {
    LocalMetadataResult result = new LocalMetadataResult(request);
    String path;
    Metadata metadata = request.getMetadata();
    String context = request.getContext();
    RemoteRepository remote = request.getRepository();
    if (remote != null) {
        path = getPathForRemoteMetadata(metadata, remote, context);
    } else {
        path = getPathForLocalMetadata(metadata);
    }
    File file = new File(getRepository().getBasedir(), path);
    if (file.isFile()) {
        result.setFile(file);
    }
    return result;
}
Also used : LocalMetadataResult(org.sonatype.aether.repository.LocalMetadataResult) Metadata(org.sonatype.aether.metadata.Metadata) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) File(java.io.File)

Example 69 with RemoteRepository

use of org.sonatype.aether.repository.RemoteRepository in project sonatype-aether by sonatype.

the class SimpleLocalRepositoryManager method getRepositoryKey.

String getRepositoryKey(RemoteRepository repository, String context) {
    String key;
    if (repository.isRepositoryManager()) {
        // repository serves dynamic contents, take request parameters into account for key
        StringBuilder buffer = new StringBuilder(128);
        buffer.append(repository.getId());
        buffer.append('-');
        SortedSet<String> subKeys = new TreeSet<String>();
        for (RemoteRepository mirroredRepo : repository.getMirroredRepositories()) {
            subKeys.add(mirroredRepo.getId());
        }
        SimpleDigest digest = new SimpleDigest();
        digest.update(context);
        for (String subKey : subKeys) {
            digest.update(subKey);
        }
        buffer.append(digest.digest());
        key = buffer.toString();
    } else {
        // repository serves static contents, its id is sufficient as key
        key = repository.getId();
    }
    return key;
}
Also used : TreeSet(java.util.TreeSet) RemoteRepository(org.sonatype.aether.repository.RemoteRepository)

Example 70 with RemoteRepository

use of org.sonatype.aether.repository.RemoteRepository in project sonatype-aether by sonatype.

the class DefaultArtifactResolverTest method testResolveRemoteArtifactUnsuccessful.

@Test
public void testResolveRemoteArtifactUnsuccessful() throws IOException, ArtifactResolutionException {
    RecordingRepositoryConnector connector = new RecordingRepositoryConnector() {

        @Override
        public void get(Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads) {
            super.get(artifactDownloads, metadataDownloads);
            ArtifactDownload download = artifactDownloads.iterator().next();
            ArtifactTransferException exception = new ArtifactNotFoundException(download.getArtifact(), null, "not found");
            download.setException(exception);
        }
    };
    connector.setExpectGet(artifact);
    remoteRepositoryManager.setConnector(connector);
    ArtifactRequest request = new ArtifactRequest(artifact, null, "");
    request.addRepository(new RemoteRepository("id", "default", "file:///"));
    try {
        resolver.resolveArtifact(session, request);
        fail("expected exception");
    } catch (ArtifactResolutionException e) {
        connector.assertSeenExpected();
        assertNotNull(e.getResults());
        assertEquals(1, e.getResults().size());
        ArtifactResult result = e.getResults().get(0);
        assertSame(request, result.getRequest());
        assertFalse(result.getExceptions().isEmpty());
        assertTrue(result.getExceptions().get(0) instanceof ArtifactNotFoundException);
        Artifact resolved = result.getArtifact();
        assertNull(resolved);
    }
}
Also used : ArtifactResolutionException(org.sonatype.aether.resolution.ArtifactResolutionException) ArtifactTransferException(org.sonatype.aether.transfer.ArtifactTransferException) ArtifactRequest(org.sonatype.aether.resolution.ArtifactRequest) LocalArtifactRequest(org.sonatype.aether.repository.LocalArtifactRequest) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) Collection(java.util.Collection) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) ArtifactNotFoundException(org.sonatype.aether.transfer.ArtifactNotFoundException) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) Artifact(org.sonatype.aether.artifact.Artifact) LocalArtifactResult(org.sonatype.aether.repository.LocalArtifactResult) ArtifactResult(org.sonatype.aether.resolution.ArtifactResult) Test(org.junit.Test)

Aggregations

RemoteRepository (org.sonatype.aether.repository.RemoteRepository)71 Artifact (org.sonatype.aether.artifact.Artifact)30 Test (org.junit.Test)27 DefaultArtifact (org.sonatype.aether.util.artifact.DefaultArtifact)18 File (java.io.File)15 RepositorySystemSession (org.sonatype.aether.RepositorySystemSession)15 TestRepositorySystemSession (org.sonatype.aether.test.impl.TestRepositorySystemSession)13 StubArtifact (org.sonatype.aether.test.util.impl.StubArtifact)13 Dependency (org.sonatype.aether.graph.Dependency)12 ArtifactRequest (org.sonatype.aether.resolution.ArtifactRequest)12 ArtifactResult (org.sonatype.aether.resolution.ArtifactResult)12 LocalArtifactRequest (org.sonatype.aether.repository.LocalArtifactRequest)11 CollectRequest (org.sonatype.aether.collection.CollectRequest)10 LocalArtifactResult (org.sonatype.aether.repository.LocalArtifactResult)10 ArrayList (java.util.ArrayList)8 RepositorySystem (org.sonatype.aether.RepositorySystem)8 IOException (java.io.IOException)7 Before (org.junit.Before)7 Metadata (org.sonatype.aether.metadata.Metadata)7 DependencyRequest (org.sonatype.aether.resolution.DependencyRequest)7