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.Velocity;
30  import org.apache.velocity.runtime.log.NullLogChute;
31  
32  /**
33   * This class tests strange Velocimacro issues.
34   *
35   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
36   * @version $Id: VelocimacroTestCase.java 477002 2006-11-20 01:07:43Z henning $
37   */
38  public class VelocimacroTestCase extends TestCase
39  {
40      private String template1 = "#macro(foo $a)$a#end #macro(bar $b)#foo($b)#end #foreach($i in [1..3])#bar($i)#end";
41      private String result1 = "  123";
42  
43      public VelocimacroTestCase(String name)
44      {
45          super(name);
46      }
47  
48      public void setUp()
49              throws Exception
50      {
51          /*
52           *  setup local scope for templates
53           */
54          Velocity.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.TRUE);
55  
56          Velocity.setProperty(
57                  Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
58  
59          Velocity.init();
60      }
61  
62      public static Test suite()
63      {
64          return new TestSuite(VelocimacroTestCase.class);
65      }
66  
67      /**
68       * Runs the test.
69       */
70      public void testVelociMacro ()
71              throws Exception
72      {
73          VelocityContext context = new VelocityContext();
74  
75          StringWriter writer = new StringWriter();
76          Velocity.evaluate(context, writer, "vm_chain1", template1);
77  
78          String out = writer.toString();
79  
80          if( !result1.equals( out ) )
81          {
82              fail("output incorrect.");
83          }
84      }
85  }