1   package org.apache.velocity.test;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.    
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   * This class tests strange Velocimacro issues.
36   *
37   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
38   * @version $Id: VelocimacroTestCase.java 832247 2009-11-03 01:29:30Z wglass $
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           *  setup local scope for templates
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       * Runs the test.
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       * Test case for evaluating max calling depths of macros
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 }