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.compiler;
21  
22  import java.io.StringWriter;
23  
24  import javax.faces.component.UIForm;
25  import javax.faces.component.UIOutput;
26  import javax.faces.component.UIPanel;
27  import javax.faces.component.UISelectItem;
28  import javax.faces.component.UISelectOne;
29  import javax.faces.component.UIViewRoot;
30  import javax.faces.component.html.HtmlForm;
31  import javax.faces.component.html.HtmlOutputText;
32  import javax.faces.component.html.HtmlPanelGrid;
33  import javax.faces.component.html.HtmlSelectOneMenu;
34  
35  import junit.framework.Assert;
36  
37  import org.apache.myfaces.config.RuntimeConfig;
38  import org.apache.myfaces.config.impl.digester.elements.FaceletsProcessing;
39  import org.apache.myfaces.renderkit.html.HtmlFormRenderer;
40  import org.apache.myfaces.renderkit.html.HtmlGridRenderer;
41  import org.apache.myfaces.renderkit.html.HtmlMenuRenderer;
42  import org.apache.myfaces.renderkit.html.HtmlTextRenderer;
43  import org.apache.myfaces.test.mock.MockResponseWriter;
44  import org.apache.myfaces.view.facelets.FaceletTestCase;
45  import org.junit.Test;
46  
47  public class XMLFaceletsProcessingTestCase extends FaceletTestCase {
48  
49      @Override
50      protected void setupComponents() throws Exception
51      {
52          application.addComponent(UIViewRoot.COMPONENT_TYPE,
53                  UIViewRoot.class.getName());
54          application.addComponent(HtmlForm.COMPONENT_TYPE,
55                  HtmlForm.class.getName());
56          application.addComponent(HtmlPanelGrid.COMPONENT_TYPE,
57                  HtmlPanelGrid.class.getName());
58          application.addComponent(HtmlSelectOneMenu.COMPONENT_TYPE,
59                  HtmlSelectOneMenu.class.getName());
60          application.addComponent(UISelectItem.COMPONENT_TYPE,
61                  UISelectItem.class.getName()); 
62          application.addComponent(HtmlOutputText.COMPONENT_TYPE,
63                  HtmlOutputText.class.getName());
64      }
65  
66      @Override
67      protected void setupRenderers() throws Exception
68      {
69          renderKit.addRenderer(UIForm.COMPONENT_FAMILY,
70                  "javax.faces.Form", new HtmlFormRenderer());
71          renderKit.addRenderer(UIOutput.COMPONENT_FAMILY,
72                  "javax.faces.Text", new HtmlTextRenderer());
73          renderKit.addRenderer(UISelectOne.COMPONENT_FAMILY,
74                  "javax.faces.Menu", new HtmlMenuRenderer());        
75          renderKit.addRenderer(UIPanel.COMPONENT_FAMILY,
76                  "javax.faces.Grid", new HtmlGridRenderer());
77          
78      } 
79      
80      
81  
82      @Override
83      protected void setUpExternalContext() throws Exception
84      {
85          super.setUpExternalContext();
86          
87          FaceletsProcessing item = new FaceletsProcessing();
88          item.setFileExtension(".view.xml");
89          item.setProcessAs(FaceletsProcessing.PROCESS_AS_XML);
90          RuntimeConfig.getCurrentInstance(externalContext).addFaceletProcessingConfiguration(FaceletsProcessing.PROCESS_AS_XML, item);
91      }
92  
93      @Test
94      public void testXMLProcessing1() throws Exception
95      {
96          UIViewRoot root = facesContext.getViewRoot();
97          vdl.buildView(facesContext, root, "testXMLProcessing1.view.xml");
98  
99          StringWriter sw = new StringWriter();
100         MockResponseWriter mrw = new MockResponseWriter(sw);
101         facesContext.setResponseWriter(mrw);
102 
103         root.encodeAll(facesContext);
104 
105         sw.flush();
106         
107         String resp = sw.toString();
108         
109         Assert.assertFalse("Response contains DOCTYPE declaration", resp.contains("<!DOCTYPE"));
110         Assert.assertFalse("Response contains xml declaration", resp.contains("<?xml"));
111         Assert.assertFalse("Response contains xml processing instructions", resp.contains("<?name"));
112         Assert.assertFalse("Response contains cdata section", resp.contains("<![CDATA["));
113         Assert.assertFalse("Response contains cdata section", resp.contains("cdata not consumed"));
114         Assert.assertTrue("Response does not escape characters", resp.contains("In this mode, if you put a double quote, it will be replaced by &quot; : &quot"));
115         Assert.assertFalse("Response contains comments", resp.contains("<!--"));
116         
117     }
118 }