mirror of
https://github.com/cryptomator/cryptomator.git
synced 2026-05-18 10:41:26 +00:00
Worked on TODOs in SharedFileChannelTest
* Now testing for correct exception thrown from write and read
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,11 +14,11 @@ import org.hamcrest.TypeSafeDiagnosingMatcher;
|
||||
public class PropertyMatcher<T, P> extends TypeSafeDiagnosingMatcher<T> {
|
||||
|
||||
private final Class<T> expectedType;
|
||||
private final Function<T, P> getter;
|
||||
private final Function<? super T, P> getter;
|
||||
private final String name;
|
||||
private final Matcher<? super P> subMatcher;
|
||||
|
||||
public PropertyMatcher(Class<T> type, Function<T, P> getter, String name, Matcher<? super P> subMatcher) {
|
||||
public PropertyMatcher(Class<T> type, Function<? super T, P> getter, String name, Matcher<? super P> subMatcher) {
|
||||
super(type);
|
||||
this.expectedType = type;
|
||||
this.getter = getter;
|
||||
@@ -32,7 +32,7 @@ public class PropertyMatcher<T, P> extends TypeSafeDiagnosingMatcher<T> {
|
||||
.appendText(expectedType.getSimpleName()) //
|
||||
.appendText(" with a ") //
|
||||
.appendText(name) //
|
||||
.appendText(" that is ") //
|
||||
.appendText(" that ") //
|
||||
.appendDescriptionOf(subMatcher);
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public class PropertyMatcher<T, P> extends TypeSafeDiagnosingMatcher<T> {
|
||||
.appendText(expectedType.getSimpleName()) //
|
||||
.appendText(" with a ") //
|
||||
.appendText(name) //
|
||||
.appendText(" that was ");
|
||||
.appendText(" that ");
|
||||
subMatcher.describeMismatch(propertyValue, mismatchDescription);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ package org.cryptomator.filesystem.nio;
|
||||
|
||||
import static java.lang.String.format;
|
||||
import static org.apache.commons.lang3.concurrent.ConcurrentUtils.constantFuture;
|
||||
import static org.cryptomator.common.test.matcher.ExceptionMatcher.ofType;
|
||||
import static org.cryptomator.filesystem.nio.SharedFileChannel.EOF;
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
@@ -262,12 +262,13 @@ public class SharedFileChannelTest {
|
||||
public void testReadFullyWrapsExceptionFromReadInUncheckedIOException() throws InterruptedException, ExecutionException {
|
||||
ByteBuffer buffer = ByteBuffer.allocate(0);
|
||||
ExecutionException exceptionFromRead = new ExecutionException(new IOException());
|
||||
@SuppressWarnings("unchecked")
|
||||
Future<Integer> result = mock(Future.class);
|
||||
when(channel.read(buffer, 0)).thenReturn(result);
|
||||
when(result.get()).thenThrow(exceptionFromRead);
|
||||
|
||||
thrown.expect(UncheckedIOException.class);
|
||||
thrown.expectCause(is(instanceOf(IOException.class))); // TODO check correct cause of cause
|
||||
thrown.expectCause(is(ofType(IOException.class).withCauseThat(is(exceptionFromRead))));
|
||||
|
||||
inTest.readFully(0, buffer);
|
||||
}
|
||||
@@ -555,12 +556,13 @@ public class SharedFileChannelTest {
|
||||
int position = 0;
|
||||
ByteBuffer buffer = ByteBuffer.allocate(count);
|
||||
ExecutionException exceptionFromWrite = new ExecutionException(new IOException());
|
||||
@SuppressWarnings("unchecked")
|
||||
Future<Integer> result = mock(Future.class);
|
||||
when(channel.write(buffer, position)).thenReturn(result);
|
||||
when(result.get()).thenThrow(exceptionFromWrite);
|
||||
|
||||
thrown.expect(UncheckedIOException.class);
|
||||
thrown.expectCause(is(instanceOf(IOException.class))); // TODO check correct cause of cause
|
||||
thrown.expectCause(is(ofType(IOException.class).withCauseThat(is(exceptionFromWrite))));
|
||||
|
||||
inTest.writeFully(position, buffer);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user