View Javadoc

1   package org.apache.velocity.tools.generic;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.junit.*;
23  import static org.junit.Assert.*;
24  import java.util.*;
25  
26  /**
27   * <p>Tests for LoopTool</p>
28   *
29   * @author Nathan Bubna
30   * @since VelocityTools 2.0
31   * @version $Id$
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          // skip nothing
54          loop.skip(0);
55          assertEquals(i.next(), ARRAY[0]);
56          // skip one (should be on 2 now)
57          loop.skip(1);
58          assertEquals(i.next(), ARRAY[2]);
59          // end this
60          loop.pop();
61          // start over to skip 2
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          // these should not do anything
74          loop.skip(1, null);
75          loop.skip(1, "");
76          loop.skip(1, "test");
77          // these should work
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         // these shouldn't stop anything
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         // this should only stop j and k
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         // these shouldn't stop anything
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         // this should only stop j
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         // try to watch unwatchable things
154         Iterator i = loop.watch(null);
155         assertNull(i);
156         assertNull(loop.watch(new Object()));
157         // watch an array
158         assertNotNull(loop.watch(ARRAY));
159         // watch an iterator
160         assertNotNull(loop.watch(loop.watch(ARRAY)));
161         //TODO: watch enumeration
162         // watch collection
163         assertNotNull(loop.watch(LIST));
164         // watch map
165         assertNotNull(loop.watch(new HashMap()));
166         // watch iterable
167         assertNotNull(loop.watch(new MyIterable()));
168         // watch object w/iterator method
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         // null names are invalid
188         Iterator i = loop.watch(ARRAY, null);
189         assertNull(i);
190         // empty names are ok
191         assertNotNull(loop.watch(ARRAY, ""));
192         // so are real ones
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         // check short syntax too
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         // check short syntax too
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         // sync an array with itself
273         Iterator i = loop.watch(ARRAY).sync(ARRAY, "twin");
274         while (i.hasNext())
275         {
276             // make sure they match
277             assertEquals(i.next(), loop.get("twin"));
278         }
279 
280         // sync a shorter array with a longer one to be
281         // sure the values turn to null once they're gone
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         // sync arrays with nested loops using default names
305         // the way we iterate over both i and j together below
306         // is, of course, impossible in a template, but it
307         // makes writing a reasonable test for this method a lot
308         // easier.
309         //NOTE: this reliese on the default name for synced iterators
310         //      being "synced", for i being "loop0", and for j being "loop1"
311         Iterator i = loop.watch(ARRAY).sync(other);
312         Iterator j = loop.watch(other).sync(ARRAY);
313         while (i.hasNext() && j.hasNext())
314         {
315             // i and loop.synced (aka loop.get("loop1","synced")) should match
316             assertEquals(i.next(), loop.get("synced"));
317             // j and loop.get("loop0","synced") should match
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         // test that skipped iterations are still included
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         // check short syntax too
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