1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.util;
20
21 import junit.framework.TestCase;
22
23 import javax.faces.component.NamingContainer;
24 import javax.faces.component.UIComponent;
25 import javax.faces.component.UIForm;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.apache.myfaces.trinidad.component.UIXCommand;
31 import org.apache.myfaces.trinidad.component.UIXInput;
32 import org.apache.myfaces.trinidad.component.UIXPanel;
33 import org.apache.myfaces.trinidad.component.UIXTable;
34
35 public class FindRelativeComponentTest extends TestCase
36 {
37 public static final Test suite()
38 {
39 return new TestSuite(FindRelativeComponentTest.class);
40 }
41
42 public static void main(String[] args) throws Throwable
43 {
44 junit.textui.TestRunner.run(suite());
45 }
46
47 public FindRelativeComponentTest(
48 String testName)
49 {
50 super(testName);
51 }
52
53 static private class TestNamingContainer extends UIXPanel
54 implements NamingContainer
55 {
56
57 }
58
59
60
61 @SuppressWarnings("unchecked")
62 public void testSiblingButtons()
63 {
64
65 UIXCommand button1 = new UIXCommand();
66 button1.setId("commandButton1");
67 UIXCommand button2 = new UIXCommand();
68 button2.setId("commandButton2");
69 UIXPanel rootPanel = new UIXPanel();
70 rootPanel.getChildren().add(button1);
71 rootPanel.getChildren().add(button2);
72
73 UIComponent cmp =
74 ComponentUtils.findRelativeComponent(button2, "commandButton1");
75
76 assertEquals(cmp, button1);
77
78 cmp = ComponentUtils.findRelativeComponent(button2, "::::::commandButton1");
79
80 assertEquals(cmp, button1);
81
82
83 }
84
85
86 @SuppressWarnings("unchecked")
87 public void testSiblingWithTable()
88 {
89
90
91
92
93 UIXCommand button1 = new UIXCommand();
94 button1.setId("commandButton1");
95
96 UIXTable table1 = new UIXTable();
97 table1.setId("table1");
98 UIXPanel rootPanel = new UIXPanel();
99 rootPanel.getChildren().add(button1);
100 rootPanel.getChildren().add(table1);
101 UIXPanel tableChild = new UIXPanel();
102 tableChild.setId("tableChildId");
103 table1.getChildren().add(tableChild);
104
105
106 UIComponent cmp =
107 ComponentUtils.findRelativeComponent(table1,"::commandButton1");
108
109 assertEquals(button1, cmp);
110
111 cmp = ComponentUtils.findRelativeComponent(table1, "commandButton1");
112
113
114
115 assertEquals(null, cmp);
116
117 cmp = ComponentUtils.findRelativeComponent(button1, "table1");
118 assertEquals(table1, cmp);
119
120 cmp = ComponentUtils.findRelativeComponent(button1, "tableChildId");
121 assertEquals(null, cmp);
122
123 cmp = ComponentUtils.findRelativeComponent(button1, "table1:tableChildId");
124 assertEquals(tableChild, cmp);
125
126 cmp = ComponentUtils.findRelativeComponent(table1, "tableChildId");
127 assertEquals(tableChild, cmp);
128
129 cmp = ComponentUtils.findRelativeComponent(tableChild, "table1");
130 assertEquals(table1, cmp);
131
132 cmp = ComponentUtils.findRelativeComponent(tableChild, ":commandButton1");
133 assertEquals(button1, cmp);
134
135 cmp = ComponentUtils.findRelativeComponent(tableChild, ":::commandButton1");
136 assertEquals(button1, cmp);
137
138 cmp = ComponentUtils.findRelativeComponent(tableChild, "::commandButton1");
139 assertEquals(button1, cmp);
140
141
142
143 }
144
145
146
147 @SuppressWarnings("unchecked")
148 public void testRelativeSearch()
149 {
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165 UIForm form = new UIForm();
166 TestNamingContainer ncRoot = new TestNamingContainer(); ncRoot.setId("ncRoot");
167 UIXCommand button1 = new UIXCommand();
168 button1.setId("commandButton1");
169 UIXCommand button2 = new UIXCommand();
170 button2.setId("commandButton2");
171
172 form.getChildren().add(ncRoot);
173 ncRoot.getChildren().add(button1);
174 ncRoot.getChildren().add(button2);
175
176 TestNamingContainer nc = new TestNamingContainer(); nc.setId("nc1");
177 UIXInput inputA = new UIXInput(); inputA.setId("inputA");
178 UIXPanel panel = new UIXPanel(); panel.setId("panel1");
179 UIXInput input1 = new UIXInput(); input1.setId("input1");
180 ncRoot.getChildren().add(nc);
181 nc.getChildren().add(inputA);
182 nc.getChildren().add(panel);
183 panel.getChildren().add(input1);
184
185
186
187 UIComponent cmp =
188 ComponentUtils.findRelativeComponent(input1,":::commandButton1");
189 assertEquals(button1, cmp);
190
191
192 cmp = ComponentUtils.findRelativeComponent(input1, "::::ncRoot:commandButton1");
193 assertEquals(button1, cmp);
194
195 cmp = ComponentUtils.findRelativeComponent(input1, ":::ncRoot:commandButton1");
196 assertEquals(button1, cmp);
197
198
199
200
201 cmp = ComponentUtils.findRelativeComponent(inputA, ":::commandButton1");
202 assertEquals(button1, cmp);
203
204
205 cmp = ComponentUtils.findRelativeComponent(inputA, "::ncRoot:commandButton1");
206
207 assertEquals(button1, cmp);
208
209 }
210
211
212 }
213