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.List;
25
26 import junit.framework.TestCase;
27
28 import org.apache.velocity.VelocityContext;
29 import org.apache.velocity.app.Velocity;
30 import org.apache.velocity.runtime.RuntimeConstants;
31 import org.apache.velocity.runtime.log.NullLogChute;
32 import org.apache.velocity.test.provider.ForeachMethodCallHelper;
33
34
35
36
37
38
39
40 public class ForeachTestCase extends TestCase
41 {
42 private VelocityContext context;
43
44 public ForeachTestCase(String name)
45 {
46 super(name);
47 }
48
49 public void setUp()
50 throws Exception
51 {
52
53 Velocity.setProperty(RuntimeConstants.MAX_NUMBER_LOOPS,
54 new Integer(3));
55
56 Velocity.setProperty(
57 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
58
59 Velocity.init();
60
61 context = new VelocityContext();
62 }
63
64
65
66
67 public void testMaxNbrLoopsConstraint()
68 throws Exception
69 {
70 StringWriter writer = new StringWriter();
71 String template = "#foreach ($item in [1..10])$item #end";
72 Velocity.evaluate(context, writer, "test", template);
73 assertEquals("Max number loops not enforced",
74 "1 2 3 ", writer.toString());
75 }
76
77
78
79
80
81 public void testMethodCall()
82 throws Exception
83 {
84 List col = new ArrayList();
85 col.add(new Integer(100));
86 col.add("STRVALUE");
87 context.put("helper", new ForeachMethodCallHelper());
88 context.put("col", col);
89
90 StringWriter writer = new StringWriter();
91 Velocity.evaluate(context, writer, "test",
92 "#foreach ( $item in $col )$helper.getFoo($item) " +
93 "#end");
94 assertEquals("Method calls while looping over varying classes failed",
95 "int 100 str STRVALUE ", writer.toString());
96 }
97 }