aboutsummaryrefslogtreecommitdiff
path: root/velocity-engine-core/src/test/java/org/apache/velocity/test/ArrayMethodsTestCase.java
blob: ceb82e809c9a9182ff17865f975701cf3fb811cc (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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 java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Used to check that method calls on Array references work properly
 * and that they produce the same results as the same methods would on
 * a fixed-size {@link List}.
 */
public class ArrayMethodsTestCase extends BaseTestCase
{
    public ArrayMethodsTestCase(final String name)
    {
        super(name);
    }

    /**
     * Runs the test.
     */
    public void testArrayMethods() throws Exception
    {
        // test an array of string objects
        Object array = new String[] { "foo", "bar", "baz" };
        checkResults(array, "woogie", true);

        // test an array of primitive ints
        array = new int[] { 1, 3, 7 };
        checkResults(array, 11, false);

        // test an array of mixed objects, including null
        array = new Object[] { 2.2, null };
        checkResults(array, "whatever", true);
        // then set all the values to null
        checkResults(array, null, true);

        // then try an empty array
        array = new Object[] {};
        checkResults(array, null, true);

        // while we have an empty array and list in the context,
        // make sure $array.get(0) and $list.get(0) throw
        // the same type of exception (MethodInvocationException)
        Throwable lt = null;
        Throwable at = null;
        try
        {
            evaluate("$list.get(0)");
        }
        catch (Throwable t)
        {
            lt = t;
        }
        try
        {
            evaluate("$array.get(0)");
        }
        catch (Throwable t)
        {
            at = t;
        }
        assertEquals(lt.getClass(), at.getClass());
    }

    private void checkResults(Object array, Object setme,
                              boolean compareToList) throws Exception
    {
        context.put("array", array);
        if (compareToList)
        {
            // create a list to match...
            context.put("list", new ArrayList(Arrays.asList((Object[])array)));
        }

        // if the object to be set is null, then remove instead of put
        if (setme != null)
        {
            context.put("setme", setme);
        }
        else
        {
            context.remove("setme");
        }

        info("Changing to an array of: " + array.getClass().getComponentType());
        info("Changing setme to: " + setme);

        int size = Array.getLength(array);
        checkResult("size()", String.valueOf(size), compareToList);

        boolean isEmpty = (size == 0);
        checkResult("isEmpty()", String.valueOf(isEmpty), compareToList);

        checkPropertyResult("empty", String.valueOf(isEmpty), compareToList);

        // Since 2.1, arrays are rendered the same way as lists
        String renderArray = evaluate("$array");
        String renderList = evaluate("$list");
        System.err.println("<<< " + renderArray);
        System.err.println(">>> " + renderList);
        if (compareToList) assertTrue(renderArray.equals(renderList));
        else assertFalse(renderArray.equals(renderList));

        for (int i=0; i < size; i++)
        {
            // put the index in the context, so we can try
            // both an explicit index and a reference index
            context.put("index", i);

            Object value = Array.get(array, i);
            String get = "get($index)";
            String set = "set("+i+", $setme)";
            if (value == null)
            {
                checkEmptyResult(get, compareToList);
                // set should return null
                checkEmptyResult(set, compareToList);
            }
            else
            {
                checkResult(get, value.toString(), compareToList);
                // set should return the old get value
                checkResult(set, value.toString(), compareToList);
            }

            // check that set() actually changed the value
            assertEquals(setme, Array.get(array, i));

            // and check that get() now returns setme
            if (setme == null)
            {
                checkEmptyResult(get, compareToList);
            }
            else
            {
                checkResult(get, setme.toString(), compareToList);

                // now check that contains() properly finds the new value
                checkResult("contains($setme)", "true", compareToList);
            }
        }
    }

    private void checkEmptyResult(String method, boolean compareToList)
        throws Exception
    {
        checkResult(method, "", compareToList);
    }

    private void checkResult(String method, String expected,
                             boolean compareToList) throws Exception
    {
        String result = evaluate("$!array."+method);
        assertEquals(expected, result);

        String listResult = null;
        if (compareToList)
        {
            listResult = evaluate("$!list."+method);
            assertEquals(result, listResult);
        }

        info("    <$!array." + method + "> resolved to <" + result + ">");
        if (compareToList)
        {
            info("    <$!list."+method+"> resolved to "+listResult+">");
        }
    }

    private void checkPropertyResult(String property, String expected,
                             boolean compareToList) throws Exception
    {
        String result = evaluate("$!array."+property);
        assertEquals(expected, result);

        String listResult = null;
        if (compareToList)
        {
            listResult = evaluate("$!list."+property);
            assertEquals(result, listResult);
        }

        info("    <$!array."+property+"> resolved to <"+result+">");
        if (compareToList)
        {
            info("    <$!list."+property+"> resolved to "+listResult+">");
        }
    }

}