Embedded FTPServer for UnitTest on Windows: FileUpload 551 error

444 views Asked by At

I want to embed a simple FTPServer into my unit tests for testing FTP client functionality for file upload. I tried the Apache FTPServer (ftpserver-core-1.0.6.jar).

The file upload fails with

551 /C:/<correctDestinationPath>/myFile.txt: Error on output file.

Here my code for setting up the server:

    FtpServerFactory serverFactory = new FtpServerFactory();

    BaseUser user = new BaseUser();
    user.setName("anonymous");
    user.setPassword( System.getProperty( "user.name" ) + "@" + InetAddress.getLocalHost().getHostName() );
    user.setHomeDirectory( TEST_TARGET_DIR_LOCAL );
    List<Authority> authorities = new ArrayList<Authority>();
    authorities.add(new WritePermission());
    user.setAuthorities(authorities);
    UserManager userManager = serverFactory.getUserManager();
    userManager.save(user);

    myServer = serverFactory.createServer();
    myServer.start();

This works perfectly for connecting and logging in:

220 Service ready for new user.
USER *******
331 Guest login okay, send your complete e-mail address as password.
PASS *******
230 User logged in, proceed.
SYST
215 UNIX Type: Apache FtpServer
TYPE I
200 Command TYPE okay.

The productive code calls the FTPClient as follows:

myFtpClient.storeFile( "C:\<correctDestinationPath>\myFile.txt", anInputStream );

Notice that this is a correct path on my windows machine. But the FTPServer scrambles it to a unix-like path "/C:/correctDestinationPath/myFile.txt".

How can I configure the FTPServer that it works also for windows? The unit test itself should of course be OS neutral.

1

There are 1 answers

0
Heri On BEST ANSWER

I found the solution myself. Two possibilities:

  1. Setting the home directory of the user to an absolute path (e.g. ""C:\") and upload the file without path (only "myFtpClient.storeFile( "myFile.txt", anInputStream );" -> file is stored in user.home

  2. NOT setting the home directory at all and upload the file with the full qualified path.