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.util.ArrayList;
23  import java.util.Iterator;
24  
25  import org.apache.velocity.exception.VelocityException;
26  import org.apache.velocity.runtime.RuntimeConstants;
27  
28  /**
29   * This class tests support for strict foreach mode.
30   */
31  public class StrictForeachTestCase extends BaseTestCase
32  {
33      public StrictForeachTestCase(String name)
34      {
35         super(name);
36      }
37  
38      public void setUp() throws Exception
39      {
40          super.setUp();
41          engine.setProperty(RuntimeConstants.SKIP_INVALID_ITERATOR, Boolean.FALSE);
42          context.put("good", new GoodIterable());
43          context.put("bad", new BadIterable());
44          context.put("ugly", new UglyIterable());
45      }
46  
47      public void testGood()
48      {
49          try
50          {
51              evaluate("#foreach( $i in $good )$i#end");
52          }
53          catch (VelocityException ve)
54          {
55              fail("Doing #foreach on $good should not have exploded!");
56          }
57      }
58  
59      public void testBad()
60      {
61          try
62          {
63              evaluate("#foreach( $i in $bad )$i#end");
64              fail("Doing #foreach on $bad should have exploded!");
65          }
66          catch (VelocityException ve)
67          {
68              // success!
69          }
70      }
71  
72      public void testUgly()
73      {
74          try
75          {
76              evaluate("#foreach( $i in $ugly )$i#end");
77              fail("Doing #foreach on $ugly should have exploded!");
78          }
79          catch (VelocityException ve)
80          {
81              // success!
82          }
83      }
84  
85  
86      public static class GoodIterable
87      {
88          public Iterator iterator()
89          {
90              return new ArrayList().iterator();
91          }
92      }
93  
94      public static class BadIterable
95      {
96          public Object iterator()
97          {
98              return new Object();
99          }
100     }
101 
102     public static class UglyIterable
103     {
104         public Iterator iterator()
105         {
106             return null;
107         }
108     }
109 }