Author: gvanmatre Date: Mon Mar 6 13:29:16 2006 New Revision: 383678 URL: http://svn.apache.org/viewcvs?rev=383678&view=rev Log: Added a test for a comment nested in a HTML template.
Added: struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/layout.html (with props) Modified: struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/CommentTestCase.java struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/comment.html Modified: struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/CommentTestCase.java URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/CommentTestCase.java?rev=383678&r1=383677&r2=383678&view=diff ============================================================================== --- struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/CommentTestCase.java (original) +++ struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/CommentTestCase.java Mon Mar 6 13:29:16 2006 @@ -47,22 +47,30 @@ } + //family, renderer type, Myfaces Impl, Sun RI Impl protected String[][] RENDERERS = { {"javax.faces.Output", "javax.faces.Text", - "org.apache.myfaces.renderkit.html.HtmlTextRenderer", "com.sun.faces.renderkit.html_basic.TextRenderer",}, + "org.apache.myfaces.renderkit.html.HtmlTextRenderer", + "com.sun.faces.renderkit.html_basic.TextRenderer",}, {"javax.faces.Input", "javax.faces.Text", - "org.apache.myfaces.renderkit.html.HtmlTextRenderer", "com.sun.faces.renderkit.html_basic.TextRenderer"}, + "org.apache.myfaces.renderkit.html.HtmlTextRenderer", + "com.sun.faces.renderkit.html_basic.TextRenderer"}, {"javax.faces.Output", "javax.faces.Label", - "org.apache.myfaces.renderkit.html.HtmlLabelRenderer","com.sun.faces.renderkit.html_basic.LabelRenderer"} + "org.apache.myfaces.renderkit.html.HtmlLabelRenderer", + "com.sun.faces.renderkit.html_basic.LabelRenderer"} }; public void setUp() { super.setUp(); + //Utility class is loaded as a managed bean in shale_core/META-INF/faces-config.xml servletContext.setAttribute(ShaleConstants.TAG_UTILITY_BEAN, new Tags()); + + //It's necessary to explicitly set the kit id because it's lost when using the Sun RI. facesContext.getViewRoot().setRenderKitId(RenderKitFactory.HTML_BASIC_RENDER_KIT); + //loads the RI or myfaces renderers from the RENDERERS static array. for (int i = 0; i < RENDERERS.length; i++) { Renderer renderer = null; @@ -90,11 +98,13 @@ } + //Tests the processing of a comment in a HTML template public void testComment() throws Exception{ - + //done by the startup context listener loadConfigFiles(null, null); + Clay clay = (Clay) application.createComponent("org.apache.shale.clay.component.Clay"); clay.setId("test"); clay.setJsfid("/org/apache/shale/clay/config/comment.html"); @@ -110,6 +120,51 @@ //start a document buffResponsewriter.startDocument(); + //render HTML + clay.encodeBegin(facesContext); + clay.encodeChildren(facesContext); + clay.encodeEnd(facesContext); + + //end the document + buffResponsewriter.endDocument(); + + Parser p = new Parser(); + List nodes = p.parse(writer.getBuffer()); + assertEquals("1 root node", nodes.size(), 1); + + Node comment = findComment((Node) nodes.get(0)); + assertNotNull("comment found", comment); + assertTrue("is a comment", comment.isComment()); + + StringBuffer buff = concatCommentText(comment); + assertEquals(buff.toString(), "<!-- <span jsfid=\"outputText\"/> -->"); + + writer.close(); + + } + + //Tests the handling of a comment in a nested template. + public void testNested() throws Exception { + loadConfigFiles(null, null); + + Clay clay = (Clay) application.createComponent("org.apache.shale.clay.component.Clay"); + clay.setId("test"); + clay.setJsfid("/org/apache/shale/clay/config/layout.html"); + clay.setManagedBeanName("test"); + + //the clayJsfid in the layout.html is a early EL "#{viewId}" + request.setAttribute("viewId", "/org/apache/shale/clay/config/comment.html"); + + //builds a buffer to write the page to + StringWriter writer = new StringWriter(); + //create a buffered response writer + ResponseWriter buffResponsewriter = facesContext.getRenderKit() + .createResponseWriter(writer, null, response.getCharacterEncoding()); + //push buffered writer to the faces context + facesContext.setResponseWriter(buffResponsewriter); + //start a document + buffResponsewriter.startDocument(); + clay.encodeBegin(facesContext); clay.encodeChildren(facesContext); clay.encodeEnd(facesContext); @@ -120,9 +175,39 @@ Parser p = new Parser(); List nodes = p.parse(writer.getBuffer()); assertEquals("1 root node", nodes.size(), 1); - Node table = (Node) nodes.get(0); - assertEquals("2 root node", table.getChildren().size(), 2); - Node comment = (Node) table.getChildren().get(1); + + Node comment = findComment((Node) nodes.get(0)); + assertNotNull("comment found", comment); + assertTrue("is a comment", comment.isComment()); + + StringBuffer buff = concatCommentText(comment); + assertEquals(buff.toString(), "<!-- <span jsfid=\"outputText\"/> -->"); + + writer.close(); + } + + //Recursively traverse the parsed HTML document tree returning + //the first comment node. + private Node findComment(Node node) { + if (node.isComment()) + return node; + + Iterator ci = node.getChildren().iterator(); + while (ci.hasNext()) { + Node child = (Node) ci.next(); + Node comment = findComment(child); + if (comment != null) + return comment; + } + + return null; + + } + + //Concatenate all of the parsed tokens in the comment + //into a single StringBuffer + private StringBuffer concatCommentText(Node comment) { + assertTrue("is comment node", comment.isComment()); StringBuffer buff = new StringBuffer(); buff.append(comment.getToken().getRawText()); @@ -130,12 +215,9 @@ while (ci.hasNext()) { Node child = (Node) ci.next(); buff.append(child.getToken().getRawText()); - } - - assertEquals(buff.toString(), "<!-- <span jsfid=\"outputText\"/> -->"); - - writer.close(); + } + return buff; } } Modified: struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/comment.html URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/comment.html?rev=383678&r1=383677&r2=383678&view=diff ============================================================================== --- struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/comment.html (original) +++ struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/comment.html Mon Mar 6 13:29:16 2006 @@ -1 +1,9 @@ -<table><tr><td><label for=input id=label>Mock Address 1:</label></td><td><input id=input type=text size=45 value="test"></td></tr><!-- <span jsfid="outputText"/> --></table> \ No newline at end of file +<table> + <tr> + <td><label for=input id=label>Mock Address 1:</label></td> + <td><input id=input type=text size=45 value="test"></td> + </tr> + <tr> + <td><!-- <span jsfid="outputText"/> --></td> + </tr> +</table> \ No newline at end of file Added: struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/layout.html URL: http://svn.apache.org/viewcvs/struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/layout.html?rev=383678&view=auto ============================================================================== --- struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/layout.html (added) +++ struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/layout.html Mon Mar 6 13:29:16 2006 @@ -0,0 +1,8 @@ +<html> + <head><title>Test</title></head> + <body> + <span jsfid="clay" clayJsfid="#{viewId}" allowBody="false"> + Mock Body + </span> + </body> +</html> \ No newline at end of file Propchange: struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/layout.html ------------------------------------------------------------------------------ svn:eol-style = native Propchange: struts/shale/trunk/clay-plugin/src/test/org/apache/shale/clay/config/layout.html ------------------------------------------------------------------------------ svn:keywords = Date Author Id Revision HeadURL --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]