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.test.BaseTestCase;
23  import org.apache.velocity.test.misc.TestLogChute;
24  import org.apache.velocity.runtime.RuntimeConstants;
25  
26  /**
27   *  Test the #stop directive
28   */
29  public class StopDirectiveTestCase extends BaseTestCase
30  {
31      public StopDirectiveTestCase(String name)
32      {
33          super(name);
34      }
35    
36      public void setUp() throws Exception
37      {
38          super.setUp();
39          engine.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, "test/stop/");
40          engine.setProperty(RuntimeConstants.VM_LIBRARY, "vmlib1.vm");
41      }
42  
43      public void testStop()
44      {
45        // Make it works through the evaluate method call
46        assertEvalEquals("Text1", "Text1#{stop}Text2");
47        // Make sure stop works in a template
48        assertTmplEquals("Text 1", "stop1.vm");
49        // Make sure stop works when called from a velocity macro
50        assertTmplEquals("Text123stuff1", "stop2.vm");
51        // Make sure stop works when called located in another parsed file
52        assertTmplEquals("text1blaa1", "stop3.vm");
53      }
54  
55      public void testNestedStopAll()
56      {
57          addTemplate("ns", ",template"+
58                            "#macro(vm),macro${bodyContent}macro#end"+
59                            "#define($define),define"+
60                              "#foreach($i in [1..2]),foreach"+
61                                "#{stop}foreach"+
62                              "#{end}define"+
63                            "#{end}"+
64                            "#@vm(),bodyContent"+
65                              "${define}bodyContent"+
66                            "#{end}template");
67          String expected = "evaluate,template,macro,bodyContent,define,foreach";
68          assertEvalEquals(expected, "#evaluate('evaluate#parse(\"ns\")evaluate')");
69      }
70  
71      public void testStopMessage()
72      {
73          log.setEnabledLevel(TestLogChute.DEBUG_ID);
74          log.off();
75          context.put("log", log);
76  
77          assertEvalEquals("a", "a$!log.startCapture()#stop('woogie!')b");
78  
79          info("Log: "+log.getLog());
80          assertTrue(log.getLog().indexOf("StopCommand: woogie!") >= 0);
81      }
82  
83  }