From 5a479c3644db1d14e18950c0f5141b5516f3db0d Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 9 Nov 2014 12:29:12 +0100 Subject: [PATCH] Add unit test for returning not-yet loaded bookmarked posts. --- src/test/java/net/pterodactylus/sone/Matchers.java | 29 ++++++++++++++++++++++ .../memory/MemoryBookmarkDatabaseTest.java | 26 ++++++++++++++++--- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/test/java/net/pterodactylus/sone/Matchers.java b/src/test/java/net/pterodactylus/sone/Matchers.java index c1052d5..c16322c 100644 --- a/src/test/java/net/pterodactylus/sone/Matchers.java +++ b/src/test/java/net/pterodactylus/sone/Matchers.java @@ -98,6 +98,10 @@ public class Matchers { return new PostMatcher(postId, time, text, recipient); } + public static Matcher isPostWithId(String postId) { + return new PostIdMatcher(postId); + } + public static Matcher isPostReply(String postReplyId, String postId, long time, String text) { return new PostReplyMatcher(postReplyId, postId, time, text); @@ -312,6 +316,31 @@ public class Matchers { } + 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/MemoryBookmarkDatabaseTest.java b/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java index c5ce475..06c5b96 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java @@ -1,7 +1,9 @@ package net.pterodactylus.sone.database.memory; import static com.google.common.base.Optional.fromNullable; +import static net.pterodactylus.sone.Matchers.isPostWithId; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.is; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyString; @@ -52,14 +54,20 @@ public class MemoryBookmarkDatabaseTest { @Before public void setupPosts() { - createPost("PostId1"); - createPost("PostId2"); + createAndRegisterPost("PostId1"); + createAndRegisterPost("PostId2"); } - private void createPost(String postId) { + private Post createAndRegisterPost(String postId) { + Post post = createPost(postId); + posts.put(postId, post); + return post; + } + + private Post createPost(String postId) { Post post = mock(Post.class); when(post.getId()).thenReturn(postId); - posts.put(postId, post); + return post; } @Test @@ -121,4 +129,14 @@ public class MemoryBookmarkDatabaseTest { verify(configurationLoader).saveBookmarkedPosts(any(Set.class)); } + @Test + public void bookmarkedPostsIncludeNotYetLoadedPosts() { + bookmarkDatabase.bookmarkPost(posts.get("PostId1")); + bookmarkDatabase.bookmarkPost(createPost("PostId3")); + final Set bookmarkedPosts = + bookmarkDatabase.getBookmarkedPosts(); + assertThat(bookmarkedPosts, + contains(isPostWithId("PostId1"), isPostWithId("PostId3"))); + } + } -- 2.7.4