Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8378411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:55:19+00:00 2026-06-09T15:55:19+00:00

I am having a bit of trouble working out how to use Apache Mina.

  • 0

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<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 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:55:20+00:00Added an answer on June 9, 2026 at 3:55 pm

    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();
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a bit of trouble working out when an image has been
I'm having a bit of trouble working something out with regards to javascript and
I'm currently working with PowerPoint in VB.NET and having a bit of trouble getting
I'm working on an ASP.NET WebForms CMS application, and having a bit of trouble
OK, I'm working on a spellcheck program in C. The bit I'm having trouble
Having a bit of trouble using the List.Find with a custom predicate i have
I'm having a bit of trouble with a C++ program I'm working on. I've
I'm having a bit of trouble getting a SQL query to work. Some background
Having a bit of trouble getting jQuery and Prototype working together. The error console
I'm having a bit of trouble figuring out how to reduce memory usage and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.