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 junit.framework.Test;
24  import junit.framework.TestSuite;
25  import org.apache.velocity.VelocityContext;
26  import org.apache.velocity.app.VelocityEngine;
27  import org.apache.velocity.context.Context;
28  import org.apache.velocity.exception.MethodInvocationException;
29  import org.apache.velocity.test.provider.TestProvider;
30  import org.apache.velocity.util.ExceptionUtils;
31  
32  
33  
34  /**
35   * Test thrown exceptions include a proper cause (under JDK 1.4+).
36   *
37   * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
38   * @version $Id: WrappedExceptionTestCase.java 463298 2006-10-12 16:10:32Z henning $
39   */
40  public class WrappedExceptionTestCase extends BaseTestCase implements TemplateTestBase
41  {
42      VelocityEngine ve;
43  
44      /**
45       * Default constructor.
46       */
47      public WrappedExceptionTestCase(String name)
48      {
49          super(name);
50      }
51  
52      public static Test suite ()
53      {
54          return new TestSuite(WrappedExceptionTestCase.class);
55      }
56  
57      public void setUp() throws Exception
58      {
59          ve = new VelocityEngine();
60          ve.init();
61      }
62  
63  
64      public void testMethodException() throws Exception
65      {
66  
67          // accumulate a list of invalid references
68          Context context = new VelocityContext();
69          StringWriter writer = new StringWriter();
70          context.put("test",new TestProvider());
71  
72          try
73          {
74              ve.evaluate(context,writer,"test","$test.getThrow()");
75              fail ("expected an exception");
76          }
77          catch (MethodInvocationException E)
78          {
79              assertEquals(Exception.class,E.getCause().getClass());
80              assertEquals("From getThrow()",E.getCause().getMessage());
81          }
82  
83      }
84      public void testExceptionUtils()
85      {
86          Error e = new Error("Inside");
87          RuntimeException re = ExceptionUtils.createRuntimeException("Outside", e);
88          assertEquals("cause was set", e,re.getCause());
89      }
90  
91  }