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.component.html.ext;
20  
21  import java.io.IOException;
22  import java.io.Serializable;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.HashSet;
26  import java.util.List;
27  
28  import javax.faces.component.UIColumn;
29  import javax.faces.component.UIComponent;
30  import javax.faces.component.UIData;
31  import javax.faces.component.UIInput;
32  import javax.faces.context.FacesContext;
33  import javax.faces.el.ValueBinding;
34  import javax.faces.model.ListDataModel;
35  
36  import junit.framework.Test;
37  
38  import org.apache.myfaces.test.AbstractTomahawkViewControllerTestCase;
39  import org.apache.myfaces.test.utils.TestUtils;
40  
41  /**
42   * @author Mathias Br´┐Żkelmann (latest modification by $Author: paulsp $)
43   * @version $Revision: 491829 $ $Date: 2007-01-02 11:13:29 -0500 (mar, 02 ene 2007) $
44   */
45  public class HtmlDataTableTest extends AbstractTomahawkViewControllerTestCase
46  {
47  
48      private HtmlDataTable _dataTable;
49  
50      public HtmlDataTableTest(String name)
51      {
52          super(name);
53      }
54  
55      /*
56      public static Test suite()
57      {
58          return null; // keep this method or maven won't run it
59      }*/
60  
61      protected void setUp() throws Exception
62      {
63          super.setUp();
64          _dataTable = new HtmlDataTable();
65      }
66  
67      protected void tearDown() throws Exception
68      {
69          super.tearDown();
70          _dataTable = null;
71      }
72  
73      /*
74       * Test method for
75       * 'org.apache.myfaces.component.html.ext.HtmlDataTable.getClientId(FacesContext)'
76       */
77      public void testGetClientIdFacesContext()
78      {
79          assertEquals(-1, _dataTable.getRowIndex());
80          String baseClientId = _dataTable.getClientId(facesContext);
81          assertNotNull(baseClientId);
82          Collection rowClientIds = new HashSet();
83          for (int i = 0; i < 10; i++)
84          {
85              _dataTable.setRowIndex(i);
86              assertTrue("Duplicate client id while iterating rows", rowClientIds
87                      .add(_dataTable.getClientId(facesContext)));
88          }
89          _dataTable.setRowIndex(-1);
90          assertEquals(baseClientId, _dataTable.getClientId(facesContext));
91      }
92  
93      /*
94       * Test method for
95       * 'org.apache.myfaces.component.html.ext.HtmlDataTable.findComponent(String clientId)'
96       */
97      public void testFindComponent()
98      {
99          UIInput input = createInputInTree(facesContext);
100 
101         UIData data = (UIData) input.getParent().getParent();
102 
103         UIComponent comp = data.findComponent(":data:1:input");
104 
105         assertTrue(comp instanceof UIInput);
106 
107         UIInput uiInput = (UIInput) comp;
108 
109         assertEquals(uiInput.getValue(), "test2");
110 
111     }
112 
113     /**
114      * Verify component renders with the default renderer.
115      */
116     public void testDefaultRenderer()
117     {
118         // Define the component
119         UIData component = new HtmlDataTable();
120         component.setId("TestComponent");
121 
122         // Add rows to the table
123         List rows = new ArrayList();
124         rows.add(new TestData("test1", "test1"));
125         rows.add(new TestData("test2", "test2"));
126         rows.add(new TestData("test3", "test3"));
127         component.setValue(new ListDataModel(rows));
128 
129         // Render the component
130         try
131         {
132             TestUtils.renderComponent(facesContext, component);
133         }
134         catch (IOException e)
135         {
136             fail(e.getMessage());
137         }
138 
139         // Verify component was rendered
140         assertIdExists(component.getId());
141     }
142 
143     private UIInput createInputInTree(FacesContext context)
144     {
145 
146         UIData uiData = new HtmlDataTable();
147         uiData.setId("data");
148 
149         List li = new ArrayList();
150         li.add(new TestData("test1", "test1"));
151         li.add(new TestData("test2", "test2"));
152         li.add(new TestData("test3", "test3"));
153 
154         uiData.setValue(new ListDataModel(li));
155         uiData.setVar("detail");
156 
157         UIColumn column = new UIColumn();
158 
159         uiData.getChildren().add(column);
160 
161         UIInput input = new UIInput();
162         input.setId("input");
163         input.setValueBinding("value",
164                 createValueBinding("#{detail.description}"));
165 
166         column.getChildren().add(input);
167 
168         facesContext.getViewRoot().getChildren().add(uiData);
169 
170         return input;
171     }
172 
173     private ValueBinding createValueBinding(String expr)
174     {
175         return facesContext.getApplication().createValueBinding(expr);
176     }
177 
178     public static class TestData implements Serializable
179     {
180         private String _description;
181         private String _value;
182 
183         public TestData()
184         {
185 
186         }
187 
188         TestData(String description, String value)
189         {
190             _description = description;
191             _value = value;
192         }
193 
194         public String getDescription()
195         {
196             return _description;
197         }
198 
199         public void setDescription(String description)
200         {
201             _description = description;
202         }
203 
204         public String getValue()
205         {
206             return _value;
207         }
208 
209         public void setValue(String value)
210         {
211             _value = value;
212         }
213     }
214 
215 }