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
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.app.VelocityEngine;
29 import org.apache.velocity.context.Context;
30 import org.apache.velocity.exception.MethodInvocationException;
31 import org.apache.velocity.runtime.RuntimeConstants;
32 import org.apache.velocity.test.misc.ExceptionGeneratingDirective;
33 import org.apache.velocity.test.misc.ExceptionGeneratingEventHandler;
34 import org.apache.velocity.test.misc.ExceptionGeneratingResourceLoader;
35 import org.apache.velocity.test.provider.TestProvider;
36
37
38
39
40
41
42
43 public class ExceptionTestCase extends BaseTestCase implements TemplateTestBase
44 {
45 VelocityEngine ve;
46
47
48
49
50 public ExceptionTestCase(String name)
51 {
52 super(name);
53 }
54
55 public static Test suite ()
56 {
57 return new TestSuite(ExceptionTestCase.class);
58 }
59
60
61 public void testReferenceInsertionEventHandlerException()
62 throws Exception
63 {
64 ve = new VelocityEngine();
65 ve.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION,ExceptionGeneratingEventHandler.class.getName());
66 ve.init();
67 assertException(ve);
68 }
69
70
71
72
73
74
75 public void testMethodExceptionEventHandlerException()
76 throws Exception
77 {
78 ve = new VelocityEngine();
79 ve.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION,ExceptionGeneratingEventHandler.class.getName());
80 ve.init();
81 Context context = new VelocityContext();
82 context.put ("test",new TestProvider());
83 assertMethodInvocationException(ve,context,"$test.getThrow()");
84 assertMethodInvocationException(ve,context,"$test.throw");
85 }
86
87 public void testNullSetEventHandlerException()
88 throws Exception
89 {
90 ve = new VelocityEngine();
91 ve.setProperty(RuntimeConstants.EVENTHANDLER_NULLSET,ExceptionGeneratingEventHandler.class.getName());
92 ve.init();
93 assertException(ve,"#set($test = $abc)");
94 }
95
96 public void testIncludeEventHandlerException()
97 throws Exception
98 {
99 ve = new VelocityEngine();
100 ve.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE,ExceptionGeneratingEventHandler.class.getName());
101 ve.init();
102 assertException(ve,"#include('dummy')");
103 }
104
105 public void testResourceLoaderException()
106 throws Exception
107 {
108 ve = new VelocityEngine();
109 ve.setProperty(RuntimeConstants.RESOURCE_LOADER,"except");
110 ve.setProperty("except.resource.loader.class",ExceptionGeneratingResourceLoader.class.getName());
111 try
112 {
113 ve.init();
114 ve.getTemplate("test.txt");
115 fail("Should have thrown RuntimeException");
116 }
117 catch (RuntimeException E)
118 {
119
120 }
121 }
122
123
124 public void testDirectiveException()
125 throws Exception
126 {
127 ve = new VelocityEngine();
128 ve.setProperty("userdirective",ExceptionGeneratingDirective.class.getName());
129 ve.init();
130 assertException(ve,"#Exception() test #end");
131 }
132
133
134
135 public void assertException(VelocityEngine ve)
136 throws Exception
137 {
138 Context context = new VelocityContext();
139 context.put ("test","test");
140 assertException(ve,context,"this is a $test");
141 }
142
143 public void assertException(VelocityEngine ve, String input)
144 throws Exception
145 {
146 Context context = new VelocityContext();
147 context.put ("test","test");
148 assertException(ve,context,input);
149 }
150
151 public void assertException(VelocityEngine ve, Context context, String input)
152 throws Exception
153 {
154 try
155 {
156 StringWriter writer = new StringWriter();
157 ve.evaluate(context,writer,"test",input);
158 fail("Expected RuntimeException");
159 }
160 catch (RuntimeException E)
161 {
162
163 }
164 }
165 public void assertMethodInvocationException(VelocityEngine ve, Context context, String input)
166 throws Exception
167 {
168 try
169 {
170 StringWriter writer = new StringWriter();
171 ve.evaluate(context,writer,"test",input);
172 fail("Expected MethodInvocationException");
173 }
174 catch (MethodInvocationException E)
175 {
176
177 }
178 }
179
180
181 }