Commons-vfs 模拟文件系统

发布于 2024-11-27 07:58:37 字数 1904 浏览 1 评论 0原文

我尝试使用 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] 测试用例:testCreationOfChildrenFiles(com.usengineeringsolutions.bridgewatch.vfs.CommonsVfsLearningSpikeTest):导致错误
[junit] 名称“file:///rootVfs/childFolder”中的文件系统 URI“file:///”不正确,应为“/rootVfs/”。
[junit] org.apache.commons.vfs.FileSystemException:名称“file:///rootVfs/childFolder”中的文件系统 URI“file:///”不正确,需要“/rootVfs/”。
[junit] 在 org.apache.commons.vfs.provider.AbstractFileSystem.resolveFile(AbstractFileSystem.java:274)
[junit] 在 org.apache.commons.vfs.provider.AbstractFileSystem.resolveFile(AbstractFileSystem.java:267)
[junit] 在 org.apache.commons.vfs.provider.AbstractFileObject.resolveFile(AbstractFileObject.java:670)
[junit] 在 com.usengineeringsolutions.bridgewatch.vfs.CommonsVfsLearningSpikeTest.testCreationOfChildrenFiles(CommonsVfsLearningSpikeTest.java:27)
[朱尼特]
[朱尼特]

I'm trying to use commons-vfs as a filesystem wrapper in order to more easily unit test some code that needs to touch the filesystem. Right now I'm just getting familiar with the API. What I would like to do is create a virtual filesystem, and add a couple of files (a folder and then a file in that folder to the root).

Here's a test class I've written to testdrive the 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);

}   

}

Currently I'm getting the following error:

[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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原野 2024-12-04 07:58:37

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

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

I've just started using vfs, and in unit tests for components that depend on vfs, I took the approach of using the "ram://" filesystem rather than trying to fully mock the VFS interfaces.

It means the unit tests are no longer "pure" since the test behavior is now dependent upon more than just the SUT (subject under test), but that was a compromise I was happy to live with for expediency in getting it working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文