01: package org.apache.velocity.test;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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 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: * This class tests the Foreach loop.
36: *
37: * @author Daniel Rall
38: * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
39: */
40: public class ForeachTestCase extends TestCase {
41: private VelocityContext context;
42:
43: public ForeachTestCase(String name) {
44: super (name);
45: }
46:
47: public void setUp() throws Exception {
48: // Limit the loop to three iterations.
49: Velocity.setProperty(RuntimeConstants.MAX_NUMBER_LOOPS,
50: new Integer(3));
51:
52: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
53: NullLogChute.class.getName());
54:
55: Velocity.init();
56:
57: context = new VelocityContext();
58: }
59:
60: /**
61: * Tests limiting of the number of loop iterations.
62: */
63: public void testMaxNbrLoopsConstraint() throws Exception {
64: StringWriter writer = new StringWriter();
65: String template = "#foreach ($item in [1..10])$item #end";
66: Velocity.evaluate(context, writer, "test", template);
67: assertEquals("Max number loops not enforced", "1 2 3 ", writer
68: .toString());
69: }
70:
71: /**
72: * Tests proper method execution during a Foreach loop with items
73: * of varying classes.
74: */
75: public void testMethodCall() throws Exception {
76: List col = new ArrayList();
77: col.add(new Integer(100));
78: col.add("STRVALUE");
79: context.put("helper", new ForeachMethodCallHelper());
80: context.put("col", col);
81:
82: StringWriter writer = new StringWriter();
83: Velocity.evaluate(context, writer, "test",
84: "#foreach ( $item in $col )$helper.getFoo($item) "
85: + "#end");
86: assertEquals(
87: "Method calls while looping over varying classes failed",
88: "int 100 str STRVALUE ", writer.toString());
89: }
90: }
|