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  
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.List;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.myfaces.trinidad.model.ChildPropertyTreeModel;
31  import org.apache.myfaces.trinidad.model.TreeModel;
32  import org.apache.shale.test.base.AbstractJsfTestCase;
33  
34  public class ChildPropertyTreeModelTest extends AbstractJsfTestCase
35  {
36    public ChildPropertyTreeModelTest(String name)
37    {
38      super(name);
39    }
40  
41    @Override
42    protected void setUp() throws Exception
43    {
44      super.setUp();
45    }
46  
47    @Override
48    protected void tearDown() throws Exception
49    {
50      super.tearDown();
51    }
52    public static Test suite()
53    {
54      return new TestSuite(ChildPropertyTreeModelTest.class);
55    }
56    
57    public void testInitialState() throws IntrospectionException
58    {
59      TreeModel model = createModel();
60      _testTree(model, _ROOTS);
61    }
62  
63    private void _followPath(TreeModel model, int[] path)
64    {
65      model.setRowKey(null);
66      int lastIndex = path.length - 1;
67      for(int i=0; i<lastIndex; i++)
68      {
69        model.setRowIndex(path[i]);
70        model.enterContainer();
71      }
72      model.setRowIndex(path[lastIndex]);
73    }
74    
75    public void testGetSetRowKey() throws IntrospectionException
76    {
77      TreeModel model = createModel();
78      
79      Bean root = _ROOTS.get(0);
80      Bean sub = root.getKids().get(1);
81  
82      int[] testPath = {0, 1};
83      _followPath(model, testPath);
84      Object key = model.getRowKey();
85      model.setRowKey(null);
86      _testTree(model, _ROOTS);
87  
88      model.setRowKey(key);
89      _testTree(model, sub);
90    }
91    
92    public void testGetContainerRowKey() throws IntrospectionException
93    {
94      TreeModel model = createModel();
95      int[] testPath1 = {0, 1};
96      _followPath(model, testPath1);
97      Object path1 = model.getRowKey();
98  
99      int[] testPath2 = {0, 1, 1};
100     _followPath(model, testPath2);
101     Object path2 = model.getRowKey();
102         
103     model.setRowKey(null);
104     assertEquals("getContainerRowKey(key)", path1, model.getContainerRowKey(path2));
105   }
106 
107   public void testGetDepth() throws IntrospectionException
108   {
109     TreeModel model = createModel();
110     int[] testPath1 = {0, 1};
111     _followPath(model, testPath1);
112     assertEquals("getDepth", testPath1.length - 1, model.getDepth());
113     Object path1 = model.getRowKey();
114 
115     int[] testPath2 = {0, 1, 1};
116     _followPath(model, testPath2);
117     assertEquals("getDepth", testPath2.length - 1, model.getDepth());
118     Object path2 = model.getRowKey();
119         
120     model.setRowKey(null);
121     assertEquals("getDepth(key)", testPath1.length - 1, model.getDepth(path1));
122     assertEquals("getDepth(key)", testPath2.length - 1, model.getDepth(path2));
123   }
124 
125   /**
126    * Tests getRowData, isContainer, enterContainer, exitContainer, getRowCount,
127    * setRowIndex, getRowIndex, getContainerRowKey
128    */
129   private void _testTree(TreeModel model, List<Bean> data)
130   {
131     int sz = data.size();
132     assertEquals("rowCount", sz, model.getRowCount());
133     assertEquals("initial rowIndex", -1, model.getRowIndex());
134 
135     if (sz > 0)
136     {
137       int oldIndex = model.getRowIndex();
138       for(int i=0; i<sz; i++)
139       {
140         Bean child = data.get(i);
141         model.setRowIndex(i);
142         assertEquals("rowIndex before enterContainer", i, model.getRowIndex());
143         _testTree(model, child);
144         assertEquals("rowIndex after exitContainer", i, model.getRowIndex());
145       }
146       model.setRowIndex(oldIndex);
147     }
148   }
149 
150   /**
151    * Tests getRowData, isContainer, enterContainer, exitContainer, getRowCount,
152    * setRowIndex, getRowIndex, getContainerRowKey
153    */
154   private void _testTree(TreeModel model, Bean bean)
155   {
156     assertEquals("rowData", bean, model.getRowData());
157     
158     List<Bean> kids = bean.getKids();
159     boolean hasChildren = (kids != null);
160     assertEquals("isContainer", hasChildren, model.isContainer());
161     
162     if (hasChildren)
163     {
164       Object parentKey = model.getRowKey();
165       model.enterContainer();
166       assertEquals("getContainerRowKey", parentKey, model.getContainerRowKey());
167       
168       _testTree(model, kids);
169       model.exitContainer();
170       assertEquals("rowData after exit", bean, model.getRowData());
171       assertEquals("isContainer after exit", hasChildren, model.isContainer());
172     }
173   }
174 
175   public static TreeModel createModel() throws IntrospectionException
176   {
177     TreeModel model = new ChildPropertyTreeModel(_ROOTS, "kids");
178     return model;
179   }
180   
181   private static final List<Bean> _ROOTS = _createTree();
182   
183   private static List<Bean> _createTree()
184   {
185     List<Bean> roots = new ArrayList<Bean>(3);
186 
187     Bean root, sub, subsub;
188 
189     roots.add(root = new Bean()); // path = 0
190       root.addKid(new Bean());    // path = 0,0
191       root.addKid(sub = new Bean()); // path = 0,1
192         sub.addKid(new Bean());
193         sub.addKid(subsub = new Bean()); // path=0,1,1
194           subsub.addKid(new Bean());
195           subsub.addKid(new Bean());
196           subsub.addKid(new Bean());
197         sub.addKid(new Bean());
198   
199         
200     roots.add(new Bean()); // path = 1
201 
202     roots.add(root = new Bean());
203       root.addKid(sub = new Bean()); // path = 2,0
204         sub.addKid(new Bean());
205         sub.addKid(new Bean());
206 
207     return Collections.unmodifiableList(roots);
208   }
209   
210   
211   public static final class Bean
212   {
213     public List<Bean> getKids()
214     {
215       return _kids;
216     }
217     
218     protected void addKid(Bean kid)
219     {
220       if (_kids == null)
221       {
222         _kids = new ArrayList<Bean>(3);
223       }
224 
225       _kids.add(kid);
226     }
227     
228     private List<Bean> _kids = null;
229   }
230 }