1   package org.apache.velocity.test.issues;
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.test.BaseTestCase;
23  
24  /**
25   * This class tests VELOCITY-615.
26   */
27  public class Velocity615TestCase extends BaseTestCase
28  {
29      public Velocity615TestCase(String name)
30      {
31         super(name);
32      }
33  
34      public void setUp() throws Exception
35      {
36          super.setUp();
37          engine.setProperty("velocimacro.permissions.allow.inline", "true");
38          engine.setProperty("velocimacro.permissions.allow.inline.to.replace.global", "false");
39          engine.setProperty("velocimacro.permissions.allow.inline.local.scope", "true");
40          engine.setProperty("velocimacro.context.localscope", "false");
41          engine.setProperty("velocimacro.arguments.strict", "true");
42      }
43  
44      public void testIt()
45      {
46          String template = "#set( $foo = 'old' )"+
47                            "#macro( test $foo )"+
48                              "#set( $foo = \"new $foo \" )"+
49                              "$foo"+
50                            "#end"+
51                            "#test( 'foo' )"+
52                            "$foo";
53          assertEvalEquals("new foo new foo ", template);
54      }
55  
56      public void testForIrrationallyFearedRelatedPossibleProblem()
57      {
58          context.put("i", new Inc());
59          String template = "#macro( test $a )"+
60                              "$a"+
61                              "$a"+
62                            "#end"+
63                            "#test( \"$i\" )$i";
64          assertEvalEquals("012", template);
65      }
66  
67      public void testForIrrationallyFearedRelatedPossibleProblem2()
68      {
69          context.put("i", new Inc());
70          String template = "#macro( test $a )"+
71                              "#set( $a = 'a' )"+
72                              "$a"+
73                              "$a"+
74                            "#end"+
75                            "#test( \"$i\" )$i";
76          assertEvalEquals("aa0", template);
77      }
78  
79      public void testForIrrationallyFearedRelatedPossibleProblem3()
80      {
81          context.put("i", new Inc());
82          String template = "#macro( test $a )"+
83                              "$a"+
84                              "$a"+
85                            "#end"+
86                            "#test( $i )$i";
87          assertEvalEquals("012", template);
88      }
89  
90      public void testForIrrationallyFearedRelatedPossibleProblem4()
91      {
92          context.put("i", new Inc());
93          String template = "#macro( test $a )"+
94                              "$a"+
95                              "$a"+
96                            "#end"+
97                            "#test( $i.plus() )$i";
98          assertEvalEquals("012", template);
99      }
100 
101     public void testForIrrationallyFearedRelatedPossibleProblem5()
102     {
103         context.put("i", new Inc());
104         String template = "#macro( test $a )"+
105                             "#set( $a = $i )"+
106                             "$a"+
107                             "$a"+
108                           "#end"+
109                           "#test( 'a' )$i";
110         assertEvalEquals("012", template);
111     }
112 
113     public void testVelocity681()
114     {
115         String template = "#macro(myMacro $result)"+
116                           "  #set($result = 'some value')"+
117                           "#end"+
118                           "#myMacro($x)"+
119                           "$x";
120         assertEvalEquals("$x", template);
121     }
122 
123     public static class Inc
124     {
125         private int i=0;
126 
127         public int plus()
128         {
129             return i++;
130         }
131 
132         public String toString()
133         {
134             return String.valueOf(i++);
135         }
136     }
137 
138 }