From: David ‘Bombe’ Roden Date: Tue, 4 Nov 2014 06:07:30 +0000 (+0100) Subject: Add unit test for identity accessor. X-Git-Tag: 0.9-rc1^2~3^2~55 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=57bcf86329bee09c54f643731c0d95fbaac0cfd1;p=Sone.git Add unit test for identity accessor. --- 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())); + } + +}