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.runtime.parser.node.ASTTextblock;
23  
24  /**
25   * This class tests the Textblock directive.
26   */
27  public class TextblockTestCase extends BaseTestCase
28  {
29      // these are all here so that the test case adapts instantly
30      // to changes in the textblock start/end sequences
31      private static final String START = ASTTextblock.START;
32      private static final String END = ASTTextblock.END;
33      private static final String PARTIAL_START = START.substring(0, START.length() - 1);
34      private static final String PARTIAL_END = END.substring(1, END.length());
35      private static final String END_OF_START = START.substring(START.length() - 1, START.length());
36      private static final String START_OF_END = END.substring(0, 1);
37  
38      public TextblockTestCase(String name)
39      {
40          super(name);
41          //DEBUG = true;
42      }
43  
44      public String textblock(String s)
45      {
46          return START + s + END;
47      }
48  
49      public void assertTextblockEvalEquals(String s) throws Exception
50      {
51          assertEvalEquals(s, textblock(s));
52      }
53  
54      /**
55       * https://issues.apache.org/jira/browse/VELOCITY-661
56       */
57      public void testTextblockAjaxcode() throws Exception
58      {
59          String s = "var myId = 'someID';$('#test).append($.template('<div id=\"${myId}\"></div>').apply({myId: myId}));";
60          assertEvalEquals(s + " 123", textblock(s)+" #foreach($i in [1..3])$i#end");
61      }
62  
63      public void testLooseTextblockEnd() throws Exception
64      {
65          // just like a multi-line comment end (*#), this must be
66          // followed by a character.  by itself, it bombs for some reason.
67          assertEvalEquals(END+" ", END+" ");
68      }
69  
70      public void testTextblockStartInTextblock() throws Exception
71      {
72          assertTextblockEvalEquals(START);
73      }
74  
75      public void testTextblockEndBetweenTwoTextblockHalves() throws Exception
76      {
77          // just like a multi-line comment end (*#), the end token
78          // in the middle must be followed by some character.
79          // by itself, it bombs.  not sure why that is, but the
80          // same has been true of multi-line comments without complaints,
81          // so i'm not going to worry about it just yet.
82          assertEvalEquals(" "+END+"  ", textblock(" ")+END+" "+textblock(" "));
83      }
84  
85      public void testZerolengthTextblock() throws Exception
86      {
87          assertTextblockEvalEquals("");
88      }
89  
90      public void testTextblockInsideForeachLoop() throws Exception
91      {
92          String s = "var myId = 'someID';$('#test).append($.template('<div id=\"${myId}\"></div>').apply({myId: myId}));";
93          assertEvalEquals("1 "+s+"2 "+s+"3 "+s, "#foreach($i in [1..3])$i "+ textblock(s) + "#end");
94      }
95  
96      public void testSingleHashInsideTextblock() throws Exception
97      {
98          assertTextblockEvalEquals(" # ");
99      }
100 
101     public void testDollarInsideTextblock() throws Exception
102     {
103         assertTextblockEvalEquals("$");
104     }
105 
106     public void testTextblockInsideComment() throws Exception
107     {
108         String s = "FOOBAR";
109         assertEvalEquals("", "#* comment "+textblock(s) + " *#");
110     }
111 
112     public void testPartialStartEndTokensInsideTextblock() throws Exception
113     {
114         assertTextblockEvalEquals(PARTIAL_START+"foo"+PARTIAL_END);
115     }
116 
117     public void testDupeTokenChars() throws Exception
118     {
119         assertTextblockEvalEquals(END_OF_START+START_OF_END);
120         assertTextblockEvalEquals(END_OF_START+END_OF_START+START_OF_END+START_OF_END);
121         assertTextblockEvalEquals(END_OF_START+END_OF_START+"#"+START_OF_END+START_OF_END);
122     }
123 
124     /**
125      * https://issues.apache.org/jira/browse/VELOCITY-584
126      */
127     public void testServerSideIncludeEscaping() throws Exception
128     {
129         assertTextblockEvalEquals("<!--#include file=\"wisdom.inc\"--> ");
130     }
131     
132     /**
133      * https://issues.apache.org/jira/browse/VELOCITY-676
134      */
135     public void testLineCommentInsideTextblock() throws Exception
136     {
137         assertTextblockEvalEquals("##x");
138     }
139 
140 }