开发者

Commons-vfs mocking up a filesystem

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)
[ju开发者_Go百科nit]     at com.usengineeringsolutions.bridgewatch.vfs.CommonsVfsLearningSpikeTest.testCreationOfChildrenFiles(CommonsVfsLearningSpikeTest.java:27)
[junit]
[junit]


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜