1   package org.apache.velocity.test;
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 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  {
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          // Limit the loop to three iterations.
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       * Tests limiting of the number of loop iterations.
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       * Tests proper method execution during a Foreach loop with items
79       * of varying classes.
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  }