java - Commons-vfs 模拟一个文件系统

标签 java apache-commons-vfs

我正在尝试使用 commons-vfs 作为文件系统包装器,以便更轻松地对一些需要接触文件系统的代码进行单元测试。现在我只是熟悉 API。我想做的是创建一个虚拟文件系统,并添加几个文件(一个文件夹,然后是该文件夹中的一个文件到根目录)。

这是我编写的用于测试 API 的测试类:

public class CommonsVfsLearningSpikeTest extends Base {
FileSystemManager fsManager;
FileObject rootVFS;

@Before public void createFixture() throws Exception{
    this.fsManager = VFS.getManager();
    this.rootVFS = fsManager.createVirtualFileSystem("rootVfs");
}

@Test public void testCreationOfDefaultFileSystem() throws Exception {
    assertNotNull(fsManager);
}

@Test public void testCreationOfVFS() throws Exception {
    //root file has an empty base name
    assertEquals("", rootVFS.getName().getBaseName());
}

@Test public void testCreationOfChildrenFiles() throws Exception {
    FileObject childFolder = rootVFS.resolveFile("childFolder");
    childFolder.createFolder();
    assertNotNull(childFolder );

    FileObject childFile = rootVFS.resolveFile("childFolder/childFile");
    childFile.createFile();
    assertNotNull(childFile);

}   

目前我收到以下错误:

[junit] Testcase: testCreationOfChildrenFiles(com.usengineeringsolutions.bridgewatch.vfs.CommonsVfsLearningSpikeTest):      Caused an ERROR
[junit] Incorrect file system URI "file:///" in name "file:///rootVfs/childFolder", was expecting "/rootVfs/".
[junit] org.apache.commons.vfs.FileSystemException: Incorrect file system URI "file:///" in name "file:///rootVfs/childFolder", was expecting "/rootVfs/".
[junit]     at org.apache.commons.vfs.provider.AbstractFileSystem.resolveFile(AbstractFileSystem.java:274)
[junit]     at org.apache.commons.vfs.provider.AbstractFileSystem.resolveFile(AbstractFileSystem.java:267)
[junit]     at org.apache.commons.vfs.provider.AbstractFileObject.resolveFile(AbstractFileObject.java:670)
[junit]     at com.usengineeringsolutions.bridgewatch.vfs.CommonsVfsLearningSpikeTest.testCreationOfChildrenFiles(CommonsVfsLearningSpikeTest.java:27)
[junit]
[junit]

最佳答案

我刚刚开始使用 vfs,在对依赖于 vfs 的组件进行单元测试时,我采用了使用“ram://”文件系统的方法,而不是尝试完全模拟 VFS 接口(interface)。

这意味着单元测试不再是“纯粹的”,因为测试行为现在不仅仅依赖于 SUT(被测对象),但这是一个妥协,我很乐意接受它,以便让它正常工作.

关于java - Commons-vfs 模拟一个文件系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6916787/

相关文章:

java - 如何将这个匿名方法写成一行呢?

java - 类声明,通过它可以与任何类型的数据进行交互

java - 如何使用 commons-VFS 在 SFTP 期间跳过密码提示

java - 如何在受用户名和密码保护的远程位置创建并写入 CSV 文件?

java - Web 脚本中的 Alfresco 节点锁定

java - 无法执行目标 org.springframework.boot :spring-boot-maven-plugin:1. 5.8.RELEASE:run

java - 为什么我们需要 struts 中的 global-forwards 和 global-exceptions?

java - Apache Commonfs VFS 避免增加临时目录

java - Windows机器上无法通过VFS下载文件?

java - 如何监控 Apache Commons VFS 中文件传输的进度?