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  import junit.framework.TestCase;
27  import org.apache.velocity.VelocityContext;
28  import org.apache.velocity.app.VelocityEngine;
29  import org.apache.velocity.runtime.RuntimeConstants;
30  import org.apache.velocity.test.misc.TestLogChute;
31  
32  /**
33   * Base for test cases that use evaluate, instead of going
34   * through the resource loaders.
35   */
36  public class BaseEvalTestCase extends TestCase
37  {
38      protected VelocityEngine engine;
39      protected VelocityContext context;
40      protected boolean DEBUG = false;
41      protected TestLogChute log;
42  
43      public BaseEvalTestCase(String name)
44      {
45          super(name);
46      }
47  
48      public void setUp() throws Exception
49      {
50          engine = new VelocityEngine();
51  
52          //by default, make the engine's log output go to the test-report
53          log = new TestLogChute(false, false);
54          log.setEnabledLevel(TestLogChute.INFO_ID);
55          log.setSystemErrLevel(TestLogChute.WARN_ID);
56          engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, log);
57  
58          context = new VelocityContext();
59          setContext(context);
60      }
61  
62      public void tearDown()
63      {
64          engine = null;
65          context = null;
66      }
67  
68      public void testBase()
69      {
70          assertEvalEquals("","");
71          assertEvalEquals("abc\n123","abc\n123");
72      }
73  
74      protected void setProperties(VelocityEngine engine)
75      {
76          // extension hook
77      }
78  
79      protected void setContext(VelocityContext context)
80      {
81          // extension hook
82      }
83  
84      protected void assertContextValue(String key, Object expected)
85      {
86          if (DEBUG)
87          {
88              engine.getLog().info("Expected value of '"+key+"': "+expected);
89          }
90          Object value = context.get(key);
91          if (DEBUG)
92          {
93              engine.getLog().info("Result: "+value);
94          }
95          assertEquals(expected, value);
96      }
97  
98      protected void assertEvalEquals(String expected, String template)
99      {
100         if (DEBUG)
101         {
102             engine.getLog().info("Expectation: "+expected);
103         }
104         assertEquals(expected, evaluate(template));
105     }
106 
107     protected Exception assertEvalException(String evil)
108     {
109         return assertEvalException(evil, null);
110     }
111 
112     protected Exception assertEvalException(String evil, Class exceptionType)
113     {
114         try
115         {
116             if (!DEBUG)
117             {
118                 log.off();
119             }
120             evaluate(evil);
121             fail("Template '"+evil+"' should have thrown an exception.");
122         }
123         catch (Exception e)
124         {
125             if (exceptionType != null && !exceptionType.isAssignableFrom(e.getClass()))
126             {
127                 fail("Was expecting template '"+evil+"' to throw "+exceptionType+" not "+e);
128             }
129             return e;
130         }
131         finally
132         {
133             if (!DEBUG)
134             {
135                 log.on();
136             }
137         }
138         return null;
139     }
140 
141     protected Exception assertEvalExceptionAt(String evil, String template,
142                                               int line, int col)
143     {
144         String loc = template+"[line "+line+", column "+col+"]";
145         if (DEBUG)
146         {
147             engine.getLog().info("Expectation: Exception at "+loc);
148         }
149         Exception e = assertEvalException(evil, null);
150         if (e.getMessage().indexOf(loc) < 1)
151         {
152             fail("Was expecting exception at "+loc+" instead of "+e.getMessage());
153         }
154         else if (DEBUG)
155         {
156             engine.getLog().info("Result: "+e.getMessage());
157         }
158         return e;
159     }
160 
161     protected Exception assertEvalExceptionAt(String evil, int line, int col)
162     {
163          return assertEvalExceptionAt(evil, "", line, col);
164     }
165 
166     protected String evaluate(String template)
167     {
168         StringWriter writer = new StringWriter();
169         try
170         {
171             if (DEBUG)
172             {
173                 engine.getLog().info("Template: "+template);
174             }
175 
176             // use template as its own name, since our templates are short
177             engine.evaluate(context, writer, template, template);
178 
179             String result = writer.toString();
180             if (DEBUG)
181             {
182                 engine.getLog().info("Result: "+result);
183             }
184             return result;
185         }
186         catch (RuntimeException re)
187         {
188             if (DEBUG)
189             {
190                 engine.getLog().info("RuntimeException!", re);
191             }
192             throw re;
193         }
194         catch (Exception e)
195         {
196             if (DEBUG)
197             {
198                 engine.getLog().info("Exception!", e);
199             }
200             throw new RuntimeException(e);
201         }
202     }
203 }