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.view.facelets.tag.composite;
20  
21  import javax.el.ExpressionFactory;
22  import javax.faces.component.UICommand;
23  import javax.faces.component.UIComponent;
24  import javax.faces.component.UINamingContainer;
25  import javax.faces.component.UIViewRoot;
26  import javax.faces.event.ActionEvent;
27  
28  import org.apache.myfaces.config.RuntimeConfig;
29  import org.apache.myfaces.test.mock.MockExternalContext;
30  import org.apache.myfaces.view.facelets.FaceletTestCase;
31  import org.junit.Assert;
32  import org.junit.Test;
33  
34  public class CompositeComponentActionListenerTestCase extends FaceletTestCase
35  {
36      @Override
37      protected void setupComponents() throws Exception
38      {
39          super.setupComponents();
40          application.addComponent(CompositeTestComponent.class.getName(), 
41                  CompositeTestComponent.class.getName());
42          application.addComponent(SimpleComponent.class.getName(), SimpleComponent.class.getName());
43      }
44  
45      @Override
46      protected void setUpServletObjects() throws Exception
47      {
48          super.setUpServletObjects();
49          servletContext.addInitParameter("javax.faces.FACELETS_LIBRARIES", "/test-facelet.taglib.xml");
50      }
51      
52      @Override
53      protected ExpressionFactory createExpressionFactory()
54      {
55          return new org.apache.el.ExpressionFactoryImpl();
56      }
57      
58      @Test
59      public void testSimpleActionListenerTarget() throws Exception
60      {
61          MockAttributeBean bean = new MockAttributeBean();
62          
63          facesContext.getExternalContext().getRequestMap().put("bean",
64                  bean);
65          
66          UIViewRoot root = facesContext.getViewRoot();
67          vdl.buildView(facesContext, root, "testSimpleAttributeActionListenerTarget.xhtml");
68  
69          
70          UIComponent panelGroup1 = root.findComponent("testGroup1");
71          Assert.assertNotNull(panelGroup1);
72          UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
73          Assert.assertNotNull(compositeComponent1);
74          UICommand button1 = (UICommand) compositeComponent1.findComponent("testComponent");
75          Assert.assertNotNull(button1);
76          Assert.assertNotNull(button1.getActionListeners());
77          Assert.assertEquals(1, button1.getActionListeners().length);
78          
79          bean.setActionListener1Called(false);
80          button1.getActionListeners()[0].processAction(new ActionEvent(button1));
81          Assert.assertTrue(bean.isActionListener1Called());
82  
83          UIComponent panelGroup2 = root.findComponent("testGroup2");
84          Assert.assertNotNull(panelGroup2);
85          UINamingContainer compositeComponent2 = (UINamingContainer) panelGroup2.getChildren().get(0);
86          Assert.assertNotNull(compositeComponent2);
87          UICommand button2 = (UICommand) compositeComponent2.findComponent("testComponent");
88          Assert.assertNotNull(button2);
89          Assert.assertNotNull(button2.getActionListeners());
90          Assert.assertEquals(1, button2.getActionListeners().length);
91          
92          bean.setActionListener2Called(false);
93          button2.getActionListeners()[0].processAction(new ActionEvent(button2));
94          Assert.assertTrue(bean.isActionListener2Called());
95      }
96  
97      @Test
98      public void testCompositeActionListenerTarget() throws Exception
99      {
100         MockAttributeBean bean = new MockAttributeBean();
101         
102         facesContext.getExternalContext().getRequestMap().put("bean",
103                 bean);
104         
105         UIViewRoot root = facesContext.getViewRoot();
106         vdl.buildView(facesContext, root, "testCompositeAttributeActionListenerTarget.xhtml");
107 
108         UIComponent panelGroup1 = root.findComponent("testGroup1");
109         Assert.assertNotNull(panelGroup1);
110         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
111         Assert.assertNotNull(compositeComponent1);
112         
113         UINamingContainer compositeComponent1inner = (UINamingContainer) compositeComponent1.findComponent("simpleAttributeMethodExpressionTarget");
114         Assert.assertNotNull(compositeComponent1inner);
115         UICommand button1 = (UICommand) compositeComponent1inner.findComponent("testComponent");
116         Assert.assertNotNull(button1);
117         Assert.assertNotNull(button1.getActionListeners());
118         Assert.assertEquals(1, button1.getActionListeners().length);
119         
120         bean.setActionListener1Called(false);
121         button1.getActionListeners()[0].processAction(new ActionEvent(button1));
122         Assert.assertTrue(bean.isActionListener1Called());
123 
124         UIComponent panelGroup2 = root.findComponent("testGroup2");
125         Assert.assertNotNull(panelGroup2);
126         UINamingContainer compositeComponent2 = (UINamingContainer) panelGroup2.getChildren().get(0);
127         Assert.assertNotNull(compositeComponent2);
128         
129         UINamingContainer compositeComponent2inner = (UINamingContainer) compositeComponent2.findComponent("simpleAttributeMethodExpressionTarget");
130         Assert.assertNotNull(compositeComponent2inner);
131         UICommand button2 = (UICommand) compositeComponent2inner.findComponent("testComponent");
132         Assert.assertNotNull(button2);
133         Assert.assertNotNull(button2.getActionListeners());
134         Assert.assertEquals(1, button2.getActionListeners().length);
135         
136         bean.setActionListener2Called(false);
137         button2.getActionListeners()[0].processAction(new ActionEvent(button2));
138         Assert.assertTrue(bean.isActionListener2Called());
139 
140     }
141     
142     @Test
143     public void testSimpleActionListenerTarget2() throws Exception
144     {
145         MockAttributeBean bean = new MockAttributeBean();
146         
147         facesContext.getExternalContext().getRequestMap().put("bean",
148                 bean);
149         
150         UIViewRoot root = facesContext.getViewRoot();
151         vdl.buildView(facesContext, root, "testSimpleAttributeActionListenerTarget2.xhtml");
152 
153         
154         UIComponent panelGroup1 = root.findComponent("testGroup1");
155         Assert.assertNotNull(panelGroup1);
156         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
157         Assert.assertNotNull(compositeComponent1);
158         UICommand button1 = (UICommand) compositeComponent1.findComponent("testComponent");
159         Assert.assertNotNull(button1);
160         Assert.assertNotNull(button1.getActionListeners());
161         Assert.assertEquals(1, button1.getActionListeners().length);
162         
163         bean.setActionListener1Called(false);
164         button1.getActionListeners()[0].processAction(new ActionEvent(button1));
165         Assert.assertTrue(bean.isActionListener1Called());
166         
167         UICommand button1n = (UICommand) compositeComponent1.findComponent("testComponentNoTarget");
168         
169         Assert.assertNotNull(button1n);
170         Assert.assertNotNull(button1n.getActionListeners());
171         Assert.assertEquals(1, button1n.getActionListeners().length);
172         
173         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
174         bean.setActionListener1Called(false);
175         button1n.getActionListeners()[0].processAction(new ActionEvent(button1n));
176         Assert.assertTrue(bean.isActionListener1Called());
177         compositeComponent1.popComponentFromEL(facesContext);
178         
179         UIComponent panelGroup2 = root.findComponent("testGroup2");
180         Assert.assertNotNull(panelGroup2);
181         UINamingContainer compositeComponent2 = (UINamingContainer) panelGroup2.getChildren().get(0);
182         Assert.assertNotNull(compositeComponent2);
183         UICommand button2 = (UICommand) compositeComponent2.findComponent("testComponent");
184         Assert.assertNotNull(button2);
185         Assert.assertNotNull(button2.getActionListeners());
186         Assert.assertEquals(1, button2.getActionListeners().length);
187         
188         bean.setActionListener2Called(false);
189         button2.getActionListeners()[0].processAction(new ActionEvent(button2));
190         Assert.assertTrue(bean.isActionListener2Called());
191         
192         UICommand button2n = (UICommand) compositeComponent2.findComponent("testComponentNoTarget");
193         
194         Assert.assertNotNull(button2n);
195         Assert.assertNotNull(button2n.getActionListeners());
196         Assert.assertEquals(1, button2n.getActionListeners().length);
197         
198         compositeComponent2.pushComponentToEL(facesContext, compositeComponent2);
199         bean.setActionListener2Called(false);
200         button2n.getActionListeners()[0].processAction(new ActionEvent(button2n));
201         Assert.assertTrue(bean.isActionListener2Called());
202         compositeComponent2.popComponentFromEL(facesContext);
203     }
204 
205     @Test
206     public void testCompositeActionListenerTarget2() throws Exception
207     {
208         MockAttributeBean bean = new MockAttributeBean();
209         
210         facesContext.getExternalContext().getRequestMap().put("bean",
211                 bean);
212         
213         UIViewRoot root = facesContext.getViewRoot();
214         vdl.buildView(facesContext, root, "testCompositeAttributeActionListenerTarget2.xhtml");
215 
216         UIComponent panelGroup1 = root.findComponent("testGroup1");
217         Assert.assertNotNull(panelGroup1);
218         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
219         Assert.assertNotNull(compositeComponent1);
220         
221         UINamingContainer compositeComponent1target = (UINamingContainer) compositeComponent1.findComponent("simpleAttributeMethodExpressionTarget");
222         Assert.assertNotNull(compositeComponent1target);
223         UICommand button1target = (UICommand) compositeComponent1target.findComponent("testComponent");
224         Assert.assertNotNull(button1target);
225         Assert.assertNotNull(button1target.getActionListeners());
226         Assert.assertEquals(1, button1target.getActionListeners().length);
227         
228         bean.setActionListener1Called(false);
229         button1target.getActionListeners()[0].processAction(new ActionEvent(button1target));
230         Assert.assertTrue(bean.isActionListener1Called());
231 
232         UICommand button1notarget = (UICommand) compositeComponent1target.findComponent("testComponentNoTarget");
233         Assert.assertNotNull(button1notarget);
234         Assert.assertNotNull(button1notarget.getActionListeners());
235         Assert.assertEquals(1, button1notarget.getActionListeners().length);
236         
237         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
238         compositeComponent1target.pushComponentToEL(facesContext, compositeComponent1target);
239         bean.setActionListener1Called(false);
240         button1notarget.getActionListeners()[0].processAction(new ActionEvent(button1notarget));
241         Assert.assertTrue(bean.isActionListener1Called());
242         compositeComponent1target.popComponentFromEL(facesContext);
243         compositeComponent1.popComponentFromEL(facesContext);
244         
245         UINamingContainer compositeComponent1notarget = (UINamingContainer) compositeComponent1.findComponent("simpleAttributeMethodExpressionNoTarget");
246         Assert.assertNotNull(compositeComponent1notarget);
247         UICommand buttonnotarget1target = (UICommand) compositeComponent1notarget.findComponent("testComponent");
248         Assert.assertNotNull(buttonnotarget1target);
249         Assert.assertNotNull(buttonnotarget1target.getActionListeners());
250         Assert.assertEquals(1, buttonnotarget1target.getActionListeners().length);
251         
252         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
253         compositeComponent1notarget.pushComponentToEL(facesContext, compositeComponent1notarget);
254         bean.setActionListener1Called(false);
255         buttonnotarget1target.getActionListeners()[0].processAction(new ActionEvent(buttonnotarget1target));
256         Assert.assertTrue(bean.isActionListener1Called());
257         compositeComponent1notarget.popComponentFromEL(facesContext);
258         compositeComponent1.popComponentFromEL(facesContext);
259         
260         UICommand buttonnotarget1notarget = (UICommand) compositeComponent1notarget.findComponent("testComponentNoTarget");
261         Assert.assertNotNull(buttonnotarget1notarget);
262         Assert.assertNotNull(buttonnotarget1notarget.getActionListeners());
263         Assert.assertEquals(1, buttonnotarget1notarget.getActionListeners().length);
264         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
265         compositeComponent1notarget.pushComponentToEL(facesContext, compositeComponent1notarget);
266         bean.setActionListener1Called(false);
267         buttonnotarget1notarget.getActionListeners()[0].processAction(new ActionEvent(buttonnotarget1notarget));
268         Assert.assertTrue(bean.isActionListener1Called());
269         compositeComponent1notarget.popComponentFromEL(facesContext);
270         compositeComponent1.popComponentFromEL(facesContext);
271         
272         UIComponent panelGroup2 = root.findComponent("testGroup2");
273         Assert.assertNotNull(panelGroup2);
274         UINamingContainer compositeComponent2 = (UINamingContainer) panelGroup2.getChildren().get(0);
275         Assert.assertNotNull(compositeComponent2);
276         
277         UINamingContainer compositeComponent2target = (UINamingContainer) compositeComponent2.findComponent("simpleAttributeMethodExpressionTarget");
278         Assert.assertNotNull(compositeComponent2target);
279         UICommand button2target = (UICommand) compositeComponent2target.findComponent("testComponent");
280         Assert.assertNotNull(button2target);
281         Assert.assertNotNull(button2target.getActionListeners());
282         Assert.assertEquals(1, button2target.getActionListeners().length);
283         
284         bean.setActionListener2Called(false);
285         button2target.getActionListeners()[0].processAction(new ActionEvent(button2target));
286         Assert.assertTrue(bean.isActionListener2Called());
287 
288         UICommand button2notarget = (UICommand) compositeComponent2target.findComponent("testComponentNoTarget");
289         Assert.assertNotNull(button2notarget);
290         Assert.assertNotNull(button2notarget.getActionListeners());
291         Assert.assertEquals(1, button2notarget.getActionListeners().length);
292         
293         compositeComponent2.pushComponentToEL(facesContext, compositeComponent2);
294         compositeComponent2target.pushComponentToEL(facesContext, compositeComponent2target);
295         bean.setActionListener2Called(false);
296         button2notarget.getActionListeners()[0].processAction(new ActionEvent(button2notarget));
297         Assert.assertTrue(bean.isActionListener2Called());
298         compositeComponent2target.popComponentFromEL(facesContext);
299         compositeComponent2.popComponentFromEL(facesContext);
300         
301         UINamingContainer compositeComponent2notarget = (UINamingContainer) compositeComponent2.findComponent("simpleAttributeMethodExpressionNoTarget");
302         Assert.assertNotNull(compositeComponent2notarget);
303         UICommand buttonnotarget2target = (UICommand) compositeComponent2notarget.findComponent("testComponent");
304         Assert.assertNotNull(buttonnotarget2target);
305         Assert.assertNotNull(buttonnotarget2target.getActionListeners());
306         Assert.assertEquals(1, buttonnotarget2target.getActionListeners().length);
307         
308         compositeComponent2.pushComponentToEL(facesContext, compositeComponent2);
309         compositeComponent2notarget.pushComponentToEL(facesContext, compositeComponent2notarget);
310         bean.setActionListener2Called(false);
311         buttonnotarget2target.getActionListeners()[0].processAction(new ActionEvent(buttonnotarget2target));
312         Assert.assertTrue(bean.isActionListener2Called());
313         compositeComponent2notarget.popComponentFromEL(facesContext);
314         compositeComponent2.popComponentFromEL(facesContext);
315         
316         UICommand buttonnotarget2notarget = (UICommand) compositeComponent2notarget.findComponent("testComponentNoTarget");
317         Assert.assertNotNull(buttonnotarget2notarget);
318         Assert.assertNotNull(buttonnotarget2notarget.getActionListeners());
319         Assert.assertEquals(1, buttonnotarget2notarget.getActionListeners().length);
320         compositeComponent2.pushComponentToEL(facesContext, compositeComponent2);
321         compositeComponent2notarget.pushComponentToEL(facesContext, compositeComponent2notarget);
322         bean.setActionListener2Called(false);
323         buttonnotarget2notarget.getActionListeners()[0].processAction(new ActionEvent(buttonnotarget2notarget));
324         Assert.assertTrue(bean.isActionListener2Called());
325         compositeComponent2notarget.popComponentFromEL(facesContext);
326         compositeComponent2.popComponentFromEL(facesContext);
327     }
328     
329     @Test
330     public void testSimpleActionListenerNoTarget() throws Exception
331     {
332         MockAttributeBean bean = new MockAttributeBean();
333         
334         facesContext.getExternalContext().getRequestMap().put("bean",
335                 bean);
336         
337         UIViewRoot root = facesContext.getViewRoot();
338         vdl.buildView(facesContext, root, "testSimpleAttributeActionListenerNoTarget.xhtml");
339 
340         UIComponent panelGroup1 = root.findComponent("testGroup1");
341         Assert.assertNotNull(panelGroup1);
342         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
343         Assert.assertNotNull(compositeComponent1);
344         UICommand button1 = (UICommand) compositeComponent1.findComponent("testComponentNoTarget");
345         Assert.assertNotNull(button1);
346         Assert.assertNotNull(button1.getActionListeners());
347         Assert.assertEquals(1, button1.getActionListeners().length);
348         
349         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
350         button1.pushComponentToEL(facesContext,  button1);
351         bean.setActionListener1Called(false);
352         button1.getActionListeners()[0].processAction(new ActionEvent(button1));
353         Assert.assertTrue(bean.isActionListener1Called());
354         button1.popComponentFromEL(facesContext);
355         compositeComponent1.popComponentFromEL(facesContext);
356 
357         UIComponent panelGroup2 = root.findComponent("testGroup2");
358         Assert.assertNotNull(panelGroup2);
359         UINamingContainer compositeComponent2 = (UINamingContainer) panelGroup2.getChildren().get(0);
360         Assert.assertNotNull(compositeComponent2);
361         UICommand button2 = (UICommand) compositeComponent2.findComponent("testComponentNoTarget");
362         Assert.assertNotNull(button2);
363         Assert.assertNotNull(button2.getActionListeners());
364         Assert.assertEquals(1, button2.getActionListeners().length);
365 
366         
367         compositeComponent2.pushComponentToEL(facesContext, compositeComponent2);
368         button2.pushComponentToEL(facesContext,  button2);
369         bean.setActionListener2Called(false);
370         button2.getActionListeners()[0].processAction(new ActionEvent(button2));
371         Assert.assertTrue(bean.isActionListener2Called());
372         button2.popComponentFromEL(facesContext);
373         compositeComponent2.popComponentFromEL(facesContext);
374 
375     }
376     
377     @Test
378     public void testCompositeActionListenerNoTarget() throws Exception
379     {
380         MockAttributeBean bean = new MockAttributeBean();
381         
382         facesContext.getExternalContext().getRequestMap().put("bean",
383                 bean);
384         
385         UIViewRoot root = facesContext.getViewRoot();
386         vdl.buildView(facesContext, root, "testCompositeAttributeActionListenerNoTarget.xhtml");
387 
388         UIComponent panelGroup1 = root.findComponent("testGroup1");
389         Assert.assertNotNull(panelGroup1);
390         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
391         Assert.assertNotNull(compositeComponent1);
392 
393         UINamingContainer compositeComponent1inner = (UINamingContainer) compositeComponent1.findComponent("simpleAttributeActionMethodExpressionNoTarget");
394         Assert.assertNotNull(compositeComponent1inner);
395         UICommand testComponentNoTarget1 = (UICommand) compositeComponent1inner.findComponent("testComponentNoTarget");
396         Assert.assertNotNull(testComponentNoTarget1);
397         Assert.assertNotNull(testComponentNoTarget1.getActionListeners());
398         Assert.assertEquals(1, testComponentNoTarget1.getActionListeners().length);
399 
400         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
401         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1inner);
402         bean.setActionListener1Called(false);
403         testComponentNoTarget1.getActionListeners()[0].processAction(new ActionEvent(testComponentNoTarget1));
404         Assert.assertTrue(bean.isActionListener1Called());
405         compositeComponent1inner.popComponentFromEL(facesContext);
406         compositeComponent1.popComponentFromEL(facesContext);
407 
408         UIComponent panelGroup2 = root.findComponent("testGroup2");
409         Assert.assertNotNull(panelGroup2);
410         UINamingContainer compositeComponent2 = (UINamingContainer) panelGroup2.getChildren().get(0);
411         Assert.assertNotNull(compositeComponent2);
412 
413         UINamingContainer compositeComponent2inner = (UINamingContainer) compositeComponent2.findComponent("simpleAttributeActionMethodExpressionNoTarget");
414         Assert.assertNotNull(compositeComponent2inner);
415         UICommand testComponentNoTarget2 = (UICommand) compositeComponent2inner.findComponent("testComponentNoTarget");
416         Assert.assertNotNull(testComponentNoTarget2);
417         Assert.assertNotNull(testComponentNoTarget2.getActionListeners());
418         Assert.assertEquals(1, testComponentNoTarget2.getActionListeners().length);
419 
420         compositeComponent2.pushComponentToEL(facesContext, compositeComponent2);
421         compositeComponent2.pushComponentToEL(facesContext, compositeComponent2inner);
422         bean.setActionListener2Called(false);
423         testComponentNoTarget2.getActionListeners()[0].processAction(new ActionEvent(testComponentNoTarget2));
424         Assert.assertTrue(bean.isActionListener2Called());
425         compositeComponent2inner.popComponentFromEL(facesContext);
426         compositeComponent2.popComponentFromEL(facesContext);
427     
428     }
429 
430     @Test
431     public void testCompositeActionListenerNoTarget2() throws Exception
432     {
433         MockAttributeBean bean = new MockAttributeBean();
434         
435         facesContext.getExternalContext().getRequestMap().put("bean",
436                 bean);
437         
438         UIViewRoot root = facesContext.getViewRoot();
439         vdl.buildView(facesContext, root, "testCompositeAttributeActionListenerNoTarget2.xhtml");
440         
441         UIComponent panelGroup1 = root.findComponent("testGroup1");
442         Assert.assertNotNull(panelGroup1);
443         UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
444         Assert.assertNotNull(compositeComponent1);
445         
446         UINamingContainer compositeComponent1n1 = (UINamingContainer) compositeComponent1.findComponent("compositeAttributeMethodExpressionNoTarget");
447         Assert.assertNotNull(compositeComponent1n1);
448         UINamingContainer compositeComponent1n2 = (UINamingContainer) compositeComponent1n1.findComponent("simpleAttributeMethodExpressionNoTarget");
449         Assert.assertNotNull(compositeComponent1n2);
450         UICommand button1 = (UICommand) compositeComponent1n2.findComponent("testComponentNoTarget");
451         Assert.assertNotNull(button1);
452         Assert.assertNotNull(button1.getActionListeners());
453         Assert.assertEquals(1, button1.getActionListeners().length);
454         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1);
455         compositeComponent1.pushComponentToEL(facesContext, compositeComponent1n2);
456         bean.setActionListener1Called(false);
457         button1.getActionListeners()[0].processAction(new ActionEvent(button1));
458         Assert.assertTrue(bean.isActionListener1Called());
459         compositeComponent1n2.popComponentFromEL(facesContext);
460         compositeComponent1.popComponentFromEL(facesContext);
461     }
462 }