wealthfront / hitchfs

A facility for safely testing filesystem operations in Java

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The purpose of FakeFile is to provide an easy way to test file system 
operations in Java (particularly those in java.io.File) as safely as possible.

The primary benefits are:

  - Dangerous file operations, such as delete, are limited to the behavior
    that you define in your tests.  This helps prevent bugs from performing 
    accidental damage, and is especially useful when working with someone else's
    code or library.
    
    // Example
    StubFileSystem fs = new StubFileSystem() {
      @Override
      public boolean delete(FakeFile fake) {
        throw new RuntimeException("deleting me is really bad!");
      }
    };
    FakeFile fake = fs.file("something-critical.txt");
    fake.delete(); // causes the RuntimeException to be thrown.  
  
  - I/O operations are costly, and wasteful in tests. The should be avoided as 
    much as possible, but at the same time, reading/writing files is likely one 
    of the most critical operations for your application to do correctly, and requires
    significant testing.  FakeFile allows you to test while redirecting to in-memory
    objects such as ByteArrayInputStream or writing to a MessageDigestOutputStream and
    just verifying an md5 sum in your test:  

    // Reading Example
    final String expected = "this is some text in memory.";
    StubFileSystem fs = new StubFileSystem() {
      @Override
      public InputStream input(File file) throws FileNotFoundException {
        return new ByteArrayInputStream(expected.getBytes());
      }
    };
    FakeFile file = fs.file("fakedir/doesntexist.txt");
    Reader reader = fs.reader(file);
    char[] chars = new char[30];
    int len = reader.read(chars);
    reader.close();
    assertEquals(expected, new String(chars, 0, len));
    
    // Writing Example
    final MessageDigestOutputStream md5 = MessageDigestOutputStream.md5();
    StubFileSystem fs = new StubFileSystem() {
      @Override
      public OutputStream output(File file) throws FileNotFoundException {
        return md5;
      }
    };
    FakeFile file = fs.file("fakedir/fakefile2.txt");
    Writer writer = fs.writer(file);
    writer.write("fake file with text.");
    writer.close();
    assertEquals("9d2110c9a94894f10cfee35afaf8ceb2", md5.getDigestAsHex());
    

Then, when you're in your real application, just use DefaultFileSystem to get
the real Java implementations of File, File Streams, and File Reader/Writers.
    
FakeFile is still in early stages, and I have some ideas for where to take it next, but I'd 
love to hear feedback and other ideasg.  Thanks!  -John

About

A facility for safely testing filesystem operations in Java