2012-08-07 78 views
13

我在處理如何使用Apache Mina時遇到了一些麻煩。他們的文檔對於我的無能的大腦來說是有點缺乏的。我看到有用的起始代碼在 Java SFTP server library?使用Apache Mina作爲模擬/內存SFTP服務器進行單元測試

我無法弄清楚的是如何使用它。我想設置一個單元測試,檢查我的SFTP代碼,使用米娜作爲一種模擬服務器,即,能夠編寫單元測試,如:

@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代碼來從中獲取文件?

我確定這是一個很好的API,沒有太多的指導,任何人都可以幫忙嗎?

回答

7

這裏是我做的測試(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(); 
    } 
相關問題