Java – unit testing using Apache Mina as an emulated / in memory SFTP server

I'm working on how to use Apache Mina Their documents are a little inadequate for my powerless brain I've seen useful starting code

What I can't imagine is how to use it I want to set up a unit test to check my SFTP code. Using Mina as a simulation server, I can write a unit test, such as:

@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() {

}

The question is what to put in testgetfile()

I've been browsing the test code to see if more configuration is needed to specify the root directory, user name and authentication key file name So do I need to use the client or my own SFTP api code to get and pull files?

I believe this is a good API, but without a lot of guidance, anyone can help?

Solution

This 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,numRead));
      }
    } finally {    
      reader.close();
    }

    return fileData.toString();
  }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>