From 521f08bf196e8d74e8ab4ca1be4aace02ac56379 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 2 Aug 2014 11:40:45 +0200 Subject: [PATCH] =?utf8?q?Add=20test=20for=20=E2=80=9Cset=20property?= =?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 | 3 +- .../sone/core/WebOfTrustUpdaterTest.java | 83 ++++++++++++++++++++++ 2 files changed, 85 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 08200bb..aac4924 100644 --- a/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java +++ b/src/main/java/net/pterodactylus/sone/core/WebOfTrustUpdater.java @@ -550,7 +550,8 @@ public class WebOfTrustUpdater extends AbstractService { * * @author David ‘Bombe’ Roden */ - private class SetPropertyJob extends WebOfTrustUpdateJob { + @VisibleForTesting + class SetPropertyJob extends WebOfTrustUpdateJob { /** The own identity to update properties on. */ private 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 a743378..40230ab 100644 --- a/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java +++ b/src/test/java/net/pterodactylus/sone/core/WebOfTrustUpdaterTest.java @@ -4,6 +4,7 @@ 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.hamcrest.Matchers.not; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; @@ -13,6 +14,7 @@ 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.SetPropertyJob; import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustContextUpdateJob; import net.pterodactylus.sone.core.WebOfTrustUpdater.WebOfTrustUpdateJob; import net.pterodactylus.sone.freenet.plugin.PluginException; @@ -138,4 +140,85 @@ public class WebOfTrustUpdaterTest { assertThat(removeContextJob.waitForCompletion(), is(false)); } + @Test + public void settingAPropertySetsTheProperty() throws PluginException { + String propertyName = "property-name"; + String propertyValue = "property-value"; + SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue); + setPropertyJob.run(); + verify(webOfTrustConnector).setProperty(eq(ownIdentity), eq(propertyName), eq(propertyValue)); + verify(ownIdentity).setProperty(eq(propertyName), eq(propertyValue)); + assertThat(setPropertyJob.waitForCompletion(), is(true)); + } + + @Test + public void settingAPropertyToNullRemovesTheProperty() throws PluginException { + String propertyName = "property-name"; + SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, null); + setPropertyJob.run(); + verify(webOfTrustConnector).removeProperty(eq(ownIdentity), eq(propertyName)); + verify(ownIdentity).removeProperty(eq(propertyName)); + assertThat(setPropertyJob.waitForCompletion(), is(true)); + } + + @Test + public void pluginExceptionWhileSettingAPropertyIsHandled() throws PluginException { + String propertyName = "property-name"; + String propertyValue = "property-value"; + doThrow(PluginException.class).when(webOfTrustConnector).setProperty(eq(ownIdentity), eq(propertyName), eq(propertyValue)); + SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue); + setPropertyJob.run(); + verify(webOfTrustConnector).setProperty(eq(ownIdentity), eq(propertyName), eq(propertyValue)); + verify(ownIdentity, never()).setProperty(eq(propertyName), eq(propertyValue)); + assertThat(setPropertyJob.waitForCompletion(), is(false)); + } + + @Test + public void setPropertyJobsWithSameClassPropertyAndValueAreEqual() { + String propertyName = "property-name"; + String propertyValue = "property-value"; + SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue); + SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue); + assertThat(firstSetPropertyJob, is(secondSetPropertyJob)); + assertThat(secondSetPropertyJob, is(firstSetPropertyJob)); + assertThat(firstSetPropertyJob.hashCode(), is(secondSetPropertyJob.hashCode())); + } + + @Test + public void setPropertyJobsWithDifferentClassesAreNotEqual() { + String propertyName = "property-name"; + String propertyValue = "property-value"; + SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue); + SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue) { + }; + assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob))); + } + + @Test + public void nullIsNotASetProjectJobEither() { + String propertyName = "property-name"; + String propertyValue = "property-value"; + SetPropertyJob setPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue); + assertThat(setPropertyJob, not(is((Object) null))); + } + + @Test + public void setPropertyJobsWithDifferentPropertiesAreNotEqual() { + String propertyName = "property-name"; + String propertyValue = "property-value"; + SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue); + SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName + "2", propertyValue); + assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob))); + } + + @Test + public void setPropertyJobsWithDifferentOwnIdentitiesAreNotEqual() { + OwnIdentity otherOwnIdentity = mock(OwnIdentity.class); + String propertyName = "property-name"; + String propertyValue = "property-value"; + SetPropertyJob firstSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(ownIdentity, propertyName, propertyValue); + SetPropertyJob secondSetPropertyJob = webOfTrustUpdater.new SetPropertyJob(otherOwnIdentity, propertyName, propertyValue); + assertThat(firstSetPropertyJob, not(is(secondSetPropertyJob))); + } + } -- 2.7.4