简体   繁体   English

用 Java 模拟 SFTP、FTP、FTPS、本地文件系统服务器

[英]Mocking SFTP, FTP, FTPS, Local File System Server in Java

I need to test FTP/FTPS/SFTP/Local File System protocols in Java.我需要在 Java 中测试 FTP/FTPS/SFTP/本地文件系统协议。

I need a mock server which can be used in any of these methods.我需要一个可用于任何这些方法的模拟服务器。

I could find a MockFTPServer.我可以找到一个 MockFTPServer。 According to my understanding, it can be used only for simple FTP protocol and not for FTPS/SFTP/Local File System.据我了解,它只能用于简单的FTP协议,不能用于FTPS/SFTP/Local File System。

Can anybody suggest if there is any mock implementation available for a server which supports FTP/FTPS/SFTP/Local File System in Java?任何人都可以建议是否有任何模拟实现可用于支持 Java 中的 FTP/FTPS/SFTP/本地文件系统的服务器?

Thanks,谢谢,

Vijay Bhore维杰·博尔

There is Fake SFTP server rule .假 SFTP 服务器规则 It is a rule/library for JUnit 4 that runs an SFTP server during test.它是 JUnit 4 的规则/库,在测试期间运行 SFTP 服务器。 It provides convenience method that help you to put files onto the server and get files from it.它提供了方便的方法,可帮助您将文件放到服务器上并从中获取文件。

Full Disclosure: I'm the author of Fake SFTP server rule.完全披露:我是假 SFTP 服务器规则的作者。

The questions is a bit older, but I post my answer anyway as it might help someone else as well.这些问题有点旧,但无论如何我都会发布我的答案,因为它也可能对其他人有所帮助。

I´ve written an article on how create a mock sftp server using Testcontainers and the atmoz/sftp Docker image, which is adaptable to your other requirements as well.我写了一篇关于如何使用 Testcontainers 和 atmoz/sftp Docker 镜像创建模拟 sftp 服务器的文章,它也适用于您的其他要求。

The full example can be seen here完整的例子可以在这里看到

You would define your TestContainer with SFTP like that你会像这样用 SFTP 定义你的 TestContainer

 private static final GenericContainer sftp = new GenericContainer(
            new ImageFromDockerfile()
                    .withDockerfileFromBuilder(builder ->
                            builder
                                    .from("atmoz/sftp:latest")
                                    .run("mkdir -p /home/" + USER + "/upload; chmod -R 007 /home/" + USER)
                                    .build()))
            //.withFileSystemBind(sftpHomeDirectory.getAbsolutePath(), "/home/" + USER + REMOTE_PATH, BindMode.READ_WRITE) //uncomment to mount host directory - not required / recommended
            .withExposedPorts(PORT)
            .withCommand(USER + ":" + PASSWORD + ":1001:::upload");

When you need an FTPS server or any other protocoll, you can pick another image like this for example and adapt the container configuration.当您需要 FTPS 服务器或任何其他协议时,您可以选择另一个像这样的图像并调整容器配置。 https://hub.docker.com/r/bozorgiyan/ftps-server https://hub.docker.com/r/bozorgiyan/ftps-server

As for the Local File System, I´m not sure if there is anything special required.至于本地文件系统,我不确定是否有任何特殊要求。 With JUnit 5 you can easily create a temporary directory like so使用 JUnit 5,您可以轻松地创建一个临时目录,如下所示

@TempDir
File mockFileSystemDirectory;

And you can create a util class that rewrites your paths to that directory like so:您可以创建一个 util 类来重写您到该目录的路径,如下所示:

public static File convertToFakeFileSystem(File yourFile, File fakeFileSystem) {
    return new File(fakeFileSystem.getAbsolutePath() + yourFile
            .getAbsolutePath()
            .replaceAll("C://", "/"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM