A Java library which provides a suite of hamcrest matchers for file/directory existance and permissions
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
.gitignore
.travis.yml
LICENSE
README.md
pom.xml

README.md

Hamcrest-Path Build Status

Licensed under BSD License.

What is Hamcrest-Path?

Hamcrest-Path is a Java Hamcrest extension library, which provides a suite of hamcrest-style matchers for file/directory existence and permissions.

Without the hamcrest-path library, testing whether a file is readable would need to be written as:

Path path = Paths.get("/path/to/some/file");
assertThat(Files.isReadable(path), is(true));

If the test failed, a non-informative error message would be generated stating an accurate but cryptic message:

Expected: is <true>, but: was <false>

With the hamcrest-path library, the test can be re-written as:

Path path = Paths.get("/path/to/some/file");
assertThat(path, is(readable()));

Not only is this more concise, but if the assertion fails, a more informative error message is generated:

Expected: is a readable file or directory, but: </path/to/some/file> does not exist. 

Downloads

You can obtain the hamcrest-path binaries from maven central, To include in your project:

A Maven project:

<dependency>
    <groupId>ca.seinesoftware</groupId>
    <artifactId>hamcrest-path</artifactId>
    <version>1.0.1</version>
    <scope>test</scope>
</dependency>

A project which uses ivy for dependency management:

<dependency org="ca.seinesoftware" name="hamcrest-path" rev="1.0.1"/>

Usage

The following code tests whether the user's home directory exists, and is both readable and writable:

import static ca.seinesoftware.hamcrest.path.PathMatcher.*;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;

import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Test;

public class HomeTest {
    @Test
    public void testHomeDirectory() {
        Path home = Paths.get(System.getProperty("user.home"));
        assertThat(home, exists());
        assertThat(home, is(aDirectory());
        assertThat(home, is(both(readable()).and(writable())));
    }
}

Other matchers include:

  • aRegularFile - Tests whether the file system entry is a regular file
  • aSymbolicLink - Tests whether the file system entry is a symbolic link
  • sameFile - Tests if two paths reference the same file system entry
  • executable - Test whether the user has execute permissions for the file system entry
  • hidden - Test whether the file system entry is hidden

Reporting Bugs/Issues

If you find an issue with Java Hamcrest, please report it via the GitHub issue tracker, after first checking that it hasn't been raised already.

Acknowledgements

Thanks to the developers at Java Hamcrest. Without their hardwork and core libraries there'd be nothing to be extend and we be stuck with old school, non-declarative, non-reusable, assertions.