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  
20  package org.apache.myfaces.view.facelets.tag.composite;
21  
22  import javax.el.ExpressionFactory;
23  import javax.faces.component.UICommand;
24  import javax.faces.component.UIComponent;
25  import javax.faces.component.UINamingContainer;
26  import javax.faces.component.UIViewRoot;
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.apache.myfaces.view.facelets.bean.HelloWorld;
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  public class CompositeComponentConditionalButtonTestCase extends FaceletTestCase
36  {
37  
38      @Override
39      protected void setupComponents() throws Exception
40      {
41          super.setupComponents();
42      }
43      
44      @Override
45      protected ExpressionFactory createExpressionFactory()
46      {
47          return new org.apache.el.ExpressionFactoryImpl();
48      }
49  
50      @Test
51      public void testConditionalButtonTargets() throws Exception
52      {
53          HelloWorld helloWorld = new HelloWorld(); 
54          
55          facesContext.getExternalContext().getRequestMap().put("helloWorldBean",
56                  helloWorld);
57          
58          UIViewRoot root = facesContext.getViewRoot();
59          vdl.buildView(facesContext, root, "testConditionalButtonTargets.xhtml");
60  
61          //The first component has a default command button
62          UIComponent form = root.findComponent("testForm1");
63          Assert.assertNotNull(form);
64          UINamingContainer compositeComponent1 = (UINamingContainer) form.findComponent("actionSource1");
65          Assert.assertNotNull(compositeComponent1);
66          UICommand button1 = (UICommand) compositeComponent1.findComponent("button");
67          Assert.assertNotNull(button1);
68          Assert.assertEquals("submit", button1.getActionExpression().invoke(facesContext.getELContext(), null));
69          
70          Assert.assertNotNull(button1.getActionListeners());
71          Assert.assertEquals(1, button1.getActionListeners().length);
72          
73          UINamingContainer compositeComponent2 = (UINamingContainer) form.findComponent("actionSource2");
74          Assert.assertNotNull(compositeComponent2);
75          UICommand button2 = (UICommand) compositeComponent2.findComponent("button");
76          Assert.assertNotNull(button2);
77          //Since the button is outside cc:implementation, by the spec it cannot be taken into account as a valid "targets" value.
78          Assert.assertEquals("fail", button2.getActionExpression().invoke(facesContext.getELContext(), null));
79          //It also cannot be target of cc:actionSource
80          Assert.assertNotNull(button2.getActionListeners());
81          Assert.assertEquals(0, button2.getActionListeners().length);
82  
83          //StringWriter sw = new StringWriter();
84          //MockResponseWriter mrw = new MockResponseWriter(sw);
85          //facesContext.setResponseWriter(mrw);
86          
87          //root.encodeAll(facesContext);
88          //sw.flush();
89          //System.out.print(sw.toString());
90      }
91  }