Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Worked on TODOs in SharedFileChannelTest
* Now testing for correct exception thrown from write and read
- Loading branch information
1 parent
406a997
commit e241c5b
Showing
3 changed files
with
57 additions
and
7 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
main/commons-test/src/main/java/org/cryptomator/common/test/matcher/ExceptionMatcher.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.cryptomator.common.test.matcher; | ||
|
||
import java.util.Optional; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.Matcher; | ||
import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
|
||
public class ExceptionMatcher<T extends Throwable> extends TypeSafeDiagnosingMatcher<T> { | ||
|
||
public static <T extends Throwable> ExceptionMatcher<T> ofType(Class<T> exceptionType) { | ||
return new ExceptionMatcher<>(exceptionType); | ||
} | ||
|
||
private final Class<T> exceptionType; | ||
private final Optional<Matcher<T>> subMatcher; | ||
|
||
private ExceptionMatcher(Class<T> exceptionType) { | ||
super(exceptionType); | ||
this.exceptionType = exceptionType; | ||
this.subMatcher = Optional.empty(); | ||
} | ||
|
||
private ExceptionMatcher(Class<T> exceptionType, Matcher<T> subMatcher) { | ||
super(exceptionType); | ||
this.exceptionType = exceptionType; | ||
this.subMatcher = Optional.of(subMatcher); | ||
} | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
subMatcher.ifPresent(description::appendDescriptionOf); | ||
} | ||
|
||
@Override | ||
protected boolean matchesSafely(T item, Description mismatchDescription) { | ||
if (subMatcher.map(matcher -> !matcher.matches(item)).orElse(false)) { | ||
subMatcher.get().describeMismatch(item, mismatchDescription); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
public Matcher<T> withCauseThat(Matcher<? super Throwable> matcher) { | ||
return new ExceptionMatcher<T>(exceptionType, new PropertyMatcher<>(exceptionType, Throwable::getCause, "cause", 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
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