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  
24  import junit.framework.Test;
25  import junit.framework.TestSuite;
26  
27  import org.apache.velocity.VelocityContext;
28  import org.apache.velocity.app.VelocityEngine;
29  import org.apache.velocity.context.Context;
30  import org.apache.velocity.exception.MethodInvocationException;
31  import org.apache.velocity.runtime.RuntimeConstants;
32  import org.apache.velocity.test.misc.ExceptionGeneratingDirective;
33  import org.apache.velocity.test.misc.ExceptionGeneratingEventHandler;
34  import org.apache.velocity.test.misc.ExceptionGeneratingResourceLoader;
35  import org.apache.velocity.test.provider.TestProvider;
36  
37  /**
38   * Test case for miscellaneous Exception related issues.
39   *
40   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
41   * @version $Id: ExceptionTestCase.java 463298 2006-10-12 16:10:32Z henning $
42   */
43  public class ExceptionTestCase extends BaseTestCase implements TemplateTestBase
44  {
45      VelocityEngine ve;
46  
47      /**
48       * Default constructor.
49       */
50      public ExceptionTestCase(String name)
51      {
52          super(name);
53      }
54  
55      public static Test suite ()
56      {
57          return new TestSuite(ExceptionTestCase.class);
58      }
59  
60  
61      public void testReferenceInsertionEventHandlerException()
62      throws Exception
63      {
64          ve = new VelocityEngine();
65          ve.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION,ExceptionGeneratingEventHandler.class.getName());
66          ve.init();
67          assertException(ve);
68      }
69  
70      /**
71       * Note - this is the one case where RuntimeExceptions *are not* passed through
72       * verbatim.
73       * @throws Exception
74       */
75      public void testMethodExceptionEventHandlerException()
76      throws Exception
77      {
78          ve = new VelocityEngine();
79          ve.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION,ExceptionGeneratingEventHandler.class.getName());
80          ve.init();
81          Context context = new VelocityContext();
82          context.put ("test",new TestProvider());
83          assertMethodInvocationException(ve,context,"$test.getThrow()");
84          assertMethodInvocationException(ve,context,"$test.throw");
85      }
86  
87      public void testNullSetEventHandlerException()
88      throws Exception
89      {
90          ve = new VelocityEngine();
91          ve.setProperty(RuntimeConstants.EVENTHANDLER_NULLSET,ExceptionGeneratingEventHandler.class.getName());
92          ve.init();
93          assertException(ve,"#set($test = $abc)");
94      }
95  
96      public void testIncludeEventHandlerException()
97      throws Exception
98      {
99          ve = new VelocityEngine();
100         ve.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE,ExceptionGeneratingEventHandler.class.getName());
101         ve.init();
102         assertException(ve,"#include('dummy')");
103     }
104 
105     public void testResourceLoaderException()
106     throws Exception
107     {
108         ve = new VelocityEngine();
109         ve.setProperty(RuntimeConstants.RESOURCE_LOADER,"except");
110         ve.setProperty("except.resource.loader.class",ExceptionGeneratingResourceLoader.class.getName());
111         try
112         {
113             ve.init();  // tries to get the macro file
114             ve.getTemplate("test.txt");
115             fail("Should have thrown RuntimeException");
116         }
117         catch (RuntimeException E)
118         {
119             // do nothing
120         }
121     }
122 
123 
124     public void testDirectiveException()
125     throws Exception
126     {
127         ve = new VelocityEngine();
128         ve.setProperty("userdirective",ExceptionGeneratingDirective.class.getName());
129         ve.init();
130         assertException(ve,"#Exception() test #end");
131     }
132 
133 
134 
135     public void assertException(VelocityEngine ve)
136     throws Exception
137     {
138         Context context = new VelocityContext();
139         context.put ("test","test");
140         assertException(ve,context,"this is a $test");
141     }
142 
143     public void assertException(VelocityEngine ve, String input)
144     throws Exception
145     {
146         Context context = new VelocityContext();
147         context.put ("test","test");
148         assertException(ve,context,input);
149     }
150 
151     public void assertException(VelocityEngine ve, Context context, String input)
152     throws Exception
153     {
154         try
155         {
156             StringWriter writer = new StringWriter();
157             ve.evaluate(context,writer,"test",input);
158             fail("Expected RuntimeException");
159         }
160         catch (RuntimeException E)
161         {
162             // do nothing
163         }
164     }
165     public void assertMethodInvocationException(VelocityEngine ve, Context context, String input)
166     throws Exception
167     {
168         try
169         {
170             StringWriter writer = new StringWriter();
171             ve.evaluate(context,writer,"test",input);
172             fail("Expected MethodInvocationException");
173         }
174         catch (MethodInvocationException E)
175         {
176             // do nothing
177         }
178     }
179 
180 
181 }