Đôi khi ta cần dùng các server ftp, http trong các test chẳng hạn trong unit test. Dưới đây giới thiệu một số server như thế
FakeFtpServer
http://mockftpserver.sourceforge.net/fakeftpserver-getting-started.html
Tải jar về tại đây : https://mvnrepository.com/artifact/org.mockftpserver/MockFtpServer/2.7.1
MockFtpServer-2.7.1.jar
FakeFtpServer Rất dễ sử dụng để test như dưới đây
import org.mockftpserver.fake.FakeFtpServer; import org.mockftpserver.fake.UserAccount; import org.mockftpserver.fake.filesystem.DirectoryEntry; import org.mockftpserver.fake.filesystem.FileSystem; import org.mockftpserver.fake.filesystem.UnixFakeFileSystem; public class FakeFtpServerUtil { private static FakeFtpServer fakeFtpServer; private static final String DEFAULT_HOME_DIR = "/test"; private static final String DEFAULT_USER = "toan"; private static final String DEFAULT_PASSWORD = "matkhau"; private static final int DEFAULT_PORT = 21; public static void setUpFakeFtpServer() { fakeFtpServer = new FakeFtpServer(); fakeFtpServer.setServerControlPort(DEFAULT_PORT); FileSystem fileSystem = new UnixFakeFileSystem(); DirectoryEntry directoryEntry = new DirectoryEntry(DEFAULT_HOME_DIR); fileSystem.add(directoryEntry); fakeFtpServer.setFileSystem(fileSystem); UserAccount userAccount = new UserAccount(DEFAULT_USER, DEFAULT_PASSWORD, DEFAULT_HOME_DIR); fakeFtpServer.addUserAccount(userAccount); fakeFtpServer.start(); } public static void shutdownFtpServer() { if (fakeFtpServer != null) { fakeFtpServer.stop(); fakeFtpServer = null; } } }
FakeFtpServer tuy rất tiện để dùng trong test nhưng có nhược điểm là dùng bộ nhớ trong cho hệ thống tập tin của server ftp, nên nếu ta muốn dùng một thư mục vật lý cho hệ thống lưu tập tin của server ftp thì Apache FtpServer là một lựa chọn tốt như trình bày dưới đây.
Apache FtpServer
https://mina.apache.org/ftpserver-project/embedding_ftpserver.html
Tải về tại đây
https://mvnrepository.com/artifact/org.apache.ftpserver/ftpserver-core/1.1.1
https://mvnrepository.com/artifact/org.apache.ftpserver/ftplet-api/1.1.1
https://mvnrepository.com/artifact/org.apache.mina/mina-core/2.1.4
import java.util.ArrayList; import java.util.List; import org.apache.ftpserver.FtpServer; import org.apache.ftpserver.FtpServerFactory; import org.apache.ftpserver.filesystem.nativefs.NativeFileSystemFactory; import org.apache.ftpserver.ftplet.Authority; import org.apache.ftpserver.ftplet.FileSystemView; import org.apache.ftpserver.ftplet.FtpException; import org.apache.ftpserver.ftplet.User; import org.apache.ftpserver.ftplet.UserManager; import org.apache.ftpserver.listener.ListenerFactory; import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory; import org.apache.ftpserver.usermanager.impl.BaseUser; import org.apache.ftpserver.usermanager.impl.WritePermission; import org.apache.ftpserver.usermanager.impl.WriteRequest; public class EmbeddedFtpServer { private static FtpServer ftpServer; public static void setUpFtpServer() throws FtpException { FtpServerFactory serverFactory = new FtpServerFactory(); ListenerFactory factory = new ListenerFactory(); factory.setPort(21); // NativeFileSystemFactory fsf = new NativeFileSystemFactory() { // @Override // public FileSystemView createFileSystemView(final User user) throws FtpException { // final FileSystemView fsView = super.createFileSystemView(user); // fsView.changeWorkingDirectory("/test"); // return fsView; // } // }; // fsf.setCreateHome(true); // serverFactory.setFileSystem(fsf); // replace the default listener serverFactory.addListener("default", factory.createListener()); // create user final PropertiesUserManagerFactory userManagerFactory = new PropertiesUserManagerFactory(); final UserManager userManager = userManagerFactory.createUserManager(); BaseUser user = new BaseUser(); user.setName("toan"); user.setPassword("matkhau"); Listauthorities = new ArrayList (); WritePermission writePermission = new WritePermission(); writePermission.authorize(new WriteRequest()); authorities.add(writePermission); user.setAuthorities(authorities); userManager.save(user); serverFactory.setUserManager(userManager); ftpServer = serverFactory.createServer(); ftpServer.start(); } public static void shutdownFtpServer() { if (ftpServer != null) { ftpServer.stop(); ftpServer = null; } } }
Chú ý:
- Trong Ubuntu, cần trao quyền đọc/viết để test có thể thao tác trên thư mục mà server ftp dùng, chẳng hạn trao quyền cho mọi người dùng trên thư mục "/toan" như sau :
$ chmod -R 777 /toan
- Trong Ubuntu, người dùng không phải là root chỉ có quyền sử dụng các cổng lớn hơn 1024, nên trong các test nên đổi port cho server ftp lớn hơn giá trị này.
MockServer
https://www.mock-server.com/
import static org.mockserver.integration.ClientAndServer.startClientAndServer; import static org.mockserver.model.HttpRequest.request; import static org.mockserver.model.HttpResponse.response; import org.mockserver.integration.ClientAndServer; public class MockHttpServerUtil { private static ClientAndServer mockHttpServer; public static void setUpMockHttpServer() { mockHttpServer = startClientAndServer(5050); mockHttpServer .when(request() .withPath("/toan/rootTest/") ) .respond(response() .withStatusCode(200)); mockHttpServer .when(request() .withPath("/toan/rootTest/test.txt") ) .respond(response() .withStatusCode(200)); } public static void shutdownFtpServer() { if (mockHttpServer != null) { mockHttpServer.stop(); mockHttpServer = null; } } }
MockServer dùng rất tốt trong việc giả lập các trả lời cho các truy vấn http, tuy nhiên nó không cho phép ta thao tác trực tiếp với hệ thống thư mục vật lý như một server http thực thụ cho phép. Java Lightweight HTTP Server cho phép thực hiện điều này như trình bày bên dưới.
Java Lightweight HTTP Server
https://www.freeutils.net/source/jlhttp/
Tải về tại đây : https://mvnrepository.com/artifact/net.freeutils/jlhttp/2.5
import java.io.File; import java.io.IOException; import net.freeutils.httpserver.HTTPServer; import net.freeutils.httpserver.HTTPServer.ContextHandler; import net.freeutils.httpserver.HTTPServer.FileContextHandler; import net.freeutils.httpserver.HTTPServer.Request; import net.freeutils.httpserver.HTTPServer.Response; import net.freeutils.httpserver.HTTPServer.VirtualHost; public class EmbeddedHttpServer { private static HTTPServer httpServer; public static void setUpFakeHttpServer() throws IOException { httpServer = new HTTPServer(5020); VirtualHost host = httpServer.getVirtualHost(null); httpServer.addVirtualHost(host); host.addContext("/toan/rootTest/", new ContextHandler() { @Override public int serve(Request req, Response resp) throws IOException { resp.send(200, "OK"); return 0; } }); host.addContext("/toan/rootTest/test.txt", new ContextHandler() { @Override public int serve(Request req, Response resp) throws IOException { resp.send(200, "OK"); return 0; } }); File tempDirectory = new File("/toan/rootTest"); host.setAllowGeneratedIndex(true); host.addContext("/toan/rootTest", new FileContextHandler(tempDirectory)); httpServer.start(); } public static void shutdownFtpServer() { if (httpServer != null) { httpServer.stop(); httpServer = null; } } }