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 java.util.ArrayList;
23  import org.apache.velocity.runtime.RuntimeConstants;
24  /**
25   * Test index syntax e.g, $foo[1]
26   */
27  public class IndexTestCase extends BaseTestCase
28  {
29      public IndexTestCase(String name)
30      {
31          super(name);
32          //DEBUG = true;
33      }
34  
35      public void setUp() throws Exception
36      {
37          super.setUp();
38          engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE);
39          engine.setProperty(RuntimeConstants.COUNTER_INITIAL_VALUE, new Integer(0));
40          
41          context.put("NULL", null);
42          context.put("red", "blue");
43          
44          int[] a = {1, 2, 3};
45          context.put("a", a);        
46          String[] str = {"a", "ab", "abc"};
47          context.put("str", str);
48  
49          ArrayList alist = new ArrayList();
50          alist.add(new Integer(1));
51          alist.add(new Integer(2));
52          alist.add(new Integer(3));
53          alist.add(a);
54          alist.add(null);
55          context.put("alist", alist);
56  
57          Foo foo = new Foo();
58          foo.bar = alist;
59          context.put("foo", foo);
60  
61          Boo boo = new Boo();
62          context.put("boo", boo);
63      }
64  
65      public void testCallingIndex()
66      {
67          assertEvalEquals("1 -3-", "$a[0] -$a[ 2 ]-");
68          assertEvalEquals("x21", "#set($i = 1)x$a[$i]1");
69          assertEvalEquals("3", "$a[ $a[ $a[0] ] ]");
70          assertEvalEquals("ab", "$str[1]");
71          assertEvalEquals("123", "$alist[0]$alist[1]$alist[2]");
72          assertEvalEquals("1][2-[3]", "$alist[0]][$alist[$a[0]]-[$alist[2]]");
73  
74          assertEvalEquals("2", "$alist[3][1]");
75          assertEvalEquals("3 [1]", "$alist[2] [1]");
76  
77          assertEvalEquals("4", "#set($bar = [3, 4, 5])$bar[1]");
78          assertEvalEquals("21", "#set($i = 1)#set($bar = [3, $a[$i], $a[0]])$bar[1]$bar[2]");
79  
80          assertEvalEquals("2", "$foo.bar[1]");
81          assertEvalEquals("2", "$foo.getBar()[1]");
82          assertEvalEquals("2", "$foo.getBar()[3][1]");
83  
84          assertEvalEquals(" a  ab  abc ", "#foreach($i in $foo.bar[3]) $str[$velocityCount] #end");
85  
86          assertEvalEquals("apple", "#set($hash = {'a':'apple', 'b':'orange'})$hash['a']");
87  
88          assertEvalEquals("xx ", "#if($alist[4] == $NULL)xx#end #if($alist[4])yy#end");
89  
90          assertEvalEquals("BIG TRUEaBIG FALSE", "$foo[true]a$foo[false]");
91          assertEvalEquals("junk foobar ", "$foo[\"junk\"]");
92          assertEvalEquals("GOT NULL", "#set($i=$NULL)$boo[$i]");
93          
94          assertEvalEquals("321", "$a[-1]$a[ -2]$a[-3 ]");
95          assertEvalEquals("67xx", "#set($hash={1:11, 5:67, 23:2})$hash[5]$!hash[6]#if(!$hash[1000])xx#end");
96          
97          // Some cases that should be evaluated as text
98          assertEvalEquals("[]", "[]");
99          assertEvalEquals("$[]", "$[]");
100         assertEvalEquals("$.[]", "$.[]");
101         assertEvalEquals("$[1]", "$[1]");
102         assertEvalEquals("$[1]1", "$[1]1");
103         assertEvalEquals("$1[1]1", "$1[1]1");
104         assertEvalEquals("blue.[]", "$red.[]");
105         assertEvalEquals("blue[]", "${red}[]");
106         assertEvalEquals("blue][", "$red][");
107         assertEvalEquals("1[]", "${a[0]}[]");
108         assertEvalEquals("1a$[]", "$a[0]a$[]");
109         assertEvalEquals("$![]", "$![]");
110         assertEvalEquals("$\\![]", "$\\![]");
111     }
112 
113     public void testIndexSetting()
114     {
115         assertEvalEquals("foo", "#set($str[1] = \"foo\")$str[1]");
116         assertEvalEquals("5150", "#set($alist[1] = 5150)$alist[1]");
117         assertEvalEquals("15", "$alist[3][0]#set($alist[3][0] = 5)$alist[3][0]");
118         assertEvalEquals("orange","#set($blaa = {\"apple\":\"grape\"})#set($blaa[\"apple\"] = \"orange\")$blaa[\"apple\"]");
119         assertEvalEquals("null","#set($str[0] = $NULL)#if($str[0] == $NULL)null#end");
120         assertEvalEquals("null","#set($blaa = {\"apple\":\"grape\"})#set($blaa[\"apple\"] = $NULL)#if($blaa[\"apple\"] == $NULL)null#end");
121         assertEvalEquals("2112", "#set($a[-1] = 2112)$a[2]");
122         assertEvalEquals("3344","#set($hash = {1:11, 2:22, 5:66})#set($hash[2]=33)#set($hash[3]=44)$hash[2]$hash[3]");
123     }
124     
125     
126     public void testErrorHandling()
127     {
128         assertEvalExceptionAt("$boo['throwex']", 1, 5);
129         assertEvalExceptionAt("$boo[]", 1, 6);
130         assertEvalExceptionAt("$boo[blaa]", 1, 6);
131         assertEvalExceptionAt("#set($foo[1] = 3)", 1, 10);
132         assertEvalExceptionAt("$a[500]", 1, 3);
133     }
134     
135     
136     public static class Foo
137     {
138         public Object bar = null;
139         public Object getBar()
140         {
141             return bar;
142         }
143 
144         public String get(Boolean bool)
145         {
146             if (bool.booleanValue())
147                 return "BIG TRUE";
148             else
149                 return "BIG FALSE";
150         }
151         
152         public String get(String str)
153         {
154             return str + " foobar ";
155         }
156     }
157 
158     public static class Boo
159     {
160         public Object get(Object obj)
161         {
162             if (obj == null)
163               return "GOT NULL";
164             else if (obj.equals("throwex"))
165                 throw new RuntimeException("Generated Exception");
166 
167             return obj;
168         }        
169     }    
170 }