Skip to content
This repository was archived by the owner on Jan 26, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions README → README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#HitchFS

##FakeFile
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.

* 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.
```java
// Example
StubFileSystem fs = new StubFileSystem() {
@Override
Expand All @@ -17,14 +17,16 @@ The primary benefits are:
};
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
* 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:


```java
// Reading Example
final String expected = "this is some text in memory.";
StubFileSystem fs = new StubFileSystem() {
Expand Down Expand Up @@ -53,7 +55,7 @@ The primary benefits are:
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.
Expand Down