From c042b65a5b7a81bad6261006866c254f243e62d1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 1 Aug 2014 02:44:40 +0200 Subject: [PATCH] Increase test coverage. --- .../pterodactylus/sone/core/WebOfTrustUpdater.java | 3 +- .../sone/core/WebOfTrustUpdaterTest.java | 45 +++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java index 237c8c4..08200bb 100644 --- a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java +++ b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java @@ -422,7 +422,8 @@ public class WebOfTrustUpdater extends AbstractService { * * @author David ‘Bombe’ Roden */ - private class WebOfTrustContextUpdateJob extends WebOfTrustUpdateJob { + @VisibleForTesting + class WebOfTrustContextUpdateJob extends WebOfTrustUpdateJob { /** The own identity whose contexts to manage. */ protected final OwnIdentity ownIdentity; diff --git a/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java b/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java index c8057c5..a743378 100644 --- a/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java +++ b/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java @@ -2,15 +2,18 @@ package net.pterodactylus.sone.core; import static java.lang.Thread.sleep; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.is; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; import net.pterodactylus.sone.core.WebOfTrustUpdater.AddContextJob; import net.pterodactylus.sone.core.WebOfTrustUpdater.RemoveContextJob; +import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustContextUpdateJob; import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustUpdateJob; import net.pterodactylus.sone.freenet.plugin.PluginException; import net.pterodactylus.sone.freenet.wot.OwnIdentity; @@ -28,9 +31,10 @@ public class WebOfTrustUpdaterTest { private static final String CONTEXT = "test-context"; private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class); private final WebOfTrustUpdater webOfTrustUpdater = new WebOfTrustUpdater(webOfTrustConnector); - private final OwnIdentity ownIdentity = mock(OwnIdentity.class); + private final OwnIdentity ownIdentity = when(mock(OwnIdentity.class).getId()).thenReturn("own-identity-id").getMock(); private final WebOfTrustUpdateJob successfulWebOfTrustUpdateJob = createWebOfTrustUpdateJob(true); private final WebOfTrustUpdateJob failingWebOfTrustUpdateJob = createWebOfTrustUpdateJob(false); + private final WebOfTrustContextUpdateJob contextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT); private final AddContextJob addContextJob = webOfTrustUpdater.new AddContextJob(ownIdentity, CONTEXT); private final RemoveContextJob removeContextJob = webOfTrustUpdater.new RemoveContextJob(ownIdentity, CONTEXT); @@ -62,6 +66,45 @@ public class WebOfTrustUpdaterTest { } @Test + public void webOfTrustContextUpdateJobsAreEqualIfTheirClassOwnIdentityAndContextAreEqual() { + WebOfTrustContextUpdateJob secondContextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT); + assertThat(contextUpdateJob.equals(secondContextUpdateJob), is(true)); + assertThat(secondContextUpdateJob.equals(contextUpdateJob), is(true)); + assertThat(contextUpdateJob.hashCode(), is(secondContextUpdateJob.hashCode())); + } + + @Test + public void webOfTrustContextUpdatesJobsAreNotEqualIfTheirClassDiffers() { + assertThat(contextUpdateJob.equals(addContextJob), is(false)); + } + + @Test + public void webOfTrustContextUpdateJobToStringContainsIdentityAndContext() { + assertThat(contextUpdateJob.toString(), containsString(ownIdentity.toString())); + assertThat(contextUpdateJob.toString(), containsString(CONTEXT)); + } + + @Test + public void webOfTrustContextUpdateJobsAreNotEqualIfTheIdentitiesDiffer() { + OwnIdentity ownIdentity = mock(OwnIdentity.class); + WebOfTrustContextUpdateJob secondContextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT); + assertThat(contextUpdateJob.equals(secondContextUpdateJob), is(false)); + assertThat(secondContextUpdateJob.equals(contextUpdateJob), is(false)); + } + + @Test + public void webOfTrustContextUpdateJobsAreNotEqualIfTheirContextsDiffer() { + WebOfTrustContextUpdateJob secondContextUpdateJob = webOfTrustUpdater.new WebOfTrustContextUpdateJob(ownIdentity, CONTEXT + CONTEXT); + assertThat(contextUpdateJob.equals(secondContextUpdateJob), is(false)); + assertThat(secondContextUpdateJob.equals(contextUpdateJob), is(false)); + } + + @Test + public void webOfTrustContextUpdateJobsAreNotEqualToNull() { + assertThat(contextUpdateJob.equals(null), is(false)); + } + + @Test public void addContextJobAddsTheContext() throws PluginException { addContextJob.run(); verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT)); -- 2.7.4