1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.StringWriter;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import junit.framework.TestCase;
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.app.VelocityEngine;
29 import org.apache.velocity.runtime.RuntimeConstants;
30 import org.apache.velocity.test.misc.TestLogChute;
31
32
33
34
35
36 public class BaseEvalTestCase extends TestCase
37 {
38 protected VelocityEngine engine;
39 protected VelocityContext context;
40 protected boolean DEBUG = false;
41 protected TestLogChute log;
42
43 public BaseEvalTestCase(String name)
44 {
45 super(name);
46 }
47
48 public void setUp() throws Exception
49 {
50 engine = new VelocityEngine();
51
52
53 log = new TestLogChute(false, false);
54 log.setEnabledLevel(TestLogChute.INFO_ID);
55 log.setSystemErrLevel(TestLogChute.WARN_ID);
56 engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, log);
57
58 context = new VelocityContext();
59 setContext(context);
60 }
61
62 public void tearDown()
63 {
64 engine = null;
65 context = null;
66 }
67
68 public void testBase()
69 {
70 assertEvalEquals("","");
71 assertEvalEquals("abc\n123","abc\n123");
72 }
73
74 protected void setProperties(VelocityEngine engine)
75 {
76
77 }
78
79 protected void setContext(VelocityContext context)
80 {
81
82 }
83
84 protected void assertContextValue(String key, Object expected)
85 {
86 if (DEBUG)
87 {
88 engine.getLog().info("Expected value of '"+key+"': "+expected);
89 }
90 Object value = context.get(key);
91 if (DEBUG)
92 {
93 engine.getLog().info("Result: "+value);
94 }
95 assertEquals(expected, value);
96 }
97
98 protected void assertEvalEquals(String expected, String template)
99 {
100 if (DEBUG)
101 {
102 engine.getLog().info("Expectation: "+expected);
103 }
104 assertEquals(expected, evaluate(template));
105 }
106
107 protected Exception assertEvalException(String evil)
108 {
109 return assertEvalException(evil, null);
110 }
111
112 protected Exception assertEvalException(String evil, Class exceptionType)
113 {
114 try
115 {
116 if (!DEBUG)
117 {
118 log.off();
119 }
120 evaluate(evil);
121 fail("Template '"+evil+"' should have thrown an exception.");
122 }
123 catch (Exception e)
124 {
125 if (exceptionType != null && !exceptionType.isAssignableFrom(e.getClass()))
126 {
127 fail("Was expecting template '"+evil+"' to throw "+exceptionType+" not "+e);
128 }
129 return e;
130 }
131 finally
132 {
133 if (!DEBUG)
134 {
135 log.on();
136 }
137 }
138 return null;
139 }
140
141 protected Exception assertEvalExceptionAt(String evil, String template,
142 int line, int col)
143 {
144 String loc = template+"[line "+line+", column "+col+"]";
145 if (DEBUG)
146 {
147 engine.getLog().info("Expectation: Exception at "+loc);
148 }
149 Exception e = assertEvalException(evil, null);
150 if (e.getMessage().indexOf(loc) < 1)
151 {
152 fail("Was expecting exception at "+loc+" instead of "+e.getMessage());
153 }
154 else if (DEBUG)
155 {
156 engine.getLog().info("Result: "+e.getMessage());
157 }
158 return e;
159 }
160
161 protected Exception assertEvalExceptionAt(String evil, int line, int col)
162 {
163 return assertEvalExceptionAt(evil, "", line, col);
164 }
165
166 protected String evaluate(String template)
167 {
168 StringWriter writer = new StringWriter();
169 try
170 {
171 if (DEBUG)
172 {
173 engine.getLog().info("Template: "+template);
174 }
175
176
177 engine.evaluate(context, writer, template, template);
178
179 String result = writer.toString();
180 if (DEBUG)
181 {
182 engine.getLog().info("Result: "+result);
183 }
184 return result;
185 }
186 catch (RuntimeException re)
187 {
188 if (DEBUG)
189 {
190 engine.getLog().info("RuntimeException!", re);
191 }
192 throw re;
193 }
194 catch (Exception e)
195 {
196 if (DEBUG)
197 {
198 engine.getLog().info("Exception!", e);
199 }
200 throw new RuntimeException(e);
201 }
202 }
203
204
205
206
207 protected void assertTmplEquals(String expected, String template)
208 {
209 if (DEBUG)
210 {
211 engine.getLog().info("Expected: '" + expected + "'");
212 }
213
214 StringWriter writer = new StringWriter();
215 try
216 {
217 engine.mergeTemplate(template, "utf-8", context, writer);
218 }
219 catch (RuntimeException re)
220 {
221 if (DEBUG)
222 {
223 engine.getLog().info("RuntimeException!", re);
224 }
225 throw re;
226 }
227 catch (Exception e)
228 {
229 if (DEBUG)
230 {
231 engine.getLog().info("Exception!", e);
232 }
233 throw new RuntimeException(e);
234 }
235
236 if (DEBUG)
237 {
238 engine.getLog().info("Result: '" + writer.toString() + "'");
239 }
240 assertEquals(expected, writer.toString());
241 }
242 }