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