2009/05/20 - Apache Shale has been retired.
For more information, please explore the Attic. 
 
1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
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  
34  public class CommentTestCase extends AbstractTestCaseConfig {
35  
36      
37      public CommentTestCase(String name) {
38          super(name);
39      }
40  
41      
42      public static Test suite() {
43  
44          return (new TestSuite(CommentTestCase.class));
45  
46      }
47  
48      
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          
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                              
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      
94      public void testComment() throws Exception{
95          
96          
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         
106         StringWriter writer = new StringWriter();
107         
108         ResponseWriter buffResponsewriter = facesContext.getRenderKit()
109                         .createResponseWriter(writer, null, response.getCharacterEncoding());
110         
111         facesContext.setResponseWriter(buffResponsewriter);
112         
113         buffResponsewriter.startDocument();
114         
115         
116         clay.encodeBegin(facesContext);
117         clay.encodeChildren(facesContext);
118         clay.encodeEnd(facesContext);
119         
120         
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     
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         
148         request.setAttribute("viewId", "/org/apache/shale/clay/config/comment.html");
149         
150         
151         StringWriter writer = new StringWriter();
152         
153         ResponseWriter buffResponsewriter = facesContext.getRenderKit()
154         .createResponseWriter(writer, null, response.getCharacterEncoding());
155         
156         facesContext.setResponseWriter(buffResponsewriter);
157         
158         buffResponsewriter.startDocument();
159         
160         clay.encodeBegin(facesContext);
161         clay.encodeChildren(facesContext);
162         clay.encodeEnd(facesContext);
163         
164         
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     
182     
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     
200     
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 }