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.util.Iterator;
21  import java.util.List;
22  
23  import javax.faces.context.ResponseWriter;
24  import javax.faces.render.Renderer;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  import org.apache.shale.clay.component.Clay;
30  import org.apache.shale.clay.parser.Node;
31  import org.apache.shale.clay.parser.Parser;
32  
33  // test processing comment blocks
34  public class CommentTestCase extends AbstractTestCaseConfig {
35  
36      // Construct a new instance of this test case.
37      public CommentTestCase(String name) {
38          super(name);
39      }
40  
41      // Return the tests included in this test case.
42      public static Test suite() {
43  
44          return (new TestSuite(CommentTestCase.class));
45  
46      }
47  
48      //family, renderer type, Myfaces Impl, Sun RI Impl
49      protected String[][] RENDERERS = {
50              {"javax.faces.Output", "javax.faces.Text", 
51               "org.apache.myfaces.renderkit.html.HtmlTextRenderer", 
52               "com.sun.faces.renderkit.html_basic.TextRenderer",},        
53              {"javax.faces.Input", "javax.faces.Text", 
54               "org.apache.myfaces.renderkit.html.HtmlTextRenderer", 
55               "com.sun.faces.renderkit.html_basic.TextRenderer"},
56              {"javax.faces.Output", "javax.faces.Label", 
57               "org.apache.myfaces.renderkit.html.HtmlLabelRenderer",
58               "com.sun.faces.renderkit.html_basic.LabelRenderer"}
59              
60      };
61      
62      protected void setUp() throws Exception {
63          super.setUp();
64                          
65          //loads the RI or myfaces renderers from the RENDERERS static array.
66          for (int i = 0; i < RENDERERS.length; i++) {
67              
68              Renderer renderer = null;
69              renderer: for (int j = 2; j < 4; j++) {
70                  try {
71                      Class clazz = Class.forName(RENDERERS[i][j]);
72                      if (clazz != null) {
73                          renderer = (Renderer) clazz.newInstance();
74                          if (renderer != null) {
75                              //System.out.println(RENDERERS[i][j]);
76                              break renderer;
77                          }
78                      }
79                  } catch (ClassNotFoundException e) {
80                  } catch (InstantiationException e) {
81                  } catch (IllegalAccessException e) {
82                  }
83              }
84              
85              if (renderer != null) {
86                  facesContext.getRenderKit().addRenderer(RENDERERS[i][0], RENDERERS[i][1],
87                          renderer);
88              }
89          }        
90          
91      }
92  
93      //Tests the processing of a comment in a HTML template
94      public void testComment() throws Exception{
95          
96          //done by the startup context listener
97          loadConfigFiles(null, null);
98          
99          
100         Clay clay = (Clay) application.createComponent("org.apache.shale.clay.component.Clay");    
101         clay.setId("test");
102         clay.setJsfid("/org/apache/shale/clay/config/comment.html");
103         clay.setManagedBeanName("test");
104                 
105         //builds a buffer to write the page to
106         StringWriter writer = new StringWriter();
107         //create a buffered response writer
108         ResponseWriter buffResponsewriter = facesContext.getRenderKit()
109                         .createResponseWriter(writer, null, response.getCharacterEncoding());
110         //push buffered writer to the faces context
111         facesContext.setResponseWriter(buffResponsewriter);
112         //start a document
113         buffResponsewriter.startDocument();
114         
115         //render HTML
116         clay.encodeBegin(facesContext);
117         clay.encodeChildren(facesContext);
118         clay.encodeEnd(facesContext);
119         
120         //end the document
121         buffResponsewriter.endDocument();
122         
123         Parser p = new Parser();
124         List nodes = p.parse(writer.getBuffer());
125         assertEquals("1 root node", 1, nodes.size());
126 
127         Node comment = findComment((Node) nodes.get(0));
128         assertNotNull("comment found", comment);       
129         assertTrue("is a comment", comment.isComment());
130         
131         StringBuffer buff = concatCommentText(comment);      
132         assertEquals("<!-- <span jsfid=\"outputText\"/> -->", buff.toString());
133         
134         writer.close();
135         
136     } 
137     
138     //Tests the handling of a comment in a nested template.
139     public void testNested() throws Exception {
140         loadConfigFiles(null, null);
141         
142         Clay clay = (Clay) application.createComponent("org.apache.shale.clay.component.Clay");    
143         clay.setId("test");
144         clay.setJsfid("/org/apache/shale/clay/config/layout.html");
145         clay.setManagedBeanName("test");
146         
147         //the clayJsfid in the layout.html is a early EL "#{viewId}"
148         request.setAttribute("viewId", "/org/apache/shale/clay/config/comment.html");
149         
150         //builds a buffer to write the page to
151         StringWriter writer = new StringWriter();
152         //create a buffered response writer
153         ResponseWriter buffResponsewriter = facesContext.getRenderKit()
154         .createResponseWriter(writer, null, response.getCharacterEncoding());
155         //push buffered writer to the faces context
156         facesContext.setResponseWriter(buffResponsewriter);
157         //start a document
158         buffResponsewriter.startDocument();
159         
160         clay.encodeBegin(facesContext);
161         clay.encodeChildren(facesContext);
162         clay.encodeEnd(facesContext);
163         
164         //end the document
165         buffResponsewriter.endDocument();
166         
167         Parser p = new Parser();
168         List nodes = p.parse(writer.getBuffer());
169         assertEquals("1 root node", 1, nodes.size());
170         
171         Node comment = findComment((Node) nodes.get(0));
172         assertNotNull("comment found", comment);       
173         assertTrue("is a comment", comment.isComment());
174         
175         StringBuffer buff = concatCommentText(comment);      
176         assertEquals("<!-- <span jsfid=\"outputText\"/> -->", buff.toString());
177       
178         writer.close();
179     }
180     
181     //Recursively traverse the parsed HTML document tree returning 
182     //the first comment node.
183     private Node findComment(Node node) {
184         if (node.isComment())
185            return node;
186         
187         Iterator ci = node.getChildren().iterator();
188         while (ci.hasNext()) {
189            Node child = (Node) ci.next();
190            Node comment = findComment(child);
191            if (comment != null)
192               return comment;     
193         }
194         
195         return null;
196         
197     }
198     
199     //Concatenate all of the parsed tokens in the comment 
200     //into a single StringBuffer
201     private StringBuffer concatCommentText(Node comment) {
202         assertTrue("is comment node", comment.isComment());
203         
204         StringBuffer buff = new StringBuffer();
205         buff.append(comment.getToken().getRawText());
206         Iterator ci = comment.getChildren().iterator();
207         while (ci.hasNext()) {
208             Node child = (Node) ci.next();
209             buff.append(child.getToken().getRawText());    
210         } 
211         
212         return buff;
213     }
214     
215 
216 }