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.exception.MacroOverflowException;
30  import org.apache.velocity.app.Velocity;
31  import org.apache.velocity.test.misc.TestLogChute;
32  
33  /**
34   * This class tests strange Velocimacro issues.
35   *
36   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
37   * @version $Id: VelocimacroTestCase.java 704299 2008-10-14 03:13:16Z nbubna $
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           *  setup local scope for templates
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       * Runs the test.
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       * Test case for evaluating max calling depths of macros
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 }