01: package org.apache.velocity.test;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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: * Test thrown exceptions include a proper cause (under JDK 1.4+).
34: *
35: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
36: * @version $Id: WrappedExceptionTestCase.java 463298 2006-10-12 16:10:32Z henning $
37: */
38: public class WrappedExceptionTestCase extends BaseTestCase implements
39: TemplateTestBase {
40: VelocityEngine ve;
41:
42: /**
43: * Default constructor.
44: */
45: public WrappedExceptionTestCase(String name) {
46: super (name);
47: }
48:
49: public static Test suite() {
50: return new TestSuite(WrappedExceptionTestCase.class);
51: }
52:
53: public void setUp() throws Exception {
54: ve = new VelocityEngine();
55: ve.init();
56: }
57:
58: public void testMethodException() throws Exception {
59:
60: // accumulate a list of invalid references
61: Context context = new VelocityContext();
62: StringWriter writer = new StringWriter();
63: context.put("test", new TestProvider());
64:
65: try {
66: ve.evaluate(context, writer, "test", "$test.getThrow()");
67: fail("expected an exception");
68: } catch (MethodInvocationException E) {
69: assertEquals(Exception.class, E.getCause().getClass());
70: assertEquals("From getThrow()", E.getCause().getMessage());
71: }
72:
73: }
74:
75: public void testExceptionUtils() {
76: Error e = new Error("Inside");
77: RuntimeException re = ExceptionUtils.createRuntimeException(
78: "Outside", e);
79: assertEquals("cause was set", e, re.getCause());
80: }
81:
82: }
|