2 * Sone - IdentityAccessorTest.java - Copyright © 2010–2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.template;
20 import static com.google.common.collect.Sets.newHashSet;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.is;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
26 import net.pterodactylus.sone.data.Mocks;
27 import net.pterodactylus.sone.freenet.wot.DefaultOwnIdentity;
28 import net.pterodactylus.sone.freenet.wot.IdentityManager;
29 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
31 import org.junit.Before;
32 import org.junit.Test;
35 * Unit test for {@link IdentityAccessor}.
37 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
39 public class IdentityAccessorTest {
41 private final Mocks mocks = new Mocks();
42 private final IdentityAccessor identityAccessor = new IdentityAccessor(mocks.core);
46 IdentityManager identityManager = mock(IdentityManager.class);
47 when(mocks.core.getIdentityManager()).thenReturn(identityManager);
51 public void uniqueNicknameWithoutIdIsFound() {
52 when(mocks.core.getIdentityManager().getAllOwnIdentities()).thenReturn(newHashSet(
53 (OwnIdentity) new DefaultOwnIdentity("Test", "Test", "R", "I")
55 OwnIdentity ownIdentity = new DefaultOwnIdentity("Unique", "Unique", "R", "I");
56 String uniqueNickname = (String) identityAccessor.get(null, ownIdentity, "uniqueNickname");
57 assertThat(uniqueNickname, is("Unique"));
61 public void uniqueNicknameWithIdIsFound() {
62 OwnIdentity ownIdentity = new DefaultOwnIdentity("Unique", "Unique", "R", "I");
63 when(mocks.core.getIdentityManager().getAllOwnIdentities()).thenReturn(newHashSet(
65 new DefaultOwnIdentity("Unequivocal", "Unique", "R", "I")
67 String uniqueNickname = (String) identityAccessor.get(null, ownIdentity, "uniqueNickname");
68 assertThat(uniqueNickname, is("Unique@Uni"));
72 public void reflectionAccessorIsUsed() {
73 OwnIdentity ownIdentity = new DefaultOwnIdentity("Unique", "Unique", "R", "I");
74 String id = (String) identityAccessor.get(null, ownIdentity, "id");
75 assertThat(id, is(ownIdentity.getId()));