From 844bd06ee895aa3a050192b5063b1efe1a85f80b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 28 Oct 2013 22:08:18 +0100 Subject: [PATCH] Turn Mocks into a stateful object. --- .../java/net/pterodactylus/sone/data/Mocks.java | 25 +++++++++------- .../sone/fcp/CreatePostCommandTest.java | 33 +++++++++------------ .../sone/fcp/CreateReplyCommandTest.java | 34 +++++++++------------- .../sone/fcp/DeletePostCommandTest.java | 27 +++++++---------- .../sone/fcp/DeleteReplyCommandTest.java | 23 +++++---------- 5 files changed, 60 insertions(+), 82 deletions(-) diff --git a/src/test/java/net/pterodactylus/sone/data/Mocks.java b/src/test/java/net/pterodactylus/sone/data/Mocks.java index 5181f04..d75f5f6 100644 --- a/src/test/java/net/pterodactylus/sone/data/Mocks.java +++ b/src/test/java/net/pterodactylus/sone/data/Mocks.java @@ -47,16 +47,23 @@ import org.mockito.stubbing.Answer; */ public class Mocks { - private static final Multimap sonePosts = create(); + private final Multimap 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.absent()); return core; } - public static Database mockDatabase() { + private static Database mockDatabase() { Database database = mock(Database.class); when(database.getSone(anyString())).thenReturn(Optional.absent()); when(database.getPost(anyString())).thenReturn(Optional.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() { @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.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; } diff --git a/src/test/java/net/pterodactylus/sone/fcp/CreatePostCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/CreatePostCommandTest.java index 5af1e7e..6cc6f61 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/CreatePostCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/CreatePostCommandTest.java @@ -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.of(capturingPostCreated)); + when(mocks.core.postCreated()).thenReturn(Optional.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.of(capturingPostCreated)); + when(mocks.core.postCreated()).thenReturn(Optional.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.of(capturingPostCreated)); + when(mocks.core.postCreated()).thenReturn(Optional.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.of(capturingPostCreated)); + when(mocks.core.postCreated()).thenReturn(Optional.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.of(capturingPostCreated)); + when(mocks.core.postCreated()).thenReturn(Optional.of(capturingPostCreated)); SimpleFieldSet createPostFieldSet = new SimpleFieldSetBuilder() .put("Text", "Text of the post.") diff --git a/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java index 6317c8f..70f9996 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/CreateReplyCommandTest.java @@ -18,10 +18,6 @@ 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.of(capturingPostReplyCreated)); + when(mocks.core.postReplyCreated()).thenReturn(Optional.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.of(capturingPostReplyCreated)); + when(mocks.core.postReplyCreated()).thenReturn(Optional.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.of(capturingPostReplyCreated)); + when(mocks.core.postReplyCreated()).thenReturn(Optional.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.of(capturingPostReplyCreated)); + when(mocks.core.postReplyCreated()).thenReturn(Optional.of(capturingPostReplyCreated)); SimpleFieldSet createReplyFieldSet = new SimpleFieldSetBuilder() .put("Message", "CreateReply") .put("Sone", "SoneId") diff --git a/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java index 1d282ca..5b06277 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/DeletePostCommandTest.java @@ -17,11 +17,6 @@ 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 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") diff --git a/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java b/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java index 4ab8cdf..422a470 100644 --- a/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java +++ b/src/test/java/net/pterodactylus/sone/fcp/DeleteReplyCommandTest.java @@ -17,11 +17,6 @@ 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 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") -- 2.7.4