Turn Mocks into a stateful object.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 28 Oct 2013 21:08:18 +0000 (22:08 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Fri, 28 Feb 2014 21:25:48 +0000 (22:25 +0100)
src/test/java/net/pterodactylus/sone/data/Mocks.java
src/test/java/net/pterodactylus/sone/fcp/CreatePostCommandTest.java
src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java
src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java
src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java

index 5181f04..d75f5f6 100644 (file)
@@ -47,16 +47,23 @@ import org.mockito.stubbing.Answer;
  */
 public class Mocks {
 
-       private static final Multimap<Sone, Post> sonePosts = create();
+       private final Multimap<Sone, Post> sonePosts = create();
+       public final Database database;
+       public final Core core;
 
-       public static Core mockCore(Database database) {
+       public Mocks() {
+               database = mockDatabase();
+               core = mockCore(database);
+       }
+
+       private static Core mockCore(Database database) {
                Core core = mock(Core.class);
                when(core.getDatabase()).thenReturn(database);
                when(core.getSone(anyString())).thenReturn(Optional.<Sone>absent());
                return core;
        }
 
-       public static Database mockDatabase() {
+       private static Database mockDatabase() {
                Database database = mock(Database.class);
                when(database.getSone(anyString())).thenReturn(Optional.<Sone>absent());
                when(database.getPost(anyString())).thenReturn(Optional.<Post>absent());
@@ -64,12 +71,11 @@ public class Mocks {
                return database;
        }
 
-       public static Sone mockLocalSone(Core core, final String id) {
+       public Sone mockLocalSone(final String id) {
                final Sone sone = mock(Sone.class);
                when(sone.getId()).thenReturn(id);
                when(sone.isLocal()).thenReturn(true);
                when(sone.getProfile()).thenReturn(new Profile(sone));
-               final Database database = core.getDatabase();
                when(sone.newPostBuilder()).thenReturn(new DefaultPostBuilder(database, id));
                when(sone.newPostReplyBuilder(anyString())).then(new Answer<PostReplyBuilder>() {
                        @Override
@@ -88,12 +94,11 @@ public class Mocks {
                return sone;
        }
 
-       public static Sone mockRemoteSone(Core core, final String id) {
+       public Sone mockRemoteSone(final String id) {
                final Sone sone = mock(Sone.class);
                when(sone.getId()).thenReturn(id);
                when(sone.isLocal()).thenReturn(false);
                when(sone.getProfile()).thenReturn(new Profile(sone));
-               final Database database = core.getDatabase();
                when(sone.newPostBuilder()).thenThrow(IllegalStateException.class);
                when(sone.newPostReplyBuilder(Matchers.<String>anyObject())).thenThrow(IllegalStateException.class);
                when(core.getSone(eq(id))).thenReturn(of(sone));
@@ -107,21 +112,19 @@ public class Mocks {
                return sone;
        }
 
-       public static Post mockPost(Core core, Sone sone, String postId) {
+       public Post mockPost(Sone sone, String postId) {
                Post post = mock(Post.class);
                when(post.getId()).thenReturn(postId);
                when(post.getSone()).thenReturn(sone);
-               Database database = core.getDatabase();
                when(database.getPost(eq(postId))).thenReturn(of(post));
                sonePosts.put(sone, post);
                return post;
        }
 
-       public static PostReply mockPostReply(Core core, Sone sone, String replyId) {
+       public PostReply mockPostReply(Sone sone, String replyId) {
                PostReply postReply = mock(PostReply.class);
                when(postReply.getId()).thenReturn(replyId);
                when(postReply.getSone()).thenReturn(sone);
-               Database database = core.getDatabase();
                when(database.getPostReply(eq(replyId))).thenReturn(of(postReply));
                return postReply;
        }
index 5af1e7e..6cc6f61 100644 (file)
@@ -19,9 +19,6 @@ package net.pterodactylus.sone.fcp;
 
 import static com.google.common.base.Optional.of;
 import static java.lang.System.currentTimeMillis;
-import static net.pterodactylus.sone.data.Mocks.mockCore;
-import static net.pterodactylus.sone.data.Mocks.mockDatabase;
-import static net.pterodactylus.sone.data.Mocks.mockLocalSone;
 import static net.pterodactylus.sone.database.PostBuilder.PostCreated;
 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
 import static org.hamcrest.MatcherAssert.assertThat;
@@ -33,10 +30,9 @@ import static org.hamcrest.Matchers.notNullValue;
 import static org.hamcrest.Matchers.nullValue;
 import static org.mockito.Mockito.when;
 
-import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Mocks;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
 import net.pterodactylus.sone.freenet.fcp.Command.Response;
 import net.pterodactylus.sone.freenet.fcp.FcpException;
@@ -54,16 +50,15 @@ import org.junit.Test;
 public class CreatePostCommandTest {
 
        private final long now = currentTimeMillis();
-       private final Database database = mockDatabase();
-       private final Core core = mockCore(database);
-       private final CreatePostCommand createPostCommand = new CreatePostCommand(core);
+       private final Mocks mocks = new Mocks();
+       private final CreatePostCommand createPostCommand = new CreatePostCommand(mocks.core);
 
        @Test
        public void verifyThatCreatingAPostWorks() throws FcpException {
-               Sone sone = mockLocalSone(core, "Sone");
-               mockLocalSone(core, "OtherSone");
+               Sone sone = mocks.mockLocalSone("Sone");
+               mocks.mockLocalSone("OtherSone");
                CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
-               when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
+               when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
 
                SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
                                .put("Sone", "Sone")
@@ -83,9 +78,9 @@ public class CreatePostCommandTest {
 
        @Test
        public void verifyThatCreatingAPostWithoutRecipientWorks() throws FcpException {
-               Sone sone = mockLocalSone(core, "Sone");
+               Sone sone = mocks.mockLocalSone("Sone");
                CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
-               when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
+               when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
 
                SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
                                .put("Sone", "Sone")
@@ -104,9 +99,9 @@ public class CreatePostCommandTest {
 
        @Test
        public void verifyThatCreatingAPostDirectedToTheSendingSoneCausesAnError() throws FcpException {
-               mockLocalSone(core, "Sone");
+               mocks.mockLocalSone("Sone");
                CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
-               when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
+               when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
 
                SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
                                .put("Sone", "Sone")
@@ -121,9 +116,9 @@ public class CreatePostCommandTest {
 
        @Test(expected = FcpException.class)
        public void verifyThatCreatingAPostWithoutTextCausesAnError() throws FcpException {
-               mockLocalSone(core, "Sone");
+               mocks.mockLocalSone("Sone");
                CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
-               when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
+               when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
 
                SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
                                .put("Sone", "Sone")
@@ -133,9 +128,9 @@ public class CreatePostCommandTest {
 
        @Test(expected = FcpException.class)
        public void verifyThatCreatingAPostWithoutSoneCausesAnError() throws FcpException {
-               mockLocalSone(core, "Sone");
+               mocks.mockLocalSone("Sone");
                CapturingPostCreated capturingPostCreated = new CapturingPostCreated();
-               when(core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
+               when(mocks.core.postCreated()).thenReturn(Optional.<PostCreated>of(capturingPostCreated));
 
                SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder()
                                .put("Text", "Text of the post.")
index 6317c8f..70f9996 100644 (file)
 package net.pterodactylus.sone.fcp;
 
 import static java.lang.System.currentTimeMillis;
-import static net.pterodactylus.sone.data.Mocks.mockCore;
-import static net.pterodactylus.sone.data.Mocks.mockDatabase;
-import static net.pterodactylus.sone.data.Mocks.mockLocalSone;
-import static net.pterodactylus.sone.data.Mocks.mockPost;
 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
@@ -31,10 +27,9 @@ import static org.hamcrest.Matchers.notNullValue;
 import static org.hamcrest.core.AllOf.allOf;
 import static org.mockito.Mockito.when;
 
-import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Mocks;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.database.PostReplyBuilder.PostReplyCreated;
 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
 import net.pterodactylus.sone.freenet.fcp.Command.Response;
@@ -53,16 +48,15 @@ import org.junit.Test;
 public class CreateReplyCommandTest {
 
        private final long now = currentTimeMillis();
-       private final Database database = mockDatabase();
-       private final Core core = mockCore(database);
-       private final CreateReplyCommand createReplyCommand = new CreateReplyCommand(core);
+       private final Mocks mocks = new Mocks();
+       private final CreateReplyCommand createReplyCommand = new CreateReplyCommand(mocks.core);
 
        @Test
        public void verifyThatCreatingAFullySpecifiedReplyWorks() throws FcpException {
-               Sone sone = mockLocalSone(core, "SoneId");
-               mockPost(core, sone, "PostId");
+               Sone sone = mocks.mockLocalSone("SoneId");
+               mocks.mockPost(sone, "PostId");
                CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
-               when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
+               when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
                SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
                                .put("Message", "CreateReply")
                                .put("Sone", "SoneId")
@@ -84,10 +78,10 @@ public class CreateReplyCommandTest {
 
        @Test(expected = FcpException.class)
        public void verifyThatCreatingAReplyWithoutSoneCausesAnError() throws FcpException {
-               Sone sone = mockLocalSone(core, "SoneId");
-               mockPost(core, sone, "PostId");
+               Sone sone = mocks.mockLocalSone("SoneId");
+               mocks.mockPost(sone, "PostId");
                CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
-               when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
+               when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
                SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
                                .put("Message", "CreateReply")
                                .put("Post", "PostId")
@@ -98,9 +92,9 @@ public class CreateReplyCommandTest {
 
        @Test(expected = FcpException.class)
        public void verifyThatCreatingAReplyWithoutPostCausesAnError() throws FcpException {
-               mockLocalSone(core, "SoneId");
+               mocks.mockLocalSone("SoneId");
                CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
-               when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
+               when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
                SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
                                .put("Message", "CreateReply")
                                .put("Sone", "SoneId")
@@ -111,10 +105,10 @@ public class CreateReplyCommandTest {
 
        @Test(expected = FcpException.class)
        public void verifyThatCreatingAReplyWithoutTextCausesAnError() throws FcpException {
-               Sone sone = mockLocalSone(core, "SoneId");
-               mockPost(core, sone, "PostId");
+               Sone sone = mocks.mockLocalSone("SoneId");
+               mocks.mockPost(sone, "PostId");
                CapturingPostReplyCreated capturingPostReplyCreated = new CapturingPostReplyCreated();
-               when(core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
+               when(mocks.core.postReplyCreated()).thenReturn(Optional.<PostReplyCreated>of(capturingPostReplyCreated));
                SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder()
                                .put("Message", "CreateReply")
                                .put("Sone", "SoneId")
index 1d282ca..5b06277 100644 (file)
 
 package net.pterodactylus.sone.fcp;
 
-import static net.pterodactylus.sone.data.Mocks.mockCore;
-import static net.pterodactylus.sone.data.Mocks.mockDatabase;
-import static net.pterodactylus.sone.data.Mocks.mockLocalSone;
-import static net.pterodactylus.sone.data.Mocks.mockPost;
-import static net.pterodactylus.sone.data.Mocks.mockRemoteSone;
 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
@@ -29,10 +24,9 @@ import static org.hamcrest.Matchers.notNullValue;
 import static org.mockito.ArgumentCaptor.forClass;
 import static org.mockito.Mockito.doNothing;
 
-import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Mocks;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
 import net.pterodactylus.sone.freenet.fcp.Command.Response;
 import net.pterodactylus.sone.freenet.fcp.FcpException;
@@ -49,16 +43,15 @@ import org.mockito.ArgumentCaptor;
  */
 public class DeletePostCommandTest {
 
-       private final Database database = mockDatabase();
-       private final Core core = mockCore(database);
-       private final DeletePostCommand deletePostCommand = new DeletePostCommand(core);
+       private final Mocks mocks = new Mocks();
+       private final DeletePostCommand deletePostCommand = new DeletePostCommand(mocks.core);
 
        @Test
        public void verifyThatDeletingAPostWorks() throws FcpException {
-               Sone sone = mockLocalSone(core, "Sone");
-               Post post = mockPost(core, sone, "PostId");
+               Sone sone = mocks.mockLocalSone("Sone");
+               Post post = mocks.mockPost(sone, "PostId");
                ArgumentCaptor<Post> deletedPost = forClass(Post.class);
-               doNothing().when(core).deletePost(deletedPost.capture());
+               doNothing().when(mocks.core).deletePost(deletedPost.capture());
                SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
                                .put("Message", "DeletePost")
                                .put("Post", "PostId")
@@ -72,8 +65,8 @@ public class DeletePostCommandTest {
 
        @Test
        public void verifyThatDeletingAPostFromANonLocalSoneCausesAnError() throws FcpException {
-               Sone sone = mockRemoteSone(core, "Sone");
-               Post post = mockPost(core, sone, "PostId");
+               Sone sone = mocks.mockRemoteSone("Sone");
+               Post post = mocks.mockPost(sone, "PostId");
                SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
                                .put("Message", "DeletePost")
                                .put("Post", "PostId")
@@ -95,8 +88,8 @@ public class DeletePostCommandTest {
 
        @Test(expected = FcpException.class)
        public void verifyThatDeletingAPostWithAnInvalidPostIdCausesAnError() throws FcpException {
-               Sone sone = mockLocalSone(core, "Sone");
-               mockPost(core, sone, "PostId");
+               Sone sone = mocks.mockLocalSone("Sone");
+               mocks.mockPost(sone, "PostId");
                SimpleFieldSet deletePostFieldSet = new SimpleFieldSetBuilder()
                                .put("Message", "DeletePost")
                                .put("Post", "OtherPostId")
index 4ab8cdf..422a470 100644 (file)
 
 package net.pterodactylus.sone.fcp;
 
-import static net.pterodactylus.sone.data.Mocks.mockCore;
-import static net.pterodactylus.sone.data.Mocks.mockDatabase;
-import static net.pterodactylus.sone.data.Mocks.mockLocalSone;
-import static net.pterodactylus.sone.data.Mocks.mockPostReply;
-import static net.pterodactylus.sone.data.Mocks.mockRemoteSone;
 import static net.pterodactylus.sone.freenet.fcp.Command.AccessType.DIRECT;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.is;
@@ -29,10 +24,9 @@ import static org.hamcrest.Matchers.notNullValue;
 import static org.mockito.ArgumentCaptor.forClass;
 import static org.mockito.Mockito.doNothing;
 
-import net.pterodactylus.sone.core.Core;
+import net.pterodactylus.sone.data.Mocks;
 import net.pterodactylus.sone.data.PostReply;
 import net.pterodactylus.sone.data.Sone;
-import net.pterodactylus.sone.database.Database;
 import net.pterodactylus.sone.freenet.SimpleFieldSetBuilder;
 import net.pterodactylus.sone.freenet.fcp.Command.Response;
 import net.pterodactylus.sone.freenet.fcp.FcpException;
@@ -49,16 +43,15 @@ import org.mockito.ArgumentCaptor;
  */
 public class DeleteReplyCommandTest {
 
-       private final Database database = mockDatabase();
-       private final Core core = mockCore(database);
-       private final DeleteReplyCommand deleteReplyCommand = new DeleteReplyCommand(core);
+       private final Mocks mocks = new Mocks();
+       private final DeleteReplyCommand deleteReplyCommand = new DeleteReplyCommand(mocks.core);
 
        @Test
        public void verifyThatDeletingAReplyWorks() throws FcpException {
-               Sone sone = mockLocalSone(core, "SoneId");
-               PostReply postReply = mockPostReply(core, sone, "ReplyId");
+               Sone sone = mocks.mockLocalSone("SoneId");
+               PostReply postReply = mocks.mockPostReply(sone, "ReplyId");
                ArgumentCaptor<PostReply> postReplyCaptor = forClass(PostReply.class);
-               doNothing().when(core).deleteReply(postReplyCaptor.capture());
+               doNothing().when(mocks.core).deleteReply(postReplyCaptor.capture());
                SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
                                .put("Message", "DeleteReply")
                                .put("Reply", "ReplyId")
@@ -89,8 +82,8 @@ public class DeleteReplyCommandTest {
 
        @Test
        public void verifyThatDeletingAReplyFromANonLocalSoneCausesAnError() throws FcpException {
-               Sone sone = mockRemoteSone(core, "SoneId");
-               mockPostReply(core, sone, "ReplyId");
+               Sone sone = mocks.mockRemoteSone("SoneId");
+               mocks.mockPostReply(sone, "ReplyId");
                SimpleFieldSet deleteReplyFieldSet = new SimpleFieldSetBuilder()
                                .put("Message", "DeleteReply")
                                .put("Reply", "ReplyId")