+ public static Matcher<InputStream> delivers(final byte[] data) {
+ return new TypeSafeMatcher<InputStream>() {
+ byte[] readData = new byte[data.length];
+
+ @Override
+ protected boolean matchesSafely(InputStream inputStream) {
+ int offset = 0;
+ try {
+ while (true) {
+ int r = inputStream.read();
+ if (r == -1) {
+ return offset == data.length;
+ }
+ if (offset == data.length) {
+ return false;
+ }
+ if (data[offset] != (readData[offset] = (byte) r)) {
+ return false;
+ }
+ offset++;
+ }
+ } catch (IOException ioe1) {
+ return false;
+ }
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendValue(data);
+ }
+
+ @Override
+ protected void describeMismatchSafely(InputStream item,
+ Description mismatchDescription) {
+ mismatchDescription.appendValue(readData);
+ }
+ };
+ }
+
+ public static Matcher<Post> isPost(String postId, long time,
+ String text, Optional<String> recipient) {
+ return new PostMatcher(postId, time, text, recipient);
+ }
+
+ public static Matcher<PostReply> isPostReply(String postReplyId,
+ String postId, long time, String text) {
+ return new PostReplyMatcher(postReplyId, postId, time, text);
+ }
+
+ private static class PostMatcher extends TypeSafeDiagnosingMatcher<Post> {
+
+ private final String postId;
+ private final long time;
+ private final String text;
+ private final Optional<String> recipient;
+
+ private PostMatcher(String postId, long time, String text,
+ Optional<String> recipient) {
+ this.postId = postId;
+ this.time = time;
+ this.text = text;
+ this.recipient = recipient;
+ }
+
+ @Override
+ protected boolean matchesSafely(Post post,
+ Description mismatchDescription) {
+ if (!post.getId().equals(postId)) {
+ mismatchDescription.appendText("ID is not ")
+ .appendValue(postId);
+ return false;
+ }
+ if (post.getTime() != time) {
+ mismatchDescription.appendText("Time is not @")
+ .appendValue(time);
+ return false;
+ }
+ if (!post.getText().equals(text)) {
+ mismatchDescription.appendText("Text is not ")
+ .appendValue(text);
+ return false;
+ }
+ if (recipient.isPresent()) {
+ if (!post.getRecipientId().isPresent()) {
+ mismatchDescription.appendText(
+ "Recipient not present");
+ return false;
+ }
+ if (!post.getRecipientId().get().equals(recipient.get())) {
+ mismatchDescription.appendText("Recipient is not ")
+ .appendValue(recipient.get());
+ return false;
+ }
+ } else {
+ if (post.getRecipientId().isPresent()) {
+ mismatchDescription.appendText("Recipient is present");
+ return false;
+ }
+ }
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("is post with ID ")
+ .appendValue(postId);
+ description.appendText(", created at @").appendValue(time);
+ description.appendText(", text ").appendValue(text);
+ if (recipient.isPresent()) {
+ description.appendText(", directed at ")
+ .appendValue(recipient.get());
+ }
+ }
+
+ }
+
+ private static class PostReplyMatcher
+ extends TypeSafeDiagnosingMatcher<PostReply> {
+
+ private final String postReplyId;
+ private final String postId;
+ private final long time;
+ private final String text;
+
+ private PostReplyMatcher(String postReplyId, String postId, long time,
+ String text) {
+ this.postReplyId = postReplyId;
+ this.postId = postId;
+ this.time = time;
+ this.text = text;
+ }
+
+ @Override
+ protected boolean matchesSafely(PostReply postReply,
+ Description mismatchDescription) {
+ if (!postReply.getId().equals(postReplyId)) {
+ mismatchDescription.appendText("is post reply ")
+ .appendValue(postReply.getId());
+ return false;
+ }
+ if (!postReply.getPostId().equals(postId)) {
+ mismatchDescription.appendText("is reply to ")
+ .appendValue(postReply.getPostId());
+ return false;
+ }
+ if (postReply.getTime() != time) {
+ mismatchDescription.appendText("is created at @").appendValue(
+ postReply.getTime());
+ return false;
+ }
+ if (!postReply.getText().equals(text)) {
+ mismatchDescription.appendText("says ")
+ .appendValue(postReply.getText());
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText("is post reply ").appendValue(postReplyId);
+ description.appendText(", replies to post ").appendValue(postId);
+ description.appendText(", is created at @").appendValue(time);
+ description.appendText(", says ").appendValue(text);
+ }
+
+ }
+