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.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   * This class tests the Foreach loop.
37   *
38   * @author Daniel Rall
39   * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
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          // Limit the loop to three iterations.
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       * Tests limiting of the number of loop iterations.
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       * Tests proper method execution during a Foreach loop over a Collection
80       * with items of varying classes.
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      * Tests that #foreach will be able to retrieve an iterator from
101      * an arbitrary object that happens to have an iterator() method.
102      * (With the side effect of supporting the new Java 5 Iterable interface)
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 }