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