Author: amarkhel
Date: 2009-08-21 14:25:38 -0400 (Fri, 21 Aug 2009)
New Revision: 15267
Added:
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/TemplateReaderTest.java
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/template-simple.xml
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/template.xml
Log:
Add test to Template Renderer
Added:
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/TemplateReaderTest.java
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/TemplateReaderTest.java
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/TemplateReaderTest.java 2009-08-21
18:25:38 UTC (rev 15267)
@@ -0,0 +1,108 @@
+package org.richfaces.cdk;
+
+
+import static org.junit.Assert.*;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.richfaces.builder.model.JavaClass;
+import org.richfaces.builder.model.JavaMethod;
+import org.richfaces.builder.templates.TemplateReader;
+
+public class TemplateReaderTest {
+
+ private static final String IF_FRAGMENT = "if (";
+ private static final String START_DIV_FRAGMENT =
"responseWriter.startElement(\"div\", component);";
+ private static final String START_TABLE_FRAGMENT =
"responseWriter.startElement(\"table\", component);";
+ private static final String END_DIV_FRAGMENT =
"responseWriter.endElement(\"div\");";
+ private static final String END_TABLE_FRAGMENT =
"responseWriter.endElement(\"table\");";
+ private static final String WRITE_ATTRIBBUTE_FRAGMENT =
"responseWriter.writeAttribute(\"id\", component.getClientId(context),
\"id\");";
+ private static final String TEMPLATE_XML = "template.xml";
+ private static final String TEMPLATE_SIMPLE_XML = "template-simple.xml";
+ private static final String FULL_NAME =
"org.richfaces.renderkit.html.TabPanelRenderer";
+ private static final String SUPERCLASS = "javax.faces.render.Renderer";
+ private static final String PACKAGE = "org.richfaces.renderkit.html";
+ private static final String TAB_PANEL_RENDERER = "TabPanelRenderer";
+ private static final String ENCODE_CHILDREN_METHOD = "encodeChildren";
+ private static final String ENCODE_END_METHOD = "encodeEnd";
+ private static final String ENCODE_BEGIN_METHOD = "encodeBegin";
+ InputStream stream = null;
+
+ @Before
+ public void create() {
+ }
+
+ @After
+ public void destroy(){
+ try {
+ stream.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ @Test
+ public void testSimpleTemplate() throws Exception {
+ stream = this.getClass().getResourceAsStream(TEMPLATE_SIMPLE_XML);
+ JavaClass claz = TemplateReader.parse(stream);
+ /*JavaClassRenderer renderer = new JavaClassRenderer();
+ renderer.render(claz, new PrintWriter(System.out));*/
+ assertEquals(claz.getFullName(), FULL_NAME);
+ assertEquals(claz.getName(), TAB_PANEL_RENDERER);
+ assertEquals(claz.getPakg().getName(), PACKAGE);
+ assertEquals(claz.getSuperClass().getFullName(), SUPERCLASS);
+ assertTrue(containMethod(claz, ENCODE_BEGIN_METHOD));
+ assertTrue(containMethod(claz, ENCODE_END_METHOD));
+ assertTrue(containMethod(claz, ENCODE_CHILDREN_METHOD));
+ }
+
+ @Test
+ public void testFullTemplate() throws Exception {
+ stream = this.getClass().getResourceAsStream(TEMPLATE_XML);
+ JavaClass claz = TemplateReader.parse(stream);
+ /*JavaClassRenderer renderer = new JavaClassRenderer();
+ renderer.render(claz, new PrintWriter(System.err));*/
+ assertEquals(claz.getFullName(), FULL_NAME);
+ assertEquals(claz.getName(), TAB_PANEL_RENDERER);
+ assertEquals(claz.getPakg().getName(), PACKAGE);
+ assertEquals(claz.getSuperClass().getFullName(), SUPERCLASS);
+ assertTrue(containMethod(claz, ENCODE_BEGIN_METHOD));
+ assertTrue(containMethod(claz, ENCODE_CHILDREN_METHOD));
+ assertTrue(containMethod(claz, ENCODE_END_METHOD));
+ JavaMethod method = getMethodByName(claz, ENCODE_CHILDREN_METHOD);
+ assertNotNull(method);
+ String code = method.getMethodBody().toCode();
+ assertTrue(code.lastIndexOf(IF_FRAGMENT) != -1);
+ assertTrue(code.lastIndexOf(START_TABLE_FRAGMENT) != -1);
+ assertTrue(code.lastIndexOf(WRITE_ATTRIBBUTE_FRAGMENT) != -1);
+ assertTrue(code.lastIndexOf(START_DIV_FRAGMENT) != -1);
+ assertTrue(code.lastIndexOf(END_TABLE_FRAGMENT) != -1);
+ assertTrue(code.lastIndexOf(END_DIV_FRAGMENT) != -1);
+ assertTrue(code.lastIndexOf(START_TABLE_FRAGMENT) <
code.lastIndexOf(START_DIV_FRAGMENT));
+ assertTrue(code.lastIndexOf(WRITE_ATTRIBBUTE_FRAGMENT) <
code.lastIndexOf(START_DIV_FRAGMENT));
+ assertTrue(code.lastIndexOf(WRITE_ATTRIBBUTE_FRAGMENT) <
code.lastIndexOf(END_TABLE_FRAGMENT));
+ assertTrue(code.lastIndexOf(END_TABLE_FRAGMENT) >
code.lastIndexOf(END_DIV_FRAGMENT));
+ }
+
+ private JavaMethod getMethodByName(JavaClass clazz, String name) {
+ for(JavaMethod method : clazz.getMethods()){
+ if(method.getName().equals(name)){
+ return method;
+ }
+ }
+ return null;
+ }
+
+ private boolean containMethod(JavaClass clazz, String name){
+ for(JavaMethod method : clazz.getMethods()){
+ if(method.getName().equals(name)){
+ return true;
+ }
+ }
+ return false;
+ }
+}
Property changes on:
root/cdk/trunk/plugins/generator/src/test/java/org/richfaces/cdk/TemplateReaderTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Author Id Revision Date
Name: svn:eol-style
+ native
Added:
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/template-simple.xml
===================================================================
---
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/template-simple.xml
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/template-simple.xml 2009-08-21
18:25:38 UTC (rev 15267)
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<cdk:root
xmlns:cdk="http://richfaces.org/cdk"
+ class="org.richfaces.renderkit.html.TabPanelRenderer"
componentclass="org.richfaces.component.UITabPanel">
+ <cdk:body>
+ <table border="0" cellpadding="0" cellspacing="0"
id="#{clientId}"
+ class="rich-tabpanel #{component.attributes['styleClass']}"/>
+</cdk:body>
+</cdk:root>
\ No newline at end of file
Property changes on:
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/template-simple.xml
___________________________________________________________________
Name: svn:mime-type
+ text/xml
Name: svn:eol-style
+ native
Added: root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/template.xml
===================================================================
--- root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/template.xml
(rev 0)
+++
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/template.xml 2009-08-21
18:25:38 UTC (rev 15267)
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<cdk:root
xmlns:cdk="http://richfaces.org/cdk"
+ class="org.richfaces.renderkit.html.TabPanelRenderer"
+ componentclass="org.richfaces.component.UITabPanel">
+<cdk:body>
+<cdk:clientid var="clientId" />
+<table border="0" cellpadding="0" cellspacing="0"
id="#{clientId}"
+ class="rich-tabpanel #{component.attributes['styleClass']}">
+ <tbody>
+ <tr>
+ <td align="#{component.attributes['headerAlignment']}"
class="rich-tab-bottom-line
#{component.attributes['headerClass']}">
+ <div style="display: none;">
+ <cdk:if test="#{clientSide}">
+ <input autocomplete="off" type="hidden"
+ id="#{clientId}_input"
+ name="#{clientId}"
+ value=" " />
+ </cdk:if>
+ </div>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+ </cdk:body>
+</cdk:root>
\ No newline at end of file
Property changes on:
root/cdk/trunk/plugins/generator/src/test/resources/org/richfaces/cdk/template.xml
___________________________________________________________________
Name: svn:mime-type
+ text/xml
Name: svn:eol-style
+ native