2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to you under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.shale.clay.config;
18  
19  import java.io.StringWriter;
20  import java.net.URL;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import javax.faces.context.ResponseWriter;
25  import javax.faces.render.Renderer;
26  
27  import junit.framework.Test;
28  import junit.framework.TestSuite;
29  
30  import org.apache.shale.clay.component.Clay;
31  import org.apache.shale.clay.parser.Node;
32  import org.apache.shale.clay.parser.Parser;
33  
34  // test logic to handle encoding types in document templates
35  public class CharsetTestCase extends AbstractTestCaseConfig {
36  
37      // Construct a new instance of this test case.
38      public CharsetTestCase(String name) {
39          super(name);
40      }
41  
42      // Return the tests included in this test case.
43      public static Test suite() {
44  
45          return (new TestSuite(CharsetTestCase.class));
46  
47      }
48  
49      //family, renderer type, Myfaces Impl, Sun RI Impl
50      protected String[][] RENDERERS = {
51              {"javax.faces.Output", "javax.faces.Text", 
52               "org.apache.myfaces.renderkit.html.HtmlTextRenderer", 
53               "com.sun.faces.renderkit.html_basic.TextRenderer",},        
54              {"javax.faces.Input", "javax.faces.Text", 
55               "org.apache.myfaces.renderkit.html.HtmlTextRenderer", 
56               "com.sun.faces.renderkit.html_basic.TextRenderer"},
57              {"javax.faces.Output", "javax.faces.Label", 
58               "org.apache.myfaces.renderkit.html.HtmlLabelRenderer",
59               "com.sun.faces.renderkit.html_basic.LabelRenderer"}
60              
61      };
62      
63      protected void setUp() throws Exception {
64          super.setUp();
65                          
66          //loads the RI or myfaces renderers from the RENDERERS static array.
67          for (int i = 0; i < RENDERERS.length; i++) {
68              
69              Renderer renderer = null;
70              renderer: for (int j = 2; j < 4; j++) {
71                  try {
72                      Class clazz = Class.forName(RENDERERS[i][j]);
73                      if (clazz != null) {
74                          renderer = (Renderer) clazz.newInstance();
75                          if (renderer != null) {
76                              //System.out.println(RENDERERS[i][j]);
77                              break renderer;
78                          }
79                      }
80                  } catch (ClassNotFoundException e) {
81                  } catch (InstantiationException e) {
82                  } catch (IllegalAccessException e) {
83                  }
84              }
85              
86              if (renderer != null) {
87                  facesContext.getRenderKit().addRenderer(RENDERERS[i][0], RENDERERS[i][1],
88                          renderer);
89              }
90          }        
91          
92      }
93  
94      public void testFindCharacterEncoding() throws Exception {
95          //done by the startup context listener
96          loadConfigFiles(null, null);
97            
98          ClayTemplateParser parser = new ClayTemplateParser();
99          parser.setConfig(htmlTemplateConfigBean);
100 
101         
102         URL noOverrideURL = facesContext.getExternalContext().getResource("/org/apache/shale/clay/config/comment.html");
103         String enc = parser.getCharacterEncoding(noOverrideURL);
104         assertEquals("VM default", System.getProperty("file.encoding"), enc);
105         
106         servletContext.addInitParameter(Globals.CLAY_HTML_CHARSET, "UTF-16");
107         enc = parser.getCharacterEncoding(noOverrideURL);
108         assertEquals("web.xml override", "UTF-16", enc);
109 
110         servletContext.addInitParameter(Globals.CLAY_HTML_CHARSET, "BOGUS");
111         enc = parser.getCharacterEncoding(noOverrideURL);
112         assertEquals("web.xml bogus override", System.getProperty("file.encoding"), enc);
113             
114         URL overrideURL = facesContext.getExternalContext().getResource("/org/apache/shale/clay/config/some-utf-8.html");
115         enc = parser.getCharacterEncoding(overrideURL);
116         assertEquals("template override", "UTF-8", enc);
117 
118     }
119     
120     
121     public void testUtf8() throws Exception{
122         
123         //done by the startup context listener
124         loadConfigFiles(null, null);
125         response.setCharacterEncoding("UTF-8");
126         
127         
128         Clay clay = (Clay) application.createComponent("org.apache.shale.clay.component.Clay");    
129         clay.setId("test");
130         clay.setJsfid("/org/apache/shale/clay/config/some-utf-8.html");
131         clay.setManagedBeanName("test");
132                 
133         //builds a buffer to write the page to
134         StringWriter writer = new StringWriter();
135         //create a buffered response writer
136         ResponseWriter buffResponsewriter = facesContext.getRenderKit()
137                         .createResponseWriter(writer, null, response.getCharacterEncoding());
138         //push buffered writer to the faces context
139         facesContext.setResponseWriter(buffResponsewriter);
140         //start a document
141         buffResponsewriter.startDocument();
142         
143         //render HTML
144         clay.encodeBegin(facesContext);
145         clay.encodeChildren(facesContext);
146         clay.encodeEnd(facesContext);
147         
148         //end the document
149         buffResponsewriter.endDocument();
150         
151         Parser p = new Parser();
152         List nodes = p.parse(writer.getBuffer());
153         assertEquals("2 root node", 2, nodes.size());
154 
155         Node paragraph = (Node) nodes.get(1);
156         assertNotNull(paragraph);
157         
158         // contact rendered output into a buffer
159         StringBuffer snippet = concatText(paragraph);
160         assertNotNull(snippet);
161         
162         int i = snippet.indexOf("\u0119");
163         assertTrue("polish language char \u0119", i > -1);        
164                 
165         i = snippet.indexOf("\u00f3");
166         assertTrue("polish language char \u00f3", i > -1);        
167                 
168         i = snippet.indexOf("\u0107");
169         assertTrue("polish language char \u0107", i > -1);        
170         
171         i = snippet.indexOf("\u017a");
172         assertTrue("polish language char \u017a", i > -1);        
173 
174         i = snippet.indexOf("\u017c");
175         assertTrue("polish language char \u017c", i > -1);        
176 
177         i = snippet.indexOf("\u015b");
178         assertTrue("polish language char \u015b", i > -1);        
179 
180         i = snippet.indexOf("\u0105");
181         assertTrue("polish language char \u0105", i > -1);        
182 
183         i = snippet.indexOf("\u221e");
184         assertTrue("infinity char \u221e", i > -1);        
185         
186         i = snippet.indexOf("\u03c6");
187         assertTrue("small greek letter Phi char \u03c6", i > -1);        
188 
189         i = snippet.indexOf("\u03c6");
190         assertTrue("integral sign char \u03c6", i > -1);        
191 
192         writer.close();
193         
194     } 
195     
196     private StringBuffer concatText(Node node) {
197         
198         StringBuffer buff = new StringBuffer();
199         buff.append(node.getToken().getRawText());
200         Iterator ci = node.getChildren().iterator();
201         while (ci.hasNext()) {
202             Node child = (Node) ci.next();
203             buff.append(child.getToken().getRawText());    
204         } 
205         
206         return buff;
207     }
208     
209            
210 
211 }