From: David ‘Bombe’ Roden Date: Thu, 27 Nov 2014 05:50:49 +0000 (+0100) Subject: Add and use a matcher for incomplete posts. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=13f2ccb2b985cd206a593f3f40d397a6befcce6e;p=Sone.git Add and use a matcher for incomplete posts. --- diff --git a/src/test/java/net/pterodactylus/sone/Matchers.java b/src/test/java/net/pterodactylus/sone/Matchers.java index c16322c..3ac4972 100644 --- a/src/test/java/net/pterodactylus/sone/Matchers.java +++ b/src/test/java/net/pterodactylus/sone/Matchers.java @@ -27,6 +27,7 @@ import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; +import com.google.common.base.Objects; import com.google.common.base.Optional; import org.hamcrest.Description; import org.hamcrest.Matcher; @@ -93,13 +94,103 @@ public class Matchers { }; } + public static class IncompletePostMatcher extends TypeSafeDiagnosingMatcher { + + private String id; + private String sender; + private boolean checkRecipient; + private String recipient; + private long time; + private String text; + + private IncompletePostMatcher() { + } + + public static IncompletePostMatcher matches() { + return new IncompletePostMatcher(); + } + + public IncompletePostMatcher id(String id) { + this.id = id; + return this; + } + + public IncompletePostMatcher sender(String sender) { + this.sender = sender; + return this; + } + + public IncompletePostMatcher recipient(String recipient) { + this.checkRecipient = true; + this.recipient = recipient; + return this; + } + + public IncompletePostMatcher time(long time) { + this.time = time; + return this; + } + + public IncompletePostMatcher text(String text) { + this.text = text; + return this; + } + + @Override + protected boolean matchesSafely(Post post, Description mismatchDescription) { + if ((id != null) && !id.matches(post.getId())) { + mismatchDescription.appendText("ID is ").appendValue(post.getId()); + return false; + } + if ((sender != null) && !sender.matches(post.getSone().getId())) { + mismatchDescription.appendText("sender is ").appendValue(post.getSone().getId()); + return false; + } + if (checkRecipient && !Objects.equal(recipient, post.getRecipientId().orNull())) { + mismatchDescription.appendText("recipient is ").appendValue(post.getRecipientId().orNull()); + return false; + } + if ((time != 0) && (time != post.getTime())) { + mismatchDescription.appendText("time is ").appendValue(post.getTime()); + return false; + } + if ((text != null) && !text.equals(post.getText())) { + mismatchDescription.appendText("text is ").appendValue(post.getText()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("is post"); + if (id != null) { + description.appendText(" with ID ").appendValue(id); + } + if (sender != null) { + description.appendText(" with sender ").appendValue(sender); + } + if (checkRecipient) { + description.appendText(" with recipient ").appendValue(recipient); + } + if (time != 0) { + description.appendText(" with time ").appendValue(time); + } + if (text != null) { + description.appendText(" with text ").appendValue(text); + } + } + + } + + public static Matcher isPost(String postId, long time, String text, Optional recipient) { - return new PostMatcher(postId, time, text, recipient); + return IncompletePostMatcher.matches().id(postId).time(time).text(text).recipient(recipient.orNull()); } public static Matcher isPostWithId(String postId) { - return new PostIdMatcher(postId); + return IncompletePostMatcher.matches().id(postId); } public static Matcher isPostReply(String postReplyId, @@ -249,98 +340,6 @@ public class Matchers { }; } - private static class PostMatcher extends TypeSafeDiagnosingMatcher { - - private final String postId; - private final long time; - private final String text; - private final Optional recipient; - - private PostMatcher(String postId, long time, String text, - Optional 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 PostIdMatcher extends TypeSafeDiagnosingMatcher { - - private final String id; - - private PostIdMatcher(String id) { - this.id = id; - } - - @Override - protected boolean matchesSafely(Post item, - Description mismatchDescription) { - if (!item.getId().equals(id)) { - mismatchDescription.appendText("post has ID ").appendValue(item.getId()); - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("post with ID ").appendValue(id); - } - - } - private static class PostReplyMatcher extends TypeSafeDiagnosingMatcher { diff --git a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java index cc5babb..7cba93b 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryDatabaseTest.java @@ -44,6 +44,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import net.pterodactylus.sone.Matchers.IncompletePostMatcher; import net.pterodactylus.sone.TestAlbumBuilder; import net.pterodactylus.sone.TestImageBuilder; import net.pterodactylus.sone.TestPostBuilder; @@ -227,7 +228,7 @@ public class MemoryDatabaseTest { Post postWithoutRecipient = createPost(Optional.absent()); memoryDatabase.storePost(postWithoutRecipient); assertThat(memoryDatabase.getDirectedPosts(RECIPIENT_ID), - contains(postWithRecipient)); + contains(IncompletePostMatcher.matches().recipient(RECIPIENT_ID))); } private Post createPost(Optional recipient) {