Add unit test for CollectionAccessor.
[Sone.git] / src / test / java / net / pterodactylus / sone / template / CollectionAccessorTest.java
1 /*
2  * Sone - CollectionAccessorTest.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.template;
19
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;
24
25 import java.util.Collection;
26
27 import net.pterodactylus.sone.data.Mocks;
28 import net.pterodactylus.sone.data.Sone;
29
30 import org.junit.Test;
31
32 /**
33  * Unit test for {@link CollectionAccessor}.
34  *
35  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
36  */
37 public class CollectionAccessorTest {
38
39         private final Mocks mocks = new Mocks();
40         private final CollectionAccessor collectionAccessor = new CollectionAccessor();
41
42         @Test
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"));
50         }
51
52         @Test
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"));
60         }
61
62         @Test
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()));
67         }
68
69         @Test
70         public void nullCollectionReturnsNull() {
71                 assertThat(collectionAccessor.get(null, null, null), nullValue());
72         }
73
74 }