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