1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
43
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
57
58
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
75
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
95
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
115
116 public void testDefaultRenderer()
117 {
118
119 UIData component = new HtmlDataTable();
120 component.setId("TestComponent");
121
122
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
130 try
131 {
132 TestUtils.renderComponent(facesContext, component);
133 }
134 catch (IOException e)
135 {
136 fail(e.getMessage());
137 }
138
139
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 }