1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.model;
20
21 import java.beans.IntrospectionException;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.ObjectInputStream;
26 import java.io.ObjectOutputStream;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.Iterator;
30 import java.util.List;
31
32 import junit.framework.Test;
33 import junit.framework.TestSuite;
34
35 import org.apache.myfaces.trinidad.model.RowKeySet;
36 import org.apache.myfaces.trinidad.model.RowKeySetTreeImpl;
37 import org.apache.myfaces.trinidad.model.TreeModel;
38 import org.apache.shale.test.base.AbstractJsfTestCase;
39
40
41
42
43
44 public class RowKeySetTreeImplTest extends AbstractJsfTestCase
45 {
46 public RowKeySetTreeImplTest(String name)
47 {
48 super(name);
49 }
50
51 @Override
52 protected void setUp() throws Exception
53 {
54 super.setUp();
55 }
56
57 @Override
58 protected void tearDown() throws Exception
59 {
60 super.tearDown();
61 }
62
63 public static Test suite()
64 {
65 return new TestSuite(RowKeySetTreeImplTest.class);
66 }
67
68 public void testInitialState() throws IntrospectionException
69 {
70 TestPathSet pathSet = new TestPathSet();
71 _testNotInSet(pathSet, _0, _011, _2, _20);
72 }
73
74 public void testIterator() throws IntrospectionException
75 {
76 TestPathSet pathSet = new TestPathSet();
77 _add(pathSet, _0, _011, _20);
78
79 Iterator<Object> iter = pathSet.iterator();
80
81
82
83 assertEquals(_0, iter.next());
84 assertEquals(_011, iter.next());
85 assertEquals(_20, iter.next());
86 assertFalse(iter.hasNext());
87 }
88
89 public void testInitialAddAll() throws IntrospectionException
90 {
91 RowKeySet pathSet = new RowKeySetTreeImpl(true);
92 TreeModel model = ChildPropertyTreeModelTest.createModel();
93 pathSet.setCollectionModel(model);
94
95 _testInSet(pathSet, _0, _011, _2, _20);
96 }
97
98 public void testAdd() throws IntrospectionException
99 {
100 TestPathSet pathSet = new TestPathSet();
101
102 assertTrue(pathSet.add(_0));
103 assertTrue("contained", pathSet.contains(_0));
104 _testNotInSet(pathSet, _011, _2, _20);
105
106 assertFalse(pathSet.add(_0));
107 }
108
109 public void testRemove() throws IntrospectionException
110 {
111 TestPathSet pathSet = new TestPathSet();
112 pathSet.add(_2);
113 assertFalse(pathSet.remove(_0));
114 pathSet.add(_0);
115 assertTrue(pathSet.remove(_0));
116 _testNotInSet(pathSet, _0, _011, _20);
117 assertTrue("is contained", pathSet.contains(_2));
118 }
119
120 public void testAddAll() throws IntrospectionException
121 {
122 TestPathSet pathSet = new TestPathSet();
123 TreeModel model = pathSet.getTreeModel();
124
125 model.setRowKey(_2);
126 pathSet.addAll();
127
128 _testNotInSet(pathSet, _0, _011);
129 _testInSet(pathSet, _2, _20);
130 }
131
132 public void testAddAllCollection() throws IntrospectionException
133 {
134 RowKeySet set1 = new TestPathSet();
135 RowKeySet set2 = new TestPathSet();
136 _add(set1, _011, _2, _20);
137 set2.add(_0);
138
139 set2.addAll(set1);
140 _testInSet(set1, _011, _2, _20);
141 _testNotInSet(set1, _0);
142 _testInSet(set2, _0, _011, _2, _20);
143
144
145 set1 = new TestPathSet(true);
146 set2 = new TestPathSet();
147 set1.remove(_2);
148 set1.remove(_011);
149 set2.add(_2);
150
151 set2.addAll(set1);
152 _testInSet(set2, _0, _2, _20);
153 _testNotInSet(set2, _011);
154
155
156 set1 = new TestPathSet();
157 set2 = new TestPathSet();
158 set1.getCollectionModel().setRowKey(_2);
159 set1.addAll();
160
161 set2.addAll(set1);
162 _testInSet(set2, _2, _20);
163 _testNotInSet(set2, _0, _011);
164 }
165
166 public void testRemoveAllCollection() throws IntrospectionException
167 {
168 RowKeySet set1 = new TestPathSet();
169 RowKeySet set2 = new TestPathSet();
170 _add(set1, _011, _2, _20);
171 _add(set2, _0, _2, _20);
172
173 set2.removeAll(set1);
174 _testInSet(set1, _011, _2, _20);
175 _testNotInSet(set1, _0);
176 _testInSet(set2, _0);
177 _testNotInSet(set2, _011, _2, _20);
178
179
180 set1 = new TestPathSet(true);
181 set2 = new TestPathSet();
182 set1.remove(_2);
183 _add(set2, _011, _2);
184
185 set2.removeAll(set1);
186 _testInSet(set2, _2);
187 _testNotInSet(set2, _011, _20);
188
189
190 set1 = new TestPathSet();
191 set2 = new TestPathSet();
192 set1.getCollectionModel().setRowKey(_2);
193 set1.addAll();
194 _add(set2, _20, _011);
195
196 set2.removeAll(set1);
197 _testInSet(set2, _011);
198 _testNotInSet(set2, _0, _2, _20);
199 }
200
201 public void testClone() throws IntrospectionException, CloneNotSupportedException
202 {
203 RowKeySet pathSet = new RowKeySetTreeImpl();
204 TreeModel model = ChildPropertyTreeModelTest.createModel();
205 pathSet.setCollectionModel(model);
206 model.setRowKey(_2);
207 pathSet.addAll();
208 pathSet.add(_011);
209
210 RowKeySet clone = pathSet.clone();
211
212 clone.remove(_2);
213
214
215 assertFalse(pathSet.contains(_0));
216 _testInSet(pathSet, _011, _2, _20);
217
218
219 _testNotInSet(clone, _0, _2);
220 _testInSet(clone, _011, _20);
221 }
222
223 public void testSize() throws IntrospectionException
224 {
225 RowKeySet set = new TestPathSet();
226 assertEquals("size", 0, set.size());
227 _add(set, _2, _20, _0, _011);
228 assertEquals("size", 4, set.size());
229
230 set = new RowKeySetTreeImpl(true);
231 TreeModel model = ChildPropertyTreeModelTest.createModel();
232 set.setCollectionModel(model);
233 assertEquals("addAll:size", 14, set.size());
234
235 set.remove(_011);
236 assertEquals("addAll:size", 13, set.size());
237
238 model.setRowKey(_011);
239 set.removeAll();
240 assertEquals("addAll:size", 10, set.size());
241
242 }
243
244 public void testClear() throws IntrospectionException
245 {
246 TestPathSet pathSet = new TestPathSet();
247 _add(pathSet, _2, _20, _0, _011);
248
249 pathSet.clear();
250
251 _testNotInSet(pathSet, _0, _011, _2, _20);
252 }
253
254 public void testRemoveAll() throws IntrospectionException
255 {
256 TestPathSet pathSet = new TestPathSet();
257 TreeModel model = pathSet.getTreeModel();
258 _add(pathSet, _2, _20, _0, _011);
259
260 model.setRowKey(_2);
261 pathSet.removeAll();
262
263 _testInSet(pathSet, _0, _011);
264 _testNotInSet(pathSet, _2, _20);
265 }
266
267 public void testSerialization()
268 throws IOException, ClassNotFoundException, IntrospectionException
269 {
270 TreeModel model = ChildPropertyTreeModelTest.createModel();
271 final byte[] bytes;
272 {
273 RowKeySet pathSet = new RowKeySetTreeImpl();
274 pathSet.setCollectionModel(model);
275 pathSet.add(_2);
276 ByteArrayOutputStream bos = new ByteArrayOutputStream();
277 ObjectOutputStream out = new ObjectOutputStream(bos);
278 out.writeObject(pathSet);
279 out.close();
280 bytes = bos.toByteArray();
281 }
282
283
284
285
286 assertTrue(bytes.length < 1000);
287 assertTrue(bytes.length > 615);
288
289 ObjectInputStream in =
290 new ObjectInputStream(new ByteArrayInputStream(bytes));
291 RowKeySet pathSet = (RowKeySet) in.readObject();
292 in.close();
293 pathSet.setCollectionModel(model);
294
295
296 _testNotInSet(pathSet, _0, _011, _20);
297 assertTrue("is contained", pathSet.contains(_2));
298 }
299
300 private void _testInSet(RowKeySet set, Object ... keys)
301 {
302 for(Object key:keys)
303 {
304 assertTrue("must contain key:"+key, set.contains(key));
305 }
306 }
307
308 private void _testNotInSet(RowKeySet set, Object ... keys)
309 {
310 for(Object key:keys)
311 {
312 assertFalse("must not contain key:"+key, set.contains(key));
313 }
314 }
315
316 private void _add(RowKeySet set, Object ... keys)
317 {
318 for(Object key:keys)
319 {
320 set.add(key);
321 }
322 }
323
324 private static List<Object> _createPath(Object ... rowKeys)
325 {
326 return Collections.unmodifiableList(Arrays.asList(rowKeys));
327 }
328
329 private static final class TestPathSet extends RowKeySetTreeImpl
330 {
331 public TestPathSet(boolean def) throws IntrospectionException
332 {
333 super(def);
334 _model = ChildPropertyTreeModelTest.createModel();
335 setCollectionModel(_model);
336 }
337
338 public TestPathSet() throws IntrospectionException
339 {
340 this(false);
341 }
342
343 public TreeModel getTreeModel()
344 {
345 return _model;
346 }
347
348 private final TreeModel _model;
349 }
350
351 private static final List<Object> _0 = _createPath(0);
352 private static final List<Object> _011 = _createPath(0, 1, 1);
353 private static final List<Object> _2 = _createPath(2);
354 private static final List<Object> _20 = _createPath(2, 0);
355 }