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.trinidad.util;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.io.ObjectInputStream;
25  import java.io.ObjectOutputStream;
26  
27  import javax.faces.component.UIData;
28  import javax.faces.component.UIForm;
29  import javax.faces.component.UIInput;
30  import javax.faces.component.UINamingContainer;
31  import javax.faces.component.UIOutput;
32  import javax.faces.component.UIPanel;
33  import javax.faces.component.UIViewRoot;
34  
35  import junit.framework.Test;
36  import junit.framework.TestSuite;
37  
38  import org.apache.myfaces.trinidadbuild.test.FacesTestCase;
39  
40  public class ComponentReferenceTest extends FacesTestCase
41  {
42    public static final Test suite()
43    {
44      return new TestSuite(ComponentReferenceTest.class);
45    }
46    
47    public static void main(String[] args) throws Throwable
48    {
49      junit.textui.TestRunner.run(suite());
50    }
51  
52    public ComponentReferenceTest(
53      String testName)
54    {
55      super(testName);
56    }
57    
58    public void testVerySimpleFinder()
59    {
60      UIViewRoot root = facesContext.getViewRoot();
61      root.setId("root");
62      UIInput input1 = new UIInput(); input1.setId("input1");
63  
64      // build the Tree...
65      root.getChildren().add(input1);
66  
67      // Get the ComponentReference util
68      ComponentReference<UIInput> uiRef = ComponentReference.newUIComponentReference(input1);
69  
70      // find the component...
71      UIInput referencedComp = uiRef.getComponent();
72  
73      assertEquals(input1, referencedComp);
74    }
75  
76    @SuppressWarnings("unchecked")
77    public void testFailoverOnCustomFacet() throws IOException, ClassNotFoundException
78    {
79      UIViewRoot root = facesContext.getViewRoot();
80      root.setId("root");
81      UIForm form = new UIForm(); form.setId("form");
82      UINamingContainer nc1 = new UINamingContainer(); nc1.setId("nc1");
83      UINamingContainer nc2 = new UINamingContainer(); nc2.setId("nc2");
84      UINamingContainer nc3 = new UINamingContainer(); nc3.setId("nc3");
85      UIPanel panel = new UIPanel(); panel.setId("panel");
86      UIInput input = new UIInput(); input.setId("input1");
87  
88      // build the Tree...
89      panel.getFacets().put("fancyFacet", input);
90      nc3.getChildren().add(panel);
91      nc2.getChildren().add(new UIOutput());
92      nc2.getChildren().add(new UIOutput());
93      nc2.getChildren().add(nc3);
94      nc1.getChildren().add(nc2);
95      form.getChildren().add(new UIOutput());
96      form.getChildren().add(new UIOutput());
97      form.getChildren().add(new UIOutput());
98      form.getChildren().add(nc1);
99      root.getChildren().add(new UIOutput());
100     root.getChildren().add(form);
101 
102     // Get the ComponentReference util
103     ComponentReference<UIInput> uiRef = ComponentReference.newUIComponentReference(input);
104 
105     // find the component...
106     UIInput referencedComp = uiRef.getComponent();
107     assertEquals(input, referencedComp);
108     
109     // find it again!
110     assertEquals(input, uiRef.getComponent());
111 
112     // fake the failover
113     ByteArrayOutputStream baos = new ByteArrayOutputStream();
114     ObjectOutputStream oos = new ObjectOutputStream(baos);
115 
116     oos.writeObject(uiRef);
117 
118     ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
119     ObjectInputStream ois = new ObjectInputStream(bais);
120 
121     uiRef = (ComponentReference<UIInput>) ois.readObject();
122 
123     referencedComp = uiRef.getComponent();
124     assertEquals(input, referencedComp);
125 
126   }
127 
128   
129   @SuppressWarnings("unchecked")
130   public void testFailover() throws IOException, ClassNotFoundException
131   {
132     UIViewRoot root = facesContext.getViewRoot();
133     root.setId("root");
134     UIInput input1 = new UIInput(); input1.setId("input1");
135 
136     // build the Tree...
137     root.getChildren().add(input1);
138 
139     // Get the ComponentReference util
140     ComponentReference<UIInput> uiRef = ComponentReference.newUIComponentReference(input1);
141 
142     // find the component...
143     UIInput referencedComp = uiRef.getComponent();
144     assertEquals(input1, referencedComp);
145     
146     // fake the failover
147     ByteArrayOutputStream baos = new ByteArrayOutputStream();
148     ObjectOutputStream oos = new ObjectOutputStream(baos);
149 
150     oos.writeObject(uiRef);
151     oos.flush();
152 
153     ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
154     ObjectInputStream ois = new ObjectInputStream(bais);
155 
156     uiRef = (ComponentReference<UIInput>) ois.readObject();
157 
158     referencedComp = uiRef.getComponent();
159     assertEquals(input1, referencedComp);
160   }
161 
162   public void testEmptyViewRootOnGetComponent()
163   {
164     UIViewRoot root = facesContext.getViewRoot();
165     root.setId("root");
166     UINamingContainer nc1 = new UINamingContainer(); nc1.setId("nc1");
167     UINamingContainer nc2 = new UINamingContainer(); nc2.setId("nc2");
168     UINamingContainer nc3 = new UINamingContainer(); nc3.setId("nc3");
169 
170     // build the tree
171     root.getChildren().add(nc1);
172     nc1.getChildren().add(nc2);
173     nc2.getChildren().add(nc3);
174 
175     // Get the ComponentReference util
176     ComponentReference<UINamingContainer> uiRef = ComponentReference.newUIComponentReference(nc3);
177 
178     // find the component...
179     UINamingContainer referencedComp = uiRef.getComponent();
180 
181     assertEquals(nc3, referencedComp);
182 
183     // clear the ViewRoot
184     root.getChildren().clear();
185 
186     // now, the getComponent should return NULL
187     assertNull(uiRef.getComponent());
188   }
189 
190   public void testNoComponentWithoutAnId()
191   {
192     UIViewRoot root = facesContext.getViewRoot();
193     root.setId("root");
194     UIInput input1 = new UIInput();
195 
196     // build the Tree...
197     root.getChildren().add(input1);
198 
199     ComponentReference ref = ComponentReference.newUIComponentReference(input1);
200 
201     // Get the ComponentReference util
202     try
203     {
204       ref.getComponent();
205       
206       // find the component...
207       fail("IllegalStateException expected");
208     }
209     catch (IllegalStateException e)
210     {
211       // suppress it - this is as expected
212     }
213   }
214 
215   public void testViewRoot()
216   {
217     UIViewRoot root = facesContext.getViewRoot();
218     root.setId("root");
219 
220     // Get the ComponentReference util
221     ComponentReference<UIViewRoot> uiRef = ComponentReference.newUIComponentReference(root);
222 
223     // find the component...
224     UIViewRoot referencedComp = uiRef.getComponent();
225     assertEquals(root, referencedComp);
226   }
227 
228   public void testNamingContainerViewRoot()
229   {
230     UIViewRoot root = facesContext.getViewRoot();
231     root.setId("root");
232     UINamingContainer nc1 = new UINamingContainer(); nc1.setId("nc1");
233     UINamingContainer nc2 = new UINamingContainer(); nc2.setId("nc2");
234     UINamingContainer nc3 = new UINamingContainer(); nc3.setId("nc3");
235 
236     // build the tree
237     root.getChildren().add(nc1);
238     nc1.getChildren().add(nc2);
239     nc2.getChildren().add(nc3);
240 
241     // Get the ComponentReference util
242     ComponentReference<UINamingContainer> uiRef = ComponentReference.newUIComponentReference(nc3);
243 
244     // find the component...
245     UINamingContainer referencedComp = uiRef.getComponent();
246 
247     assertEquals(nc3, referencedComp);
248   }
249 
250   public void testMovingInsideNamingContainer()
251   {
252     UIViewRoot root = facesContext.getViewRoot();
253     root.setId("root");
254     UINamingContainer nc1 = new UINamingContainer(); nc1.setId("nc1");
255     UINamingContainer nc2 = new UINamingContainer(); nc2.setId("nc2");
256     UINamingContainer nc3 = new UINamingContainer(); nc3.setId("nc3");
257 
258     // build the tree
259     root.getChildren().add(nc1);
260     nc1.getChildren().add(nc2);
261     nc2.getChildren().add(nc3);
262 
263     // Get the ComponentReference util
264     ComponentReference<UINamingContainer> uiRef = ComponentReference.newUIComponentReference(nc3);
265 
266     // find the component...
267     UINamingContainer referencedComp = uiRef.getComponent();
268     assertEquals(nc3, referencedComp);
269 
270     // let's move the NC3 component one level up;
271     nc2.getChildren().remove(nc3);
272     nc1.getChildren().add(nc3);
273 
274     // and we can not find the component...
275     referencedComp = uiRef.getComponent();
276     assertNull(referencedComp);
277   }
278 
279   public void testDeferredMovingInsideNamingContainer()
280   {
281     UIViewRoot root = facesContext.getViewRoot();
282     root.setId("root");
283     UINamingContainer nc1 = new UINamingContainer(); nc1.setId("nc1");
284     UINamingContainer nc2 = new UINamingContainer(); nc2.setId("nc2");
285     UINamingContainer nc3 = new UINamingContainer(); nc3.setId("nc3");
286 
287     // almost build the tree
288     nc1.getChildren().add(nc2);
289     nc2.getChildren().add(nc3);
290 
291     // Get the ComponentReference util, this will be a deferred component reference since the
292     // component wasn't attached
293     ComponentReference<UINamingContainer> uiRef = ComponentReference.newUIComponentReference(nc3);
294 
295     // now finish building the component tree
296     root.getChildren().add(nc1);
297 
298     // find the component...
299     UINamingContainer referencedComp = uiRef.getComponent();
300     assertEquals(nc3, referencedComp);
301 
302     // let's move the NC3 component one level up;
303     nc2.getChildren().remove(nc3);
304     nc1.getChildren().add(nc3);
305 
306     // and we can not find the component...
307     referencedComp = uiRef.getComponent();
308     assertNull(referencedComp);
309   }
310 
311   public void testComponentNotInTree()
312   {
313     UINamingContainer nc1 = new UINamingContainer(); nc1.setId("nc1");
314     UINamingContainer nc2 = new UINamingContainer(); nc2.setId("nc2");
315     UINamingContainer nc3 = new UINamingContainer(); nc3.setId("nc3");
316 
317     // build the tree
318     nc1.getChildren().add(nc2);
319     nc2.getChildren().add(nc3);
320 
321     // Get the ComponentReference util
322     ComponentReference ref = ComponentReference.newUIComponentReference(nc3);
323 
324     try
325     {
326       // find the component...
327       ref.getComponent();
328       
329       fail("IllegalStateException expected");
330     }
331     catch (IllegalStateException e)
332     {
333       // suppress it - this is as expected
334     }
335   }
336 
337   public void testFindInPanelComponents()
338   {
339     UIViewRoot root = facesContext.getViewRoot(); root.setId("root");
340     UIPanel panel1 = new UIPanel(); panel1.setId("panel1");
341     UIPanel panel2 = new UIPanel(); panel2.setId("panel2");
342     UIPanel panel3 = new UIPanel(); panel3.setId("panel3");
343     UIOutput txt = new UIOutput(); txt.setId("txt");
344 
345     // build the Tree...
346     root.getChildren().add(panel1);
347     panel1.getChildren().add(panel2);
348     panel2.getChildren().add(panel3);
349     panel3.getChildren().add(txt);
350 
351     // Get the ComponentReference util
352     ComponentReference<UIOutput> uiRef = ComponentReference.newUIComponentReference(txt);
353 
354     // find the component...
355     UIOutput referencedComp = uiRef.getComponent();
356 
357     assertEquals(txt, referencedComp);
358   }
359 
360   public void testCustomFacet()
361   {
362     UIViewRoot root = facesContext.getViewRoot();
363     root.setId("root");
364     UIForm form = new UIForm(); form.setId("form");
365     UINamingContainer nc1 = new UINamingContainer(); nc1.setId("nc1");
366     UINamingContainer nc2 = new UINamingContainer(); nc2.setId("nc2");
367     UINamingContainer nc3 = new UINamingContainer(); nc3.setId("nc3");
368     UIPanel panel = new UIPanel(); panel.setId("panel");
369     UIInput input = new UIInput(); input.setId("input1");
370 
371     // build the Tree...
372     panel.getFacets().put("fancyFacet", input);
373     nc3.getChildren().add(panel);
374     nc2.getChildren().add(new UIOutput());
375     nc2.getChildren().add(new UIOutput());
376     nc2.getChildren().add(nc3);
377     nc1.getChildren().add(nc2);
378     form.getChildren().add(new UIOutput());
379     form.getChildren().add(new UIOutput());
380     form.getChildren().add(new UIOutput());
381     form.getChildren().add(nc1);
382     root.getChildren().add(new UIOutput());
383     root.getChildren().add(form);
384 
385     // Get the ComponentReference util
386     ComponentReference<UIInput> uiRef = ComponentReference.newUIComponentReference(input);
387 
388     // find the component...
389     UIInput referencedComp = uiRef.getComponent();
390     assertEquals(input, referencedComp);
391     
392     // find it again!
393     assertEquals(input, uiRef.getComponent());
394   }
395 
396   public void testMoreFacets()
397   {
398     UIViewRoot root = facesContext.getViewRoot();
399     root.setId("root");
400     UIForm form = new UIForm(); form.setId("form");
401     UINamingContainer nc1 = new UINamingContainer(); nc1.setId("nc1");
402     UINamingContainer nc2 = new UINamingContainer(); nc2.setId("nc2");
403     UINamingContainer nc3 = new UINamingContainer(); nc3.setId("nc3");
404     UIPanel panel = new UIPanel(); panel.setId("panel");
405     UIInput input = new UIInput(); input.setId("input1");
406 
407     // build the Tree...
408     panel.getFacets().put("f1", new UIOutput());
409     panel.getFacets().put("f2", new UIOutput());
410     panel.getFacets().put("f3", new UIOutput());
411     panel.getFacets().put("f4", new UIOutput());
412     panel.getFacets().put("f5", new UIOutput());
413 
414     // add the important facet
415     panel.getFacets().put("f0", input);
416 
417     nc3.getChildren().add(panel);
418     nc2.getChildren().add(new UIOutput());
419     nc2.getChildren().add(new UIOutput());
420     nc2.getChildren().add(nc3);
421     nc1.getChildren().add(nc2);
422     form.getChildren().add(new UIOutput());
423     form.getChildren().add(new UIOutput());
424     form.getChildren().add(new UIOutput());
425     form.getChildren().add(nc1);
426     root.getChildren().add(new UIOutput());
427     root.getChildren().add(form);
428 
429     // Get the ComponentReference util
430     ComponentReference<UIInput> uiRef = ComponentReference.newUIComponentReference(input);
431 
432     // find the component...
433     UIInput referencedComp = uiRef.getComponent();
434     assertEquals(input, referencedComp);
435     
436     // find it again!
437     assertEquals(input, uiRef.getComponent());
438   }
439 
440   public void testCustomFacetWithFind()
441   {
442     UIViewRoot root = new UIViewRoot();
443     root.setId("root");
444     UIForm form = new UIForm(); form.setId("form");
445     UINamingContainer nc1 = new UINamingContainer(); nc1.setId("nc1");
446     UINamingContainer nc2 = new UINamingContainer(); nc2.setId("nc2");
447     UINamingContainer nc3 = new UINamingContainer(); nc3.setId("nc3");
448     UIPanel panel = new UIPanel(); panel.setId("panel");
449     UIInput input = new UIInput(); input.setId("input1");
450 
451     // build the Tree...
452     panel.getFacets().put("fancyFacet", input);
453     panel.getChildren().add(input);
454     nc3.getChildren().add(panel);
455     nc2.getChildren().add(new UIOutput());
456     nc2.getChildren().add(new UIOutput());
457     nc2.getChildren().add(nc3);
458     nc1.getChildren().add(nc2);
459     form.getChildren().add(new UIOutput());
460     form.getChildren().add(new UIOutput());
461     form.getChildren().add(new UIOutput());
462     form.getChildren().add(nc1);
463     root.getChildren().add(new UIOutput());
464     root.getChildren().add(form);
465     
466     facesContext.setViewRoot(root);
467 
468     // Get the ComponentReference util
469     ComponentReference<UIInput> uiRef = ComponentReference.newUIComponentReference(input);
470 
471     // find the component...
472     UIInput referencedComp = uiRef.getComponent();
473     assertEquals(input, referencedComp);
474 
475     // find it again!
476     assertEquals(input, uiRef.getComponent());
477 
478   }
479   
480   public void testUIDataFooterFacet()
481   {
482     UIViewRoot root = facesContext.getViewRoot();
483     root.setId("root");
484     UIForm form = new UIForm(); form.setId("form");
485     UINamingContainer nc1 = new UINamingContainer(); nc1.setId("nc1");
486     UINamingContainer nc2 = new UINamingContainer(); nc2.setId("nc2");
487     UINamingContainer nc3 = new UINamingContainer(); nc3.setId("nc3");
488     UIData table = new UIData(); table.setId("table1");
489     UIInput input = new UIInput(); input.setId("input1");
490 
491     // build the Tree...
492     table.setFooter(input);
493     nc3.getChildren().add(table);
494     nc2.getChildren().add(new UIOutput());
495     nc2.getChildren().add(new UIOutput());
496     nc2.getChildren().add(nc3);
497     nc1.getChildren().add(nc2);
498     form.getChildren().add(new UIOutput());
499     form.getChildren().add(new UIOutput());
500     form.getChildren().add(new UIOutput());
501     form.getChildren().add(nc1);
502     root.getChildren().add(new UIOutput());
503     root.getChildren().add(form);
504 
505     // Get the ComponentReference util
506     ComponentReference<UIInput> uiRef = ComponentReference.newUIComponentReference(input);
507 
508     // find the component...
509     UIInput referencedComp = uiRef.getComponent();
510 
511     assertEquals(input, referencedComp);
512   }
513 
514   public void testIndex()
515   {
516     UIViewRoot root = facesContext.getViewRoot();
517     root.setId("root");
518     UIForm form = new UIForm(); form.setId("form");
519     UINamingContainer nc1 = new UINamingContainer(); nc1.setId("nc1");
520     UINamingContainer nc2 = new UINamingContainer(); nc2.setId("nc2");
521     UINamingContainer nc3 = new UINamingContainer(); nc3.setId("nc3");
522     UIInput input1 = new UIInput(); input1.setId("input1");
523 
524     // build the Tree...
525     nc3.getChildren().add(input1);
526     nc2.getChildren().add(new UIOutput());
527     nc2.getChildren().add(new UIOutput());
528     nc2.getChildren().add(nc3);
529     nc1.getChildren().add(nc2);
530     form.getChildren().add(new UIOutput());
531     form.getChildren().add(new UIOutput());
532     form.getChildren().add(new UIOutput());
533     form.getChildren().add(nc1);
534     root.getChildren().add(new UIOutput());
535     root.getChildren().add(form);
536 
537     // Get the ComponentReference util
538     ComponentReference<UIInput> uiRef = ComponentReference.newUIComponentReference(input1);
539 
540     // find the component...
541     UIInput referencedComp = uiRef.getComponent();
542 
543     assertEquals(input1, referencedComp);
544   }
545 }