Home | History | Annotate | Download | only in io
      1 package org.hamcrest.io;
      2 
      3 import org.hamcrest.Description;
      4 import org.hamcrest.FeatureMatcher;
      5 import org.hamcrest.Matcher;
      6 import org.hamcrest.TypeSafeDiagnosingMatcher;
      7 
      8 import java.io.File;
      9 import java.io.IOException;
     10 
     11 import static org.hamcrest.core.IsEqual.equalTo;
     12 
     13 public final class FileMatchers {
     14 
     15     public static Matcher<File> anExistingDirectory() {
     16         return fileChecker(IS_DIRECTORY, "an existing directory", "is not a directory");
     17     }
     18 
     19     public static Matcher<File> anExistingFileOrDirectory() {
     20         return fileChecker(EXISTS, "an existing file or directory", "does not exist");
     21     }
     22 
     23     public static Matcher<File> anExistingFile() {
     24         return fileChecker(IS_FILE, "an existing File", "is not a file");
     25     }
     26 
     27     public static Matcher<File> aReadableFile() {
     28         return fileChecker(CAN_READ, "a readable File", "cannot be read");
     29     }
     30 
     31     public static Matcher<File> aWritableFile() {
     32         return fileChecker(CAN_WRITE, "a writable File", "cannot be written to");
     33     }
     34 
     35     public static Matcher<File> aFileWithSize(long size) {
     36         return aFileWithSize(equalTo(size));
     37     }
     38 
     39     public static Matcher<File> aFileWithSize(final Matcher<Long> expected) {
     40         return new FeatureMatcher<File, Long>(expected, "A file with size", "size") {
     41             @Override protected Long featureValueOf(File actual) { return actual.length(); }
     42         };
     43     }
     44 
     45     public static Matcher<File> aFileNamed(final Matcher<String> expected) {
     46         return new FeatureMatcher<File, String>(expected, "A file with name", "name") {
     47             @Override protected String featureValueOf(File actual) { return actual.getName(); }
     48         };
     49     }
     50 
     51     public static Matcher<File> aFileWithCanonicalPath(final Matcher<String> expected) {
     52         return new FeatureMatcher<File, String>(expected, "A file with canonical path", "path") {
     53             @Override protected String featureValueOf(File actual) {
     54                 try {
     55                     return actual.getCanonicalPath();
     56                 } catch (IOException e) {
     57                     return "Exception: " + e.getMessage();
     58                 }
     59             }
     60         };
     61     }
     62 
     63     public static Matcher<File> aFileWithAbsolutePath(final Matcher<String> expected) {
     64         return new FeatureMatcher<File, String>(expected, "A file with absolute path", "path") {
     65             @Override protected String featureValueOf(File actual) { return actual.getAbsolutePath(); }
     66         };
     67     }
     68 
     69     public static interface FileStatus {
     70         boolean check(File actual);
     71     }
     72 
     73     public static final FileStatus CAN_WRITE = new FileStatus() {
     74         @Override public boolean check(File actual) { return actual.canWrite(); }
     75     };
     76     public static final FileStatus CAN_READ = new FileStatus() {
     77         @Override public boolean check(File actual) { return actual.canRead(); }
     78     };
     79 
     80     public static final FileStatus IS_FILE = new FileStatus() {
     81         @Override public boolean check(File actual) { return actual.isFile(); }
     82     };
     83 
     84     public static final FileStatus IS_DIRECTORY = new FileStatus() {
     85         @Override public boolean check(File actual) { return actual.isDirectory(); }
     86     };
     87 
     88     public static final FileStatus EXISTS = new FileStatus() {
     89         @Override public boolean check(File actual) { return actual.exists(); }
     90     };
     91 
     92     private static Matcher<File> fileChecker(final FileStatus fileStatus, final String successDescription, final String failureDescription) {
     93         return new TypeSafeDiagnosingMatcher<File>() {
     94             public boolean matchesSafely(File actual, Description mismatchDescription) {
     95                 final boolean result = fileStatus.check(actual);
     96                 if (!result) {
     97                     mismatchDescription.appendText(failureDescription);
     98                 }
     99                 return result;
    100             }
    101 
    102             public void describeTo(Description description) {
    103                 description.appendText(successDescription);
    104             }
    105         };
    106     }
    107 }
    108