1 package net.pterodactylus.sone.template;
3 import static java.util.Arrays.asList;
4 import static org.hamcrest.MatcherAssert.assertThat;
5 import static org.hamcrest.Matchers.notNullValue;
6 import static org.hamcrest.Matchers.sameInstance;
7 import static org.mockito.Mockito.mock;
8 import static org.mockito.Mockito.when;
10 import java.util.HashSet;
13 import net.pterodactylus.sone.core.Core;
14 import net.pterodactylus.sone.freenet.wot.Identity;
15 import net.pterodactylus.sone.freenet.wot.IdentityManager;
16 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
17 import net.pterodactylus.sone.test.GuiceKt;
19 import com.google.inject.Guice;
20 import com.google.inject.Injector;
21 import org.hamcrest.Matchers;
22 import org.junit.Before;
23 import org.junit.Test;
26 * Unit test for {@link IdentityAccessor}.
28 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
30 public class IdentityAccessorTest {
32 private static final String TEST_ID =
33 "LrNQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g";
34 private static final String TEST_ID_WITH_CHANGED_LETTER =
35 "LrMQbyBBZW-7pHqChtp9lfPA7eXFPW~FLbJ2WrvEx5g";
36 private final Core core = mock(Core.class);
37 private final IdentityAccessor accessor = new IdentityAccessor(core);
38 private final IdentityManager identityManager =
39 mock(IdentityManager.class);
40 private final OwnIdentity identity = mock(OwnIdentity.class);
43 public void setupCore() {
44 when(core.getIdentityManager()).thenReturn(identityManager);
48 public void setupIdentity() {
49 setupIdentity(identity, TEST_ID, "Test");
52 private void setupIdentity(Identity identity, String id,
54 when(identity.getId()).thenReturn(id);
55 when(identity.getNickname()).thenReturn(nickname);
58 private void serveIdentities(Set<OwnIdentity> identities) {
59 when(identityManager.getAllOwnIdentities()).thenReturn(identities);
63 public void accessorReturnsTheCorrectlyAbbreviatedNickname() {
64 OwnIdentity ownIdentity = mock(OwnIdentity.class);
65 setupIdentity(ownIdentity, TEST_ID_WITH_CHANGED_LETTER, "Test");
66 serveIdentities(new HashSet(asList(identity, ownIdentity)));
67 assertThat(accessor.get(null, identity, "uniqueNickname"),
68 Matchers.<Object>is("Test@LrN"));
72 public void accessorComparesTheFullLengthIfNecessary() {
73 OwnIdentity ownIdentity = mock(OwnIdentity.class);
74 setupIdentity(ownIdentity, TEST_ID, "Test");
75 serveIdentities(new HashSet(asList(identity, ownIdentity)));
76 assertThat(accessor.get(null, identity, "uniqueNickname"),
77 Matchers.<Object>is("Test@" + TEST_ID));
81 public void reflectionAccessorIsUsedForOtherMembers() {
82 assertThat(accessor.get(null, identity, "hashCode"),
83 Matchers.<Object>is(identity.hashCode()));
87 public void accessorCanBeCreatedByGuice() {
88 Injector injector = Guice.createInjector(
89 GuiceKt.supply(Core.class).byInstance(mock(Core.class))
91 assertThat(injector.getInstance(IdentityAccessor.class), notNullValue());
95 public void accessorIsCreatedAsSingleton() {
96 Injector injector = Guice.createInjector(
97 GuiceKt.supply(Core.class).byInstance(mock(Core.class))
99 IdentityAccessor firstAccessor = injector.getInstance(IdentityAccessor.class);
100 IdentityAccessor secondAccessor = injector.getInstance(IdentityAccessor.class);
101 assertThat(firstAccessor, sameInstance(secondAccessor));