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.jsf.html;
21  
22  import java.io.StringWriter;
23  import javax.el.MethodExpression;
24  import javax.faces.component.ActionSource2;
25  import javax.faces.component.UIComponent;
26  import javax.faces.component.UIForm;
27  import javax.faces.component.UIOutput;
28  import javax.faces.component.UIParameter;
29  import javax.faces.component.UIViewRoot;
30  import javax.faces.component.html.HtmlCommandButton;
31  import javax.faces.component.html.HtmlForm;
32  import javax.faces.component.html.HtmlOutputText;
33  import javax.faces.component.html.HtmlPanelGrid;
34  
35  import org.apache.myfaces.renderkit.html.HtmlButtonRenderer;
36  import org.apache.myfaces.renderkit.html.HtmlFormRenderer;
37  import org.apache.myfaces.renderkit.html.HtmlGridRenderer;
38  import org.apache.myfaces.renderkit.html.HtmlTextRenderer;
39  import org.apache.myfaces.test.mock.MockResponseWriter;
40  import org.apache.myfaces.view.facelets.FaceletTestCase;
41  import org.junit.Assert;
42  import org.junit.Test;
43  
44  public class HtmlTestCase extends FaceletTestCase {
45      
46      @Override
47      protected void setupComponents() throws Exception
48      {
49          application.addComponent(UIViewRoot.COMPONENT_TYPE,
50                  UIViewRoot.class.getName());
51          application.addComponent(HtmlCommandButton.COMPONENT_TYPE,
52                  HtmlCommandButton.class.getName());
53          application.addComponent(HtmlForm.COMPONENT_TYPE,
54                  HtmlForm.class.getName());
55          application.addComponent(HtmlPanelGrid.COMPONENT_TYPE,
56                  HtmlPanelGrid.class.getName());
57          application.addComponent(HtmlOutputText.COMPONENT_TYPE,
58                  HtmlOutputText.class.getName());
59          application.addComponent(UIParameter.COMPONENT_TYPE,
60                  UIParameter.class.getName());
61      }
62  
63      @Override
64      protected void setupConvertersAndValidators() throws Exception
65      {
66      }
67  
68      @Override
69      protected void setupRenderers() throws Exception
70      {
71          renderKit.addRenderer(UIOutput.COMPONENT_FAMILY,
72                  "javax.faces.Text", new HtmlTextRenderer());
73          renderKit.addRenderer(UIForm.COMPONENT_FAMILY,
74                  "javax.faces.Form", new HtmlFormRenderer());
75          renderKit.addRenderer(HtmlCommandButton.COMPONENT_FAMILY,
76                  "javax.faces.Button", new HtmlButtonRenderer());
77          renderKit.addRenderer(HtmlPanelGrid.COMPONENT_FAMILY,
78                  "javax.faces.Grid", new HtmlGridRenderer());
79      }    
80      
81      @Test
82      public void testCommandComponent() throws Exception {
83          request.getSession().setAttribute("test", new MockBean());
84          UIViewRoot root = facesContext.getViewRoot();
85          vdl.buildView(facesContext, root, "componentOwner.xml");
86          
87          UIComponent c = root.findComponent("cmd");
88          Assert.assertNotNull("cmd", c);
89          
90          Object v = c.getAttributes().get("id");
91          Assert.assertEquals("id", "cmd", v);
92          
93          ActionSource2 as2 = (ActionSource2) c;
94          MethodExpression me = as2.getActionExpression();
95          Assert.assertNotNull("method", me);
96          
97          String result = (String) me.invoke(facesContext.getELContext(), null);
98          //System.out.println(result);
99      }
100     
101     @Test
102     public void testCommandButton() throws Exception {
103         UIViewRoot root = facesContext.getViewRoot();
104         vdl.buildView(facesContext, root, "commandButton.xml");
105         
106         UIComponent c = root.findComponent("form:button");
107         Assert.assertNotNull("button", c);
108         
109         Object v = c.getAttributes().get("id");
110         Assert.assertEquals("id", "button", v);
111     }
112 
113     @Test
114     public void testPanelGrid() throws Exception {
115         UIViewRoot root = facesContext.getViewRoot();
116         vdl.buildView(facesContext, root, "panelGrid.xml");
117     }
118     
119     @Test
120     public void testEmptyHtml() throws Exception {
121         UIViewRoot root = facesContext.getViewRoot();
122         vdl.buildView(facesContext, root, "testEmptyHtmlAttribute.xhtml");
123         
124         StringWriter sw = new StringWriter();
125         MockResponseWriter mrw = new MockResponseWriter(sw);
126         facesContext.setResponseWriter(mrw);
127         
128         root.encodeAll(facesContext);
129         sw.flush();
130 
131         Assert.assertTrue(sw.toString().contains("alt=\"\""));
132     }
133             
134 
135 }