FakeFtpServer is the top-level class for a "fake" implementation of an FTP Server,
suitable for testing FTP client code or standing in for a live FTP server.
FakeFtpServer provides a high-level abstraction for an FTP Server and is suitable
for most testing and simulation scenarios. You define a filesystem (internal, in-memory) containing
an arbitrary set of files and directories. These files and directories can (optionally) have
associated access permissions. You also configure a set of one or more user accounts that
control which users can login to the FTP server, and their home (default) directories. The
user account is also used when assigning file and directory ownership for new files.
FakeFtpServer processes FTP client requests and responds with reply codes and
reply messages consistent with its configuration and the contents of its internal filesystem,
including file and directory permissions, if they have been configured.
FakeFtpServer can be fully configured programmatically or within the
Spring Framework or other dependency-injection container.
In general the steps for setting up and starting the FakeFtpServer are:
- Create a new FakeFtpServer instance, and optionally set the server control port.
- Create and configure a FileSystem, and attach to the FakeFtpServer instance.
- Create and configure one or more UserAccount objects and attach to the FakeFtpServer instance.
- Start the FakeFtpServer instance.
Example Code
FakeFtpServer fakeFtpServer = new FakeFtpServer();
FileSystem fileSystem = new WindowsFakeFileSystem();
fileSystem.add(new DirectoryEntry("c:\\"));
fileSystem.add(new DirectoryEntry("c:\\data"));
fileSystem.add(new FileEntry("c:\\data\\file1.txt", "abcdef 1234567890"));
fileSystem.add(new FileEntry("c:\\data\\run.exe"));
fakeFtpServer.setFileSystem(fileSystem);
// Create UserAccount with username, password, home-directory
UserAccount userAccount = new UserAccount("joe", "joe123", "c:\\");
fakeFtpServer.addUserAccounts(userAccount);
fakeFtpServer.start();
Example Code with Permissions
You can optionally set the permissions and owner/group for each file and directory, as in the following example.
FileSystem fileSystem = new UnixFakeFileSystem();
DirectoryEntry directoryEntry1 = new DirectoryEntry("/");
directoryEntry1.setPermissions(new Permissions("rwxrwx---"));
directoryEntry1.setOwner("joe");
directoryEntry1.setGroup("dev");
DirectoryEntry directoryEntry2 = new DirectoryEntry("/data");
directoryEntry2.setPermissions(Permissions.ALL);
directoryEntry2.setOwner("joe");
directoryEntry2.setGroup("dev");
FileEntry fileEntry1 = new FileEntry("/data/file1.txt", "abcdef 1234567890");
fileEntry1.setPermissionsFromString("rw-rw-rw-");
fileEntry1.setOwner("joe");
fileEntry1.setGroup("dev");
FileEntry fileEntry2 = new FileEntry("/data/run.exe");
fileEntry2.setPermissionsFromString("rwxrwx---");
fileEntry2.setOwner("mary");
fileEntry2.setGroup("dev");
fileSystem.add(directoryEntry1);
fileSystem.add(directoryEntry2);
fileSystem.add(fileEntry1);
fileSystem.add(fileEntry2);
FakeFtpServer fakeFtpServer = new FakeFtpServer();
fakeFtpServer.setFileSystem(fileSystem);
// Create UserAccount with username, password, home-directory
UserAccount userAccount = new UserAccount("joe", "joe123", "/");
fakeFtpServer.addUserAccounts(userAccount);
fakeFtpServer.start();
FTP Server Control Port
By default,
FakeFtpServer binds to the server control port of 21. You can use a different server control
port by setting the
serverControlPort
property. If you specify a value of
0
,
then a free port number will be chosen automatically; call
getServerControlPort()
AFTER
start()
has been called to determine the actual port number being used. Using a non-default
port number is usually necessary when running on Unix or some other system where that port number is
already in use or cannot be bound from a user process.
Other Configuration
The
systemName
property specifies the value returned by the
SYST
command. Note that this is typically used by an FTP client to determine how to parse
system-dependent reply text, such as directory listings. This value defaults to
"WINDOWS"
.
The helpText
property specifies a Map of help text replies sent by the
HELP
command. The keys in that Map correspond to the command names passed as
parameters to the HELP
command. An entry with the key of an empty string ("") indicates the
text used as the default help text when no command name parameter is specified for the HELP
command.
FTP Command Reply Text ResourceBundle
The default text asociated with each FTP command reply code is contained within the
"ReplyText.properties" ResourceBundle file. You can customize these messages by providing a
locale-specific ResourceBundle file on the CLASSPATH, according to the normal lookup rules of
the ResourceBundle class (e.g., "ReplyText_de.properties"). Alternatively, you can
completely replace the ResourceBundle file by calling the calling the
#setReplyTextBaseName(String) method.