1 package javax.faces.component;
2
3 import java.lang.reflect.Method;
4 import java.util.Arrays;
5 import java.util.Collection;
6 import java.util.Collections;
7
8 import org.easymock.EasyMock;
9 import static org.easymock.EasyMock.*;
10 import static org.testng.Assert.*;
11 import org.testng.annotations.Test;
12
13
14
15
16
17
18
19
20 public class UIComponentFindComponentTest extends AbstractUIComponentBaseTest
21 {
22 @Test(expectedExceptions = {NullPointerException.class})
23 public void testWithNullExperession() throws Exception
24 {
25 _testImpl.findComponent(null);
26 assertNull(_testImpl.findComponent(""));
27 }
28
29 public void testWithEmptyExperession() throws Exception
30 {
31 assertNull(_testImpl.findComponent(""));
32 }
33
34 @Test
35 public void testRootExpression() throws Exception
36 {
37 String expression = ":parent";
38 UIComponent root = _mocksControl.createMock(UIComponent.class);
39 UIComponent parent = _mocksControl.createMock(UIComponent.class);
40 _testImpl.setId("testimpl");
41 expect(_testImpl.getParent()).andReturn(parent).anyTimes();
42 expect(parent.getParent()).andReturn(root).anyTimes();
43 expect(root.getParent()).andReturn(null).anyTimes();
44 expect(parent.getId()).andReturn("parent").anyTimes();
45 expect(root.getId()).andReturn("root").anyTimes();
46 expect(root.getFacetsAndChildren()).andReturn(Collections.singletonList(parent).iterator());
47
48 _mocksControl.replay();
49
50 assertEquals(parent, _testImpl.findComponent(expression));
51 }
52
53 @Test
54 public void testRelativeExpression() throws Exception
55 {
56 String expression = "testimpl";
57 UIComponent namingContainer = _mocksControl.createMock(TestNamingContainerComponent.class);
58 UIComponent parent = _mocksControl.createMock(UIComponent.class);
59 _testImpl.setId("testimpl");
60 expect(_testImpl.getParent()).andReturn(parent).anyTimes();
61 expect(parent.getParent()).andReturn(namingContainer).anyTimes();
62 expect(parent.getId()).andReturn("parent").anyTimes();
63 expect(namingContainer.getId()).andReturn("namingContainer").anyTimes();
64 expect(namingContainer.getFacetsAndChildren()).andReturn(Collections.singletonList(parent).iterator());
65 expect(parent.getFacetsAndChildren()).andReturn(Arrays.asList(new UIComponent[]{_testImpl}).iterator());
66
67 _mocksControl.replay();
68
69 assertEquals(_testImpl, _testImpl.findComponent(expression));
70 }
71
72 @Test
73 public void testComplexRelativeExpression() throws Exception
74 {
75 String expression = "child1_1:testimpl";
76 Collection<Method> mockedMethods = getMockedMethods();
77 mockedMethods.add(UIComponentBase.class.getDeclaredMethod("getFacetsAndChildren", null));
78 mockedMethods.add(UIComponentBase.class.getDeclaredMethod("getId", null));
79 UIComponent namingContainer = _mocksControl.createMock(TestNamingContainerBaseComponent.class,
80 mockedMethods.toArray(new Method[mockedMethods.size()]));
81
82 expect(namingContainer.getId()).andReturn("namingContainer").anyTimes();
83 _testImpl.setId("testimpl");
84 UIComponent child1_1 = _mocksControl.createMock(TestNamingContainerComponent.class);
85 expect(child1_1.getId()).andReturn("child1_1").anyTimes();
86 expect(namingContainer.getFacetsAndChildren()).andReturn(Collections.singletonList(child1_1).iterator());
87
88 expect(child1_1.findComponent(EasyMock.eq("testimpl"))).andReturn(_testImpl);
89
90 _mocksControl.replay();
91
92 assertEquals(_testImpl, namingContainer.findComponent(expression));
93 }
94
95 @Test
96 public void testWithRelativeExpressionNamingContainer() throws Exception
97 {
98 String expression = "testimpl";
99 Collection<Method> mockedMethods = getMockedMethods();
100 mockedMethods.add(UIComponentBase.class.getDeclaredMethod("getFacetsAndChildren", null));
101 mockedMethods.add(UIComponentBase.class.getDeclaredMethod("getId", null));
102 UIComponent namingContainer = _mocksControl.createMock(TestNamingContainerBaseComponent.class,
103 mockedMethods.toArray(new Method[mockedMethods.size()]));
104 UIComponent parent = _mocksControl.createMock(UIComponent.class);
105 _testImpl.setId("testimpl");
106 expect(_testImpl.getParent()).andReturn(parent).anyTimes();
107 expect(parent.getParent()).andReturn(namingContainer).anyTimes();
108 expect(parent.getId()).andReturn("parent").anyTimes();
109 expect(namingContainer.getId()).andReturn("namingContainer").anyTimes();
110 expect(namingContainer.getFacetsAndChildren()).andReturn(Collections.singletonList(parent).iterator());
111 expect(parent.getFacetsAndChildren()).andReturn(Arrays.asList(new UIComponent[]{_testImpl}).iterator());
112
113 _mocksControl.replay();
114
115 assertEquals(_testImpl, namingContainer.findComponent(expression));
116 }
117
118 @Test
119 public void testOverriddenFindComponent() {
120 UIViewRoot viewRoot = new UIViewRoot();
121 UIData uiData = new UIData()
122 {
123 public UIComponent findComponent(String expr)
124 {
125 return super.findComponent(stripRowIndex(expr));
126 }
127
128 public String stripRowIndex(String searchId) {
129 if (searchId.length() > 0 && Character.isDigit(searchId.charAt(0)))
130 {
131 for (int i = 1; i < searchId.length(); ++i)
132 {
133 char c = searchId.charAt(i);
134 if (c == SEPARATOR_CHAR)
135 {
136 searchId = searchId.substring(i + 1);
137 break;
138 }
139 if (!Character.isDigit(c))
140 {
141 break;
142 }
143 }
144 }
145 return searchId;
146 }
147 };
148 uiData.setId("data");
149 UIColumn column = new UIColumn();
150 column.setId("column");
151 UICommand command = new UICommand();
152 command.setId("command");
153 viewRoot.getChildren().add(uiData);
154 uiData.getChildren().add(column);
155 column.getChildren().add(command);
156
157 assertNull(viewRoot.findComponent(":xx"));
158 assertEquals(uiData, viewRoot.findComponent(":data"));
159 assertEquals(column, viewRoot.findComponent(":data:column"));
160 assertEquals(command, viewRoot.findComponent(":data:command"));
161 assertEquals(command, viewRoot.findComponent("data:1:command"));
162 assertEquals(command, viewRoot.findComponent(":data:1:command"));
163 }
164
165 @Test
166 public void testXXFindComponent() {
167 UIViewRoot viewRoot = new UIViewRoot();
168 UIData uiData = new UIData();
169 uiData.setId("x");
170 UIColumn column = new UIColumn();
171 column.setId("column");
172 UICommand command = new UICommand();
173 command.setId("x");
174 viewRoot.getChildren().add(uiData);
175 uiData.getChildren().add(column);
176 column.getChildren().add(command);
177
178 assertNull(viewRoot.findComponent(":xx"));
179 assertNotNull(viewRoot.findComponent(":x"));
180 assertEquals(column, viewRoot.findComponent(":x:column"));
181 assertEquals(command, viewRoot.findComponent(":x:x"));
182 }
183
184 public abstract static class TestNamingContainerComponent extends UIComponent implements NamingContainer
185 {
186 }
187
188 public abstract static class TestNamingContainerBaseComponent extends UIComponentBase implements NamingContainer
189 {
190 }
191 }