使用Apache Mina作为模拟/内存SFTP服务器进行单元测试 [英] Using Apache Mina as a Mock/In Memory SFTP Server for Unit Testing

查看:372
本文介绍了使用Apache Mina作为模拟/内存SFTP服务器进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在解决如何使用Apache Mina时遇到了一些麻烦。他们的文档对于我无能为力的大脑来说有点不足。我在
Java 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?

我无法弄清楚如何使用它。我想设置一个单元测试来检查我的sftp代码,使用Mina作为一种模拟服务器,即能够编写单元测试,如:

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<NamedFactory<UserAuth>> userAuthFactories = new ArrayList<NamedFactory<UserAuth>>();
    userAuthFactories.add(new UserAuthNone.Factory());
    sshd.setUserAuthFactories(userAuthFactories);
    sshd.setPublickeyAuthenticator(new PublickeyAuthenticator());


    sshd.setCommandFactory(new ScpCommandFactory());

    List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();
    namedFactoryList.add(new SftpSubsystem.Factory());
    sshd.setSubsystemFactories(namedFactoryList);

    try {
        sshd.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Test
public void testGetFile() {

}

问题是放入 testGetFile()的内容。

我一直在浏览测试代码,想知道上面是否需要更多配置来指定根目录,用户名和身份验证密钥文件名。然后我需要使用客户端或我自己的SFTP api代码从中获取和提取文件?

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?

我确信这是一个优秀的API,只是对它没有太多的指导,有人可以帮忙吗?

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

推荐答案

这就是我所做的(JUnit):

Here is what I did (JUnit):

  @Test
  public void testPutAndGetFile() throws JSchException, SftpException, IOException
  {
    JSch jsch = new JSch();

    Hashtable<String, String> config = new Hashtable<String, String>();
    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();
  }

这篇关于使用Apache Mina作为模拟/内存SFTP服务器进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆