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.io.StringWriter;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26
27 import junit.framework.TestCase;
28
29 import org.apache.velocity.VelocityContext;
30 import org.apache.velocity.app.Velocity;
31 import org.apache.velocity.runtime.RuntimeConstants;
32 import org.apache.velocity.test.misc.TestLogChute;
33 import org.apache.velocity.test.provider.ForeachMethodCallHelper;
34
35
36
37
38
39
40
41 public class ForeachTestCase extends TestCase
42 {
43 private VelocityContext context;
44
45 public ForeachTestCase(String name)
46 {
47 super(name);
48 }
49
50 public void setUp()
51 throws Exception
52 {
53
54 Velocity.setProperty(RuntimeConstants.MAX_NUMBER_LOOPS,
55 new Integer(3));
56
57 Velocity.setProperty(
58 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
59
60 Velocity.init();
61
62 context = new VelocityContext();
63 }
64
65
66
67
68 public void testMaxNbrLoopsConstraint()
69 throws Exception
70 {
71 StringWriter writer = new StringWriter();
72 String template = "#foreach ($item in [1..10])$item #end";
73 Velocity.evaluate(context, writer, "test", template);
74 assertEquals("Max number loops not enforced",
75 "1 2 3 ", writer.toString());
76 }
77
78
79
80
81
82 public void testCollectionAndMethodCall()
83 throws Exception
84 {
85 List col = new ArrayList();
86 col.add(new Integer(100));
87 col.add("STRVALUE");
88 context.put("helper", new ForeachMethodCallHelper());
89 context.put("col", col);
90
91 StringWriter writer = new StringWriter();
92 Velocity.evaluate(context, writer, "test",
93 "#foreach ( $item in $col )$helper.getFoo($item) " +
94 "#end");
95 assertEquals("Method calls while looping over varying classes failed",
96 "int 100 str STRVALUE ", writer.toString());
97 }
98
99
100
101
102
103
104 public void testObjectWithIteratorMethod()
105 throws Exception
106 {
107 context.put("iterable", new MyIterable());
108
109 StringWriter writer = new StringWriter();
110 String template = "#foreach ($i in $iterable)$i #end";
111 Velocity.evaluate(context, writer, "test", template);
112 assertEquals("Failed to call iterator() method",
113 "1 2 3 ", writer.toString());
114 }
115
116 public void testNotReallyIterableIteratorMethod()
117 throws Exception
118 {
119 context.put("nri", new NotReallyIterable());
120
121 StringWriter writer = new StringWriter();
122 String template = "#foreach ($i in $nri)$i #end";
123 Velocity.evaluate(context, writer, "test", template);
124 assertEquals("", writer.toString());
125 }
126
127
128 public static class MyIterable
129 {
130 private List foo;
131
132 public MyIterable()
133 {
134 foo = new ArrayList();
135 foo.add(new Integer(1));
136 foo.add(new Long(2));
137 foo.add("3");
138 }
139
140 public Iterator iterator()
141 {
142 return foo.iterator();
143 }
144 }
145
146 public static class NotReallyIterable
147 {
148 public Object iterator()
149 {
150 return new Object();
151 }
152 }
153
154 }