2 * Sone - CollectionAccessorTest.java - Copyright © 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 java.util.Arrays.asList;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.is;
23 import static org.hamcrest.Matchers.nullValue;
25 import java.util.Collection;
27 import net.pterodactylus.sone.data.Mocks;
28 import net.pterodactylus.sone.data.Sone;
30 import org.junit.Test;
33 * Unit test for {@link CollectionAccessor}.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public class CollectionAccessorTest {
39 private final Mocks mocks = new Mocks();
40 private final CollectionAccessor collectionAccessor = new CollectionAccessor();
43 public void soneNamesAreReturnedSorted() {
44 Sone sone1 = mocks.mockSone("Sone1").withName("Sone1").create();
45 Sone sone2 = mocks.mockSone("Sone2").withName("Sone2").create();
46 Sone sone3 = mocks.mockSone("Sone3").withName("Sone3").create();
47 Collection<Sone> sones = asList(sone3, sone1, sone2);
48 String soneNames = (String) collectionAccessor.get(null, sones, "soneNames");
49 assertThat(soneNames, is("Sone1, Sone2, Sone3"));
53 public void collectionsWithOtherObjectsUseOnlyTheSones() {
54 Sone sone1 = mocks.mockSone("Sone1").withName("Sone1").create();
55 Sone sone2 = mocks.mockSone("Sone2").withName("Sone2").create();
56 Sone sone3 = mocks.mockSone("Sone3").withName("Sone3").create();
57 Collection<Object> sones = asList(sone3, new Object(), sone1, sone2);
58 String soneNames = (String) collectionAccessor.get(null, sones, "soneNames");
59 assertThat(soneNames, is("Sone1, Sone2, Sone3"));
63 public void reflectionAccessorIsUsed() {
64 Collection<Object> objects = asList(new Object(), new Object());
65 int size = (Integer) collectionAccessor.get(null, objects, "size");
66 assertThat(size, is(objects.size()));
70 public void nullCollectionReturnsNull() {
71 assertThat(collectionAccessor.get(null, null, null), nullValue());