1 package org.apache.velocity.tools.generic;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.junit.*;
23 import static org.junit.Assert.*;
24 import java.util.*;
25
26
27
28
29
30
31
32
33 public class LoopToolTests {
34
35 public static final String[] ARRAY = { "foo", "bar", "woogie" };
36
37 public @Test void ctorLoopTool() throws Exception
38 {
39 try
40 {
41 new LoopTool();
42 }
43 catch (Exception e)
44 {
45 fail("Constructor 'LoopTool()' failed due to: " + e);
46 }
47 }
48
49 public @Test void methodSkip_int() throws Exception
50 {
51 LoopTool loop = new LoopTool();
52 Iterator i = loop.watch(ARRAY);
53
54 loop.skip(0);
55 assertEquals(i.next(), ARRAY[0]);
56
57 loop.skip(1);
58 assertEquals(i.next(), ARRAY[2]);
59
60 loop.pop();
61
62 i = loop.watch(ARRAY);
63 loop.skip(2);
64 assertEquals(i.next(), ARRAY[2]);
65 }
66
67 public @Test void methodSkip_intString() throws Exception
68 {
69 LoopTool loop = new LoopTool();
70 Iterator i = loop.watch(ARRAY, "i");
71 Iterator j = loop.watch(ARRAY, "j");
72 Iterator k = loop.watch(ARRAY, "k");
73
74 loop.skip(1, null);
75 loop.skip(1, "");
76 loop.skip(1, "test");
77
78 loop.skip(1, "i");
79 loop.skip(1, "j");
80 loop.skip(1, "k");
81 assertEquals(i.next(), ARRAY[1]);
82 assertEquals(j.next(), ARRAY[1]);
83 assertEquals(k.next(), ARRAY[1]);
84 }
85
86 public @Test void methodStop() throws Exception
87 {
88 LoopTool loop = new LoopTool();
89 Iterator i = loop.watch(ARRAY);
90 assertTrue(i.hasNext());
91 loop.stop();
92 assertFalse(i.hasNext());
93 }
94
95 public @Test void methodStopAll() throws Exception
96 {
97 LoopTool loop = new LoopTool();
98 Iterator i = loop.watch(ARRAY);
99 Iterator j = loop.watch(ARRAY);
100 Iterator k = loop.watch(j);
101 assertTrue(i.hasNext() && j.hasNext() && k.hasNext());
102 loop.stopAll();
103 assertFalse(i.hasNext() || j.hasNext() || k.hasNext());
104 }
105
106 public @Test void methodStopTo_String() throws Exception
107 {
108 LoopTool loop = new LoopTool();
109 Iterator i = loop.watch(ARRAY, "i");
110 Iterator j = loop.watch(ARRAY, "j");
111 Iterator k = loop.watch(ARRAY, "k");
112 assertTrue(i.hasNext() && j.hasNext() && k.hasNext());
113
114
115 loop.stopTo(null);
116 assertTrue(i.hasNext() && j.hasNext() && k.hasNext());
117 loop.stopTo("");
118 assertTrue(i.hasNext() && j.hasNext() && k.hasNext());
119 loop.stopTo("test");
120 assertTrue(i.hasNext() && j.hasNext() && k.hasNext());
121
122
123 loop.stopTo("j");
124 assertTrue(i.hasNext());
125 assertFalse(j.hasNext() || k.hasNext());
126 }
127
128 public @Test void methodStop_String() throws Exception
129 {
130 LoopTool loop = new LoopTool();
131 Iterator i = loop.watch(ARRAY, "i");
132 Iterator j = loop.watch(ARRAY, "j");
133 Iterator k = loop.watch(ARRAY, "k");
134 assertTrue(i.hasNext() && j.hasNext() && k.hasNext());
135
136
137 loop.stop(null);
138 assertTrue(i.hasNext() && j.hasNext() && k.hasNext());
139 loop.stop("");
140 assertTrue(i.hasNext() && j.hasNext() && k.hasNext());
141 loop.stop("test");
142 assertTrue(i.hasNext() && j.hasNext() && k.hasNext());
143
144
145 loop.stop("j");
146 assertTrue(i.hasNext() && k.hasNext());
147 assertFalse(j.hasNext());
148 }
149
150 public @Test void methodWatch_Object() throws Exception
151 {
152 LoopTool loop = new LoopTool();
153
154 Iterator i = loop.watch(null);
155 assertNull(i);
156 assertNull(loop.watch(new Object()));
157
158 assertNotNull(loop.watch(ARRAY));
159
160 assertNotNull(loop.watch(loop.watch(ARRAY)));
161
162
163 assertNotNull(loop.watch(LIST));
164
165 assertNotNull(loop.watch(new HashMap()));
166
167 assertNotNull(loop.watch(new MyIterable()));
168
169 assertNotNull(loop.watch(new HasIteratorMethod()));
170 }
171
172 static final Collection LIST = Arrays.asList(ARRAY);
173 public static class HasIteratorMethod
174 {
175 public Iterator iterator()
176 {
177 return LoopToolTests.LIST.iterator();
178 }
179 }
180 public static class MyIterable extends HasIteratorMethod implements Iterable
181 {
182 }
183
184 public @Test void methodWatch_ObjectString() throws Exception
185 {
186 LoopTool loop = new LoopTool();
187
188 Iterator i = loop.watch(ARRAY, null);
189 assertNull(i);
190
191 assertNotNull(loop.watch(ARRAY, ""));
192
193 assertNotNull(loop.watch(ARRAY, "name"));
194 }
195
196 public @Test void methodIsFirst() throws Exception
197 {
198 LoopTool loop = new LoopTool();
199 Iterator i = loop.watch(ARRAY);
200 assertTrue(loop.isFirst());
201 i.next();
202 assertTrue(loop.isFirst());
203 i.next();
204 assertFalse(loop.isFirst());
205 }
206
207 public @Test void methodIsFirst_String() throws Exception
208 {
209 LoopTool loop = new LoopTool();
210 Iterator i = loop.watch(ARRAY, "i");
211 assertTrue(loop.isFirst());
212 i.next();
213 Iterator j = loop.watch(ARRAY, "j");
214 assertTrue(loop.isFirst());
215 assertTrue(loop.isFirst("i"));
216 assertTrue(loop.isFirst("j"));
217
218 assertTrue((Boolean)loop.get("first_i"));
219 assertTrue((Boolean)loop.get("first_j"));
220 i.next();
221 assertFalse(loop.isFirst("i"));
222 assertFalse((Boolean)loop.get("first_i"));
223 j.next();
224 assertTrue(loop.isFirst());
225 assertTrue(loop.isFirst("j"));
226 assertTrue((Boolean)loop.get("first_j"));
227 j.next();
228 assertFalse(loop.isFirst("j"));
229 assertFalse((Boolean)loop.get("first_j"));
230 }
231
232 public @Test void methodIsLast() throws Exception
233 {
234 LoopTool loop = new LoopTool();
235 Iterator i = loop.watch(ARRAY);
236 assertFalse(loop.isLast());
237 i.next();
238 assertFalse(loop.isLast());
239 i.next();
240 assertFalse(loop.isLast());
241 i.next();
242 assertTrue(loop.isLast());
243 }
244
245 public @Test void methodIsLast_String() throws Exception
246 {
247 LoopTool loop = new LoopTool();
248 Iterator i = loop.watch(ARRAY, "i");
249 assertFalse(loop.isLast());
250 i.next();
251 i.next();
252 i.next();
253 assertTrue(loop.isLast());
254 Iterator j = loop.watch(ARRAY, "j");
255 assertFalse(loop.isLast());
256 assertTrue(loop.isLast("i"));
257 assertFalse(loop.isLast("j"));
258
259 assertTrue((Boolean)loop.get("last_i"));
260 assertFalse((Boolean)loop.get("last_j"));
261 j.next();
262 j.next();
263 j.next();
264 assertTrue(loop.isLast());
265 assertTrue(loop.isLast("j"));
266 assertTrue((Boolean)loop.get("last_j"));
267 }
268
269 public @Test void methodGet() throws Exception
270 {
271 LoopTool loop = new LoopTool();
272
273 Iterator i = loop.watch(ARRAY).sync(ARRAY, "twin");
274 while (i.hasNext())
275 {
276
277 assertEquals(i.next(), loop.get("twin"));
278 }
279
280
281
282 int[] little = { 10, 20, 30 };
283 Integer[] big = { 1, 2, 3, 4, 5 };
284 i = loop.watch(big).sync(little, "little");
285 while (i.hasNext())
286 {
287 Integer val = (Integer)i.next();
288 if (val < 4)
289 {
290 assertEquals(val * 10, loop.get("little"));
291 }
292 else
293 {
294 assertNull(loop.get("little"));
295 }
296 }
297 }
298
299 public @Test void methodGet_StringString() throws Exception
300 {
301 LoopTool loop = new LoopTool();
302 int[] other = { 1, 2, 3 };
303
304
305
306
307
308
309
310
311 Iterator i = loop.watch(ARRAY).sync(other);
312 Iterator j = loop.watch(other).sync(ARRAY);
313 while (i.hasNext() && j.hasNext())
314 {
315
316 assertEquals(i.next(), loop.get("synced"));
317
318 assertEquals(j.next(), loop.get("loop0", "synced"));
319 }
320 }
321
322 public @Test void methodGetCountOrGetIndex() throws Exception
323 {
324 LoopTool loop = new LoopTool();
325 Iterator i = loop.watch(ARRAY);
326 assertEquals(0, loop.getCount());
327 assertNull(loop.getIndex());
328 i.next();
329 assertEquals(1, loop.getCount());
330 assertEquals(0, loop.getIndex());
331 i.next();
332 assertEquals(2, loop.getCount());
333 assertEquals(1, loop.getIndex());
334 i.next();
335 assertEquals(3, loop.getCount());
336 assertEquals(2, loop.getIndex());
337 loop.pop();
338
339 i = loop.watch(ARRAY);
340 loop.skip(2);
341 assertEquals(2, loop.getCount());
342 assertEquals(1, loop.getIndex());
343 }
344
345 public @Test void methodGetCountOrGetIndex_String() throws Exception
346 {
347 LoopTool loop = new LoopTool();
348 Iterator i = loop.watch(ARRAY, "i");
349 assertEquals(0, loop.getCount("i"));
350 assertNull(loop.getIndex("i"));
351 i.next();
352 assertEquals(1, loop.getCount("i"));
353 assertEquals(0, loop.getIndex("i"));
354 Iterator j = loop.watch(ARRAY, "j");
355 loop.skip(2);
356 assertEquals(2, loop.getCount("j"));
357 assertEquals(1, loop.getIndex("j"));
358 assertEquals(1, loop.getCount("i"));
359 assertEquals(0, loop.getIndex("i"));
360
361 assertEquals(2, loop.get("count_j"));
362 assertEquals(1, loop.get("index_j"));
363 assertEquals(1, loop.get("count_i"));
364 assertEquals(0, loop.get("index_i"));
365 }
366
367 public @Test void aliasMethods() throws Exception
368 {
369 LoopTool loop = new LoopTool();
370 Iterator i = loop.watch(ARRAY);
371 assertEquals(loop.isFirst(), loop.getFirst());
372 assertEquals(loop.isLast(), loop.getLast());
373 i.next();
374 assertEquals(loop.isFirst(), loop.getFirst());
375 assertEquals(loop.isLast(), loop.getLast());
376 i.next();
377 assertEquals(loop.isFirst(), loop.getFirst());
378 assertEquals(loop.isLast(), loop.getLast());
379 i.next();
380 assertEquals(loop.isFirst(), loop.getFirst());
381 assertEquals(loop.isLast(), loop.getLast());
382 }
383
384 public @Test void watchAndExclude() throws Exception
385 {
386 LoopTool loop = new LoopTool();
387 Iterator i = loop.watch(ARRAY).exclude("bar");
388 assertEquals(i.next(), "foo");
389 assertEquals(i.next(), "woogie");
390 assertTrue(loop.isLast());
391 assertFalse(i.hasNext());
392 }
393
394 public @Test void watchAndStop() throws Exception
395 {
396 LoopTool loop = new LoopTool();
397 Iterator i = loop.watch(ARRAY).stop("bar");
398 assertEquals(i.next(), "foo");
399 assertTrue(loop.isLast());
400 assertFalse(i.hasNext());
401 }
402
403 public @Test void method_getDepth() throws Exception
404 {
405 LoopTool loop = new LoopTool();
406 assertEquals(0, loop.getDepth());
407 loop.watch(ARRAY);
408 assertEquals(1, loop.getDepth());
409 loop.watch(ARRAY);
410 assertEquals(2, loop.getDepth());
411 loop.pop();
412 assertEquals(1, loop.getDepth());
413 loop.pop();
414 assertEquals(0, loop.getDepth());
415 }
416
417 }
418