java - Java 中的 FTP 单元测试(JSch 和 MockFtpServer)
问题描述
我正在使用 JCraft 的 JSch 库作为我程序的 FTP 客户端,它运行良好。但是我很难为单元测试创建一个模拟(MockFtpServer)。
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
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.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Properties;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SFTPManagerTest {
static FakeFtpServer fakeFtpServer;
@BeforeClass
public static void startFTP() throws Exception {
fakeFtpServer = new FakeFtpServer();
fakeFtpServer.setServerControlPort(9999);
FileSystem fileSystem = new UnixFakeFileSystem();
fileSystem.add(new FileEntry("/data/JFS_HCID_APPLICATION_STATUS_20190109.TXT", "<schema/>"));
fakeFtpServer.setFileSystem(fileSystem);
UserAccount userAccount = new UserAccount("user", "password", "/");
fakeFtpServer.addUserAccount(userAccount);
fakeFtpServer.start();
}
@Test
public void getFileListing() {
try {
JSch jsch = new JSch();
Session jschSession = jsch.getSession("user", "localhost", 9999);
jschSession.setPassword("password");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
jschSession.setConfig(config);
jschSession.connect();
Channel jschChannel = jschSession.openChannel("ftp");
jschChannel.connect();
ChannelSftp channel = (ChannelSftp) jschChannel;
Vector v = channel.ls("/data");
Assert.assertNotNull(v);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@AfterClass
public static void stopFTP() {
fakeFtpServer.stop();
}
}
依赖项:
- com.jcraft:jsch:0.1.55
- org.mockftpserver:MockFtpServer:2.7.1
输出:在执行测试方法期间它总是卡住/停止,没有完成。当卡住/停止时,我可以使用 WinSCP 或 FTP CLI 访问 MockFtpServer。从日志看来,来自 JSch 的命令似乎无法识别。
2019-02-18 17:54:15,996 INFO [Thread-0] org.mockftpserver.core.server.AbstractFtpServer: Connection accepted from host /127.0.0.1
2019-02-18 17:54:16,002 INFO [Thread-6] org.mockftpserver.core.command.AbstractTrackingCommandHandler: Sending reply [220 Service ready for new user. (MockFtpServer 2.7.1; see http://mockftpserver.sourceforge.net)]
2019-02-18 17:54:16,004 INFO [Thread-6] org.mockftpserver.core.session.DefaultSession: Received command: [SSH-2.0-JSCH-0.1.54]
2019-02-18 17:54:16,004 WARN [Thread-6] org.mockftpserver.core.command.UnsupportedCommandHandler: No CommandHandler is defined for command [SSH-2.0-JSCH-0.1.54]
2019-02-18 17:54:16,005 INFO [Thread-6] org.mockftpserver.core.command.AbstractTrackingCommandHandler: Sending reply [502 Command not implemented: SSH-2.0-JSCH-0.1.54.]
我哪里做错了?
解决方案
JSch 是一个 SSH/ SFTP库。您不能使用FTP服务器对其进行测试。您需要使用SFTP服务器。FTP 和 SFTP 是两个完全不相关且不同的协议。
推荐阅读
- r - 格式化ggplot2时间序列数据
- html - 如何在我的博客中将 url 视频显示为可以播放和暂停的视频?
- javascript - 如何使浏览器存储在 MacOS 上持久化?
- nativescript - 硬件按钮返回代码未正确响应
- swift - 如何在 UICollectionview 中禁用单元格销售
- css - 调试和发布 ASP.Net MVC 时的不同结果
- django - 如何在 django 中上传经过身份验证的用户的新图像时删除当前的个人资料图像
- python - 如何在 Python 中集成 HTML
- android - 部署目标选项不可见并且在 Android Studio 3.5 中出现“未找到设备”错误
- ios - 如何在使用 cordova-plugin-facebook4 的 cordova-ios 项目中将 FacebookAutoLogAppEventsEnabled 设置为 TRUE?