mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-08 16:18:05 +03:00
Merge pull request #34 from hierynomus/scp-filter-copy
Added upload filter capability to SCPUploadClient
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -76,6 +76,12 @@
|
|||||||
<version>0.9.29</version>
|
<version>0.9.29</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-all</artifactId>
|
||||||
|
<version>1.9.0-rc1</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
package net.schmizz.sshj.xfer.scp;
|
package net.schmizz.sshj.xfer.scp;
|
||||||
|
|
||||||
import net.schmizz.sshj.common.IOUtils;
|
import net.schmizz.sshj.common.IOUtils;
|
||||||
|
import net.schmizz.sshj.xfer.LocalFileFilter;
|
||||||
import net.schmizz.sshj.xfer.LocalSourceFile;
|
import net.schmizz.sshj.xfer.LocalSourceFile;
|
||||||
import net.schmizz.sshj.xfer.scp.SCPEngine.Arg;
|
import net.schmizz.sshj.xfer.scp.SCPEngine.Arg;
|
||||||
|
|
||||||
@@ -28,6 +29,7 @@ import java.util.List;
|
|||||||
public final class SCPUploadClient {
|
public final class SCPUploadClient {
|
||||||
|
|
||||||
private final SCPEngine engine;
|
private final SCPEngine engine;
|
||||||
|
private LocalFileFilter uploadFilter;
|
||||||
|
|
||||||
SCPUploadClient(SCPEngine engine) {
|
SCPUploadClient(SCPEngine engine) {
|
||||||
this.engine = engine;
|
this.engine = engine;
|
||||||
@@ -45,6 +47,10 @@ public final class SCPUploadClient {
|
|||||||
return engine.getExitStatus();
|
return engine.getExitStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setUploadFilter(LocalFileFilter uploadFilter) {
|
||||||
|
this.uploadFilter = uploadFilter;
|
||||||
|
}
|
||||||
|
|
||||||
private synchronized void startCopy(LocalSourceFile sourceFile, String targetPath)
|
private synchronized void startCopy(LocalSourceFile sourceFile, String targetPath)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
List<Arg> args = new LinkedList<Arg>();
|
List<Arg> args = new LinkedList<Arg>();
|
||||||
@@ -75,7 +81,7 @@ public final class SCPUploadClient {
|
|||||||
throws IOException {
|
throws IOException {
|
||||||
preserveTimeIfPossible(f);
|
preserveTimeIfPossible(f);
|
||||||
engine.sendMessage("D0" + getPermString(f) + " 0 " + f.getName());
|
engine.sendMessage("D0" + getPermString(f) + " 0 " + f.getName());
|
||||||
for (LocalSourceFile child : f.getChildren(null))
|
for (LocalSourceFile child : f.getChildren(uploadFilter))
|
||||||
process(child);
|
process(child);
|
||||||
engine.sendMessage("E");
|
engine.sendMessage("E");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
package net.schmizz.sshj.xfer.scp;
|
||||||
|
|
||||||
|
import net.schmizz.sshj.xfer.FileSystemFile;
|
||||||
|
import net.schmizz.sshj.xfer.LocalFileFilter;
|
||||||
|
import net.schmizz.sshj.xfer.LocalSourceFile;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
import org.mockito.verification.VerificationMode;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import static org.mockito.Matchers.endsWith;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
|
import static org.mockito.Matchers.isA;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
|
||||||
|
public class SCPUploadClientTest {
|
||||||
|
|
||||||
|
private SCPEngine engine;
|
||||||
|
private SCPUploadClient scpUploadClient;
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public TemporaryFolder temp = new TemporaryFolder();
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void init() {
|
||||||
|
engine = mock(SCPEngine.class);
|
||||||
|
scpUploadClient = new SCPUploadClient(engine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldOnlySendFilterAcceptedFilesFromDirectory() throws IOException {
|
||||||
|
scpUploadClient.setUploadFilter(new LocalFileFilter() {
|
||||||
|
@Override
|
||||||
|
public boolean accept(LocalSourceFile file) {
|
||||||
|
return !file.getName().contains("not-");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
File dir = temp.newFolder("filtered-scp-upload");
|
||||||
|
new File(dir, "not-sent.txt").createNewFile();
|
||||||
|
new File(dir, "sent.txt").createNewFile();
|
||||||
|
|
||||||
|
int copy = scpUploadClient.copy(new FileSystemFile(dir), "/tmp");
|
||||||
|
verify(engine).startedDir("filtered-scp-upload");
|
||||||
|
verify(engine).startedFile(eq("sent.txt"), isA(Long.class));
|
||||||
|
verify(engine, times(1)).startedFile(isA(String.class), isA(Long.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user