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.util.ArrayList;
23 import java.util.Iterator;
24 import org.apache.velocity.VelocityContext;
25 import org.apache.velocity.exception.VelocityException;
26 import org.apache.velocity.runtime.RuntimeConstants;
27
28
29
30
31 public class StrictForeachTestCase extends BaseEvalTestCase
32 {
33 public StrictForeachTestCase(String name)
34 {
35 super(name);
36 }
37
38 public void setUp() throws Exception
39 {
40 super.setUp();
41 engine.setProperty(RuntimeConstants.SKIP_INVALID_ITERATOR, Boolean.FALSE);
42 context.put("good", new GoodIterable());
43 context.put("bad", new BadIterable());
44 context.put("ugly", new UglyIterable());
45 }
46
47 public void testGood()
48 {
49 try
50 {
51 evaluate("#foreach( $i in $good )$i#end");
52 }
53 catch (VelocityException ve)
54 {
55 fail("Doing #foreach on $good should not have exploded!");
56 }
57 }
58
59 public void testBad()
60 {
61 try
62 {
63 evaluate("#foreach( $i in $bad )$i#end");
64 fail("Doing #foreach on $bad should have exploded!");
65 }
66 catch (VelocityException ve)
67 {
68
69 }
70 }
71
72 public void testUgly()
73 {
74 try
75 {
76 evaluate("#foreach( $i in $ugly )$i#end");
77 fail("Doing #foreach on $ugly should have exploded!");
78 }
79 catch (VelocityException ve)
80 {
81
82 }
83 }
84
85
86 public static class GoodIterable
87 {
88 public Iterator iterator()
89 {
90 return new ArrayList().iterator();
91 }
92 }
93
94 public static class BadIterable
95 {
96 public Object iterator()
97 {
98 return new Object();
99 }
100 }
101
102 public static class UglyIterable
103 {
104 public Iterator iterator()
105 {
106 return null;
107 }
108 }
109 }