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