Add list accessor that can return random elements
[Sone.git] / src / test / java / net / pterodactylus / sone / template / ListAccessorTest.java
1 package net.pterodactylus.sone.template;
2
3 import static org.hamcrest.MatcherAssert.assertThat;
4 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
5 import static org.hamcrest.Matchers.nullValue;
6 import static org.hamcrest.Matchers.sameInstance;
7
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.List;
11
12 import net.pterodactylus.util.template.Accessor;
13
14 import org.junit.Test;
15
16 /**
17  * Unit test for {@link ListAccessorTest}.
18  *
19  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
20  */
21 public class ListAccessorTest {
22
23         private final Accessor accessor = new ListAccessor();
24
25         @Test
26         public void gettingARandomElementFromAnEmptyListReturnsNull() {
27                 assertThat(accessor.get(null, Collections.emptyList(), "random"), nullValue());
28         }
29
30         @Test
31         public void gettingARandomElementFromAListOfOneWillReturnTheOneElement() {
32                 Object object = new Object();
33                 assertThat(accessor.get(null, Arrays.asList(object), "random"), sameInstance(object));
34         }
35
36         @Test
37         public void gettingRandomElementsFromAListTwoElementsWillReturnBothWithSomeProportion() {
38                 Object first = new Object();
39                 Object second = new Object();
40                 List<?> objects = Arrays.asList(first, second);
41                 int gotFirst = 0;
42                 for (int i = 0; i < 10000; i++) {
43                         if (accessor.get(null, objects, "random") == first) {
44                                 gotFirst++;
45                         }
46                 }
47                 assertThat(gotFirst, greaterThanOrEqualTo(4000));
48         }
49
50 }