aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/test/java/org/apache/velocity/test/IndexTestCase.java
blob: a585de7e3e7f65f2925ec576b951a1e8a3635997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package org.apache.velocity.test;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.velocity.runtime.RuntimeConstants;

import java.util.ArrayList;
/**
 * Test index syntax e.g, $foo[1]
 */
public class IndexTestCase extends BaseTestCase
{
    public IndexTestCase(String name)
    {
        super(name);
        //DEBUG = true;
    }

    @Override
    public void setUp() throws Exception
    {
        super.setUp();
        engine.setProperty(RuntimeConstants.RUNTIME_REFERENCES_STRICT, Boolean.TRUE);

        context.put("NULL", null);
        context.put("red", "blue");

        int[] a = {1, 2, 3};
        context.put("a", a);
        String[] str = {"a", "ab", "abc"};
        context.put("str", str);

        ArrayList alist = new ArrayList();
        alist.add(1);
        alist.add(2);
        alist.add(3);
        alist.add(a);
        alist.add(null);
        context.put("alist", alist);

        Foo foo = new Foo();
        foo.bar = alist;
        context.put("foo", foo);

        Boo boo = new Boo();
        context.put("boo", boo);
    }

    public void testCallingIndex()
    {
        assertEvalEquals("1 -3-", "$a[0] -$a[ 2 ]-");
        assertEvalEquals("x21", "#set($i = 1)x$a[$i]1");
        assertEvalEquals("3", "$a[ $a[ $a[0] ] ]");
        assertEvalEquals("ab", "$str[1]");
        assertEvalEquals("123", "$alist[0]$alist[1]$alist[2]");
        assertEvalEquals("1][2-[3]", "$alist[0]][$alist[$a[0]]-[$alist[2]]");

        assertEvalEquals("2", "$alist[3][1]");
        assertEvalEquals("3 [1]", "$alist[2] [1]");

        assertEvalEquals("4", "#set($bar = [3, 4, 5])$bar[1]");
        assertEvalEquals("21", "#set($i = 1)#set($bar = [3, $a[$i], $a[0]])$bar[1]$bar[2]");

        assertEvalEquals("2", "$foo.bar[1]");
        assertEvalEquals("2", "$foo.getBar()[1]");
        assertEvalEquals("2", "$foo.getBar()[3][1]");

        assertEvalEquals(" a  ab  abc ", "#foreach($i in $foo.bar[3]) $str[$foreach.index] #end");

        assertEvalEquals("apple", "#set($hash = {'a':'apple', 'b':'orange'})$hash['a']");

        assertEvalEquals("xx ", "#if($alist[4] == $NULL)xx#end #if($alist[4])yy#end");

        assertEvalEquals("BIG TRUEaBIG FALSE", "$foo[true]a$foo[false]");
        assertEvalEquals("junk foobar ", "$foo[\"junk\"]");
        assertEvalEquals("GOT NULL", "#set($i=$NULL)$boo[$i]");

        assertEvalEquals("321", "$a[-1]$a[ -2]$a[-3 ]");
        assertEvalEquals("67xx", "#set($hash={1:11, 5:67, 23:2})$hash[5]$!hash[6]#if(!$hash[1000])xx#end");

        // Some cases that should be evaluated as text
        assertEvalEquals("[]", "[]");
        assertEvalEquals("$[]", "$[]");
        assertEvalEquals("$.[]", "$.[]");
        assertEvalEquals("$[1]", "$[1]");
        assertEvalEquals("$[1]1", "$[1]1");
        assertEvalEquals("$1[1]1", "$1[1]1");
        assertEvalEquals("blue.[]", "$red.[]");
        assertEvalEquals("blue[]", "${red}[]");
        assertEvalEquals("blue][", "$red][");
        assertEvalEquals("1[]", "${a[0]}[]");
        assertEvalEquals("1a$[]", "$a[0]a$[]");
        assertEvalEquals("$![]", "$![]");
        assertEvalEquals("$\\![]", "$\\![]");
    }

    public void testIndexSetting()
    {
        assertEvalEquals("foo", "#set($str[1] = \"foo\")$str[1]");
        assertEvalEquals("5150", "#set($alist[1] = 5150)$alist[1]");
        assertEvalEquals("15", "$alist[3][0]#set($alist[3][0] = 5)$alist[3][0]");
        assertEvalEquals("orange","#set($blaa = {\"apple\":\"grape\"})#set($blaa[\"apple\"] = \"orange\")$blaa[\"apple\"]");
        assertEvalEquals("null","#set($str[0] = $NULL)#if($str[0] == $NULL)null#end");
        assertEvalEquals("null","#set($blaa = {\"apple\":\"grape\"})#set($blaa[\"apple\"] = $NULL)#if($blaa[\"apple\"] == $NULL)null#end");
        assertEvalEquals("2112", "#set($a[-1] = 2112)$a[2]");
        assertEvalEquals("3344","#set($hash = {1:11, 2:22, 5:66})#set($hash[2]=33)#set($hash[3]=44)$hash[2]$hash[3]");
    }


    public void testErrorHandling()
    {
        assertEvalExceptionAt("$boo['throwex']", 1, 5);
        assertEvalExceptionAt("$boo[]", 1, 6);
        assertEvalExceptionAt("$boo[blaa]", 1, 6);
        assertEvalExceptionAt("#set($foo[1] = 3)", 1, 10);
        assertEvalExceptionAt("$a[500]", 1, 3);
    }


    public static class Foo
    {
        public Object bar = null;
        public Object getBar()
        {
            return bar;
        }

        public String get(Boolean bool)
        {
            if (bool)
                return "BIG TRUE";
            else
                return "BIG FALSE";
        }

        public String get(String str)
        {
            return str + " foobar ";
        }
    }

    public static class Boo
    {
        public Object get(Object obj)
        {
            if (obj == null)
              return "GOT NULL";
            else if (obj.equals("throwex"))
                throw new RuntimeException("Generated Exception");

            return obj;
        }
    }
}