From e241c5ba05e02abc1001be103d47002680b5e336 Mon Sep 17 00:00:00 2001 From: Markus Kreusch Date: Sun, 24 Jan 2016 01:12:55 +0100 Subject: [PATCH] Worked on TODOs in SharedFileChannelTest * Now testing for correct exception thrown from write and read --- .../common/test/matcher/ExceptionMatcher.java | 48 +++++++++++++++++++ .../common/test/matcher/PropertyMatcher.java | 8 ++-- .../filesystem/nio/SharedFileChannelTest.java | 8 ++-- 3 files changed, 57 insertions(+), 7 deletions(-) create mode 100644 main/commons-test/src/main/java/org/cryptomator/common/test/matcher/ExceptionMatcher.java diff --git a/main/commons-test/src/main/java/org/cryptomator/common/test/matcher/ExceptionMatcher.java b/main/commons-test/src/main/java/org/cryptomator/common/test/matcher/ExceptionMatcher.java new file mode 100644 index 000000000..ad5f1cffb --- /dev/null +++ b/main/commons-test/src/main/java/org/cryptomator/common/test/matcher/ExceptionMatcher.java @@ -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 extends TypeSafeDiagnosingMatcher { + + public static ExceptionMatcher ofType(Class exceptionType) { + return new ExceptionMatcher<>(exceptionType); + } + + private final Class exceptionType; + private final Optional> subMatcher; + + private ExceptionMatcher(Class exceptionType) { + super(exceptionType); + this.exceptionType = exceptionType; + this.subMatcher = Optional.empty(); + } + + private ExceptionMatcher(Class exceptionType, Matcher 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 withCauseThat(Matcher matcher) { + return new ExceptionMatcher(exceptionType, new PropertyMatcher<>(exceptionType, Throwable::getCause, "cause", matcher)); + } + +} diff --git a/main/commons-test/src/main/java/org/cryptomator/common/test/matcher/PropertyMatcher.java b/main/commons-test/src/main/java/org/cryptomator/common/test/matcher/PropertyMatcher.java index 42a44faf2..85108c984 100644 --- a/main/commons-test/src/main/java/org/cryptomator/common/test/matcher/PropertyMatcher.java +++ b/main/commons-test/src/main/java/org/cryptomator/common/test/matcher/PropertyMatcher.java @@ -14,11 +14,11 @@ import org.hamcrest.TypeSafeDiagnosingMatcher; public class PropertyMatcher extends TypeSafeDiagnosingMatcher { private final Class expectedType; - private final Function getter; + private final Function getter; private final String name; private final Matcher subMatcher; - public PropertyMatcher(Class type, Function getter, String name, Matcher subMatcher) { + public PropertyMatcher(Class type, Function getter, String name, Matcher subMatcher) { super(type); this.expectedType = type; this.getter = getter; @@ -32,7 +32,7 @@ public class PropertyMatcher extends TypeSafeDiagnosingMatcher { .appendText(expectedType.getSimpleName()) // .appendText(" with a ") // .appendText(name) // - .appendText(" that is ") // + .appendText(" that ") // .appendDescriptionOf(subMatcher); } @@ -46,7 +46,7 @@ public class PropertyMatcher extends TypeSafeDiagnosingMatcher { .appendText(expectedType.getSimpleName()) // .appendText(" with a ") // .appendText(name) // - .appendText(" that was "); + .appendText(" that "); subMatcher.describeMismatch(propertyValue, mismatchDescription); return false; } diff --git a/main/filesystem-nio/src/test/java/org/cryptomator/filesystem/nio/SharedFileChannelTest.java b/main/filesystem-nio/src/test/java/org/cryptomator/filesystem/nio/SharedFileChannelTest.java index 47aca6d6b..27226ee62 100644 --- a/main/filesystem-nio/src/test/java/org/cryptomator/filesystem/nio/SharedFileChannelTest.java +++ b/main/filesystem-nio/src/test/java/org/cryptomator/filesystem/nio/SharedFileChannelTest.java @@ -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 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 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); }