Search in sources :

Example 6 with Metadata

use of org.sonatype.aether.metadata.Metadata in project sonatype-aether by sonatype.

the class ConnectorTestSuite method testTransferZeroBytesFile.

/**
     * See https://issues.sonatype.org/browse/AETHER-8
     */
@Test
public void testTransferZeroBytesFile() throws IOException, NoRepositoryConnectorException {
    File emptyFile = TestFileUtils.createTempFile("");
    Artifact artifact = new StubArtifact("gid:aid:ext:ver");
    ArtifactUpload upA = new ArtifactUpload(artifact, emptyFile);
    File dir = TestFileUtils.createTempDir("con-test");
    File downAFile = new File(dir, "downA.file");
    downAFile.deleteOnExit();
    ArtifactDownload downA = new ArtifactDownload(artifact, "", downAFile, RepositoryPolicy.CHECKSUM_POLICY_FAIL);
    Metadata metadata = new StubMetadata("gid", "aid", "ver", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT);
    MetadataUpload upM = new MetadataUpload(metadata, emptyFile);
    File downMFile = new File(dir, "downM.file");
    downMFile.deleteOnExit();
    MetadataDownload downM = new MetadataDownload(metadata, "", downMFile, RepositoryPolicy.CHECKSUM_POLICY_FAIL);
    RepositoryConnector connector = factory().newInstance(session, repository);
    connector.put(Arrays.asList(upA), Arrays.asList(upM));
    connector.get(Arrays.asList(downA), Arrays.asList(downM));
    assertNull(String.valueOf(upA.getException()), upA.getException());
    assertNull(String.valueOf(upM.getException()), upM.getException());
    assertNull(String.valueOf(downA.getException()), downA.getException());
    assertNull(String.valueOf(downM.getException()), downM.getException());
    assertEquals(0, downAFile.length());
    assertEquals(0, downMFile.length());
    connector.close();
}
Also used : ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) ArtifactUpload(org.sonatype.aether.spi.connector.ArtifactUpload) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) Metadata(org.sonatype.aether.metadata.Metadata) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) MetadataUpload(org.sonatype.aether.spi.connector.MetadataUpload) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) File(java.io.File) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) Artifact(org.sonatype.aether.artifact.Artifact) Test(org.junit.Test)

Example 7 with Metadata

use of org.sonatype.aether.metadata.Metadata in project sonatype-aether by sonatype.

the class ConnectorTestUtils method createTransfers.

/**
     * Creates transfer objects according to the given class. If the file parameter is {@code null}, a new temporary
     * file will be created for downloads. Uploads will just use the parameter as it is.
     */
public static <T extends Transfer> List<T> createTransfers(Class<T> cls, int count, File file) {
    ArrayList<T> ret = new ArrayList<T>();
    Object item;
    if (ArtifactTransfer.class.isAssignableFrom(cls)) {
        item = new StubArtifact("testGroup", "testArtifact", "sources", "jar", "will be replaced");
    } else {
        item = new StubMetadata("testGroup", "testArtifact", "will be replaced", "jar", Metadata.Nature.RELEASE_OR_SNAPSHOT, file);
    }
    for (int i = 0; i < count; i++) {
        String context = null;
        String checksumPolicy = RepositoryPolicy.CHECKSUM_POLICY_IGNORE;
        Object obj = null;
        if (cls.isAssignableFrom(ArtifactUpload.class)) {
            Artifact artifact = ((Artifact) item).setVersion((i + 1) + "-test");
            obj = new ArtifactUpload(artifact, file);
        } else if (cls.isAssignableFrom(ArtifactDownload.class)) {
            try {
                Artifact artifact = ((Artifact) item).setVersion((i + 1) + "-test");
                obj = new ArtifactDownload(artifact, context, safeFile(file), checksumPolicy);
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        } else if (cls.isAssignableFrom(MetadataUpload.class)) {
            Metadata metadata = ((StubMetadata) item).setVersion((i + 1) + "-test");
            obj = new MetadataUpload(metadata, file);
        } else if (cls.isAssignableFrom(MetadataDownload.class)) {
            try {
                Metadata metadata = ((StubMetadata) item).setVersion((i + 1) + "-test");
                obj = new MetadataDownload(metadata, context, safeFile(file), checksumPolicy);
            } catch (IOException e) {
                throw new RuntimeException(e.getMessage(), e);
            }
        }
        ret.add(cls.cast(obj));
    }
    return ret;
}
Also used : ArtifactUpload(org.sonatype.aether.spi.connector.ArtifactUpload) ArrayList(java.util.ArrayList) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) Metadata(org.sonatype.aether.metadata.Metadata) MetadataUpload(org.sonatype.aether.spi.connector.MetadataUpload) IOException(java.io.IOException) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) Artifact(org.sonatype.aether.artifact.Artifact) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) StubArtifact(org.sonatype.aether.test.util.impl.StubArtifact) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload)

Example 8 with Metadata

use of org.sonatype.aether.metadata.Metadata in project sonatype-aether by sonatype.

the class RecordingRepositoryConnector method get.

public void get(Collection<? extends ArtifactDownload> artifactDownloads, Collection<? extends MetadataDownload> metadataDownloads) {
    try {
        if (artifactDownloads != null) {
            for (ArtifactDownload artifactDownload : artifactDownloads) {
                artifactDownload.setState(State.ACTIVE);
                Artifact artifact = artifactDownload.getArtifact();
                this.actualGet.add(artifact);
                TestFileUtils.write(artifact.toString(), artifactDownload.getFile());
                artifactDownload.setState(State.DONE);
            }
        }
        if (metadataDownloads != null) {
            for (MetadataDownload metadataDownload : metadataDownloads) {
                metadataDownload.setState(State.ACTIVE);
                Metadata metadata = metadataDownload.getMetadata();
                this.actualGetMD.add(metadata);
                TestFileUtils.write(metadata.toString(), metadataDownload.getFile());
                metadataDownload.setState(State.DONE);
            }
        }
    } catch (IOException e) {
        throw new IllegalStateException("Cannot create temporary file", e);
    }
}
Also used : ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) Metadata(org.sonatype.aether.metadata.Metadata) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) IOException(java.io.IOException) Artifact(org.sonatype.aether.artifact.Artifact)

Example 9 with Metadata

use of org.sonatype.aether.metadata.Metadata in project sonatype-aether by sonatype.

the class TestLocalRepositoryManager method find.

public LocalMetadataResult find(RepositorySystemSession session, LocalMetadataRequest request) {
    Metadata metadata = request.getMetadata();
    LocalMetadataResult result = new LocalMetadataResult(request);
    File file = new File(localRepository.getBasedir(), getPathForLocalMetadata(metadata));
    result.setFile(file.isFile() ? file : null);
    return result;
}
Also used : LocalMetadataResult(org.sonatype.aether.repository.LocalMetadataResult) Metadata(org.sonatype.aether.metadata.Metadata) File(java.io.File)

Example 10 with Metadata

use of org.sonatype.aether.metadata.Metadata in project sonatype-aether by sonatype.

the class DefaultUpdateCheckManager method checkMetadata.

public void checkMetadata(RepositorySystemSession session, UpdateCheck<Metadata, MetadataTransferException> check) {
    if (check.getLocalLastUpdated() != 0 && !isUpdatedRequired(session, check.getLocalLastUpdated(), check.getPolicy())) {
        if (logger.isDebugEnabled()) {
            logger.debug("Skipped remote update check for " + check.getItem() + ", locally installed metadata up-to-date.");
        }
        check.setRequired(false);
        return;
    }
    Metadata metadata = check.getItem();
    RemoteRepository repository = check.getRepository();
    File metadataFile = check.getFile();
    if (metadataFile == null) {
        throw new IllegalArgumentException(String.format("The metadata '%s' has no file attached", metadata));
    }
    boolean fileExists = check.isFileValid() && metadataFile.exists();
    File touchFile = getTouchFile(metadata, metadataFile);
    Properties props = read(touchFile);
    String updateKey = getUpdateKey(metadataFile, repository);
    String dataKey = getDataKey(metadata, metadataFile, check.getAuthoritativeRepository());
    String error = getError(props, dataKey);
    long lastUpdated;
    if (error == null) {
        if (fileExists) {
            // last update was successful
            lastUpdated = getLastUpdated(props, dataKey);
        } else {
            // this is the first attempt ever
            lastUpdated = 0;
        }
    } else if (error.length() <= 0) {
        // metadata did not exist
        lastUpdated = getLastUpdated(props, dataKey);
    } else {
        // metadata could not be transferred
        String transferKey = getTransferKey(metadata, metadataFile, repository);
        lastUpdated = getLastUpdated(props, transferKey);
    }
    if (isAlreadyUpdated(session.getData(), updateKey)) {
        if (logger.isDebugEnabled()) {
            logger.debug("Skipped remote update check for " + check.getItem() + ", already updated during this session.");
        }
        check.setRequired(false);
        if (error != null) {
            check.setException(newException(error, metadata, repository));
        }
    } else if (lastUpdated == 0) {
        check.setRequired(true);
    } else if (isUpdatedRequired(session, lastUpdated, check.getPolicy())) {
        check.setRequired(true);
    } else if (fileExists) {
        if (logger.isDebugEnabled()) {
            logger.debug("Skipped remote update check for " + check.getItem() + ", locally cached metadata up-to-date.");
        }
        check.setRequired(false);
    } else {
        if (error == null || error.length() <= 0) {
            check.setRequired(false);
            check.setException(newException(error, metadata, repository));
        } else {
            if (session.isTransferErrorCachingEnabled()) {
                check.setRequired(false);
                check.setException(newException(error, metadata, repository));
            } else {
                check.setRequired(true);
            }
        }
    }
}
Also used : Metadata(org.sonatype.aether.metadata.Metadata) RemoteRepository(org.sonatype.aether.repository.RemoteRepository) Properties(java.util.Properties) File(java.io.File)

Aggregations

Metadata (org.sonatype.aether.metadata.Metadata)27 Test (org.junit.Test)15 Artifact (org.sonatype.aether.artifact.Artifact)10 File (java.io.File)9 StubMetadata (org.sonatype.aether.test.util.impl.StubMetadata)9 MetadataUpload (org.sonatype.aether.spi.connector.MetadataUpload)8 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)7 MetadataTransferException (org.sonatype.aether.transfer.MetadataTransferException)7 IOException (java.io.IOException)6 LocalMetadataResult (org.sonatype.aether.repository.LocalMetadataResult)6 StubArtifact (org.sonatype.aether.test.util.impl.StubArtifact)6 ArrayList (java.util.ArrayList)5 LocalMetadataRequest (org.sonatype.aether.repository.LocalMetadataRequest)5 LocalRepositoryManager (org.sonatype.aether.repository.LocalRepositoryManager)5 MetadataDownload (org.sonatype.aether.spi.connector.MetadataDownload)5 LocalRepository (org.sonatype.aether.repository.LocalRepository)4 MetadataNotFoundException (org.sonatype.aether.transfer.MetadataNotFoundException)4 RepositorySystemSession (org.sonatype.aether.RepositorySystemSession)3 RequestTrace (org.sonatype.aether.RequestTrace)3 LocalArtifactRegistration (org.sonatype.aether.repository.LocalArtifactRegistration)3