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.renderkit.html;
20  
21  import java.io.StringWriter;
22  
23  import javax.faces.component.UIParameter;
24  import javax.faces.component.html.HtmlOutputFormat;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
30  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
31  import org.apache.myfaces.test.base.AbstractJsfTestCase;
32  import org.apache.myfaces.test.mock.MockRenderKitFactory;
33  import org.apache.myfaces.test.mock.MockResponseWriter;
34  
35  public class HtmlFormatRendererTest extends AbstractJsfTestCase
36  {
37      private MockResponseWriter writer;
38      private HtmlOutputFormat outputFormat;
39      
40      public HtmlFormatRendererTest(String name)
41      {
42          super(name);
43      }
44      
45      public static Test suite() {
46          return new TestSuite(HtmlFormatRendererTest.class);
47      }
48      
49      public void setUp() throws Exception {
50          super.setUp();
51          outputFormat = new HtmlOutputFormat();
52          writer = new MockResponseWriter(new StringWriter(), null, null);
53          facesContext.setResponseWriter(writer);
54          
55          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
56          facesContext.getRenderKit().addRenderer(
57                  outputFormat.getFamily(),
58                  outputFormat.getRendererType(),
59                  new HtmlFormatRenderer());
60          
61          facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
62      }
63      
64      public void tearDown() throws Exception {
65          super.tearDown();
66      }
67      
68      public void testHtmlPropertyPassTru() throws Exception
69      {
70          HtmlRenderedAttr[] attrs = {
71                  //_UniversalProperties
72                  new HtmlRenderedAttr("dir"), 
73                  new HtmlRenderedAttr("lang"), 
74                  new HtmlRenderedAttr("title"),
75                  //_StyleProperties
76                  new HtmlRenderedAttr("style"), 
77                  new HtmlRenderedAttr("styleClass", "styleClass", "class=\"styleClass\""),
78              };
79          
80          outputFormat.setValue("outputdata");
81          HtmlCheckAttributesUtil.checkRenderedAttributes(
82                  outputFormat, facesContext, writer, attrs);
83          if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
84              fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
85          }
86      }
87      
88      public void testHtmlPropertyPassTruNotRendered() throws Exception
89      {
90          HtmlRenderedAttr[] attrs = HtmlCheckAttributesUtil.generateAttrsNotRenderedForReadOnly();
91          
92          outputFormat.setValue("outputdata");
93          HtmlCheckAttributesUtil.checkRenderedAttributes(
94                  outputFormat, facesContext, writer, attrs);
95          if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
96              fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
97          }
98      }
99      
100     /**
101      * If the disable attribute of a child UIParameter is true,
102      * he should be ignored.
103      * @throws Exception
104      */
105     public void testDisabledUIParameterNotRendered() throws Exception
106     {
107         UIParameter param1 = new UIParameter();
108         param1.setValue("value1");
109         param1.setDisable(true);
110         UIParameter param2 = new UIParameter();
111         param2.setValue("value2");
112         outputFormat.getChildren().add(param1);
113         outputFormat.getChildren().add(param2);
114         
115         outputFormat.setValue("prefix{0}-{1}suffix");
116         
117         outputFormat.encodeAll(facesContext);
118         String output = writer.getWriter().toString();
119         assertEquals("prefixvalue2-{1}suffix", output);
120     }
121 }