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.test.misc.UberspectTestException;
33 import org.apache.velocity.util.introspection.Info;
34
35
36
37
38
39
40
41
42
43
44
45 public class InfoTestCase extends BaseTestCase implements TemplateTestBase
46 {
47 VelocityEngine ve;
48
49
50
51
52 public InfoTestCase(String name)
53 {
54 super(name);
55 }
56
57 public static Test suite ()
58 {
59 return new TestSuite(InfoTestCase.class);
60 }
61
62 public void setUp() throws Exception
63 {
64 ve = new VelocityEngine();
65 ve.setProperty(
66 "runtime.introspector.uberspect", "org.apache.velocity.test.misc.UberspectTestImpl");
67
68 ve.setProperty(
69 Velocity.FILE_RESOURCE_LOADER_PATH, "test/info");
70
71 ve.init();
72 }
73
74
75
76 public void testInfoProperty() throws Exception
77 {
78
79 checkInfo("info1.vm", 1, 7);
80 }
81
82 public void testInfoMethod() throws Exception
83 {
84
85 checkInfo("info2.vm", 1, 7);
86 }
87
88 public void checkInfo(String templateName,
89 int expectedLine, int expectedCol) throws Exception
90 {
91 Context context = new VelocityContext();
92 StringWriter writer = new StringWriter();
93 Template template = ve.getTemplate(templateName, "UTF-8");
94 Info info = null;
95
96 context.put("main", this);
97
98 try
99 {
100 template.merge(context, writer);
101 writer.flush();
102 fail("Uberspect should have thrown an exception");
103 }
104 catch (UberspectTestException E)
105 {
106 info = E.getInfo();
107 }
108 finally
109 {
110 writer.close();
111 }
112 assertInfoEqual(info, templateName, expectedLine, expectedCol);
113
114 }
115
116 private void assertInfoEqual(Info i, String name, int line, int column)
117 {
118 assertEquals("Template Name", name, i.getTemplateName());
119 assertEquals("Template Line", line, i.getLine());
120 assertEquals("Template Column", column, i.getColumn());
121 }
122
123 }