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.Template;
28 import org.apache.velocity.VelocityContext;
29 import org.apache.velocity.app.Velocity;
30 import org.apache.velocity.app.VelocityEngine;
31 import org.apache.velocity.context.Context;
32 import org.apache.velocity.exception.ParseErrorException;
33 import org.apache.velocity.exception.ResourceNotFoundException;
34
35
36
37
38
39
40
41
42
43
44
45 public class IncludeErrorTestCase extends BaseTestCase implements TemplateTestBase
46 {
47 VelocityEngine ve;
48
49
50
51
52 public IncludeErrorTestCase(String name)
53 {
54 super(name);
55 }
56
57 public static Test suite ()
58 {
59 return new TestSuite(IncludeErrorTestCase.class);
60 }
61
62 public void setUp() throws Exception
63 {
64 ve = new VelocityEngine();
65 ve.setProperty(
66 Velocity.FILE_RESOURCE_LOADER_PATH, "test/includeerror");
67
68 ve.init();
69 }
70
71
72
73 public void testMissingParseError() throws Exception
74 {
75 checkException("missingparse.vm",ResourceNotFoundException.class);
76 }
77
78 public void testMissingIncludeError() throws Exception
79 {
80 checkException("missinginclude.vm",ResourceNotFoundException.class);
81 }
82
83 public void testParseError() throws Exception
84 {
85 checkException("parsemain.vm",ParseErrorException.class);
86 }
87
88 public void testParseError2() throws Exception
89 {
90 checkException("parsemain2.vm",ParseErrorException.class);
91 }
92
93
94
95
96
97
98
99
100 private void checkException(String templateName,Class exceptionClass)
101 throws Exception
102 {
103 Context context = new VelocityContext();
104 StringWriter writer = new StringWriter();
105 Template template = ve.getTemplate(templateName, "UTF-8");
106
107 try
108 {
109 template.merge(context, writer);
110 writer.flush();
111 fail("File should have thrown an exception");
112 }
113 catch (Exception E)
114 {
115 assertTrue(exceptionClass.isAssignableFrom(E.getClass()));
116 }
117 finally
118 {
119 writer.close();
120 }
121
122 }
123
124 }