From d249bcc142a9e351e1774ac326ff5e961e1813d9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 30 Jul 2014 14:20:53 +0200 Subject: [PATCH] =?utf8?q?Add=20test=20for=20=E2=80=9Cadd=20context?= =?utf8?q?=E2=80=9D=20job.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/sone/core/WebOfTrustUpdater.java | 4 +- .../sone/core/WebOfTrustUpdaterTest.java | 48 ++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java diff --git a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java index dd3de58..9bd475a 100644 --- a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java +++ b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java @@ -34,6 +34,7 @@ import net.pterodactylus.sone.freenet.wot.WebOfTrustException; import net.pterodactylus.util.logging.Logging; import net.pterodactylus.util.service.AbstractService; +import com.google.common.annotations.VisibleForTesting; import com.google.inject.Inject; /** @@ -474,7 +475,8 @@ public class WebOfTrustUpdater extends AbstractService { * * @author David ‘Bombe’ Roden */ - private class AddContextJob extends WebOfTrustContextUpdateJob { + @VisibleForTesting + class AddContextJob extends WebOfTrustContextUpdateJob { /** * Creates a new add-context job. diff --git a/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java b/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java new file mode 100644 index 0000000..0589d9d --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java @@ -0,0 +1,48 @@ +package net.pterodactylus.sone.core; + +import static org.hamcrest.MatcherAssert.assertThat; +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 net.pterodactylus.sone.core.WebOfTrustUpdater.AddContextJob; +import net.pterodactylus.sone.freenet.plugin.PluginException; +import net.pterodactylus.sone.freenet.wot.OwnIdentity; +import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector; + +import org.junit.Test; + +/** + * Unit test for {@link WebOfTrustUpdater} and its subclasses. + * + * @author David ‘Bombe’ Roden + */ +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 AddContextJob addContextJob = webOfTrustUpdater.new AddContextJob(ownIdentity, CONTEXT); + + @Test + public void addContextJobAddsTheContext() throws PluginException { + addContextJob.run(); + verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT)); + verify(ownIdentity).addContext(eq(CONTEXT)); + assertThat(addContextJob.waitForCompletion(), is(true)); + } + + @Test + public void exceptionWhileAddingAContextIsExposed() throws PluginException { + doThrow(PluginException.class).when(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT)); + addContextJob.run(); + verify(webOfTrustConnector).addContext(eq(ownIdentity), eq(CONTEXT)); + verify(ownIdentity, never()).addContext(eq(CONTEXT)); + assertThat(addContextJob.waitForCompletion(), is(false)); + } + +} -- 2.7.4