Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Jakarta-velocity Wiki" 
for change notification.

The following page has been changed by ShinobuKawaiYoshida:
http://wiki.apache.org/jakarta-velocity/TestingVelocity

The comment on the change is:
VTL snippets.

------------------------------------------------------------------------------
  
  Check out the 
[http://svn.apache.org/repos/asf/jakarta/velocity/trunk/src/java/org/apache/velocity/test/
 test source code] for more on how this is done.
  
+ '''''Approach 3:'''''  VTL snippets.
+ 
+ Instead of testing the whole template, I came up with a way to test template 
snippets.  This was more or less used as learning tests.  There are three 
classes used: !VelocityTestTool, !AbstractVelocityTestCase and 
!AbstractVelocityMockStrutsTestCase.  Developers will subclass 
!AbstractVelocity*!TestCase and do the usuall JUnit assertion like this:
+ {{{
+ public class EscapeToolTest extends AbstractVelocityTestCase
+ {
+     public void testStringJava() throws Exception
+     {
+         this.getContext().put("ctx", this.getContext());
+         this.getContext().put("escape", new EscapeTool());
+         this.getContext().put("java", "He didn't say, \"Stop!\"");
+ 
+         this.setTemplate("" +
+                 "$escape.java($java)\n" +
+                 "");
+ 
+         this.setExpected("" +
+                 "He didn't say, \\\"Stop!\\\"\n" +
+                 "");
+ 
+         this.assertVelocity();
+     }
+ }
+ }}}
+ You may have noticed that the snippets are set as java String objects, so 
there is no need for template files.  (But you will need to escape special java 
characters.)
+ 
+ Here is what each class looks like:
+  * !VelocityTestTool.java
+ {{{
+ public class VelocityTestTool
+ {
+ 
+     public static VelocityEngine newEngine()
+     {
+         VelocityEngine engine = new VelocityEngine();
+ 
+         engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, 
"org.apache.velocity.tools.generic.log.CommonsLogLogSystem");
+         engine.setProperty("runtime.log.logsystem.commons.logging.name", 
"test.velocity.log");
+ 
+         return engine;
+     }
+ 
+     public static void initSingletonLogger()
+     {
+         Velocity.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, 
"org.apache.velocity.tools.generic.log.CommonsLogLogSystem");
+         Velocity.setProperty("runtime.log.logsystem.commons.logging.name", 
"test.velocity.log");
+     }
+ 
+ }
+ }}}
+  * !AbstractVelocityTestCase.java
+ {{{
+ public abstract class AbstractVelocityTestCase extends TestCase
+ {
+ 
+     private Log log = LogFactory.getLog(this.getClass());
+     protected final Log getLog()
+     {
+         return this.log;
+     }
+ 
+     /* (non-Javadoc)
+      * @see junit.framework.TestCase#setUp()
+      */
+     protected void setUp() throws Exception
+     {
+         super.setUp();
+ 
+         this.setEngine(VelocityTestTool.newEngine());
+         this.setTemplate("");
+         this.setContext(new VelocityContext());
+         this.setExpected("");
+     }
+ 
+     /* (non-Javadoc)
+      * @see junit.framework.TestCase#tearDown()
+      */
+     protected void tearDown() throws Exception
+     {
+         this.setEngine(null);
+         this.setTemplate(null);
+         this.setContext(null);
+         this.setExpected(null);
+ 
+         super.tearDown();
+     }
+ 
+     private VelocityEngine engine = null;
+     private String template = "";
+     private Context context = null;
+     private String expected = null;
+ 
+     /**
+      * @return Returns the engine.
+      */
+     protected VelocityEngine getEngine()
+     {
+         return this.engine;
+     }
+     /**
+      * @param engine The engine to set.
+      */
+     protected void setEngine(VelocityEngine engine)
+     {
+         this.engine = engine;
+     }
+     /**
+      * @return Returns the template.
+      */
+     protected String getTemplate()
+     {
+         return this.template;
+     }
+     /**
+      * @param template The template to set.
+      */
+     protected void setTemplate(String template)
+     {
+         this.template = template;
+     }
+     /**
+      * @return Returns the context.
+      */
+     protected Context getContext()
+     {
+         return this.context;
+     }
+     /**
+      * @param context The context to set.
+      */
+     protected void setContext(Context context)
+     {
+         this.context = context;
+     }
+     /**
+      * @return Returns the expected.
+      */
+     protected String getExpected()
+     {
+         return this.expected;
+     }
+     /**
+      * @param expected The expected to set.
+      */
+     protected void setExpected(String expected)
+     {
+         this.expected = expected;
+     }
+ 
+     protected void assertVelocity() throws ParseErrorException, 
MethodInvocationException, ResourceNotFoundException, IOException, Exception
+     {
+         this.getEngine().init();
+ 
+         StringWriter writer = new StringWriter();
+         assertTrue(this.getEngine().evaluate(this.getContext(), writer, 
this.getName(), this.getTemplate()));
+ 
+         assertEquals(this.getExpected(), String.valueOf(writer));
+     }
+ }
+ }}}
+  * !AbstractVelocityMockStrutsTestCase.java
+ {{{
+ public abstract class AbstractVelocityMockStrutsTestCase extends 
MockStrutsTestCase
+ {
+ 
+     private Log log = LogFactory.getLog(this.getClass());
+     protected final Log getLog()
+     {
+         return this.log;
+     }
+ 
+     /* (non-Javadoc)
+      * @see junit.framework.TestCase#setUp()
+      */
+     protected void setUp() throws Exception
+     {
+         super.setUp();
+ 
+         this.setEngine(VelocityTestTool.newEngine());
+         this.setTemplate("");
+         this.setContext(new ChainedContext(new VelocityContext(), 
this.getEngine(), this.getRequest(), this.getResponse(), 
this.getActionServlet().getServletContext()));
+         this.setExpected("");
+     }
+ 
+     /* (non-Javadoc)
+      * @see junit.framework.TestCase#tearDown()
+      */
+     protected void tearDown() throws Exception
+     {
+         this.setEngine(null);
+         this.setTemplate(null);
+         this.setContext(null);
+         this.setExpected(null);
+ 
+         super.tearDown();
+     }
+ 
+     private VelocityEngine engine = null;
+     private String template = "";
+     private Context context = null;
+     private String expected = null;
+ 
+     /**
+      * @return Returns the engine.
+      */
+     protected VelocityEngine getEngine()
+     {
+         return this.engine;
+     }
+     /**
+      * @param engine The engine to set.
+      */
+     protected void setEngine(VelocityEngine engine)
+     {
+         this.engine = engine;
+     }
+     /**
+      * @return Returns the template.
+      */
+     protected String getTemplate()
+     {
+         return this.template;
+     }
+     /**
+      * @param template The template to set.
+      */
+     protected void setTemplate(String template)
+     {
+         this.template = template;
+     }
+     /**
+      * @return Returns the context.
+      */
+     protected Context getContext()
+     {
+         return this.context;
+     }
+     /**
+      * @param context The context to set.
+      */
+     protected void setContext(Context context)
+     {
+         this.context = context;
+     }
+     /**
+      * @return Returns the expected.
+      */
+     protected String getExpected()
+     {
+         return this.expected;
+     }
+     /**
+      * @param expected The expected to set.
+      */
+     protected void setExpected(String expected)
+     {
+         this.expected = expected;
+     }
+ 
+     protected void assertVelocity() throws ParseErrorException, 
MethodInvocationException, ResourceNotFoundException, IOException, Exception
+     {
+         this.getEngine().init();
+ 
+         StringWriter writer = new StringWriter();
+         assertTrue(this.getEngine().evaluate(this.getContext(), writer, 
this.getName(), this.getTemplate()));
+ 
+         assertEquals(this.getExpected(), String.valueOf(writer));
+     }
+ }
+ }}}
+ 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to