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.app.VelocityEngine;
23  
24  /**
25   * This class tests the break directive.
26   */
27  public class BreakDirectiveTestCase extends BaseTestCase
28  {
29      public BreakDirectiveTestCase(String name)
30      {
31          super(name);
32      }
33  
34      protected void setUpEngine(VelocityEngine engine)
35      {
36          engine.setProperty("a.provide.scope.control", "true");
37          engine.setProperty("define.provide.scope.control", "true");
38          engine.setProperty("evaluate.provide.scope.control", "true");
39          engine.setProperty("macro.provide.scope.control", "true");
40          engine.setProperty("template.provide.scope.control", "true");
41      }
42  
43      public void testBadArgs()
44      {
45          context.put("foo","foo");
46          assertEvalException("#break($null)");
47          assertEvalException("#break($foo)");
48          assertEvalException("#break(true)");
49          assertEvalException("#break(1.2)");
50          assertEvalException("#break([0..1])");
51          assertEvalException("#break( $too $many )");
52      }
53  
54      public void testStopForeach()
55      {
56          String template = "#foreach($i in [1..5])$i#if($i>2)#break($foreach)#end#end test";
57          assertEvalEquals("123 test", template);
58  
59          // only inner should be stopped, not outer
60          String t2 = "#foreach($j in [1..2])"+template+"#end";
61          assertEvalEquals("123 test123 test", t2);
62  
63          // stop outer using #break($foreach.parent)
64          String t3 = "#foreach($i in [1..2])#foreach($j in [2..3])$i$j#if($i+$j==5)#break($foreach.parent)#end#end test#end";
65          assertEvalEquals("1213 test2223", t3);
66  
67          // without specifying scope...
68          assertEvalEquals("1, 2, 3, 4, 5",
69                           "#foreach($i in [1..10])$i#if($i > 4)#break#end, #end");
70          assertEvalEquals("1", "#foreach($i in [1..5])$i#break #end");
71          assertEvalEquals("~~~, ~~, ~, ",
72              "#foreach($i in [1..3])#foreach($j in [2..4])#if($i*$j >= 8)#break#end~#end, #end");
73      }
74  
75      public void testStopTemplate()
76      {
77          addTemplate("a", "a#break($template)b");
78          assertTmplEquals("a", "a");
79          assertEvalEquals("ac", "#parse('a')c");
80  
81          addTemplate("b", "b#{break}a");
82          assertTmplEquals("b", "b");
83      }
84  
85      public void testStopEvaluate()
86      {
87          assertEvalEquals("a", "a#break($evaluate)b");
88          assertEvalEquals("a", "#evaluate('a#break($evaluate)b')");
89          assertEvalEquals("a", "a#evaluate('#break($evaluate.topmost)')b");
90          assertEvalEquals("a", "a#{break}b");
91      }
92  
93      public void testStopDefineBlock()
94      {
95          assertEvalEquals("a", "#define($a)a#break($define)b#end$a");
96          assertEvalEquals("aa", "#define($a)a#break($define.parent)b#end#define($b)a${a}b#end$b");
97          assertEvalEquals("a", "#define($a)a#{break}b#end$a");
98      }
99  
100     public void testStopMacro()
101     {
102         assertEvalEquals("a ", "#macro(a)a #break($macro) b#end#a");
103         assertEvalEquals("b c ", "#macro(c)c #break($macro.parent) d#end"+
104                                "#macro(b)b #c c#end"+
105                                "#b");
106         assertEvalEquals("d", "#macro(d)d#{break}e#end#d");
107     }
108 
109     public void testStopMacroBodyBlock()
110     {
111         assertEvalEquals(" a ", "#macro(a) $bodyContent #end"+
112                                 "#@a()a#break($a)b#end");
113         assertEvalEquals(" b ", "#macro(b) $bodyContent #end"+
114                                 "#@b()b#{break}c#end");
115     }
116 }