From 57bcf86329bee09c54f643731c0d95fbaac0cfd1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 4 Nov 2014 07:07:30 +0100 Subject: [PATCH] Add unit test for identity accessor. --- .../sone/template/IdentityAccessorTest.java | 81 ++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java diff --git a/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java b/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java new file mode 100644 index 0000000..33149e0 --- /dev/null +++ b/src/test/java/net/pterodactylus/sone/template/IdentityAccessorTest.java @@ -0,0 +1,81 @@ +package net.pterodactylus.sone.template; + +import static java.util.Arrays.asList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.util.HashSet; +import java.util.Set; + +import net.pterodactylus.sone.core.Core; +import net.pterodactylus.sone.freenet.wot.Identity; +import net.pterodactylus.sone.freenet.wot.IdentityManager; +import net.pterodactylus.sone.freenet.wot.OwnIdentity; + +import org.hamcrest.Matchers; +import org.junit.Before; +import org.junit.Test; + +/** + * Unit test for {@link IdentityAccessor}. + * + * @author David ‘Bombe’ Roden + */ +public class IdentityAccessorTest { + + private static final String TEST_ID = + "LrNQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g"; + private static final String TEST_ID_WITH_CHANGED_LETTER = + "LrMQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g"; + private final Core core = mock(Core.class); + private final IdentityAccessor accessor = new IdentityAccessor(core); + private final IdentityManager identityManager = + mock(IdentityManager.class); + private final OwnIdentity identity = mock(OwnIdentity.class); + + @Before + public void setupCore() { + when(core.getIdentityManager()).thenReturn(identityManager); + } + + @Before + public void setupIdentity() { + setupIdentity(identity, TEST_ID, "Test"); + } + + private void setupIdentity(Identity identity, String id, + String nickname) { + when(identity.getId()).thenReturn(id); + when(identity.getNickname()).thenReturn(nickname); + } + + private void serveIdentities(Set identities) { + when(identityManager.getAllOwnIdentities()).thenReturn(identities); + } + + @Test + public void accessorReturnsTheCorrectlyAbbreviatedNickname() { + OwnIdentity ownIdentity = mock(OwnIdentity.class); + setupIdentity(ownIdentity, TEST_ID_WITH_CHANGED_LETTER, "Test"); + serveIdentities(new HashSet(asList(identity, ownIdentity))); + assertThat(accessor.get(null, identity, "uniqueNickname"), + Matchers.is("Test@LrN")); + } + + @Test + public void accessorComparesTheFullLengthIfNecessary() { + OwnIdentity ownIdentity = mock(OwnIdentity.class); + setupIdentity(ownIdentity, TEST_ID, "Test"); + serveIdentities(new HashSet(asList(identity, ownIdentity))); + assertThat(accessor.get(null, identity, "uniqueNickname"), + Matchers.is("Test@" + TEST_ID)); + } + + @Test + public void reflectionAccessorIsUsedForOtherMembers() { + assertThat(accessor.get(null, identity, "hashCode"), + Matchers.is(identity.hashCode())); + } + +} -- 2.7.4