java Mina sftp,使用Apache Mina作为模拟/内存SFTP服务器进行单元测试

I am having a bit of trouble working out how to use Apache Mina. Their documentation is a bit scant for my talentless brain to work out. I have seen the helpful starting code at

Java SFTP server library?

What I can't figure out is how to use it. I want to setup a unit test that checks my sftp code, using Mina as a kind of mock server, i.e., be able to write a unit test like:

@Before

public void beforeTestSetup() {

sshd = SshServer.setUpDefaultServer();

sshd.setPort(22);

sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser"));

List> userAuthFactories = new ArrayList>();

userAuthFactories.add(new UserAuthNone.Factory());

sshd.setUserAuthFactories(userAuthFactories);

sshd.setPublickeyAuthenticator(new PublickeyAuthenticator());

sshd.setCommandFactory(new ScpCommandFactory());

List> namedFactoryList = new ArrayList>();

namedFactoryList.add(new SftpSubsystem.Factory());

sshd.setSubsystemFactories(namedFactoryList);

try {

sshd.start();

} catch (Exception e) {

e.printStackTrace();

}

}

@Test

public void testGetFile() {

}

The question is what to put in testGetFile().

I've been trawling through the test code wondering whether more configuration is needed in the above to specify a root directory, a user name and an authentication key file name. Then I'll need to get and pull files from it using a client, or my own SFTP api code?

I'm sure this is an excellent API, there's just not a lot of instruction for it, can anyone help?

解决方案

Here is what I did (JUnit):

@Test

public void testPutAndGetFile() throws JSchException, SftpException, IOException

{

JSch jsch = new JSch();

Hashtable config = new Hashtable();

config.put("StrictHostKeyChecking", "no");

JSch.setConfig(config);

Session session = jsch.getSession( "remote-username", "localhost", PORT);

session.setPassword("remote-password");

session.connect();

Channel channel = session.openChannel( "sftp" );

channel.connect();

ChannelSftp sftpChannel = (ChannelSftp) channel;

final String testFileContents = "some file contents";

String uploadedFileName = "uploadFile";

sftpChannel.put(new ByteArrayInputStream(testFileContents.getBytes()), uploadedFileName);

String downloadedFileName = "downLoadFile";

sftpChannel.get(uploadedFileName, downloadedFileName);

File downloadedFile = new File(downloadedFileName);

Assert.assertTrue(downloadedFile.exists());

String fileData = getFileContents(downloadedFile);

Assert.assertEquals(testFileContents, fileData);

if (sftpChannel.isConnected()) {

sftpChannel.exit();

System.out.println("Disconnected channel");

}

if (session.isConnected()) {

session.disconnect();

System.out.println("Disconnected session");

}

}

private String getFileContents(File downloadedFile)

throws FileNotFoundException, IOException

{

StringBuffer fileData = new StringBuffer();

BufferedReader reader = new BufferedReader(new FileReader(downloadedFile));

try {

char[] buf = new char[1024];

for(int numRead = 0; (numRead = reader.read(buf)) != -1; buf = new char[1024]) {

fileData.append(String.valueOf(buf, 0, numRead));

}

} finally {

reader.close();

}

return fileData.toString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值