1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package ch.elca.el4j.tests.util.collections;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 import static org.junit.Assert.fail;
23
24 import java.util.NoSuchElementException;
25
26 import org.junit.Before;
27 import org.junit.Test;
28
29 import ch.elca.el4j.util.collections.TransformedList;
30 import ch.elca.el4j.util.collections.helpers.Filter;
31 import ch.elca.el4j.util.collections.helpers.Function;
32 import ch.elca.el4j.util.collections.impl.ExtendedArrayList;
33
34
35
36
37
38
39
40
41
42
43
44 public class ExtendedListTest {
45
46 private ExtendedArrayList<Integer> m_z, m_a, m_b, m_c;
47
48 private ExtendedArrayList<String> m_n;
49
50 private TransformedList<Integer, String> m_s;
51
52
53 @Before
54 public void setUp() {
55 m_z = new ExtendedArrayList<Integer>(1, 2, 3, 4, 5);
56 m_a = new ExtendedArrayList<Integer>(1, 2, 3, 4, 6, 5);
57 m_b = new ExtendedArrayList<Integer>(1, 2, 3, 4, 5, 6);
58 m_c = new ExtendedArrayList<Integer>(2, 4, 6);
59 m_s = m_z.mapped(new Function<Integer, String>() {
60 public String apply(Integer d) {
61 return d.toString();
62 }
63 });
64 m_n = new ExtendedArrayList<String>("4", "2");
65 }
66
67
68 @Test
69 public void testEquals() {
70 assertFalse(m_z.equals(m_a));
71 assertFalse(m_z.equals(m_b));
72 assertFalse(m_a.equals(m_b));
73 }
74
75
76 @Test
77 public void testRemove() {
78 m_a.remove((Integer) 6);
79 m_b.remove((Integer) 6);
80 assertEquals(m_a, m_b);
81 }
82
83
84 @Test
85 public void testAdd() {
86 m_z.add(6);
87 assertEquals(m_z, m_b);
88 }
89
90
91 @Test
92 public void testOrderLike() {
93 try {
94 m_z.orderLike(m_c);
95 fail("orderLike is required to throw an exception when provided "
96 + "with an invalied example, but did not.");
97 } catch (NoSuchElementException e) { }
98 m_b = new ExtendedArrayList<Integer>(m_a);
99 m_a.orderLike(m_c);
100 assertEquals(m_a.subList(0, 3), m_c);
101 assertTrue(m_b.containsAll(m_a));
102 }
103
104
105 @Test
106 public void testTransformedOrderLike() {
107 m_s.orderLike(m_n);
108 assertEquals(
109 "orderLike is required to propagate to the backing list",
110 m_z.subList(0, 2),
111 new ExtendedArrayList<Integer>(4, 2)
112 );
113 }
114
115
116 @Test
117 public void testFiltered() {
118 assertEquals(
119 m_a.filtered(new Filter<Integer>() {
120 public boolean accepts(Integer i) {
121 return i % 2 == 0;
122 }
123 }), m_c
124 );
125 }
126 }
127
128
129