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.TestCase;
26 import junit.framework.TestSuite;
27
28 import org.apache.velocity.VelocityContext;
29 import org.apache.velocity.app.VelocityEngine;
30 import org.apache.velocity.exception.MacroOverflowException;
31 import org.apache.velocity.runtime.RuntimeConstants;
32 import org.apache.velocity.test.misc.TestLogChute;
33
34
35
36
37
38
39
40 public class VelocimacroTestCase extends TestCase
41 {
42 private String template1 = "#macro(foo $a)$a#end #macro(bar $b)#foo($b)#end #foreach($i in [1..3])#if($i == 3)#foo($i)#else#bar($i)#end#end";
43 private String result1 = " 123";
44 private String template2 = "#macro(bar $a)#set($a = $a + 1)$a#bar($a)#end#bar(0)";
45 private String template3 = "#macro(baz $a)#set($a = $a + 1)$a#inner($a)#end#macro(inner $b)#baz($b)#end#baz(0)";
46 private String template4 = "#macro(bad $a)#set($a = $a + 1)$a#inside($a)#end#macro(inside $b)#loop($b)#end#macro(loop $c)#bad($c)#end#bad(0)";
47
48 VelocityEngine engine = new VelocityEngine();
49
50 public VelocimacroTestCase(String name)
51 {
52 super(name);
53 }
54
55 public void setUp()
56 throws Exception
57 {
58
59
60
61 engine.setProperty( RuntimeConstants.VM_PERM_INLINE_LOCAL, Boolean.TRUE);
62 engine.setProperty( RuntimeConstants.VM_MAX_DEPTH, new Integer(5));
63 engine.setProperty(
64 RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
65 engine.init();
66 }
67
68 public static Test suite()
69 {
70 return new TestSuite(VelocimacroTestCase.class);
71 }
72
73
74
75
76 public void testVelociMacro ()
77 throws Exception
78 {
79 VelocityContext context = new VelocityContext();
80
81 StringWriter writer = new StringWriter();
82 engine.evaluate(context, writer, "vm_chain1", template1);
83
84 String out = writer.toString();
85
86 if( !result1.equals( out ) )
87 {
88 fail("output incorrect.");
89 }
90 }
91
92
93
94
95 public void testVelociMacroCallMax()
96 throws Exception
97 {
98 VelocityContext context = new VelocityContext();
99 StringWriter writer = new StringWriter();
100
101 try
102 {
103 engine.evaluate(context, writer, "vm_chain2", template2);
104 fail("Did not exceed max macro call depth as expected");
105 }
106 catch (MacroOverflowException e)
107 {
108 assertEquals("Max calling depth of 5 was exceeded in macro 'bar'"+
109 " with Call Stack:bar->bar->bar->bar->bar at vm_chain2[line 1, column 15]",
110 e.getMessage());
111 }
112
113 try
114 {
115 engine.evaluate(context, writer, "vm_chain3", template3);
116 fail("Did not exceed max macro call depth as expected");
117 }
118 catch (MacroOverflowException e)
119 {
120 assertEquals("Max calling depth of 5 was exceeded in macro 'inner'"+
121 " with Call Stack:baz->inner->baz->inner->baz at vm_chain3[line 1, column 64]",
122 e.getMessage());
123 }
124
125 try
126 {
127 engine.evaluate(context, writer, "vm_chain4", template4);
128 fail("Did not exceed max macro call depth as expected");
129 }
130 catch (MacroOverflowException e)
131 {
132 assertEquals("Max calling depth of 5 was exceeded in macro 'loop'"+
133 " with Call Stack:bad->inside->loop->bad->inside at vm_chain4[line 1, column 94]",
134 e.getMessage());
135 }
136 }
137 }