From da0926405f52158c071e3b6b93aea5f97efdec15 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 31 Jul 2014 15:01:38 +0200 Subject: [PATCH] Increase test coverage. --- .../pterodactylus/sone/core/WebOfTrustUpdater.java | 4 ++- .../sone/core/WebOfTrustUpdaterTest.java | 31 ++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java index 9b49092..237c8c4 100644 --- a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java +++ b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java @@ -259,7 +259,8 @@ public class WebOfTrustUpdater extends AbstractService { * * @author David ‘Bombe’ Roden */ - private class WebOfTrustUpdateJob { + @VisibleForTesting + class WebOfTrustUpdateJob implements Runnable { /** Object for synchronization. */ @SuppressWarnings("hiding") @@ -280,6 +281,7 @@ public class WebOfTrustUpdater extends AbstractService { *

* The implementation of this class does nothing. */ + @Override public void run() { /* does nothing. */ } diff --git a/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java b/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java index 84dca44..c8057c5 100644 --- a/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java +++ b/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java @@ -1,5 +1,6 @@ package net.pterodactylus.sone.core; +import static java.lang.Thread.sleep; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; import static org.mockito.Matchers.eq; @@ -10,6 +11,7 @@ import static org.mockito.Mockito.verify; import net.pterodactylus.sone.core.WebOfTrustUpdater.AddContextJob; import net.pterodactylus.sone.core.WebOfTrustUpdater.RemoveContextJob; +import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustUpdateJob; import net.pterodactylus.sone.freenet.plugin.PluginException; import net.pterodactylus.sone.freenet.wot.OwnIdentity; import net.pterodactylus.sone.freenet.wot.WebOfTrustConnector; @@ -27,9 +29,38 @@ public class WebOfTrustUpdaterTest { private final WebOfTrustConnector webOfTrustConnector = mock(WebOfTrustConnector.class); private final WebOfTrustUpdater webOfTrustUpdater = new WebOfTrustUpdater(webOfTrustConnector); private final OwnIdentity ownIdentity = mock(OwnIdentity.class); + private final WebOfTrustUpdateJob successfulWebOfTrustUpdateJob = createWebOfTrustUpdateJob(true); + private final WebOfTrustUpdateJob failingWebOfTrustUpdateJob = createWebOfTrustUpdateJob(false); private final AddContextJob addContextJob = webOfTrustUpdater.new AddContextJob(ownIdentity, CONTEXT); private final RemoveContextJob removeContextJob = webOfTrustUpdater.new RemoveContextJob(ownIdentity, CONTEXT); + private WebOfTrustUpdateJob createWebOfTrustUpdateJob(final boolean success) { + return webOfTrustUpdater.new WebOfTrustUpdateJob() { + @Override + public void run() { + super.run(); + try { + sleep(100); + } catch (InterruptedException ie1) { + throw new RuntimeException(ie1); + } + finish(success); + } + }; + } + + @Test + public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsSuccess() throws InterruptedException { + new Thread(successfulWebOfTrustUpdateJob).start(); + assertThat(successfulWebOfTrustUpdateJob.waitForCompletion(), is(true)); + } + + @Test + public void webOfTrustUpdateJobWaitsUntilFinishedHasBeenCalledAndReturnsFailure() throws InterruptedException { + new Thread(failingWebOfTrustUpdateJob).start(); + assertThat(failingWebOfTrustUpdateJob.waitForCompletion(), is(false)); + } + @Test public void addContextJobAddsTheContext() throws PluginException { addContextJob.run(); -- 2.7.4