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 org.apache.velocity.runtime.RuntimeConstants;
23  
24  /**
25   * This class tests the BlockMacro functionality.
26   */
27  public class BlockMacroTestCase extends BaseTestCase
28  {
29      public BlockMacroTestCase(String name)
30      {
31          super(name);
32          // DEBUG = true;
33      }
34  
35      public void testMultipleBodyContentIncludes() throws Exception
36      {
37          String template = "#macro(foo $txt) Yeah, $txt! $bodyContent $bodyContent#end #@foo(\"woohoo\")jee#end";
38          String result = "  Yeah, woohoo! jee jee";
39      
40          assertEvalEquals(result, template);
41      }
42  
43      public void testNestedVelocityLogic() throws Exception
44      {
45          String template = "#macro(foo $txt) Yeah, $txt! $bodyContent#end #@foo(\"woohoo\")#foreach($i in [1..3])$i:#{end}#end";
46          String result = "  Yeah, woohoo! 1:2:3:";
47      
48          assertEvalEquals(result, template);
49      }
50  
51      public void testEmptyBody() throws Exception
52      {
53          String template = "#macro(foo $txt) Yeah, $txt! $bodyContent#end #@foo(\"woohoo\")#end";
54          String result = "  Yeah, woohoo! ";
55      
56          assertEvalEquals(result, template);
57      }
58  
59      public void testNoArgumentsEmptyBodyCall() throws Exception
60      {
61          String template = "#macro(foo) Yeah! $bodyContent#end #@foo()#end";
62          String result = "  Yeah! ";
63      
64          assertEvalEquals(result, template);
65      }
66  
67      public void testCustomBodyReference() throws Exception
68      {
69          engine.setProperty(RuntimeConstants.VM_BODY_REFERENCE, "myBody");
70          String template = "#macro(foo) Yeah! $myBody#end #@foo()#end";
71          String result = "  Yeah! ";
72      
73          assertEvalEquals(result, template);
74      }
75  
76      public void testVelocity671() throws Exception
77      {
78          engine.setProperty(RuntimeConstants.VM_PERM_INLINE_LOCAL, Boolean.TRUE);
79          String template = "#macro(echo)$bodyContent#end #@echo()Yeah!#end";
80          String result = " Yeah!";
81          assertEvalEquals(result, template);
82      }
83  
84      public void testVelocity675() throws Exception
85      {
86          assertEvalEquals("#@foo", "#@foo"); 
87      }
88      
89      public void testVelocity685() throws Exception
90      {
91          engine.setProperty(RuntimeConstants.VM_ARGUMENTS_STRICT, Boolean.TRUE);
92          assertEvalEquals(" ", "#macro(foo)#end #@foo() junk #end");
93      }
94  
95      public void testVelocity686() throws Exception
96      {
97          String template = "#macro(foo)#set( $x = $bodyContent )#end"+
98                            "#@foo()b#end a $x ";
99          assertEvalEquals(" a b ", template);
100     }
101 
102     public void testNestedBlockMacro()
103     {
104         String template = "#macro(foo)foo:$bodyContent#end"+
105                           "#macro(bar)bar:$bodyContent#end"+
106                           "#@foo()foo,#@bar()bar#end#end";
107         assertEvalEquals("foo:foo,bar:bar", template);
108     }
109 
110     public void testRecursiveBlockMacro()
111     {
112         engine.setProperty(RuntimeConstants.VM_MAX_DEPTH, "3");
113         String template = "#macro(foo)start:$bodyContent#end"+
114                           "#@foo()call:$bodyContent#end";
115         assertEvalEquals("start:call:call:call:$bodyContent", template);
116     }
117 
118     public void testBlueJoesProblem()
119     {
120         engine.setProperty("macro."+RuntimeConstants.PROVIDE_SCOPE_CONTROL, Boolean.TRUE);
121         addTemplate("a", "#macro(wrap $layout)$!macro.put($layout,$bodyContent)#parse($layout)#end"+
122                          "#@wrap('b')a#end");
123         addTemplate("b", "#@wrap('c')b$!macro.get('b')b#end");
124         addTemplate("c", "c$!macro.get('c')c");
125         assertTmplEquals("cbabc", "a");
126     }
127 }
128