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 javax.faces.component;
20  
21  import javax.faces.component.UIComponent.EventListenerWrapper;
22  import javax.faces.context.FacesContext;
23  import javax.faces.event.ComponentSystemEvent;
24  import javax.faces.event.ComponentSystemEventListener;
25  import javax.faces.render.Renderer;
26  
27  public class UIComponentEventListenerWrapperTest  extends AbstractComponentTest
28  {
29  
30      public UIComponentEventListenerWrapperTest(String arg0)
31      {
32          super(arg0);
33      }
34      
35      public void testUIComponentListenerNormalState()
36      {
37          UIComponent component = new UIOutput();
38          //This case happens when @ListenerFor is attached on the component class
39          EventListenerWrapper wrapper = new EventListenerWrapper(component, component);
40          
41          Object state = wrapper.saveState(facesContext);
42          
43          //In this case state should not be null, because state should be saved fully
44          assertNotNull(state);
45          
46          EventListenerWrapper wrapper2 = new EventListenerWrapper();
47          //For restore we need to setup the context first
48          component.pushComponentToEL(facesContext, component);
49          wrapper2.restoreState(facesContext, state);
50          component.popComponentFromEL(facesContext);
51          
52          assertNotNull(wrapper2.getComponentSystemEventListener());
53          assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
54      }
55      
56      public void testUIComponentListenerWithPSS()
57      {
58          UIComponent component = new UIOutput();
59          //This case happens when @ListenerFor is attached on the component class
60          EventListenerWrapper wrapper = new EventListenerWrapper(component, component);
61          
62          wrapper.markInitialState();
63          Object state = wrapper.saveState(facesContext);
64          
65          //In this case state should be null
66          assertNull(state);
67          
68          EventListenerWrapper wrapper2 = new EventListenerWrapper(component, component);
69          wrapper.markInitialState();
70          //For restore we need to setup the context first
71          component.pushComponentToEL(facesContext, component);
72          wrapper2.restoreState(facesContext, state);
73          component.popComponentFromEL(facesContext);
74          
75          assertNotNull(wrapper2.getComponentSystemEventListener());
76          assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
77      }
78      
79      public void testUIComponentListenerWithPSSFull()
80      {
81          UIComponent component = new UIOutput();
82          //This case happens when @ListenerFor is attached on the component class
83          EventListenerWrapper wrapper = new EventListenerWrapper(component, component);
84          
85          wrapper.markInitialState();
86          
87          wrapper.clearInitialState();
88          Object state = wrapper.saveState(facesContext);
89  
90          //In this case state should not be null, because state should be saved fully
91          assertNotNull(state);
92          
93          EventListenerWrapper wrapper2 = new EventListenerWrapper(component, component);
94          wrapper.markInitialState();
95          //For restore we need to setup the context first
96          component.pushComponentToEL(facesContext, component);
97          wrapper2.restoreState(facesContext, state);
98          component.popComponentFromEL(facesContext);
99          
100         assertNotNull(wrapper2.getComponentSystemEventListener());
101         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
102     }
103 
104     public static class MyCustomRenderer extends Renderer implements ComponentSystemEventListener {
105 
106         public void processEvent(ComponentSystemEvent event)
107         {
108             
109         }
110     }
111     
112     public void testRendererListenerNormalState()
113     {
114         UIComponent component = new UIOutput();
115         MyCustomRenderer renderer = new MyCustomRenderer();
116         component.setRendererType("org.apache.myfaces.MyCustomRenderer");
117         renderKit.addRenderer("javax.faces.Output", "org.apache.myfaces.MyCustomRenderer", renderer);
118         //This case happens when @ListenerFor is attached on the renderer class like h:outputScript or h:outputStylesheet
119         EventListenerWrapper wrapper = new EventListenerWrapper(component, renderer);
120         
121         Object state = wrapper.saveState(facesContext);
122 
123         //In this case state should not be null, because state should be saved fully
124         assertNotNull(state);
125 
126         EventListenerWrapper wrapper2 = new EventListenerWrapper();
127         //For restore we need to setup the context first
128         component.pushComponentToEL(facesContext, component);
129         wrapper2.restoreState(facesContext, state);
130         component.popComponentFromEL(facesContext);
131         
132         assertNotNull(wrapper2.getComponentSystemEventListener());
133         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
134     }
135     
136     public void testRendererListenerWithPSS()
137     {
138         UIComponent component = new UIOutput();
139         MyCustomRenderer renderer = new MyCustomRenderer();
140         component.setRendererType("org.apache.myfaces.MyCustomRenderer");
141         renderKit.addRenderer("javax.faces.Output", "org.apache.myfaces.MyCustomRenderer", renderer);
142         //This case happens when @ListenerFor is attached on the renderer class like h:outputScript or h:outputStylesheet
143         EventListenerWrapper wrapper = new EventListenerWrapper(component, renderer);
144         
145         wrapper.markInitialState();
146         Object state = wrapper.saveState(facesContext);
147 
148         //In this case state should be null
149         assertNull(state);
150         
151         EventListenerWrapper wrapper2 = new EventListenerWrapper(component, renderer);
152         wrapper.markInitialState();
153         //For restore we need to setup the context first
154         component.pushComponentToEL(facesContext, component);
155         wrapper2.restoreState(facesContext, state);
156         component.popComponentFromEL(facesContext);
157         
158         assertNotNull(wrapper2.getComponentSystemEventListener());
159         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
160     }
161     
162     public void testRendererListenerWithPSSFull()
163     {
164         UIComponent component = new UIOutput();
165         MyCustomRenderer renderer = new MyCustomRenderer();
166         component.setRendererType("org.apache.myfaces.MyCustomRenderer");
167         renderKit.addRenderer("javax.faces.Output", "org.apache.myfaces.MyCustomRenderer", renderer);
168         //This case happens when @ListenerFor is attached on the renderer class like h:outputScript or h:outputStylesheet
169         EventListenerWrapper wrapper = new EventListenerWrapper(component, renderer);
170         
171         wrapper.markInitialState();
172         
173         wrapper.clearInitialState();
174         Object state = wrapper.saveState(facesContext);
175 
176         //In this case state should not be null, because state should be saved fully
177         assertNotNull(state);
178         
179         EventListenerWrapper wrapper2 = new EventListenerWrapper(component, renderer);
180         wrapper.markInitialState();
181         //For restore we need to setup the context first
182         component.pushComponentToEL(facesContext, component);
183         wrapper2.restoreState(facesContext, state);
184         component.popComponentFromEL(facesContext);
185         
186         assertNotNull(wrapper2.getComponentSystemEventListener());
187         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
188     }
189     
190     public static class MyNonSerializableListener implements ComponentSystemEventListener {
191 
192         public void processEvent(ComponentSystemEvent event)
193         {
194             // TODO Auto-generated method stub
195             
196         }
197 
198         @Override
199         public boolean equals(Object obj)
200         {
201             return obj instanceof MyNonSerializableListener;
202         }
203         
204     }
205     
206     public void testNonSerializableListenerNormalState()
207     {
208         UIComponent component = new UIOutput();
209         ComponentSystemEventListener listener = new MyNonSerializableListener();
210         //This case happens when @ListenerFor is attached on the component class
211         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
212         
213         Object state = wrapper.saveState(facesContext);
214         
215         //In this case state should not be null, because state should be saved fully
216         assertNotNull(state);
217         
218         EventListenerWrapper wrapper2 = new EventListenerWrapper();
219         //For restore we need to setup the context first
220         component.pushComponentToEL(facesContext, component);
221         wrapper2.restoreState(facesContext, state);
222         component.popComponentFromEL(facesContext);
223         
224         assertNotNull(wrapper2.getComponentSystemEventListener());
225         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
226     }
227     
228     public void testNonSerializableListenerWithPSS()
229     {
230         UIComponent component = new UIOutput();
231         ComponentSystemEventListener listener = new MyNonSerializableListener();
232         //This case happens when @ListenerFor is attached on the component class
233         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
234         
235         wrapper.markInitialState();
236         Object state = wrapper.saveState(facesContext);
237         
238         //In this case state should be null
239         assertNull(state);
240         
241         EventListenerWrapper wrapper2 = new EventListenerWrapper(component, listener);
242         wrapper.markInitialState();
243         //For restore we need to setup the context first
244         component.pushComponentToEL(facesContext, component);
245         wrapper2.restoreState(facesContext, state);
246         component.popComponentFromEL(facesContext);
247         
248         assertNotNull(wrapper2.getComponentSystemEventListener());
249         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
250     }
251     
252     public void testNonSerializableListenerWithPSSFull()
253     {
254         UIComponent component = new UIOutput();
255         ComponentSystemEventListener listener = new MyNonSerializableListener();        
256         //This case happens when @ListenerFor is attached on the component class
257         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
258         
259         wrapper.markInitialState();
260         
261         wrapper.clearInitialState();
262         Object state = wrapper.saveState(facesContext);
263 
264         //In this case state should not be null, because state should be saved fully
265         assertNotNull(state);
266         
267         EventListenerWrapper wrapper2 = new EventListenerWrapper(component, listener);
268         wrapper.markInitialState();
269         //For restore we need to setup the context first
270         component.pushComponentToEL(facesContext, component);
271         wrapper2.restoreState(facesContext, state);
272         component.popComponentFromEL(facesContext);
273         
274         assertNotNull(wrapper2.getComponentSystemEventListener());
275         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
276     }
277 
278     public static class MySerializableListener implements ComponentSystemEventListener {
279 
280         public void processEvent(ComponentSystemEvent event)
281         {
282             // TODO Auto-generated method stub
283             
284         }
285 
286         @Override
287         public boolean equals(Object obj)
288         {
289             return obj instanceof MySerializableListener;
290         }
291         
292     }
293     
294     public void testSerializableListenerNormalState()
295     {
296         UIComponent component = new UIOutput();
297         ComponentSystemEventListener listener = new MySerializableListener();
298         //This case happens when @ListenerFor is attached on the component class
299         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
300         
301         Object state = wrapper.saveState(facesContext);
302         
303         //In this case state should not be null, because state should be saved fully
304         assertNotNull(state);
305         
306         EventListenerWrapper wrapper2 = new EventListenerWrapper();
307         //For restore we need to setup the context first
308         component.pushComponentToEL(facesContext, component);
309         wrapper2.restoreState(facesContext, state);
310         component.popComponentFromEL(facesContext);
311         
312         assertNotNull(wrapper2.getComponentSystemEventListener());
313         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
314     }
315     
316     public void testSerializableListenerWithPSS()
317     {
318         UIComponent component = new UIOutput();
319         ComponentSystemEventListener listener = new MySerializableListener();
320         //This case happens when @ListenerFor is attached on the component class
321         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
322         
323         wrapper.markInitialState();
324         Object state = wrapper.saveState(facesContext);
325         
326         //In this case state should be null
327         assertNull(state);
328         
329         EventListenerWrapper wrapper2 = new EventListenerWrapper(component, listener);
330         wrapper.markInitialState();
331         //For restore we need to setup the context first
332         component.pushComponentToEL(facesContext, component);
333         wrapper2.restoreState(facesContext, state);
334         component.popComponentFromEL(facesContext);
335         
336         assertNotNull(wrapper2.getComponentSystemEventListener());
337         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
338     }
339     
340     public void testSerializableListenerWithPSSFull()
341     {
342         UIComponent component = new UIOutput();
343         ComponentSystemEventListener listener = new MySerializableListener();        
344         //This case happens when @ListenerFor is attached on the component class
345         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
346         
347         wrapper.markInitialState();
348         
349         wrapper.clearInitialState();
350         Object state = wrapper.saveState(facesContext);
351 
352         //In this case state should not be null, because state should be saved fully
353         assertNotNull(state);
354         
355         EventListenerWrapper wrapper2 = new EventListenerWrapper(component, listener);
356         wrapper.markInitialState();
357         //For restore we need to setup the context first
358         component.pushComponentToEL(facesContext, component);
359         wrapper2.restoreState(facesContext, state);
360         component.popComponentFromEL(facesContext);
361         
362         assertNotNull(wrapper2.getComponentSystemEventListener());
363         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
364     }
365     
366     public static class MyStateHolderListener implements ComponentSystemEventListener, StateHolder {
367 
368         private Integer i = 1;
369         
370         public void setI(int value)
371         {
372             i = value;
373         }
374         
375         public void processEvent(ComponentSystemEvent event)
376         {
377             // TODO Auto-generated method stub
378             
379         }
380 
381         @Override
382         public boolean equals(Object obj)
383         {
384             if (obj instanceof MyStateHolderListener)
385             {
386                 return (this.i == ((MyStateHolderListener)obj).i); 
387             }
388             return false;
389         }
390 
391         public Object saveState(FacesContext context)
392         {
393             return i;
394         }
395 
396         public void restoreState(FacesContext context, Object state)
397         {
398             i = (Integer) state;
399         }
400 
401         public boolean isTransient()
402         {
403             return false;
404         }
405 
406         public void setTransient(boolean newTransientValue)
407         {
408         }
409         
410     }
411     
412     public void testStateHolderListenerNormalState()
413     {
414         UIComponent component = new UIOutput();
415         MyStateHolderListener listener = new MyStateHolderListener();
416         //This case happens when @ListenerFor is attached on the component class
417         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
418         
419         listener.setI(2);
420         Object state = wrapper.saveState(facesContext);
421         
422         //In this case state should not be null, because state should be saved fully
423         assertNotNull(state);
424         
425         EventListenerWrapper wrapper2 = new EventListenerWrapper();
426         //For restore we need to setup the context first
427         component.pushComponentToEL(facesContext, component);
428         wrapper2.restoreState(facesContext, state);
429         component.popComponentFromEL(facesContext);
430         
431         assertNotNull(wrapper2.getComponentSystemEventListener());
432         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
433     }
434     
435     public void testStateHolderListenerWithPSS()
436     {
437         UIComponent component = new UIOutput();
438         ComponentSystemEventListener listener = new MyStateHolderListener();
439         //This case happens when @ListenerFor is attached on the component class
440         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
441         
442         wrapper.markInitialState();
443         Object state = wrapper.saveState(facesContext);
444         
445         //In this case state should be not null, because it implements StateHolder
446         assertNotNull(state);
447         
448         MyStateHolderListener listener2 = new MyStateHolderListener();
449         listener2.setI(2);
450         EventListenerWrapper wrapper2 = new EventListenerWrapper(component, listener2);
451         wrapper.markInitialState();
452         //For restore we need to setup the context first
453         component.pushComponentToEL(facesContext, component);
454         wrapper2.restoreState(facesContext, state);
455         component.popComponentFromEL(facesContext);
456         
457         assertNotNull(wrapper2.getComponentSystemEventListener());
458         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
459     }
460     
461     public void testStateHolderListenerWithPSSFull()
462     {
463         UIComponent component = new UIOutput();
464         ComponentSystemEventListener listener = new MyStateHolderListener();        
465         //This case happens when @ListenerFor is attached on the component class
466         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
467         
468         wrapper.markInitialState();
469         
470         wrapper.clearInitialState();
471         Object state = wrapper.saveState(facesContext);
472 
473         //In this case state should not be null, because state should be saved fully
474         assertNotNull(state);
475         
476         EventListenerWrapper wrapper2 = new EventListenerWrapper(component, listener);
477         wrapper.markInitialState();
478         //For restore we need to setup the context first
479         component.pushComponentToEL(facesContext, component);
480         wrapper2.restoreState(facesContext, state);
481         component.popComponentFromEL(facesContext);
482         
483         assertNotNull(wrapper2.getComponentSystemEventListener());
484         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
485     }
486     
487     public static class MyPartialStateHolderListener implements ComponentSystemEventListener, PartialStateHolder {
488 
489         private Integer i = 1;
490         
491         private boolean markInitialState;
492         
493         public MyPartialStateHolderListener()
494         {
495         }
496 
497         public void setI(int value)
498         {
499             i = value;
500         }
501         
502         public void processEvent(ComponentSystemEvent event)
503         {
504             // TODO Auto-generated method stub
505             
506         }
507 
508         @Override
509         public boolean equals(Object obj)
510         {
511             if (obj instanceof MyPartialStateHolderListener)
512             {
513                 return (this.i == ((MyPartialStateHolderListener)obj).i); 
514             }
515             return false;
516         }
517 
518         public Object saveState(FacesContext context)
519         {
520             if (!initialStateMarked())
521             {
522                 return i;
523             }
524             else
525             {
526                 return i == 1 ? null : i;
527             }
528         }
529 
530         public void restoreState(FacesContext context, Object state)
531         {
532             if (state == null)
533             {
534                 return;
535             }
536             i = (Integer) state;
537         }
538 
539         public boolean isTransient()
540         {
541             return false;
542         }
543 
544         public void setTransient(boolean newTransientValue)
545         {
546         }
547 
548         public void clearInitialState()
549         {
550             markInitialState = false;
551         }
552 
553         public boolean initialStateMarked()
554         {
555             return markInitialState;
556         }
557 
558         public void markInitialState()
559         {
560             markInitialState = true;
561         }
562         
563     }
564     
565     public void testPartialStateHolderListenerNormalState()
566     {
567         UIComponent component = new UIOutput();
568         ComponentSystemEventListener listener = new MyPartialStateHolderListener();
569         //This case happens when @ListenerFor is attached on the component class
570         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
571         
572         Object state = wrapper.saveState(facesContext);
573         
574         //In this case state should not be null, because state should be saved fully
575         assertNotNull(state);
576         
577         EventListenerWrapper wrapper2 = new EventListenerWrapper();
578         //For restore we need to setup the context first
579         component.pushComponentToEL(facesContext, component);
580         wrapper2.restoreState(facesContext, state);
581         component.popComponentFromEL(facesContext);
582         
583         assertNotNull(wrapper2.getComponentSystemEventListener());
584         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
585     }
586     
587     public void testPartialStateHolderListenerWithPSS()
588     {
589         UIComponent component = new UIOutput();
590         ComponentSystemEventListener listener = new MyPartialStateHolderListener();
591         //This case happens when @ListenerFor is attached on the component class
592         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
593         
594         wrapper.markInitialState();
595         Object state = wrapper.saveState(facesContext);
596         
597         //In this case state should be null
598         assertNull(state);
599         
600         EventListenerWrapper wrapper2 = new EventListenerWrapper(component, listener);
601         wrapper.markInitialState();
602         //For restore we need to setup the context first
603         component.pushComponentToEL(facesContext, component);
604         wrapper2.restoreState(facesContext, state);
605         component.popComponentFromEL(facesContext);
606         
607         assertNotNull(wrapper2.getComponentSystemEventListener());
608         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
609     }
610 
611     public void testPartialStateHolderListenerWithPSS2()
612     {
613         UIComponent component = new UIOutput();
614         MyPartialStateHolderListener listener = new MyPartialStateHolderListener();
615         //This case happens when @ListenerFor is attached on the component class
616         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
617         
618         wrapper.markInitialState();
619         
620         listener.setI(2);
621         Object state = wrapper.saveState(facesContext);
622         
623         //In this case state should be not null, because something changed inside the listener
624         assertNotNull(state);
625         
626         EventListenerWrapper wrapper2 = new EventListenerWrapper(component, new MyPartialStateHolderListener());
627         wrapper.markInitialState();
628         //For restore we need to setup the context first
629         component.pushComponentToEL(facesContext, component);
630         wrapper2.restoreState(facesContext, state);
631         component.popComponentFromEL(facesContext);
632         
633         assertNotNull(wrapper2.getComponentSystemEventListener());
634         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
635     }
636     
637     public void testPartialStateHolderListenerWithPSSFull()
638     {
639         UIComponent component = new UIOutput();
640         ComponentSystemEventListener listener = new MyPartialStateHolderListener();        
641         //This case happens when @ListenerFor is attached on the component class
642         EventListenerWrapper wrapper = new EventListenerWrapper(component, listener);
643         
644         wrapper.markInitialState();
645         
646         wrapper.clearInitialState();
647         Object state = wrapper.saveState(facesContext);
648 
649         //In this case state should not be null, because state should be saved fully
650         assertNotNull(state);
651         
652         EventListenerWrapper wrapper2 = new EventListenerWrapper(component, listener);
653         wrapper.markInitialState();
654         //For restore we need to setup the context first
655         component.pushComponentToEL(facesContext, component);
656         wrapper2.restoreState(facesContext, state);
657         component.popComponentFromEL(facesContext);
658         
659         assertNotNull(wrapper2.getComponentSystemEventListener());
660         assertEquals(wrapper.getComponentSystemEventListener(), wrapper2.getComponentSystemEventListener());
661     }
662 }