### Initialize and Start StubFtpServer in Java
Source: https://mockftpserver.org/stubftpserver-getting-started.html
Loads the Spring configuration file and starts the StubFtpServer instance.
```java
ApplicationContext context = new ClassPathXmlApplicationContext("stubftpserver-beans.xml");
stubFtpServer = (StubFtpServer) context.getBean("stubFtpServer");
stubFtpServer.start();
```
--------------------------------
### Configure and start FakeFtpServer
Source: https://mockftpserver.org/fakeftpserver-getting-started.html
Initializes a server instance with a Windows-based virtual filesystem and a single user account.
```java
FakeFtpServer fakeFtpServer = new FakeFtpServer();
fakeFtpServer.addUserAccount(new UserAccount("user", "password", "c:\\data"));
FileSystem fileSystem = new WindowsFakeFileSystem();
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);
fakeFtpServer.start();
```
--------------------------------
### Start StubFtpServer with default configuration
Source: https://mockftpserver.org/stubftpserver-getting-started.html
Initializes and starts the server using default settings, returning success codes and empty data.
```java
StubFtpServer stubFtpServer = new StubFtpServer();
stubFtpServer.start();
```
--------------------------------
### Initialize and Start FakeFtpServer in Java
Source: https://mockftpserver.org/fakeftpserver-getting-started.html
Loads the Spring application context from an XML file and starts the configured FakeFtpServer instance.
```java
ApplicationContext context = new ClassPathXmlApplicationContext("fakeftpserver-beans.xml");
FakeFtpServer = (FakeFtpServer) context.getBean("FakeFtpServer");
FakeFtpServer.start();
```
--------------------------------
### Configure Filesystem Permissions and Ownership
Source: https://mockftpserver.org/fakeftpserver-filesystems.html
Demonstrates setting permissions, owners, and groups for directories and files within a WindowsFakeFileSystem.
```java
final String USER1 = "joe";
final String USER2 = "mary";
final String GROUP = "dev";
final String CONTENTS = "abcdef 1234567890";
FileSystem fileSystem = new WindowsFakeFileSystem();
DirectoryEntry directoryEntry1 = new DirectoryEntry("c:\\");
directoryEntry1.setPermissions(new Permissions("rwxrwx---"));
directoryEntry1.setOwner(USER1);
directoryEntry1.setGroup(GROUP);
DirectoryEntry directoryEntry2 = new DirectoryEntry("c:\\data");
directoryEntry2.setPermissions(Permissions.ALL);
directoryEntry2.setOwner(USER1);
directoryEntry2.setGroup(GROUP);
FileEntry fileEntry1 = new FileEntry("c:\\data\\file1.txt", CONTENTS);
fileEntry1.setPermissionsFromString("rw-rw-rw-");
fileEntry1.setOwner(USER1);
fileEntry1.setGroup(GROUP);
FileEntry fileEntry2 = new FileEntry("c:\\data\\run.exe");
fileEntry2.setPermissionsFromString("rwxrwx---");
fileEntry2.setOwner(USER2);
fileEntry2.setGroup(GROUP);
fileSystem.add(directoryEntry1);
fileSystem.add(directoryEntry2);
fileSystem.add(fileEntry1);
fileSystem.add(fileEntry2);
FakeFtpServer fakeFtpServer = new FakeFtpServer();
fakeFtpServer.setFileSystem(fileSystem);
```
--------------------------------
### Implement FTP Client Class
Source: https://mockftpserver.org/stubftpserver-getting-started.html
A sample FTP client class using Apache Commons Net to retrieve files from a remote server.
```java
public class RemoteFile {
private String server;
public String readFile(String filename) throws SocketException, IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(server);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
boolean success = ftpClient.retrieveFile(filename, outputStream);
ftpClient.disconnect();
if (!success) {
throw new IOException("Retrieve file failed: " + filename);
}
return outputStream.toString();
}
public void setServer(String server) {
this.server = server;
}
// Other methods ...
}
```
--------------------------------
### Implement JUnit Test with FakeFtpServer
Source: https://mockftpserver.org/fakeftpserver-getting-started.html
Configures a FakeFtpServer instance with a Unix filesystem and user account within JUnit lifecycle methods.
```java
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockftpserver.fake.FakeFtpServer;
import org.mockftpserver.fake.UserAccount;
import org.mockftpserver.fake.filesystem.FileEntry;
import org.mockftpserver.fake.filesystem.FileSystem;
import org.mockftpserver.fake.filesystem.UnixFakeFileSystem;
import org.mockftpserver.stub.example.RemoteFile;
import org.mockftpserver.test.AbstractTestCase;
import org.mockftpserver.test.IntegrationTest;
import java.io.IOException;
/**
* Example test using FakeFtpServer, with programmatic configuration.
*/
class RemoteFileTest extends AbstractTestCase implements IntegrationTest {
private static final String HOME_DIR = "/";
private static final String FILE = "/dir/sample.txt";
private static final String CONTENTS = "abcdef 1234567890";
private RemoteFile remoteFile;
private FakeFtpServer fakeFtpServer;
@Test
void testReadFile() throws Exception {
String contents = remoteFile.readFile(FILE);
assertEquals(CONTENTS, contents);
}
@Test
void testReadFileThrowsException() {
assertThrows(IOException.class, () -> remoteFile.readFile("NoSuchFile.txt"));
}
@BeforeEach
void before() throws Exception {
fakeFtpServer = new FakeFtpServer();
fakeFtpServer.setServerControlPort(0); // use any free port
FileSystem fileSystem = new UnixFakeFileSystem();
fileSystem.add(new FileEntry(FILE, CONTENTS));
fakeFtpServer.setFileSystem(fileSystem);
UserAccount userAccount = new UserAccount(RemoteFile.USERNAME, RemoteFile.PASSWORD, HOME_DIR);
fakeFtpServer.addUserAccount(userAccount);
fakeFtpServer.start();
int port = fakeFtpServer.getServerControlPort();
remoteFile = new RemoteFile();
remoteFile.setServer("localhost");
remoteFile.setPort(port);
}
@AfterEach
void after() throws Exception {
fakeFtpServer.stop();
}
}
```
--------------------------------
### Configure FakeFtpServer with Spring XML
Source: https://mockftpserver.org/fakeftpserver-getting-started.html
Defines a FakeFtpServer bean with a Windows file system, user accounts, and specific file permissions.
```xml
```
--------------------------------
### Configure FakeFtpServer with Spring XML
Source: https://mockftpserver.org/fakeftpserver-getting-started.html
Defines a FakeFtpServer bean with a specific control port, user account, and a Unix-style file system containing a sample file.
```xml
```
--------------------------------
### Retrieve Filesystem Entries
Source: https://mockftpserver.org/fakeftpserver-filesystems.html
Shows how to retrieve directory and file entries from the filesystem and create an input stream for file content reading.
```java
DirectoryEntry dirEntry = (DirectoryEntry)fileSystem.getEntry("c:/data");
FileEntry fileEntry = (FileEntry)fileSystem.getEntry("c:/data/file1.txt");
FileEntry newFileEntry = (FileEntry)fileSystem.getEntry("c:/data/new.txt");
InputStream inputStream = newFileEntry.createInputStream();
// read the file contents using inputStream
```
--------------------------------
### Configure a New Command
Source: https://mockftpserver.org/stubftpserver-getting-started.html
Add support for unsupported FTP commands using the StaticReplyCommandHandler.
```java
final String FEAT_TEXT = "Extensions supported:\n" +
"MLST size*;create;modify*;perm;media-type\n" +
"SIZE\n" +
"COMPRESSION\n" +
"END";
StaticReplyCommandHandler featCommandHandler = new StaticReplyCommandHandler(211, FEAT_TEXT);
stubFtpServer.setCommandHandler("FEAT", featCommandHandler);
```
--------------------------------
### Configure a stub command handler
Source: https://mockftpserver.org/fakeftpserver-getting-started.html
Uses a stub CommandHandler from the org.mockftpserver.stub.command package to override default command behavior.
```java
import org.mockftpserver.stub.command.CwdCommandHandler
FakeFtpServer ftpServer = new FakeFtpServer()
// ... set up files, directories and user accounts as usual
final int REPLY_CODE = 502;
CwdCommandHandler cwdCommandHandler = new CwdCommandHandler();
cwdCommandHandler.setReplyCode(REPLY_CODE);
ftpServer.setCommandHandler(CommandNames.CWD, cwdCommandHandler);
// ...
ftpServer.start()
```
--------------------------------
### Test FTP Client with StubFtpServer
Source: https://mockftpserver.org/stubftpserver-getting-started.html
A JUnit test class demonstrating how to configure StubFtpServer to simulate successful file retrieval and error responses.
```java
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockftpserver.core.command.CommandNames;
import org.mockftpserver.core.command.InvocationRecord;
import org.mockftpserver.stub.StubFtpServer;
import org.mockftpserver.stub.command.RetrCommandHandler;
import org.mockftpserver.test.AbstractTestCase;
import org.mockftpserver.test.IntegrationTest;
import java.io.IOException;
/**
* Example test using StubFtpServer, with programmatic configuration.
*/
class RemoteFileTest extends AbstractTestCase implements IntegrationTest {
private static final int PORT = 9981;
private static final String FILENAME = "dir/sample.txt";
private RemoteFile remoteFile;
private StubFtpServer stubFtpServer;
@Test
void testReadFile() throws Exception {
final String CONTENTS = "abcdef 1234567890";
// Replace the default RETR CommandHandler; customize returned file contents
RetrCommandHandler retrCommandHandler = new RetrCommandHandler();
retrCommandHandler.setFileContents(CONTENTS);
stubFtpServer.setCommandHandler(CommandNames.RETR, retrCommandHandler);
stubFtpServer.start();
String contents = remoteFile.readFile(FILENAME);
// Verify returned file contents
assertEquals(CONTENTS, contents);
// Verify the submitted filename
InvocationRecord invocationRecord = retrCommandHandler.getInvocation(0);
String filename = invocationRecord.getString(RetrCommandHandler.PATHNAME_KEY);
assertEquals(FILENAME, filename);
}
@Test
void testReadFileThrowsException() {
// Replace the default RETR CommandHandler; return failure reply code
RetrCommandHandler retrCommandHandler = new RetrCommandHandler();
retrCommandHandler.setFinalReplyCode(550);
stubFtpServer.setCommandHandler(CommandNames.RETR, retrCommandHandler);
stubFtpServer.start();
try {
remoteFile.readFile(FILENAME);
fail("Expected IOException");
}
catch (IOException expected) {
// Expected this
}
}
@BeforeEach
void setUp() throws Exception {
remoteFile = new RemoteFile();
remoteFile.setServer("localhost");
remoteFile.setPort(PORT);
stubFtpServer = new StubFtpServer();
stubFtpServer.setServerControlPort(PORT);
}
@AfterEach
void tearDown() throws Exception {
stubFtpServer.stop();
}
}
```
--------------------------------
### Configure StubFtpServer with Spring XML
Source: https://mockftpserver.org/stubftpserver-getting-started.html
Defines a StubFtpServer bean with custom command handlers for LIST, PWD, DELE, and RETR commands.
```xml
```
--------------------------------
### Configure a custom command with StaticReplyCommandHandler
Source: https://mockftpserver.org/fakeftpserver-getting-started.html
Uses StaticReplyCommandHandler to define a response for a specific FTP command, such as FEAT.
```java
import org.mockftpserver.core.command.StaticReplyCommandHandler
FakeFtpServer ftpServer = new FakeFtpServer()
// ... set up files, directories and user accounts as usual
StaticReplyCommandHandler featCommandHandler = new StaticReplyCommandHandler(211, "No Features");
ftpServer.setCommandHandler("FEAT", featCommandHandler);
// ...
ftpServer.start()
```
--------------------------------
### Add MockFtpServer Dependency to Maven POM
Source: https://mockftpserver.org/
Include this dependency in your project's pom.xml file to use MockFtpServer for testing.
```xml
org.mockftpserver
MockFtpServer
3.2.0
test
```
--------------------------------
### Retrieve a CommandHandler
Source: https://mockftpserver.org/stubftpserver-getting-started.html
Access an existing CommandHandler instance by its FTP command name.
```java
PwdCommandHandler pwdCommandHandler = (PwdCommandHandler) stubFtpServer.getCommandHandler("PWD");
```
--------------------------------
### Replace a CommandHandler
Source: https://mockftpserver.org/stubftpserver-getting-started.html
Override default command behavior by setting a new CommandHandler instance for a specific command.
```java
PwdCommandHandler pwdCommandHandler = new PwdCommandHandler();
pwdCommandHandler.setDirectory("some/dir");
stubFtpServer.setCommandHandler("PWD", pwdCommandHandler);
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.