Created
August 20, 2013 00:11
-
-
Save sakamotodesu/6275662 to your computer and use it in GitHub Desktop.
hamcrest customized matcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FileExist extends TypeSafeMatcher<File> { | |
private final boolean isEexpectedResult; | |
private File inspected; | |
public FileExist(boolean exceptedResult) { | |
this.isEexpectedResult = exceptedResult; | |
} | |
@Override | |
public void describeTo(Description description) { | |
description.appendText(inspected.getAbsolutePath()); | |
if (isEexpectedResult) { | |
description.appendText(" exist"); | |
} else { | |
description.appendText(" does not exist"); | |
} | |
} | |
@Override | |
protected void describeMismatchSafely(File item, Description mismatchDescription) { | |
mismatchDescription.appendText(inspected.getName()); | |
if (isEexpectedResult) { | |
mismatchDescription.appendText(" does not exist."); | |
} else { | |
mismatchDescription.appendText(" exist."); | |
} | |
} | |
@Override | |
protected boolean matchesSafely(File item) { | |
this.inspected = item; | |
return item.exists() == isEexpectedResult; | |
} | |
@Factory | |
public static <T> Matcher<File> exist() { | |
return new FileExist(true); | |
} | |
@Factory | |
public static <T> Matcher<File> NotExist() { | |
return new FileExist(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment