View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * There is a hardcoded dependency between this test and the SortableModel implementation that 
42   * happens to be used by ModelUtils.toCollectionModel().
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      // The paths come out in the order they are selected
81      assertEquals(_0, iter.next());
82      assertEquals(_011, iter.next());
83      assertEquals(_20, iter.next());
84      assertFalse(iter.hasNext());
85    }
86  
87    public void testInitialAddAll() throws IntrospectionException
88    {
89      RowKeySet pathSet = new RowKeySetTreeImpl(true);
90      TreeModel model = ChildPropertyTreeModelTest.createModel();
91      pathSet.setCollectionModel(model);
92  
93      _testInSet(pathSet, _0, _011, _2, _20);
94    }
95  
96    public void testAdd() throws IntrospectionException
97    {
98      TestPathSet pathSet = new TestPathSet();
99  
100     assertTrue(pathSet.add(_0));
101     assertTrue("contained", pathSet.contains(_0));
102     _testNotInSet(pathSet, _011, _2, _20);
103 
104     assertFalse(pathSet.add(_0));
105   }
106 
107   public void testRemove() throws IntrospectionException
108   {
109     TestPathSet pathSet = new TestPathSet();
110     pathSet.add(_2);
111     assertFalse(pathSet.remove(_0));
112     pathSet.add(_0);
113     assertTrue(pathSet.remove(_0));
114     _testNotInSet(pathSet, _0, _011, _20);
115     assertTrue("is contained", pathSet.contains(_2));
116   }
117 
118   public void testAddAll() throws IntrospectionException
119   {
120     TestPathSet pathSet = new TestPathSet();
121     TreeModel model = pathSet.getTreeModel();
122 
123     model.setRowKey(_2);
124     pathSet.addAll();
125 
126     _testNotInSet(pathSet, _0, _011);
127     _testInSet(pathSet, _2, _20);
128   }
129 
130   public void testAddAllCollection() throws IntrospectionException
131   {
132     RowKeySet set1 = new TestPathSet();
133     RowKeySet set2 = new TestPathSet();
134     _add(set1, _011, _2, _20);
135     set2.add(_0);
136 
137     set2.addAll(set1);
138     _testInSet(set1, _011, _2, _20);
139     _testNotInSet(set1, _0);
140     _testInSet(set2, _0, _011, _2, _20);
141 
142     // test with set1 having default=true:
143     set1 = new TestPathSet(true);
144     set2 = new TestPathSet();
145     set1.remove(_2);
146     set1.remove(_011);
147     set2.add(_2);
148 
149     set2.addAll(set1);
150     _testInSet(set2, _0, _2, _20);
151     _testNotInSet(set2, _011);
152 
153     // test with set1 having default=true on subtree:
154     set1 = new TestPathSet();
155     set2 = new TestPathSet();
156     set1.getCollectionModel().setRowKey(_2);
157     set1.addAll();
158 
159     set2.addAll(set1);
160     _testInSet(set2, _2, _20);
161     _testNotInSet(set2, _0, _011);
162   }
163 
164   public void testRemoveAllCollection() throws IntrospectionException
165   {
166     RowKeySet set1 = new TestPathSet();
167     RowKeySet set2 = new TestPathSet();
168     _add(set1, _011, _2, _20);
169     _add(set2, _0, _2, _20);
170 
171     set2.removeAll(set1);
172     _testInSet(set1, _011, _2, _20);
173     _testNotInSet(set1, _0);
174     _testInSet(set2, _0);
175     _testNotInSet(set2, _011, _2, _20);
176 
177     // test with set1 having default=true:
178     set1 = new TestPathSet(true);
179     set2 = new TestPathSet();
180     set1.remove(_2);
181     _add(set2, _011, _2);
182 
183     set2.removeAll(set1);
184     _testInSet(set2, _2);
185     _testNotInSet(set2, _011, _20);
186 
187     // test with set1 having default=true on subtree:
188     set1 = new TestPathSet();
189     set2 = new TestPathSet();
190     set1.getCollectionModel().setRowKey(_2);
191     set1.addAll();
192     _add(set2, _20, _011);
193 
194     set2.removeAll(set1);
195     _testInSet(set2, _011);
196     _testNotInSet(set2, _0, _2, _20);
197   }
198 
199   public void testClone() throws IntrospectionException, CloneNotSupportedException
200   {
201     RowKeySet pathSet = new RowKeySetTreeImpl();
202     TreeModel model = ChildPropertyTreeModelTest.createModel();
203     pathSet.setCollectionModel(model);
204     model.setRowKey(_2);
205     pathSet.addAll();
206     pathSet.add(_011);
207 
208     RowKeySet clone = pathSet.clone();
209     // mutate clone:
210     clone.remove(_2);
211 
212     // make sure original has not changed:
213     assertFalse(pathSet.contains(_0));
214     _testInSet(pathSet, _011, _2, _20);
215 
216     // verify clone:
217     _testNotInSet(clone, _0, _2);
218     _testInSet(clone, _011, _20);
219   }
220 
221   public void testSize() throws IntrospectionException
222   {
223     RowKeySet set = new TestPathSet();
224     assertEquals("size", 0, set.size());
225     _add(set, _2, _20, _0, _011);
226     assertEquals("size", 4, set.size());
227 
228     set = new RowKeySetTreeImpl(true);
229     TreeModel model = ChildPropertyTreeModelTest.createModel();
230     set.setCollectionModel(model);
231     assertEquals("addAll:size", 14, set.size());
232 
233     set.remove(_011);
234     assertEquals("addAll:size", 13, set.size());
235 
236     model.setRowKey(_011);
237     set.removeAll();
238     assertEquals("addAll:size", 10, set.size());
239 
240   }
241 
242   public void testClear() throws IntrospectionException
243   {
244     TestPathSet pathSet = new TestPathSet();
245     _add(pathSet, _2, _20, _0, _011);
246 
247     pathSet.clear();
248 
249     _testNotInSet(pathSet, _0, _011, _2, _20);
250   }
251 
252   public void testRemoveAll() throws IntrospectionException
253   {
254     TestPathSet pathSet = new TestPathSet();
255     TreeModel model = pathSet.getTreeModel();
256     _add(pathSet, _2, _20, _0, _011);
257 
258     model.setRowKey(_2);
259     pathSet.removeAll();
260 
261     _testInSet(pathSet, _0, _011);
262     _testNotInSet(pathSet, _2, _20);
263   }
264 
265   public void testSerialization()
266     throws IOException, ClassNotFoundException, IntrospectionException
267   {
268     TreeModel model = ChildPropertyTreeModelTest.createModel();
269     final byte[] bytes;
270     {
271       RowKeySet pathSet = new RowKeySetTreeImpl();
272       pathSet.setCollectionModel(model);
273       pathSet.add(_2);
274       ByteArrayOutputStream bos = new ByteArrayOutputStream();
275       ObjectOutputStream out = new ObjectOutputStream(bos);
276       out.writeObject(pathSet);
277       out.close();
278       bytes = bos.toByteArray();
279     }
280 
281     // we're doing a test here to warn the developer if the
282     // serialized size changes significantly. If the size changes are
283     // expected, then change the expected range for these tests:
284     assertTrue(bytes.length < 1000);
285     assertTrue(bytes.length > 615);
286 
287     ObjectInputStream in =
288       new ObjectInputStream(new ByteArrayInputStream(bytes));
289     RowKeySet pathSet = (RowKeySet) in.readObject();
290     in.close();
291     pathSet.setCollectionModel(model);
292 
293     // test state:
294     _testNotInSet(pathSet, _0, _011, _20);
295     assertTrue("is contained", pathSet.contains(_2));
296   }
297 
298   private void _testInSet(RowKeySet set, Object ... keys)
299   {
300     for(Object key:keys)
301     {
302       assertTrue("must contain key:"+key, set.contains(key));
303     }
304   }
305 
306   private void _testNotInSet(RowKeySet set, Object ... keys)
307   {
308     for(Object key:keys)
309     {
310       assertFalse("must not contain key:"+key, set.contains(key));
311     }
312   }
313 
314   private void _add(RowKeySet set, Object ... keys)
315   {
316     for(Object key:keys)
317     {
318       set.add(key);
319     }
320   }
321 
322   private static List<Object> _createPath(Object ... rowKeys)
323   {
324     return Collections.unmodifiableList(Arrays.asList(rowKeys));
325   }
326 
327   private static final class TestPathSet extends RowKeySetTreeImpl
328   {
329     public TestPathSet(boolean def) throws IntrospectionException
330     {
331       super(def);
332       _model = ChildPropertyTreeModelTest.createModel();
333       setCollectionModel(_model);
334     }
335 
336     public TestPathSet() throws IntrospectionException
337     {
338       this(false);
339     }
340 
341     public TreeModel getTreeModel()
342     {
343       return _model;
344     }
345 
346     private final TreeModel _model;
347   }
348 
349   private static final List<Object> _0 = _createPath(0);
350   private static final List<Object> _011 = _createPath(0, 1, 1);
351   private static final List<Object> _2 = _createPath(2);
352   private static final List<Object> _20 = _createPath(2, 0);
353 }