ch.cyberduck.core.googledrive.DriveUploadFeatureTest.java Source code

Java tutorial

Introduction

Here is the source code for ch.cyberduck.core.googledrive.DriveUploadFeatureTest.java

Source

package ch.cyberduck.core.googledrive;

/*
 * 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.DisabledConnectionCallback;
import ch.cyberduck.core.DisabledListProgressListener;
import ch.cyberduck.core.DisabledLoginCallback;
import ch.cyberduck.core.Local;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathCache;
import ch.cyberduck.core.features.Delete;
import ch.cyberduck.core.features.Find;
import ch.cyberduck.core.http.HttpConnectionPoolBuilder;
import ch.cyberduck.core.io.BandwidthThrottle;
import ch.cyberduck.core.io.DisabledStreamListener;
import ch.cyberduck.core.proxy.DisabledProxyFinder;
import ch.cyberduck.core.ssl.DefaultX509KeyManager;
import ch.cyberduck.core.ssl.DisabledX509TrustManager;
import ch.cyberduck.core.ssl.ThreadLocalHostnameDelegatingTrustManager;
import ch.cyberduck.core.transfer.TransferStatus;
import ch.cyberduck.test.IntegrationTest;

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

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

import static org.junit.Assert.*;

@Category(IntegrationTest.class)
public class DriveUploadFeatureTest extends AbstractDriveTest {

    @Test
    public void testWrite() throws Exception {
        final TransferStatus status = new TransferStatus();
        final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
        final byte[] content = "test".getBytes("UTF-8");
        final OutputStream out = local.getOutputStream(false);
        IOUtils.write(content, out);
        IOUtils.closeQuietly(out);
        status.setLength(content.length);
        final Path test = new Path(new DriveHomeFinderService(session).find(), UUID.randomUUID().toString(),
                EnumSet.of(Path.Type.file));
        final HttpConnectionPoolBuilder builder = new HttpConnectionPoolBuilder(session.getHost(),
                new ThreadLocalHostnameDelegatingTrustManager(new DisabledX509TrustManager(),
                        session.getHost().getHostname()),
                new DefaultX509KeyManager(), new DisabledProxyFinder());
        final DriveUploadFeature upload = new DriveUploadFeature(new DriveWriteFeature(session));
        upload.upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(),
                status, new DisabledConnectionCallback());
        test.attributes()
                .setVersionId(new DriveFileidProvider(session).getFileid(test, new DisabledListProgressListener()));
        assertTrue(session.getFeature(Find.class).find(test));
        assertEquals(content.length,
                session.list(test.getParent(), new DisabledListProgressListener()).get(test).attributes().getSize(),
                0L);
        assertEquals(content.length,
                new DriveWriteFeature(session).append(test, status.getLength(), PathCache.empty()).size, 0L);
        {
            final byte[] buffer = new byte[content.length];
            IOUtils.readFully(new DriveReadFeature(session).read(test, new TransferStatus(),
                    new DisabledConnectionCallback()), buffer);
            assertArrayEquals(content, buffer);
        }
        {
            final byte[] buffer = new byte[content.length - 1];
            final InputStream in = new DriveReadFeature(session).read(test,
                    new TransferStatus().length(content.length).append(true).skip(1L),
                    new DisabledConnectionCallback());
            IOUtils.readFully(in, buffer);
            IOUtils.closeQuietly(in);
            final byte[] reference = new byte[content.length - 1];
            System.arraycopy(content, 1, reference, 0, content.length - 1);
            assertArrayEquals(reference, buffer);
        }
        new DriveDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(),
                new Delete.DisabledCallback());
    }
}