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.html.HtmlDoctype;
24  
25  import junit.framework.Test;
26  import junit.framework.TestSuite;
27  
28  import org.apache.myfaces.test.base.AbstractJsfTestCase;
29  import org.apache.myfaces.test.mock.MockRenderKitFactory;
30  import org.apache.myfaces.test.mock.MockResponseWriter;
31  import org.apache.myfaces.test.utils.HtmlCheckAttributesUtil;
32  import org.apache.myfaces.test.utils.HtmlRenderedAttr;
33  import org.junit.Assert;
34  
35  /**
36   * @author Leonardo Uribe
37   */
38  public class HtmlDoctypeRendererTest extends AbstractJsfTestCase
39  {
40  
41      private MockResponseWriter writer ;
42      private HtmlDoctype doctype;
43  
44      public HtmlDoctypeRendererTest(String name)
45      {
46          super(name);
47      }
48      
49      public static Test suite() {
50          return new TestSuite(HtmlDoctypeRendererTest.class);
51      }
52  
53      public void setUp() throws Exception
54      {
55          super.setUp();
56  
57          doctype = new HtmlDoctype();
58  
59          writer = new MockResponseWriter(new StringWriter(), null, null);
60          facesContext.setResponseWriter(writer);
61  
62          facesContext.getViewRoot().setRenderKitId(MockRenderKitFactory.HTML_BASIC_RENDER_KIT);
63          facesContext.getRenderKit().addRenderer(
64                  doctype.getFamily(),
65                  doctype.getRendererType(),
66                  new HtmlDoctypeRenderer());
67          
68          facesContext.getAttributes().put("org.apache.myfaces.RENDERED_JSF_JS", Boolean.TRUE);
69      }
70  
71      public void tearDown()throws Exception
72      {
73          super.tearDown();
74          writer = null;
75      }
76      
77      public void testHtmlPropertyPassTru() throws Exception
78      { 
79          HtmlRenderedAttr[] attrs = {
80                  new HtmlRenderedAttr("rootElement","rootElement", "rootElement"),
81                  new HtmlRenderedAttr("public","-//W3C//DTD XHTML 1.0 Transitional//EN", "\"-//W3C//DTD XHTML 1.0 Transitional//EN\""),
82                  new HtmlRenderedAttr("system","http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"")
83          };
84  
85          HtmlCheckAttributesUtil.checkRenderedAttributes(
86                  doctype, facesContext, writer, attrs);
87          if(HtmlCheckAttributesUtil.hasFailedAttrRender(attrs)) {
88              fail(HtmlCheckAttributesUtil.constructErrorMessage(attrs, writer.getWriter().toString()));
89          }
90          
91          Assert.assertTrue("Does not match with: <!DOCTYPE rootElement PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">",
92                  writer.getWriter().toString().contains("<!DOCTYPE rootElement PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"));
93      }
94      
95      public void testHtml5Doctype() throws Exception
96      {
97          doctype.setRootElement("html");
98          
99          doctype.encodeAll(facesContext);
100         facesContext.renderResponse();
101         
102         Assert.assertTrue("Does not match with: <!DOCTYPE html>",
103                 writer.getWriter().toString().contains("<!DOCTYPE html>"));
104     }
105 
106 }