ch.cyberduck.core.spectra.SpectraUploadFeatureTest.java Source code

Java tutorial

Introduction

Here is the source code for ch.cyberduck.core.spectra.SpectraUploadFeatureTest.java

Source

package ch.cyberduck.core.spectra;

/*
 * Copyright (c) 2002-2016 iterate GmbH. All rights reserved.
 * https://cyberduck.io/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

import ch.cyberduck.core.Credentials;
import ch.cyberduck.core.DisabledCancelCallback;
import ch.cyberduck.core.DisabledConnectionCallback;
import ch.cyberduck.core.DisabledHostKeyCallback;
import ch.cyberduck.core.DisabledLoginCallback;
import ch.cyberduck.core.DisabledPasswordStore;
import ch.cyberduck.core.Host;
import ch.cyberduck.core.Local;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.Scheme;
import ch.cyberduck.core.features.Delete;
import ch.cyberduck.core.io.BandwidthThrottle;
import ch.cyberduck.core.io.DisabledStreamListener;
import ch.cyberduck.core.ssl.DefaultX509KeyManager;
import ch.cyberduck.core.ssl.DisabledX509TrustManager;
import ch.cyberduck.core.transfer.Transfer;
import ch.cyberduck.core.transfer.TransferStatus;
import ch.cyberduck.test.IntegrationTest;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.UUID;

import static org.junit.Assert.assertArrayEquals;

@Category(IntegrationTest.class)
public class SpectraUploadFeatureTest {

    @Test
    public void testUpload() throws Exception {
        final Host host = new Host(new SpectraProtocol() {
            @Override
            public Scheme getScheme() {
                return Scheme.http;
            }
        }, System.getProperties().getProperty("spectra.hostname"),
                Integer.valueOf(System.getProperties().getProperty("spectra.port")),
                new Credentials(System.getProperties().getProperty("spectra.user"),
                        System.getProperties().getProperty("spectra.key")));
        final SpectraSession session = new SpectraSession(host, new DisabledX509TrustManager(),
                new DefaultX509KeyManager());
        session.open(new DisabledHostKeyCallback());
        session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
        final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
        final int length = 32770;
        final byte[] content = RandomUtils.nextBytes(length);
        final OutputStream out = local.getOutputStream(false);
        IOUtils.write(content, out);
        out.close();
        final Path container = new Path("cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
        final Path test = new Path(container, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
        final TransferStatus writeStatus = new TransferStatus().length(content.length);
        final SpectraBulkService bulk = new SpectraBulkService(session);
        bulk.pre(Transfer.Type.upload, Collections.singletonMap(test, writeStatus),
                new DisabledConnectionCallback());
        final SpectraUploadFeature upload = new SpectraUploadFeature(new SpectraWriteFeature(session),
                new SpectraBulkService(session));
        upload.upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(),
                writeStatus, new DisabledConnectionCallback());
        final byte[] buffer = new byte[content.length];
        final TransferStatus readStatus = new TransferStatus().length(content.length);
        bulk.pre(Transfer.Type.download, Collections.singletonMap(test, readStatus),
                new DisabledConnectionCallback());
        final InputStream in = new SpectraReadFeature(session).read(test, readStatus,
                new DisabledConnectionCallback());
        IOUtils.readFully(in, buffer);
        in.close();
        assertArrayEquals(content, buffer);
        new SpectraDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(),
                new Delete.DisabledCallback());
        local.delete();
        session.close();
    }

    @Test
    public void testUploadMultipleFiles() throws Exception {
        final Host host = new Host(new SpectraProtocol() {
            @Override
            public Scheme getScheme() {
                return Scheme.http;
            }
        }, System.getProperties().getProperty("spectra.hostname"),
                Integer.valueOf(System.getProperties().getProperty("spectra.port")),
                new Credentials(System.getProperties().getProperty("spectra.user"),
                        System.getProperties().getProperty("spectra.key")));
        final SpectraSession session = new SpectraSession(host, new DisabledX509TrustManager(),
                new DefaultX509KeyManager());
        session.open(new DisabledHostKeyCallback());
        session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
        final Local local1 = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
        final TransferStatus status1;
        {
            final int length = 32770;
            final byte[] content = RandomUtils.nextBytes(length);
            final OutputStream out = local1.getOutputStream(false);
            IOUtils.write(content, out);
            out.close();
            status1 = new TransferStatus().length(content.length);
        }
        final Local local2 = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
        final TransferStatus status2;
        {
            final int length = 32770;
            final byte[] content = RandomUtils.nextBytes(length);
            final OutputStream out = local2.getOutputStream(false);
            IOUtils.write(content, out);
            out.close();
            status2 = new TransferStatus().length(content.length);
        }
        final Path container = new Path("cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume));
        final Path test1 = new Path(container, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
        final Path test2 = new Path(container, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
        final SpectraBulkService bulk = new SpectraBulkService(session);
        final HashMap<Path, TransferStatus> files = new HashMap<>();
        files.put(test1, status1);
        files.put(test2, status2);
        bulk.pre(Transfer.Type.upload, files, new DisabledConnectionCallback());
        final SpectraUploadFeature upload = new SpectraUploadFeature(new SpectraWriteFeature(session),
                new SpectraBulkService(session));
        upload.upload(test1, local1, new BandwidthThrottle(BandwidthThrottle.UNLIMITED),
                new DisabledStreamListener(), status1, new DisabledConnectionCallback());
        upload.upload(test2, local2, new BandwidthThrottle(BandwidthThrottle.UNLIMITED),
                new DisabledStreamListener(), status2, new DisabledConnectionCallback());
        new SpectraDeleteFeature(session).delete(Arrays.asList(test1, test2), new DisabledLoginCallback(),
                new Delete.DisabledCallback());
        local1.delete();
        local2.delete();
        session.close();
    }
}