1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.lang.reflect.Array;
23 import java.util.Arrays;
24 import java.util.ArrayList;
25 import java.util.List;
26
27
28
29
30
31
32 public class ArrayMethodsTestCase extends BaseEvalTestCase
33 {
34 private final static boolean PRINT_RESULTS = false;
35
36 public ArrayMethodsTestCase(final String name)
37 {
38 super(name);
39 }
40
41
42
43
44 public void testArrayMethods() throws Exception
45 {
46
47 Object array = new String[] { "foo", "bar", "baz" };
48 checkResults(array, "woogie", true);
49
50
51 array = new int[] { 1, 3, 7 };
52 checkResults(array, new Integer(11), false);
53
54
55 array = new Object[] { new Double(2.2), null };
56 checkResults(array, "whatever", true);
57
58 checkResults(array, null, true);
59
60
61 array = new Object[] {};
62 checkResults(array, null, true);
63
64
65
66
67 Throwable lt = null;
68 Throwable at = null;
69 try
70 {
71 evaluate("$list.get(0)");
72 }
73 catch (Throwable t)
74 {
75 lt = t;
76 }
77 try
78 {
79 evaluate("$array.get(0)");
80 }
81 catch (Throwable t)
82 {
83 at = t;
84 }
85 assertEquals(lt.getClass(), at.getClass());
86 }
87
88 private void checkResults(Object array, Object setme,
89 boolean compareToList) throws Exception
90 {
91 context.put("array", array);
92 if (compareToList)
93 {
94
95 context.put("list", new ArrayList(Arrays.asList((Object[])array)));
96 }
97
98
99 if (setme != null)
100 {
101 context.put("setme", setme);
102 }
103 else
104 {
105 context.remove("setme");
106 }
107
108 if (PRINT_RESULTS)
109 {
110 System.out.println("Changing to an array of: " + array.getClass().getComponentType());
111 System.out.println("Changing setme to: " + setme);
112 }
113
114 int size = Array.getLength(array);
115 checkResult("size()", String.valueOf(size), compareToList);
116
117 boolean isEmpty = (size == 0);
118 checkResult("isEmpty()", String.valueOf(isEmpty), compareToList);
119
120
121
122 assertFalse(evaluate("$array").equals(evaluate("$list")));
123
124 for (int i=0; i < size; i++)
125 {
126
127
128 context.put("index", new Integer(i));
129
130 Object value = Array.get(array, i);
131 String get = "get($index)";
132 String set = "set("+i+", $setme)";
133 if (value == null)
134 {
135 checkEmptyResult(get, compareToList);
136
137 checkEmptyResult(set, compareToList);
138 }
139 else
140 {
141 checkResult(get, value.toString(), compareToList);
142
143 checkResult(set, value.toString(), compareToList);
144 }
145
146
147 assertEquals(setme, Array.get(array, i));
148
149
150 if (setme == null)
151 {
152 checkEmptyResult(get, compareToList);
153 }
154 else
155 {
156 checkResult(get, setme.toString(), compareToList);
157
158
159 checkResult("contains($setme)", "true", compareToList);
160 }
161 }
162 }
163
164 private void checkEmptyResult(String method, boolean compareToList)
165 throws Exception
166 {
167 checkResult(method, "", compareToList);
168 }
169
170 private void checkResult(String method, String expected,
171 boolean compareToList) throws Exception
172 {
173 String result = evaluate("$!array."+method);
174 assertEquals(expected, result);
175
176 String listResult = null;
177 if (compareToList)
178 {
179 listResult = evaluate("$!list."+method);
180 assertEquals(result, listResult);
181 }
182
183 if (PRINT_RESULTS)
184 {
185 System.out.println(" <$!array."+method+"> resolved to <"+result+">");
186 if (compareToList)
187 {
188 System.out.println(" <$!list."+method+"> resolved to "+listResult+">");
189 }
190 }
191 }
192
193 }
194
195