Example test for FakeFtpServer
git-svn-id: svn://svn.code.sf.net/p/mockftpserver/code@151 531de8e6-9941-0410-b38b-9a92acbe0330
diff --git a/MockFtpServer/src/test/java/org/mockftpserver/fake/example/RemoteFileTest.java b/MockFtpServer/src/test/java/org/mockftpserver/fake/example/RemoteFileTest.java
new file mode 100644
index 0000000..3f06450
--- /dev/null
+++ b/MockFtpServer/src/test/java/org/mockftpserver/fake/example/RemoteFileTest.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2008 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.mockftpserver.fake.example;
+
+import org.mockftpserver.fake.filesystem.FileEntry;
+import org.mockftpserver.fake.filesystem.FileSystem;
+import org.mockftpserver.fake.filesystem.UnixFakeFileSystem;
+import org.mockftpserver.fake.server.FakeFtpServer;
+import org.mockftpserver.fake.user.UserAccount;
+import org.mockftpserver.stub.example.RemoteFile;
+import org.mockftpserver.test.AbstractTest;
+import org.mockftpserver.test.IntegrationTest;
+
+import java.io.IOException;
+
+/**
+ * Example test using FakeFtpServer, with programmatic configuration.
+ */
+public class RemoteFileTest extends AbstractTest implements IntegrationTest {
+
+ private static final int PORT = 9981;
+ 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;
+
+ public void testReadFile() throws Exception {
+ String contents = remoteFile.readFile(FILE);
+ assertEquals("contents", CONTENTS, contents);
+ }
+
+ public void testReadFileThrowsException() {
+ try {
+ remoteFile.readFile("NoSuchFile.txt");
+ fail("Expected IOException");
+ }
+ catch (IOException expected) {
+ // Expected this
+ }
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ remoteFile = new RemoteFile();
+ remoteFile.setServer("localhost");
+ remoteFile.setPort(PORT);
+ fakeFtpServer = new FakeFtpServer();
+ fakeFtpServer.setServerControlPort(PORT);
+
+ FileSystem fileSystem = new UnixFakeFileSystem();
+ fileSystem.add(new FileEntry(FILE, CONTENTS));
+ fakeFtpServer.setFileSystem(fileSystem);
+
+ UserAccount userAccount = new UserAccount();
+ userAccount.setUsername(RemoteFile.USERNAME);
+ userAccount.setPassword(RemoteFile.PASSWORD);
+ userAccount.setHomeDirectory(HOME_DIR);
+ fakeFtpServer.setUserAccounts(list(userAccount));
+
+ fakeFtpServer.start();
+ }
+
+ /**
+ * @see org.mockftpserver.test.AbstractTest#tearDown()
+ */
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ fakeFtpServer.stop();
+ }
+
+}
\ No newline at end of file
diff --git a/MockFtpServer/src/test/java/org/mockftpserver/stub/example/RemoteFile.java b/MockFtpServer/src/test/java/org/mockftpserver/stub/example/RemoteFile.java
index 0b8f85f..833cf56 100644
--- a/MockFtpServer/src/test/java/org/mockftpserver/stub/example/RemoteFile.java
+++ b/MockFtpServer/src/test/java/org/mockftpserver/stub/example/RemoteFile.java
@@ -15,28 +15,30 @@
*/
package org.mockftpserver.stub.example;
+import org.apache.commons.net.ftp.FTPClient;
+
import java.io.ByteArrayOutputStream;
import java.io.IOException;
-import java.net.SocketException;
-
-import org.apache.commons.net.ftp.FTPClient;
/**
* Simple FTP client code example.
- *
- * @version $Revision$ - $Date$
*
* @author Chris Mair
+ * @version $Revision$ - $Date$
*/
public class RemoteFile {
+ public static final String USERNAME = "user";
+ public static final String PASSWORD = "password";
+
private String server;
private int port;
- public String readFile(String filename) throws SocketException, IOException {
+ public String readFile(String filename) throws IOException {
FTPClient ftpClient = new FTPClient();
ftpClient.connect(server, port);
+ ftpClient.login(USERNAME, PASSWORD);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
boolean success = ftpClient.retrieveFile(filename, outputStream);
@@ -47,9 +49,10 @@
}
return outputStream.toString();
}
-
+
/**
* Set the hostname of the FTP server
+ *
* @param server - the hostname of the FTP server
*/
public void setServer(String server) {
@@ -58,11 +61,12 @@
/**
* Set the port number for the FTP server
+ *
* @param port - the port number
*/
public void setPort(int port) {
this.port = port;
}
-
+
// Other methods ...
}