Skip to content

Commit

Permalink
Worked on TODOs in SharedFileChannelTest
Browse files Browse the repository at this point in the history
* Now testing for correct exception thrown from write and read
  • Loading branch information
markuskreusch committed Jan 24, 2016
1 parent 406a997 commit e241c5b
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
@@ -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));
}

}
Expand Up @@ -14,11 +14,11 @@
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;
Expand All @@ -32,7 +32,7 @@ public void describeTo(Description description) {
.appendText(expectedType.getSimpleName()) //
.appendText(" with a ") //
.appendText(name) //
.appendText(" that is ") //
.appendText(" that ") //
.appendDescriptionOf(subMatcher);
}

Expand All @@ -46,7 +46,7 @@ protected boolean matchesSafely(T item, Description mismatchDescription) {
.appendText(expectedType.getSimpleName()) //
.appendText(" with a ") //
.appendText(name) //
.appendText(" that was ");
.appendText(" that ");
subMatcher.describeMismatch(propertyValue, mismatchDescription);
return false;
}
Expand Down
Expand Up @@ -2,8 +2,8 @@

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;
Expand Down Expand Up @@ -262,12 +262,13 @@ public void setUp() throws IOException {
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);
}
Expand Down Expand Up @@ -555,12 +556,13 @@ public void testWriteFullyWrapsIOExceptionFromWriteIntoUncheckedIOException() th
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);
}
Expand Down

0 comments on commit e241c5b

Please sign in to comment.