1 package net.pterodactylus.sone.template;
3 import static java.util.Arrays.asList;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.when;
8 import java.util.HashSet;
11 import net.pterodactylus.sone.core.Core;
12 import net.pterodactylus.sone.freenet.wot.Identity;
13 import net.pterodactylus.sone.freenet.wot.IdentityManager;
14 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
16 import org.hamcrest.Matchers;
17 import org.junit.Before;
18 import org.junit.Test;
21 * Unit test for {@link IdentityAccessor}.
23 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
25 public class IdentityAccessorTest {
27 private static final String TEST_ID =
28 "LrNQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g";
29 private static final String TEST_ID_WITH_CHANGED_LETTER =
30 "LrMQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g";
31 private final Core core = mock(Core.class);
32 private final IdentityAccessor accessor = new IdentityAccessor(core);
33 private final IdentityManager identityManager =
34 mock(IdentityManager.class);
35 private final OwnIdentity identity = mock(OwnIdentity.class);
38 public void setupCore() {
39 when(core.getIdentityManager()).thenReturn(identityManager);
43 public void setupIdentity() {
44 setupIdentity(identity, TEST_ID, "Test");
47 private void setupIdentity(Identity identity, String id,
49 when(identity.getId()).thenReturn(id);
50 when(identity.getNickname()).thenReturn(nickname);
53 private void serveIdentities(Set<OwnIdentity> identities) {
54 when(identityManager.getAllOwnIdentities()).thenReturn(identities);
58 public void accessorReturnsTheCorrectlyAbbreviatedNickname() {
59 OwnIdentity ownIdentity = mock(OwnIdentity.class);
60 setupIdentity(ownIdentity, TEST_ID_WITH_CHANGED_LETTER, "Test");
61 serveIdentities(new HashSet(asList(identity, ownIdentity)));
62 assertThat(accessor.get(null, identity, "uniqueNickname"),
63 Matchers.<Object>is("Test@LrN"));
67 public void accessorComparesTheFullLengthIfNecessary() {
68 OwnIdentity ownIdentity = mock(OwnIdentity.class);
69 setupIdentity(ownIdentity, TEST_ID, "Test");
70 serveIdentities(new HashSet(asList(identity, ownIdentity)));
71 assertThat(accessor.get(null, identity, "uniqueNickname"),
72 Matchers.<Object>is("Test@" + TEST_ID));
76 public void reflectionAccessorIsUsedForOtherMembers() {
77 assertThat(accessor.get(null, identity, "hashCode"),
78 Matchers.<Object>is(identity.hashCode()));