From 71df818934386a263bce5cc8d0a5912ccc7a030b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Mon, 23 Sep 2019 20:01:36 +0200 Subject: [PATCH] =?utf8?q?=F0=9F=8E=A8=20Replace=20identity=20tests=20with?= =?utf8?q?=20Kotlin=20version?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../sone/freenet/wot/DefaultIdentityTest.java | 150 --------------------- .../sone/freenet/wot/DefaultOwnIdentityTest.java | 40 ------ .../sone/freenet/wot/DefaultIdentityTest.kt | 142 +++++++++++++++++++ .../sone/freenet/wot/DefaultOwnIdentityTest.kt | 36 +++++ 4 files changed, 178 insertions(+), 190 deletions(-) delete mode 100644 src/test/java/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.java delete mode 100644 src/test/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentityTest.java create mode 100644 src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentityTest.kt diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.java deleted file mode 100644 index 9d59570..0000000 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.java +++ /dev/null @@ -1,150 +0,0 @@ -/* - * Sone - DefaultIdentityTest.java - Copyright © 2013–2019 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.freenet.wot; - -import static com.google.common.collect.ImmutableMap.of; -import static java.util.Arrays.asList; -import static net.pterodactylus.sone.test.Matchers.matchesRegex; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.containsInAnyOrder; -import static org.hamcrest.Matchers.empty; -import static org.hamcrest.Matchers.hasEntry; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.collection.IsIterableContainingInOrder.contains; -import static org.mockito.Mockito.mock; - -import java.util.Collections; - -import org.junit.Test; - -/** - * Unit test for {@link DefaultIdentity}. - */ -public class DefaultIdentityTest { - - protected final DefaultIdentity identity = createIdentity(); - - protected DefaultIdentity createIdentity() { - return new DefaultIdentity("Id", "Nickname", "RequestURI"); - } - - @Test - public void identityCanBeCreated() { - assertThat(identity.getId(), is("Id")); - assertThat(identity.getNickname(), is("Nickname")); - assertThat(identity.getRequestUri(), is("RequestURI")); - assertThat(identity.getContexts(), empty()); - assertThat(identity.getProperties(), is(Collections.emptyMap())); - } - - @Test - public void contextsAreAddedCorrectly() { - identity.addContext("Test"); - assertThat(identity.getContexts(), contains("Test")); - assertThat(identity.hasContext("Test"), is(true)); - } - - @Test - public void contextsAreRemovedCorrectly() { - identity.addContext("Test"); - identity.removeContext("Test"); - assertThat(identity.getContexts(), empty()); - assertThat(identity.hasContext("Test"), is(false)); - } - - @Test - public void contextsAreSetCorrectlyInBulk() { - identity.addContext("Test"); - identity.setContexts(asList("Test1", "Test2")); - assertThat(identity.getContexts(), containsInAnyOrder("Test1", "Test2")); - assertThat(identity.hasContext("Test"), is(false)); - assertThat(identity.hasContext("Test1"), is(true)); - assertThat(identity.hasContext("Test2"), is(true)); - } - - @Test - public void propertiesAreAddedCorrectly() { - identity.setProperty("Key", "Value"); - assertThat(identity.getProperties().size(), is(1)); - assertThat(identity.getProperties(), hasEntry("Key", "Value")); - assertThat(identity.getProperty("Key"), is("Value")); - } - - @Test - public void propertiesAreRemovedCorrectly() { - identity.setProperty("Key", "Value"); - identity.removeProperty("Key"); - assertThat(identity.getProperties(), is(Collections.emptyMap())); - assertThat(identity.getProperty("Key"), nullValue()); - } - - @Test - public void propertiesAreSetCorrectlyInBulk() { - identity.setProperty("Key", "Value"); - identity.setProperties(of("Key1", "Value1", "Key2", "Value2")); - assertThat(identity.getProperties().size(), is(2)); - assertThat(identity.getProperty("Key"), nullValue()); - assertThat(identity.getProperty("Key1"), is("Value1")); - assertThat(identity.getProperty("Key2"), is("Value2")); - } - - @Test - public void trustRelationshipsAreAddedCorrectly() { - OwnIdentity ownIdentity = mock(OwnIdentity.class); - Trust trust = mock(Trust.class); - identity.setTrust(ownIdentity, trust); - assertThat(identity.getTrust(ownIdentity), is(trust)); - } - - @Test - public void trustRelationshipsAreRemovedCorrectly() { - OwnIdentity ownIdentity = mock(OwnIdentity.class); - Trust trust = mock(Trust.class); - identity.setTrust(ownIdentity, trust); - identity.removeTrust(ownIdentity); - assertThat(identity.getTrust(ownIdentity), nullValue()); - } - - @Test - public void identitiesWithTheSameIdAreEqual() { - DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2"); - assertThat(identity2, is(identity)); - assertThat(identity, is(identity2)); - } - - @Test - public void twoEqualIdentitiesHaveTheSameHashCode() { - DefaultIdentity identity2 = new DefaultIdentity("Id", "Nickname2", "RequestURI2"); - assertThat(identity.hashCode(), is(identity2.hashCode())); - } - - @Test - public void nullDoesNotMatchAnIdentity() { - assertThat(identity, not(is((Object) null))); - } - - @Test - public void toStringContainsIdAndNickname() { - String identityString = identity.toString(); - assertThat(identityString, matchesRegex(".*\\bId\\b.*")); - assertThat(identityString, matchesRegex(".*\\bNickname\\b.*")); - } - -} diff --git a/src/test/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentityTest.java b/src/test/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentityTest.java deleted file mode 100644 index 138ced3..0000000 --- a/src/test/java/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentityTest.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Sone - DefaultOwnIdentityTest.java - Copyright © 2013–2019 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.sone.freenet.wot; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; - -import org.junit.Test; - -/** - * Unit test for {@link DefaultOwnIdentity}. - */ -public class DefaultOwnIdentityTest extends DefaultIdentityTest { - - @Override - protected DefaultIdentity createIdentity() { - return new DefaultOwnIdentity("Id", "Nickname", "RequestURI", "InsertURI"); - } - - @Test - public void ownIdentityCanBeCreated() { - assertThat(((OwnIdentity) identity).getInsertUri(), is("InsertURI")); - } - -} diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.kt new file mode 100644 index 0000000..1c12b33 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultIdentityTest.kt @@ -0,0 +1,142 @@ +/* + * Sone - DefaultIdentityTest.java - Copyright © 2013–2019 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.freenet.wot + +import com.google.common.collect.ImmutableMap.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.sone.test.Matchers.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.containsInAnyOrder +import org.hamcrest.Matchers.empty +import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.hasEntry +import org.hamcrest.Matchers.not +import org.hamcrest.Matchers.nullValue +import org.hamcrest.collection.IsIterableContainingInOrder.contains +import org.junit.* + +/** + * Unit test for [DefaultIdentity]. + */ +open class DefaultIdentityTest { + + protected open val identity = DefaultIdentity("Id", "Nickname", "RequestURI") + + @Test + fun `identity can be created`() { + assertThat(identity.id, equalTo("Id")) + assertThat(identity.nickname, equalTo("Nickname")) + assertThat(identity.requestUri, equalTo("RequestURI")) + assertThat(identity.contexts, empty()) + assertThat(identity.properties, equalTo(emptyMap())) + } + + @Test + fun `contexts are added correctly`() { + identity.addContext("Test") + assertThat(identity.contexts, contains("Test")) + assertThat(identity.hasContext("Test"), equalTo(true)) + } + + @Test + fun `contexts are removed correctly`() { + identity.addContext("Test") + identity.removeContext("Test") + assertThat(identity.contexts, empty()) + assertThat(identity.hasContext("Test"), equalTo(false)) + } + + @Test + fun `contexts are set correctly in bulk`() { + identity.addContext("Test") + identity.setContexts(listOf("Test1", "Test2")) + assertThat(identity.contexts, containsInAnyOrder("Test1", "Test2")) + assertThat(identity.hasContext("Test"), equalTo(false)) + assertThat(identity.hasContext("Test1"), equalTo(true)) + assertThat(identity.hasContext("Test2"), equalTo(true)) + } + + @Test + fun `properties are added correctly`() { + identity.setProperty("Key", "Value") + assertThat(identity.properties.size, equalTo(1)) + assertThat(identity.properties, hasEntry("Key", "Value")) + assertThat(identity.getProperty("Key"), equalTo("Value")) + } + + @Test + fun `properties are removed correctly`() { + identity.setProperty("Key", "Value") + identity.removeProperty("Key") + assertThat(identity.properties, equalTo(emptyMap())) + assertThat(identity.getProperty("Key"), nullValue()) + } + + @Test + fun `properties are set correctly in bulk`() { + identity.setProperty("Key", "Value") + identity.properties = of("Key1", "Value1", "Key2", "Value2") + assertThat(identity.properties.size, equalTo(2)) + assertThat(identity.getProperty("Key"), nullValue()) + assertThat(identity.getProperty("Key1"), equalTo("Value1")) + assertThat(identity.getProperty("Key2"), equalTo("Value2")) + } + + @Test + fun `trust relationships are added correctly`() { + val ownIdentity = mock() + val trust = mock() + identity.setTrust(ownIdentity, trust) + assertThat(identity.getTrust(ownIdentity), equalTo(trust)) + } + + @Test + fun `trust relationships are removed correctly`() { + val ownIdentity = mock() + val trust = mock() + identity.setTrust(ownIdentity, trust) + identity.removeTrust(ownIdentity) + assertThat(identity.getTrust(ownIdentity), nullValue()) + } + + @Test + fun `identities with the same id are equal`() { + val identity2 = DefaultIdentity("Id", "Nickname2", "RequestURI2") + assertThat(identity2, equalTo(identity)) + assertThat(identity, equalTo(identity2)) + } + + @Test + fun `two equal identities have the same hash code`() { + val identity2 = DefaultIdentity("Id", "Nickname2", "RequestURI2") + assertThat(identity.hashCode(), equalTo(identity2.hashCode())) + } + + @Test + fun `null does not match an identity`() { + assertThat(identity, not(equalTo(null as Any?))) + } + + @Test + fun `toString() contains id and nickname`() { + val identityString = identity.toString() + assertThat(identityString, matchesRegex(".*\\bId\\b.*")) + assertThat(identityString, matchesRegex(".*\\bNickname\\b.*")) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentityTest.kt b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentityTest.kt new file mode 100644 index 0000000..593b157 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/freenet/wot/DefaultOwnIdentityTest.kt @@ -0,0 +1,36 @@ +/* + * Sone - DefaultOwnIdentityTest.java - Copyright © 2013–2019 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.freenet.wot + +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* + +/** + * Unit test for [DefaultOwnIdentity]. + */ +class DefaultOwnIdentityTest : DefaultIdentityTest() { + + override val identity = DefaultOwnIdentity("Id", "Nickname", "RequestURI", "InsertURI") + + @Test + fun `own identity can be created`() { + assertThat((identity as OwnIdentity).insertUri, equalTo("InsertURI")) + } + +} -- 2.7.4