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
45
46
47
48
49 public class ExtendedListTest {
50
51 private ExtendedArrayList<Integer> m_z, m_a, m_b, m_c;
52
53 private ExtendedArrayList<String> m_n;
54
55 private TransformedList<Integer, String> m_s;
56
57
58 @Before
59 public void setUp() {
60 m_z = new ExtendedArrayList<Integer>(1, 2, 3, 4, 5);
61 m_a = new ExtendedArrayList<Integer>(1, 2, 3, 4, 6, 5);
62 m_b = new ExtendedArrayList<Integer>(1, 2, 3, 4, 5, 6);
63 m_c = new ExtendedArrayList<Integer>(2, 4, 6);
64 m_s = m_z.mapped(new Function<Integer, String>() {
65 public String apply(Integer d) {
66 return d.toString();
67 }
68 });
69 m_n = new ExtendedArrayList<String>("4", "2");
70 }
71
72
73 @Test
74 public void testEquals() {
75 assertFalse(m_z.equals(m_a));
76 assertFalse(m_z.equals(m_b));
77 assertFalse(m_a.equals(m_b));
78 }
79
80
81 @Test
82 public void testRemove() {
83 m_a.remove((Integer) 6);
84 m_b.remove((Integer) 6);
85 assertEquals(m_a, m_b);
86 }
87
88
89 @Test
90 public void testAdd() {
91 m_z.add(6);
92 assertEquals(m_z, m_b);
93 }
94
95
96 @Test
97 public void testOrderLike() {
98 try {
99 m_z.orderLike(m_c);
100 fail("orderLike is required to throw an exception when provided "
101 + "with an invalied example, but did not.");
102 } catch (NoSuchElementException e) { }
103 m_b = new ExtendedArrayList<Integer>(m_a);
104 m_a.orderLike(m_c);
105 assertEquals(m_a.subList(0, 3), m_c);
106 assertTrue(m_b.containsAll(m_a));
107 }
108
109
110 @Test
111 public void testTransformedOrderLike() {
112 m_s.orderLike(m_n);
113 assertEquals(
114 "orderLike is required to propagate to the backing list",
115 m_z.subList(0, 2),
116 new ExtendedArrayList<Integer>(4, 2)
117 );
118 }
119
120
121 @Test
122 public void testFiltered() {
123 assertEquals(
124 m_a.filtered(new Filter<Integer>() {
125 public boolean accepts(Integer i) {
126 return i % 2 == 0;
127 }
128 }), m_c
129 );
130 }
131 }
132
133
134