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 junit.framework.Test;
24 import junit.framework.TestSuite;
25 import org.apache.velocity.VelocityContext;
26 import org.apache.velocity.app.VelocityEngine;
27 import org.apache.velocity.context.Context;
28 import org.apache.velocity.exception.MethodInvocationException;
29 import org.apache.velocity.test.provider.TestProvider;
30 import org.apache.velocity.util.ExceptionUtils;
31
32
33
34
35
36
37
38
39
40 public class WrappedExceptionTestCase extends BaseTestCase implements TemplateTestBase
41 {
42 VelocityEngine ve;
43
44
45
46
47 public WrappedExceptionTestCase(String name)
48 {
49 super(name);
50 }
51
52 public static Test suite ()
53 {
54 return new TestSuite(WrappedExceptionTestCase.class);
55 }
56
57 public void setUp() throws Exception
58 {
59 ve = new VelocityEngine();
60 ve.init();
61 }
62
63
64 public void testMethodException() throws Exception
65 {
66
67
68 Context context = new VelocityContext();
69 StringWriter writer = new StringWriter();
70 context.put("test",new TestProvider());
71
72 try
73 {
74 ve.evaluate(context,writer,"test","$test.getThrow()");
75 fail ("expected an exception");
76 }
77 catch (MethodInvocationException E)
78 {
79 assertEquals(Exception.class,E.getCause().getClass());
80 assertEquals("From getThrow()",E.getCause().getMessage());
81 }
82
83 }
84 public void testExceptionUtils()
85 {
86 Error e = new Error("Inside");
87 RuntimeException re = ExceptionUtils.createRuntimeException("Outside", e);
88 assertEquals("cause was set", e,re.getCause());
89 }
90
91 }