1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.trinidad.component;
20
21 import javax.faces.component.NamingContainer;
22 import javax.faces.component.UIComponent;
23 import javax.faces.component.UIForm;
24 import javax.faces.component.UIViewRoot;
25
26 import junit.framework.Test;
27 import junit.framework.TestCase;
28 import junit.framework.TestSuite;
29
30 import org.apache.myfaces.trinidad.component.UIXPanel;
31
32 public class FindComponentTest extends TestCase
33 {
34 public static final Test suite()
35 {
36 return new TestSuite(FindComponentTest.class);
37 }
38
39 public static void main(String[] args) throws Throwable
40 {
41 junit.textui.TestRunner.run(suite());
42 }
43
44 public FindComponentTest(
45 String testName)
46 {
47 super(testName);
48 }
49
50 static private class TestNamingContainer extends UIXPanel
51 implements NamingContainer
52 {
53 @Override
54 public UIComponent findComponent(String expr)
55 {
56 addToTrace(getId());
57 addToTrace(expr);
58 return super.findComponent(expr);
59 }
60
61 public static void clearTrace()
62 {
63 _trace = new StringBuffer();
64 }
65
66
67 public static void addToTrace(String text)
68 {
69 _trace.append('/');
70 _trace.append(text);
71 }
72
73
74 public static String getTrace()
75 {
76 return _trace.toString();
77 }
78
79
80 private static StringBuffer _trace = new StringBuffer();
81 }
82
83
84
85 @SuppressWarnings("unchecked")
86 public void testNested()
87 {
88 TestNamingContainer a = new TestNamingContainer(); a.setId("a");
89 TestNamingContainer b = new TestNamingContainer(); b.setId("b");
90 TestNamingContainer d = new TestNamingContainer(); d.setId("d");
91 UIXPanel e = new UIXPanel(); e.setId("e");
92 UIXPanel g = new UIXPanel(); g.setId("g");
93 a.getChildren().add(b);
94 b.getChildren().add(d);
95 b.getChildren().add(g);
96 d.getChildren().add(e);
97
98 TestNamingContainer.clearTrace();
99 assertTrue(a == a.findComponent("a"));
100 assertEquals("/a/a", TestNamingContainer.getTrace());
101
102 TestNamingContainer.clearTrace();
103 assertTrue(a == a.findComponent(":a"));
104 assertEquals("/a/:a", TestNamingContainer.getTrace());
105
106 TestNamingContainer.clearTrace();
107 assertTrue(b == a.findComponent("b"));
108 assertEquals("/a/b", TestNamingContainer.getTrace());
109
110 TestNamingContainer.clearTrace();
111 assertTrue(b == a.findComponent(":b"));
112 assertEquals("/a/:b", TestNamingContainer.getTrace());
113
114 TestNamingContainer.clearTrace();
115 assertTrue(d == a.findComponent("b:d"));
116 assertEquals("/a/b:d/b/d", TestNamingContainer.getTrace());
117
118 TestNamingContainer.clearTrace();
119 assertTrue(d == a.findComponent(":b:d"));
120 assertEquals("/a/:b:d/b/d", TestNamingContainer.getTrace());
121
122 TestNamingContainer.clearTrace();
123 assertTrue(e == a.findComponent("b:d:e"));
124 assertEquals("/a/b:d:e/b/d:e/d/e", TestNamingContainer.getTrace());
125
126 TestNamingContainer.clearTrace();
127 assertTrue(e == a.findComponent(":b:d:e"));
128 assertEquals("/a/:b:d:e/b/d:e/d/e", TestNamingContainer.getTrace());
129
130 TestNamingContainer.clearTrace();
131 assertTrue(g == a.findComponent("b:g"));
132 assertEquals("/a/b:g/b/g", TestNamingContainer.getTrace());
133
134 TestNamingContainer.clearTrace();
135 assertTrue(g == a.findComponent(":b:g"));
136 assertEquals("/a/:b:g/b/g", TestNamingContainer.getTrace());
137 }
138
139
140 @SuppressWarnings("unchecked")
141 public void testRelativeSearch()
142 {
143
144
145
146
147
148
149
150 UIViewRoot a = new UIViewRoot(); a.setId("a");
151 UIForm b = new UIForm(); b.setId("b");
152 UIXPanel c = new UIXPanel(); c.setId("c");
153 TestNamingContainer d = new TestNamingContainer(); d.setId("d");
154 UIXPanel e = new UIXPanel(); e.setId("e");
155 UIXPanel f = new UIXPanel(); f.setId("f");
156 UIXPanel g = new UIXPanel(); g.setId("g");
157 UIXPanel h = new UIXPanel(); h.setId("h");
158 UIXPanel i = new UIXPanel(); i.setId("i");
159 a.getChildren().add(b);
160 a.getChildren().add(c);
161 b.getChildren().add(d);
162 b.getChildren().add(g);
163 c.getChildren().add(h);
164 c.getChildren().add(i);
165 d.getChildren().add(e);
166 d.getChildren().add(f);
167
168
169 assertTrue(a == a.findComponent("a"));
170 assertTrue(b == a.findComponent("b"));
171 assertTrue(c == a.findComponent("c"));
172 assertTrue(d == a.findComponent("b:d"));
173 assertTrue(e == a.findComponent("b:d:e"));
174 assertTrue(f == a.findComponent("b:d:f"));
175 assertTrue(g == a.findComponent("b:g"));
176 assertTrue(h == a.findComponent("h"));
177 assertTrue(i == a.findComponent("i"));
178
179
180 assertNull(a.findComponent("d"));
181 assertNull(a.findComponent("e"));
182 assertNull(a.findComponent("f"));
183 assertNull(a.findComponent("g"));
184
185
186 assertTrue(b == b.findComponent("b"));
187 assertTrue(d == b.findComponent("d"));
188 assertTrue(e == b.findComponent("d:e"));
189 assertTrue(f == b.findComponent("d:f"));
190 assertTrue(g == b.findComponent("g"));
191
192
193 assertNull(b.findComponent("a"));
194 assertNull(b.findComponent("c"));
195 assertNull(b.findComponent("e"));
196 assertNull(b.findComponent("f"));
197 assertNull(b.findComponent("h"));
198 assertNull(b.findComponent("i"));
199
200
201 assertTrue(a == c.findComponent("a"));
202 assertTrue(b == c.findComponent("b"));
203 assertTrue(c == c.findComponent("c"));
204 assertTrue(d == c.findComponent("b:d"));
205 assertTrue(e == c.findComponent("b:d:e"));
206 assertTrue(f == c.findComponent("b:d:f"));
207 assertTrue(g == c.findComponent("b:g"));
208 assertTrue(h == c.findComponent("h"));
209 assertTrue(i == c.findComponent("i"));
210
211
212 assertNull(c.findComponent("d"));
213 assertNull(c.findComponent("e"));
214 assertNull(c.findComponent("f"));
215 assertNull(c.findComponent("g"));
216
217
218 assertTrue(d == d.findComponent("d"));
219 assertTrue(e == d.findComponent("e"));
220 assertTrue(f == d.findComponent("f"));
221
222
223 assertNull(d.findComponent("a"));
224 assertNull(d.findComponent("b"));
225 assertNull(d.findComponent("c"));
226 assertNull(d.findComponent("g"));
227 assertNull(d.findComponent("h"));
228 assertNull(d.findComponent("i"));
229
230
231 assertTrue(d == e.findComponent("d"));
232 assertTrue(e == e.findComponent("e"));
233 assertTrue(f == e.findComponent("f"));
234
235
236 assertNull(e.findComponent("a"));
237 assertNull(e.findComponent("b"));
238 assertNull(e.findComponent("c"));
239 assertNull(e.findComponent("g"));
240 assertNull(e.findComponent("h"));
241 assertNull(e.findComponent("i"));
242
243
244 assertTrue(d == f.findComponent("d"));
245 assertTrue(e == f.findComponent("e"));
246 assertTrue(f == f.findComponent("f"));
247
248
249 assertNull(f.findComponent("a"));
250 assertNull(f.findComponent("b"));
251 assertNull(f.findComponent("c"));
252 assertNull(f.findComponent("g"));
253 assertNull(f.findComponent("h"));
254 assertNull(f.findComponent("i"));
255
256
257 assertTrue(b == g.findComponent("b"));
258 assertTrue(d == g.findComponent("d"));
259 assertTrue(e == g.findComponent("d:e"));
260 assertTrue(f == g.findComponent("d:f"));
261 assertTrue(g == g.findComponent("g"));
262
263
264 assertNull(g.findComponent("a"));
265 assertNull(g.findComponent("c"));
266 assertNull(g.findComponent("e"));
267 assertNull(g.findComponent("f"));
268 assertNull(g.findComponent("h"));
269 assertNull(g.findComponent("i"));
270
271
272 assertTrue(a == h.findComponent("a"));
273 assertTrue(b == h.findComponent("b"));
274 assertTrue(c == h.findComponent("c"));
275 assertTrue(d == h.findComponent("b:d"));
276 assertTrue(e == h.findComponent("b:d:e"));
277 assertTrue(f == h.findComponent("b:d:f"));
278 assertTrue(g == h.findComponent("b:g"));
279 assertTrue(h == h.findComponent("h"));
280 assertTrue(i == h.findComponent("i"));
281
282
283 assertNull(h.findComponent("d"));
284 assertNull(h.findComponent("e"));
285 assertNull(h.findComponent("f"));
286 assertNull(h.findComponent("g"));
287
288
289 assertTrue(a == i.findComponent("a"));
290 assertTrue(b == i.findComponent("b"));
291 assertTrue(c == i.findComponent("c"));
292 assertTrue(d == i.findComponent("b:d"));
293 assertTrue(e == i.findComponent("b:d:e"));
294 assertTrue(f == i.findComponent("b:d:f"));
295 assertTrue(g == i.findComponent("b:g"));
296 assertTrue(h == i.findComponent("h"));
297 assertTrue(i == i.findComponent("i"));
298
299
300 assertNull(i.findComponent("d"));
301 assertNull(i.findComponent("e"));
302 assertNull(i.findComponent("f"));
303 assertNull(i.findComponent("g"));
304
305
306 assertTrue(a == a.findComponent(":a"));
307 assertTrue(b == a.findComponent(":b"));
308 assertTrue(c == a.findComponent(":c"));
309 assertTrue(d == a.findComponent(":b:d"));
310 assertTrue(e == a.findComponent(":b:d:e"));
311 assertTrue(f == a.findComponent(":b:d:f"));
312 assertTrue(g == a.findComponent(":b:g"));
313 assertTrue(h == a.findComponent(":h"));
314 assertTrue(i == a.findComponent(":i"));
315
316
317 assertTrue(a == b.findComponent(":a"));
318 assertTrue(b == b.findComponent(":b"));
319 assertTrue(c == b.findComponent(":c"));
320 assertTrue(d == b.findComponent(":b:d"));
321 assertTrue(e == b.findComponent(":b:d:e"));
322 assertTrue(f == b.findComponent(":b:d:f"));
323 assertTrue(g == b.findComponent(":b:g"));
324 assertTrue(h == b.findComponent(":h"));
325 assertTrue(i == b.findComponent(":i"));
326
327
328 assertTrue(a == c.findComponent(":a"));
329 assertTrue(b == c.findComponent(":b"));
330 assertTrue(c == c.findComponent(":c"));
331 assertTrue(d == c.findComponent(":b:d"));
332 assertTrue(e == c.findComponent(":b:d:e"));
333 assertTrue(f == c.findComponent(":b:d:f"));
334 assertTrue(g == c.findComponent(":b:g"));
335 assertTrue(h == c.findComponent(":h"));
336 assertTrue(i == c.findComponent(":i"));
337
338
339 assertTrue(a == d.findComponent(":a"));
340 assertTrue(b == d.findComponent(":b"));
341 assertTrue(c == d.findComponent(":c"));
342 assertTrue(d == d.findComponent(":b:d"));
343 assertTrue(e == d.findComponent(":b:d:e"));
344 assertTrue(f == d.findComponent(":b:d:f"));
345 assertTrue(g == d.findComponent(":b:g"));
346 assertTrue(h == d.findComponent(":h"));
347 assertTrue(i == d.findComponent(":i"));
348
349
350 assertTrue(a == e.findComponent(":a"));
351 assertTrue(b == e.findComponent(":b"));
352 assertTrue(c == e.findComponent(":c"));
353 assertTrue(d == e.findComponent(":b:d"));
354 assertTrue(e == e.findComponent(":b:d:e"));
355 assertTrue(f == e.findComponent(":b:d:f"));
356 assertTrue(g == e.findComponent(":b:g"));
357 assertTrue(h == e.findComponent(":h"));
358 assertTrue(i == e.findComponent(":i"));
359
360
361 assertTrue(a == f.findComponent(":a"));
362 assertTrue(b == f.findComponent(":b"));
363 assertTrue(c == f.findComponent(":c"));
364 assertTrue(d == f.findComponent(":b:d"));
365 assertTrue(e == f.findComponent(":b:d:e"));
366 assertTrue(f == f.findComponent(":b:d:f"));
367 assertTrue(g == f.findComponent(":b:g"));
368 assertTrue(h == f.findComponent(":h"));
369 assertTrue(i == f.findComponent(":i"));
370
371
372 assertTrue(a == g.findComponent(":a"));
373 assertTrue(b == g.findComponent(":b"));
374 assertTrue(c == g.findComponent(":c"));
375 assertTrue(d == g.findComponent(":b:d"));
376 assertTrue(e == g.findComponent(":b:d:e"));
377 assertTrue(f == g.findComponent(":b:d:f"));
378 assertTrue(g == g.findComponent(":b:g"));
379 assertTrue(h == g.findComponent(":h"));
380 assertTrue(i == g.findComponent(":i"));
381
382
383 assertTrue(a == h.findComponent(":a"));
384 assertTrue(b == h.findComponent(":b"));
385 assertTrue(c == h.findComponent(":c"));
386 assertTrue(d == h.findComponent(":b:d"));
387 assertTrue(e == h.findComponent(":b:d:e"));
388 assertTrue(f == h.findComponent(":b:d:f"));
389 assertTrue(g == h.findComponent(":b:g"));
390 assertTrue(h == h.findComponent(":h"));
391 assertTrue(i == h.findComponent(":i"));
392
393
394 assertTrue(a == i.findComponent(":a"));
395 assertTrue(b == i.findComponent(":b"));
396 assertTrue(c == i.findComponent(":c"));
397 assertTrue(d == i.findComponent(":b:d"));
398 assertTrue(e == i.findComponent(":b:d:e"));
399 assertTrue(f == i.findComponent(":b:d:f"));
400 assertTrue(g == i.findComponent(":b:g"));
401 assertTrue(h == i.findComponent(":h"));
402 assertTrue(i == i.findComponent(":i"));
403 }
404
405 @SuppressWarnings("unchecked")
406 public void testAbsoluteSearch()
407 {
408
409
410
411
412
413
414
415 UIViewRoot a = new UIViewRoot(); a.setId("a");
416 UIForm b = new UIForm(); b.setId("b");
417 UIXPanel c = new UIXPanel(); c.setId("c");
418 TestNamingContainer d = new TestNamingContainer(); d.setId("d");
419 UIXPanel e = new UIXPanel(); e.setId("e");
420 UIXPanel f = new UIXPanel(); f.setId("f");
421 UIXPanel g = new UIXPanel(); g.setId("g");
422 UIXPanel h = new UIXPanel(); h.setId("h");
423 UIXPanel i = new UIXPanel(); i.setId("i");
424 a.getChildren().add(b);
425 a.getChildren().add(c);
426 b.getChildren().add(d);
427 b.getChildren().add(g);
428 c.getChildren().add(h);
429 c.getChildren().add(i);
430 d.getChildren().add(e);
431 d.getChildren().add(f);
432
433
434 assertTrue(a == a.findComponent(":a"));
435 assertTrue(b == a.findComponent(":b"));
436 assertTrue(c == a.findComponent(":c"));
437 assertTrue(d == a.findComponent(":b:d"));
438 assertTrue(e == a.findComponent(":b:d:e"));
439 assertTrue(f == a.findComponent(":b:d:f"));
440 assertTrue(g == a.findComponent(":b:g"));
441 assertTrue(h == a.findComponent(":h"));
442 assertTrue(i == a.findComponent(":i"));
443
444
445 assertTrue(a == b.findComponent(":a"));
446 assertTrue(b == b.findComponent(":b"));
447 assertTrue(c == b.findComponent(":c"));
448 assertTrue(d == b.findComponent(":b:d"));
449 assertTrue(e == b.findComponent(":b:d:e"));
450 assertTrue(f == b.findComponent(":b:d:f"));
451 assertTrue(g == b.findComponent(":b:g"));
452 assertTrue(h == b.findComponent(":h"));
453 assertTrue(i == b.findComponent(":i"));
454
455
456 assertTrue(a == c.findComponent(":a"));
457 assertTrue(b == c.findComponent(":b"));
458 assertTrue(c == c.findComponent(":c"));
459 assertTrue(d == c.findComponent(":b:d"));
460 assertTrue(e == c.findComponent(":b:d:e"));
461 assertTrue(f == c.findComponent(":b:d:f"));
462 assertTrue(g == c.findComponent(":b:g"));
463 assertTrue(h == c.findComponent(":h"));
464 assertTrue(i == c.findComponent(":i"));
465
466
467 assertTrue(a == d.findComponent(":a"));
468 assertTrue(b == d.findComponent(":b"));
469 assertTrue(c == d.findComponent(":c"));
470 assertTrue(d == d.findComponent(":b:d"));
471 assertTrue(e == d.findComponent(":b:d:e"));
472 assertTrue(f == d.findComponent(":b:d:f"));
473 assertTrue(g == d.findComponent(":b:g"));
474 assertTrue(h == d.findComponent(":h"));
475 assertTrue(i == d.findComponent(":i"));
476
477
478 assertTrue(a == e.findComponent(":a"));
479 assertTrue(b == e.findComponent(":b"));
480 assertTrue(c == e.findComponent(":c"));
481 assertTrue(d == e.findComponent(":b:d"));
482 assertTrue(e == e.findComponent(":b:d:e"));
483 assertTrue(f == e.findComponent(":b:d:f"));
484 assertTrue(g == e.findComponent(":b:g"));
485 assertTrue(h == e.findComponent(":h"));
486 assertTrue(i == e.findComponent(":i"));
487
488
489 assertTrue(a == f.findComponent(":a"));
490 assertTrue(b == f.findComponent(":b"));
491 assertTrue(c == f.findComponent(":c"));
492 assertTrue(d == f.findComponent(":b:d"));
493 assertTrue(e == f.findComponent(":b:d:e"));
494 assertTrue(f == f.findComponent(":b:d:f"));
495 assertTrue(g == f.findComponent(":b:g"));
496 assertTrue(h == f.findComponent(":h"));
497 assertTrue(i == f.findComponent(":i"));
498
499
500 assertTrue(a == g.findComponent(":a"));
501 assertTrue(b == g.findComponent(":b"));
502 assertTrue(c == g.findComponent(":c"));
503 assertTrue(d == g.findComponent(":b:d"));
504 assertTrue(e == g.findComponent(":b:d:e"));
505 assertTrue(f == g.findComponent(":b:d:f"));
506 assertTrue(g == g.findComponent(":b:g"));
507 assertTrue(h == g.findComponent(":h"));
508 assertTrue(i == g.findComponent(":i"));
509
510
511 assertTrue(a == h.findComponent(":a"));
512 assertTrue(b == h.findComponent(":b"));
513 assertTrue(c == h.findComponent(":c"));
514 assertTrue(d == h.findComponent(":b:d"));
515 assertTrue(e == h.findComponent(":b:d:e"));
516 assertTrue(f == h.findComponent(":b:d:f"));
517 assertTrue(g == h.findComponent(":b:g"));
518 assertTrue(h == h.findComponent(":h"));
519 assertTrue(i == h.findComponent(":i"));
520
521
522 assertTrue(a == i.findComponent(":a"));
523 assertTrue(b == i.findComponent(":b"));
524 assertTrue(c == i.findComponent(":c"));
525 assertTrue(d == i.findComponent(":b:d"));
526 assertTrue(e == i.findComponent(":b:d:e"));
527 assertTrue(f == i.findComponent(":b:d:f"));
528 assertTrue(g == i.findComponent(":b:g"));
529 assertTrue(h == i.findComponent(":h"));
530 assertTrue(i == i.findComponent(":i"));
531 }
532
533 @SuppressWarnings("unchecked")
534 public void testExceptions()
535 {
536
537
538
539
540
541
542
543 UIViewRoot a = new UIViewRoot(); a.setId("a");
544 UIForm b = new UIForm(); b.setId("b");
545 UIXPanel c = new UIXPanel(); c.setId("c");
546 TestNamingContainer d = new TestNamingContainer(); d.setId("d");
547 UIXPanel e = new UIXPanel(); e.setId("e");
548 UIXPanel f = new UIXPanel(); f.setId("f");
549 UIXPanel g = new UIXPanel(); g.setId("g");
550 UIXPanel h = new UIXPanel(); h.setId("h");
551 UIXPanel i = new UIXPanel(); i.setId("i");
552 a.getChildren().add(b);
553 a.getChildren().add(c);
554 b.getChildren().add(d);
555 b.getChildren().add(g);
556 c.getChildren().add(h);
557 c.getChildren().add(i);
558 d.getChildren().add(e);
559 d.getChildren().add(f);
560
561
562 try
563 {
564 a.findComponent(null);
565 fail("Should have thrown NullPointerException");
566 }
567 catch (NullPointerException ex)
568 {
569 ;
570 }
571
572 try
573 {
574 a.findComponent("a:c:h");
575 fail("Should have thrown IllegalArgumentException");
576 }
577 catch (IllegalArgumentException ex)
578 {
579 ;
580 }
581
582 try
583 {
584 a.findComponent("a:c:i");
585 fail("Should have thrown IllegalArgumentException");
586 } catch (IllegalArgumentException ex)
587 {
588 ;
589 }
590
591 try
592 {
593 a.findComponent(":a:c:h");
594 fail("Should have thrown IllegalArgumentException");
595 }
596 catch (IllegalArgumentException ex)
597 {
598 ;
599 }
600
601 try
602 {
603 a.findComponent(":a:c:i");
604 fail("Should have thrown IllegalArgumentException");
605 }
606 catch (IllegalArgumentException ex)
607 {
608 ;
609 }
610
611 try
612 {
613 a.findComponent("c:h");
614 fail("Should have thrown IllegalArgumentException");
615 }
616 catch (IllegalArgumentException ex)
617 {
618 ;
619 }
620
621 try
622 {
623 a.findComponent("c:i");
624 fail("Should have thrown IllegalArgumentException");
625 }
626 catch (IllegalArgumentException ex)
627 {
628 ;
629 }
630
631 try
632 {
633 a.findComponent(":c:h");
634 fail("Should have thrown IllegalArgumentException");
635 }
636 catch (IllegalArgumentException ex)
637 {
638 ;
639 }
640
641 try
642 {
643 a.findComponent(":c:i");
644 fail("Should have thrown IllegalArgumentException");
645 }
646 catch (IllegalArgumentException ex)
647 {
648 ;
649 }
650 }
651 }