1 package ch
.cyberduck
.core
.shared
;
4 * Copyright (c) 2002-2015 David Kocher. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * Bug fixes, suggestions and comments should be sent to feedback@cyberduck.ch
20 import ch
.cyberduck
.core
.AbstractTestCase
;
21 import ch
.cyberduck
.core
.Credentials
;
22 import ch
.cyberduck
.core
.DisabledCancelCallback
;
23 import ch
.cyberduck
.core
.DisabledConnectionCallback
;
24 import ch
.cyberduck
.core
.DisabledHostKeyCallback
;
25 import ch
.cyberduck
.core
.DisabledLoginCallback
;
26 import ch
.cyberduck
.core
.DisabledPasswordStore
;
27 import ch
.cyberduck
.core
.DisabledTranscriptListener
;
28 import ch
.cyberduck
.core
.Host
;
29 import ch
.cyberduck
.core
.Local
;
30 import ch
.cyberduck
.core
.Path
;
31 import ch
.cyberduck
.core
.Session
;
32 import ch
.cyberduck
.core
.features
.Delete
;
33 import ch
.cyberduck
.core
.features
.Read
;
34 import ch
.cyberduck
.core
.ftp
.FTPSession
;
35 import ch
.cyberduck
.core
.ftp
.FTPTLSProtocol
;
36 import ch
.cyberduck
.core
.io
.BandwidthThrottle
;
37 import ch
.cyberduck
.core
.io
.DisabledStreamListener
;
38 import ch
.cyberduck
.core
.sftp
.SFTPProtocol
;
39 import ch
.cyberduck
.core
.sftp
.SFTPSession
;
40 import ch
.cyberduck
.core
.transfer
.TransferStatus
;
42 import org
.apache
.commons
.io
.IOUtils
;
43 import org
.junit
.Test
;
45 import java
.io
.InputStream
;
46 import java
.io
.OutputStream
;
47 import java
.util
.Collections
;
48 import java
.util
.EnumSet
;
49 import java
.util
.Random
;
50 import java
.util
.UUID
;
52 import static org
.junit
.Assert
.assertArrayEquals
;
57 public class DefaultUploadFeatureTest
extends AbstractTestCase
{
60 public void testTransferSegment() throws Exception
{
62 final Host host
= new Host(new FTPTLSProtocol(), "test.cyberduck.ch", new Credentials(
63 properties
.getProperty("ftp.user"), properties
.getProperty("ftp.password")
65 final FTPSession session
= new FTPSession(host
);
69 final Host host
= new Host(new SFTPProtocol(), "test.cyberduck.ch", new Credentials(
70 properties
.getProperty("sftp.user"), properties
.getProperty("sftp.password")
72 final SFTPSession session
= new SFTPSession(host
);
77 private void run(final Session
<?
> session
) throws Exception
{
78 session
.open(new DisabledHostKeyCallback(), new DisabledTranscriptListener());
79 session
.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
80 final Local local
= new Local(System
.getProperty("java.io.tmpdir"), UUID
.randomUUID().toString());
81 final byte[] content
= new byte[32770];
82 new Random().nextBytes(content
);
83 final OutputStream out
= local
.getOutputStream(false);
84 IOUtils
.write(content
, out
);
85 IOUtils
.closeQuietly(out
);
86 final Path test
= new Path(new DefaultHomeFinderService(session
).find(), UUID
.randomUUID().toString(), EnumSet
.of(Path
.Type
.file
));
88 final TransferStatus status
= new TransferStatus().length(content
.length
/ 2);
89 new DefaultUploadFeature(session
).upload(
90 test
, local
, new BandwidthThrottle(BandwidthThrottle
.UNLIMITED
), new DisabledStreamListener(),
92 new DisabledConnectionCallback());
95 final TransferStatus status
= new TransferStatus().length(content
.length
/ 2).skip(content
.length
/ 2).append(true);
96 new DefaultUploadFeature(session
).upload(
97 test
, local
, new BandwidthThrottle(BandwidthThrottle
.UNLIMITED
), new DisabledStreamListener(),
99 new DisabledConnectionCallback());
101 final byte[] buffer
= new byte[content
.length
];
102 final Read read
= session
.getFeature(Read
.class);
103 final InputStream in
= read
.read(test
, new TransferStatus().length(content
.length
));
104 IOUtils
.readFully(in
, buffer
);
105 IOUtils
.closeQuietly(in
);
106 assertArrayEquals(content
, buffer
);
107 final Delete delete
= session
.getFeature(Delete
.class);
108 delete
.delete(Collections
.singletonList(test
), new DisabledLoginCallback(), new Delete
.Callback() {
110 public void delete(final Path file
) {