赞
踩
最近在做Junit测试,因而需要对代码进行挡泥板操作,但是在编写过程中遇到了难题,sftp这个东西没法mock出来。很是头痛!后来想的是那可以模拟临时启动一个,结束后在删掉。因而在选型时采用了sshd+jsch的模式
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.55</version>
</dependency>
package com.juneyaoair; import com.jcraft.jsch.*; import lombok.extern.slf4j.Slf4j; import org.apache.sshd.server.SshServer; import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; import org.apache.sshd.server.scp.ScpCommandFactory; import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Collections; import java.util.Properties; import java.util.Vector; @Slf4j public class SFTPMockTest { private SshServer sshd; @Before public void beforeTestSetup() throws Exception { sshd = SshServer.setUpDefaultServer(); sshd.setPort(2294); sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("host.ser"))); sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); sshd.setPasswordAuthenticator((username, password, session) -> username.equals("test") && password.equals("password")); sshd.setCommandFactory(new ScpCommandFactory()); sshd.start(); log.info("SFTP server started"); } @After public void teardown() throws Exception { sshd.stop(); } @Test public void test() throws JSchException { Channel channel = null; ChannelSftp sftp = null; try { JSch jSch = new JSch(); //以test身份登录,默认在2294端口 Session session = jSch.getSession("test", "localhost", 2294); session.setPassword("password"); Properties config = new Properties(); //设置不用检查HostKey config.put("StrictHostKeyChecking", "no"); session.setConfig(config); session.connect(); //使用sftp操作 channel = session.openChannel("sftp"); channel.connect(); sftp = (ChannelSftp) channel; //操作----------------------------------------------- sftp.cd("/"); InputStream is = new InputStream() { @Override public int read() throws IOException { return -1; } }; sftp.put(is, "emptyFile.txt"); Vector<ChannelSftp.LsEntry> files = sftp.ls("/"); files.forEach(r -> System.out.println(r.getFilename())); //操作----------------------------------------------- Assert.assertTrue(true); } catch (Exception e) { log.error("错误{}", e.getMessage()); } finally { assert sftp != null; sftp.disconnect(); channel.disconnect(); } } }
最新版为2.5.0 一般google出来的都是1.7.0版本。变化较大,官网给的还是老版本使用方式。
如
VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory();
//该处参数为path 而不是String
fileSystemFactory.setDefaultHomeDir("home.directory");
sshd.setFileSystemFactory(fileSystemFactory)
//SftpSubsystem.Factory这个内部类根本就不存在
sshd.setSubsystemFactories( Arrays.<NamedFactory<Command>>asList(new SftpSubsystem.Factory()));
//CommandFactory Command这个类高版本已经不存在了
CommandFactory myCommandFactory = new CommandFactory() {
public Command createCommand(String command) {
System.out.println("Command: " + command);
return null;
}
};
sshd.setCommandFactory(new ScpCommandFactory(myCommandFactory));
List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。