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.exception.ParseErrorException;
23  import org.apache.velocity.test.BaseEvalTestCase;
24  
25  /**
26   * This class tests VELOCITY-614.
27   */
28  public class Velocity614TestCase extends BaseEvalTestCase
29  {
30      public Velocity614TestCase(String name)
31      {
32         super(name);
33      }
34  
35      public void testSchmoo()
36      {
37          String template = "#something(Stuff)";
38          assertEvalEquals(template, template);
39      }
40  
41      public void testEscapeSchmooButNotReallySinceSchmooHasNoEscaping()
42      {
43          String template = "\\#something(Stuff)";
44          assertEvalEquals(template, template);
45      }
46  
47      public void testEscapeMacroWithBadArg()
48      {
49          String template = "#macro( evil $arg )$arg#end \\#evil(bar)";
50          assertEvalEquals(" #evil(bar)", template);
51      }
52  
53      public void testEarlyDefinedMacroWithBadArg()
54      {
55          // make sure this still bombs, but don't spam sysout
56          log.off();
57          assertEvalException("#macro( evil $arg )$arg#end #evil(bar)");
58          log.on();
59      }
60  
61      // just make sure this doesn't get broken
62      public void testLateDefinedMacroWithGoodArg()
63      {
64          String good = "#good('bar') #macro( good $arg )$arg#end";
65          assertEvalEquals("bar ", good);
66      }
67  
68      public void testDirectivesWithBadArg()
69      {
70          // make sure these all still bomb, but don't spam sysout
71          log.off();
72          assertEvalException("#foreach(Stuff in That)foo#end");
73          assertEvalException("#include(Stuff)");
74          assertEvalException("#parse(Stuff)");
75          assertEvalException("#define(Stuff)foo#end");
76          assertEvalException("#macro( name Stuff)foo#end");
77          assertEvalException("#foreach($i in [1..3])#break(Stuff)#end");
78          assertEvalException("#literal(Stuff)foo#end");
79          assertEvalException("#evaluate(Stuff)", ParseErrorException.class);
80          log.on();
81      }
82  
83      public void testLateDefinedMacroWithBadArg()
84      {
85          String evil = "#evil(bar) #macro( evil $arg )$arg#end";
86          assertEvalException(evil, ParseErrorException.class);
87      }
88  
89  }