View Javadoc

1   /*
2    * Created on 2004/11/05
3    */
4   package org.ieee.shinobu.demo.velocity;
5   
6   import java.io.IOException;
7   import java.io.StringWriter;
8   
9   import junit.framework.TestCase;
10  
11  import org.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
13  import org.apache.velocity.VelocityContext;
14  import org.apache.velocity.app.VelocityEngine;
15  import org.apache.velocity.context.Context;
16  import org.apache.velocity.exception.MethodInvocationException;
17  import org.apache.velocity.exception.ParseErrorException;
18  import org.apache.velocity.exception.ResourceNotFoundException;
19  
20  /***
21   * @author shinobu
22   */
23  public abstract class AbstractVelocityTestCase extends TestCase {
24      
25  	private Log log = LogFactory.getLog(this.getClass());
26  	protected final Log getLog() {
27  		return this.log;
28  	}
29  	
30      /* (non-Javadoc)
31       * @see junit.framework.TestCase#setUp()
32       */
33      protected void setUp() throws Exception {
34          super.setUp();
35          
36          this.setEngine(VelocityTestTool.newEngine());
37          this.setTemplate("");
38          this.setContext(new VelocityContext());
39          this.setExpected("");
40      }
41      
42      /* (non-Javadoc)
43       * @see junit.framework.TestCase#tearDown()
44       */
45      protected void tearDown() throws Exception {
46          this.setEngine(null);
47          this.setTemplate(null);
48          this.setContext(null);
49          this.setExpected(null);
50          
51          super.tearDown();
52      }
53      
54      private VelocityEngine engine = null;
55      private String template = "";
56      private Context context = null;
57      private String expected = null;
58      
59      /***
60       * @return Returns the engine.
61       */
62      protected VelocityEngine getEngine() {
63          return this.engine;
64      }
65      /***
66       * @param engine The engine to set.
67       */
68      protected void setEngine(VelocityEngine engine) {
69          this.engine = engine;
70      }
71      /***
72       * @return Returns the template.
73       */
74      protected String getTemplate() {
75          return this.template;
76      }
77      /***
78       * @param template The template to set.
79       */
80      protected void setTemplate(String template) {
81          this.template = template;
82      }
83      /***
84       * @return Returns the context.
85       */
86      protected Context getContext() {
87          return this.context;
88      }
89      /***
90       * @param context The context to set.
91       */
92      protected void setContext(Context context) {
93          this.context = context;
94      }
95      /***
96       * @return Returns the expected.
97       */
98      protected String getExpected() {
99          return this.expected;
100     }
101     /***
102      * @param expected The expected to set.
103      */
104     protected void setExpected(String expected) {
105         this.expected = expected;
106     }
107     
108     protected void assertVelocity() throws ParseErrorException, MethodInvocationException, ResourceNotFoundException, IOException, Exception {
109     	this.getEngine().init();
110     	
111     	StringWriter writer = new StringWriter();
112         assertTrue(this.getEngine().evaluate(this.getContext(), writer, this.getName(), this.getTemplate()));
113         
114         assertEquals(this.getExpected(), String.valueOf(writer));
115     }
116 }