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