Search in sources :

Example 11 with ArtifactUpload

use of org.sonatype.aether.spi.connector.ArtifactUpload 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 12 with ArtifactUpload

use of org.sonatype.aether.spi.connector.ArtifactUpload in project sonatype-aether by sonatype.

the class TransferEventTester method testFailedTransferEvents.

/**
 * Test the order of events and their properties for the unsuccessful up- and download of artifact and metadata.
 * Failure is triggered by setting the file to transfer to {@code null} for uploads, and asking for a non-existent
 * item for downloads.
 */
public static void testFailedTransferEvents(RepositoryConnectorFactory factory, TestRepositorySystemSession session, RemoteRepository repository) throws NoRepositoryConnectorException, IOException {
    RecordingTransferListener listener = new RecordingTransferListener(session.getTransferListener());
    session.setTransferListener(listener);
    RepositoryConnector connector = factory.newInstance(session, repository);
    byte[] pattern = "tmpFile".getBytes("us-ascii");
    File tmpFile = TestFileUtils.createTempFile(pattern, 10000);
    Collection<ArtifactUpload> artUps = ConnectorTestUtils.createTransfers(ArtifactUpload.class, 1, null);
    Collection<ArtifactDownload> artDowns = ConnectorTestUtils.createTransfers(ArtifactDownload.class, 1, tmpFile);
    Collection<MetadataUpload> metaUps = ConnectorTestUtils.createTransfers(MetadataUpload.class, 1, null);
    Collection<MetadataDownload> metaDowns = ConnectorTestUtils.createTransfers(MetadataDownload.class, 1, tmpFile);
    connector.put(artUps, null);
    LinkedList<TransferEvent> events = new LinkedList<TransferEvent>(listener.getEvents());
    checkFailedEvents(events, null);
    listener.clear();
    connector.get(artDowns, null);
    events = new LinkedList<TransferEvent>(listener.getEvents());
    checkFailedEvents(events, null);
    listener.clear();
    connector.put(null, metaUps);
    events = new LinkedList<TransferEvent>(listener.getEvents());
    checkFailedEvents(events, null);
    listener.clear();
    connector.get(null, metaDowns);
    events = new LinkedList<TransferEvent>(listener.getEvents());
    checkFailedEvents(events, null);
    connector.close();
    session.setTransferListener(null);
}
Also used : ArtifactUpload(org.sonatype.aether.spi.connector.ArtifactUpload) TransferEvent(org.sonatype.aether.transfer.TransferEvent) MetadataUpload(org.sonatype.aether.spi.connector.MetadataUpload) RecordingTransferListener(org.sonatype.aether.test.impl.RecordingTransferListener) LinkedList(java.util.LinkedList) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) File(java.io.File)

Example 13 with ArtifactUpload

use of org.sonatype.aether.spi.connector.ArtifactUpload in project sonatype-aether by sonatype.

the class TransferEventTester method testSuccessfulTransferEvents.

/**
 * Test the order of events and their properties for the successful up- and download of artifact and metadata.
 */
public static void testSuccessfulTransferEvents(RepositoryConnectorFactory factory, TestRepositorySystemSession session, RemoteRepository repository) throws NoRepositoryConnectorException, IOException {
    RecordingTransferListener listener = new RecordingTransferListener(session.getTransferListener());
    session.setTransferListener(listener);
    RepositoryConnector connector = factory.newInstance(session, repository);
    byte[] pattern = "tmpFile".getBytes();
    File tmpFile = TestFileUtils.createTempFile(pattern, 10000);
    long expectedBytes = tmpFile.length();
    Collection<ArtifactUpload> artUps = ConnectorTestUtils.createTransfers(ArtifactUpload.class, 1, tmpFile);
    Collection<ArtifactDownload> artDowns = ConnectorTestUtils.createTransfers(ArtifactDownload.class, 1, tmpFile);
    Collection<MetadataUpload> metaUps = ConnectorTestUtils.createTransfers(MetadataUpload.class, 1, tmpFile);
    Collection<MetadataDownload> metaDowns = ConnectorTestUtils.createTransfers(MetadataDownload.class, 1, tmpFile);
    connector.put(artUps, null);
    LinkedList<TransferEvent> events = new LinkedList<TransferEvent>(listener.getEvents());
    checkEvents(events, expectedBytes);
    listener.clear();
    connector.get(artDowns, null);
    events = new LinkedList<TransferEvent>(listener.getEvents());
    checkEvents(events, expectedBytes);
    listener.clear();
    connector.put(null, metaUps);
    events = new LinkedList<TransferEvent>(listener.getEvents());
    checkEvents(events, expectedBytes);
    listener.clear();
    connector.get(null, metaDowns);
    events = new LinkedList<TransferEvent>(listener.getEvents());
    checkEvents(events, expectedBytes);
    connector.close();
    session.setTransferListener(null);
}
Also used : ArtifactUpload(org.sonatype.aether.spi.connector.ArtifactUpload) TransferEvent(org.sonatype.aether.transfer.TransferEvent) MetadataUpload(org.sonatype.aether.spi.connector.MetadataUpload) RecordingTransferListener(org.sonatype.aether.test.impl.RecordingTransferListener) LinkedList(java.util.LinkedList) ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) File(java.io.File)

Example 14 with ArtifactUpload

use of org.sonatype.aether.spi.connector.ArtifactUpload in project sonatype-aether by sonatype.

the class ConnectorTestSuite method testProgressEventsDataBuffer.

@Test
public void testProgressEventsDataBuffer() throws UnsupportedEncodingException, IOException, NoSuchAlgorithmException, NoRepositoryConnectorException {
    byte[] bytes = "These are the test contents.\n".getBytes("UTF-8");
    int count = 120000;
    MessageDigest digest = MessageDigest.getInstance("SHA-1");
    for (int i = 0; i < count; i++) {
        digest.update(bytes);
    }
    byte[] hash = digest.digest();
    File file = TestFileUtils.createTempFile(bytes, count);
    Artifact artifact = new StubArtifact("gid:aid:ext:ver");
    ArtifactUpload upA = new ArtifactUpload(artifact, file);
    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, file);
    File downMFile = new File(dir, "downM.file");
    downMFile.deleteOnExit();
    MetadataDownload downM = new MetadataDownload(metadata, "", downMFile, RepositoryPolicy.CHECKSUM_POLICY_FAIL);
    DigestingTransferListener listener = new DigestingTransferListener();
    session.setTransferListener(listener);
    RepositoryConnector connector = factory().newInstance(session, repository);
    connector.put(Arrays.asList(upA), null);
    assertArrayEquals(hash, listener.getHash());
    listener.rewind();
    connector.put(null, Arrays.asList(upM));
    assertArrayEquals(hash, listener.getHash());
    listener.rewind();
    connector.get(Arrays.asList(downA), null);
    assertArrayEquals(hash, listener.getHash());
    listener.rewind();
    connector.get(null, Arrays.asList(downM));
    assertArrayEquals(hash, listener.getHash());
    listener.rewind();
    connector.close();
}
Also used : ArtifactUpload(org.sonatype.aether.spi.connector.ArtifactUpload) StubMetadata(org.sonatype.aether.test.util.impl.StubMetadata) Metadata(org.sonatype.aether.metadata.Metadata) MetadataUpload(org.sonatype.aether.spi.connector.MetadataUpload) 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) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) MessageDigest(java.security.MessageDigest) File(java.io.File) Test(org.junit.Test)

Example 15 with ArtifactUpload

use of org.sonatype.aether.spi.connector.ArtifactUpload in project sonatype-aether by sonatype.

the class ConnectorTestSuite method testBlocking.

@Test
public void testBlocking() throws NoRepositoryConnectorException, IOException {
    RepositoryConnector connector = factory().newInstance(session, repository);
    int count = 10;
    byte[] pattern = "tmpFile".getBytes("UTF-8");
    File tmpFile = TestFileUtils.createTempFile(pattern, 100000);
    List<ArtifactUpload> artUps = ConnectorTestUtils.createTransfers(ArtifactUpload.class, count, tmpFile);
    List<MetadataUpload> metaUps = ConnectorTestUtils.createTransfers(MetadataUpload.class, count, tmpFile);
    List<ArtifactDownload> artDowns = ConnectorTestUtils.createTransfers(ArtifactDownload.class, count, null);
    List<MetadataDownload> metaDowns = ConnectorTestUtils.createTransfers(MetadataDownload.class, count, null);
    // this should block until all transfers are done - racing condition, better way to test this?
    connector.put(artUps, metaUps);
    connector.get(artDowns, metaDowns);
    for (int i = 0; i < count; i++) {
        ArtifactUpload artUp = artUps.get(i);
        MetadataUpload metaUp = metaUps.get(i);
        ArtifactDownload artDown = artDowns.get(i);
        MetadataDownload metaDown = metaDowns.get(i);
        assertTrue(Transfer.State.DONE.equals(artUp.getState()));
        assertTrue(Transfer.State.DONE.equals(artDown.getState()));
        assertTrue(Transfer.State.DONE.equals(metaUp.getState()));
        assertTrue(Transfer.State.DONE.equals(metaDown.getState()));
    }
    connector.close();
}
Also used : ArtifactDownload(org.sonatype.aether.spi.connector.ArtifactDownload) ArtifactUpload(org.sonatype.aether.spi.connector.ArtifactUpload) RepositoryConnector(org.sonatype.aether.spi.connector.RepositoryConnector) MetadataUpload(org.sonatype.aether.spi.connector.MetadataUpload) MetadataDownload(org.sonatype.aether.spi.connector.MetadataDownload) File(java.io.File) Test(org.junit.Test)

Aggregations

ArtifactUpload (org.sonatype.aether.spi.connector.ArtifactUpload)18 MetadataUpload (org.sonatype.aether.spi.connector.MetadataUpload)13 File (java.io.File)9 Test (org.junit.Test)9 ArtifactDownload (org.sonatype.aether.spi.connector.ArtifactDownload)9 MetadataDownload (org.sonatype.aether.spi.connector.MetadataDownload)8 RepositoryConnector (org.sonatype.aether.spi.connector.RepositoryConnector)8 Artifact (org.sonatype.aether.artifact.Artifact)7 StubArtifact (org.sonatype.aether.test.util.impl.StubArtifact)6 StubMetadata (org.sonatype.aether.test.util.impl.StubMetadata)5 Metadata (org.sonatype.aether.metadata.Metadata)4 ArrayList (java.util.ArrayList)3 ArtifactTransferException (org.sonatype.aether.transfer.ArtifactTransferException)3 LinkedList (java.util.LinkedList)2 RemoteRepository (org.sonatype.aether.repository.RemoteRepository)2 RecordingTransferListener (org.sonatype.aether.test.impl.RecordingTransferListener)2 TransferEvent (org.sonatype.aether.transfer.TransferEvent)2 IOException (java.io.IOException)1 MessageDigest (java.security.MessageDigest)1 IdentityHashMap (java.util.IdentityHashMap)1