001: package org.apache.velocity.test;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.StringWriter;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026:
027: import org.apache.velocity.VelocityContext;
028: import org.apache.velocity.app.VelocityEngine;
029: import org.apache.velocity.context.Context;
030: import org.apache.velocity.exception.MethodInvocationException;
031: import org.apache.velocity.runtime.RuntimeConstants;
032: import org.apache.velocity.test.misc.ExceptionGeneratingDirective;
033: import org.apache.velocity.test.misc.ExceptionGeneratingEventHandler;
034: import org.apache.velocity.test.misc.ExceptionGeneratingResourceLoader;
035: import org.apache.velocity.test.provider.TestProvider;
036:
037: /**
038: * Test case for miscellaneous Exception related issues.
039: *
040: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
041: * @version $Id: ExceptionTestCase.java 463298 2006-10-12 16:10:32Z henning $
042: */
043: public class ExceptionTestCase extends BaseTestCase implements
044: TemplateTestBase {
045: VelocityEngine ve;
046:
047: /**
048: * Default constructor.
049: */
050: public ExceptionTestCase(String name) {
051: super (name);
052: }
053:
054: public static Test suite() {
055: return new TestSuite(ExceptionTestCase.class);
056: }
057:
058: public void testReferenceInsertionEventHandlerException()
059: throws Exception {
060: ve = new VelocityEngine();
061: ve.setProperty(
062: RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION,
063: ExceptionGeneratingEventHandler.class.getName());
064: ve.init();
065: assertException(ve);
066: }
067:
068: /**
069: * Note - this is the one case where RuntimeExceptions *are not* passed through
070: * verbatim.
071: * @throws Exception
072: */
073: public void testMethodExceptionEventHandlerException()
074: throws Exception {
075: ve = new VelocityEngine();
076: ve.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION,
077: ExceptionGeneratingEventHandler.class.getName());
078: ve.init();
079: Context context = new VelocityContext();
080: context.put("test", new TestProvider());
081: assertMethodInvocationException(ve, context, "$test.getThrow()");
082: assertMethodInvocationException(ve, context, "$test.throw");
083: }
084:
085: public void testNullSetEventHandlerException() throws Exception {
086: ve = new VelocityEngine();
087: ve.setProperty(RuntimeConstants.EVENTHANDLER_NULLSET,
088: ExceptionGeneratingEventHandler.class.getName());
089: ve.init();
090: assertException(ve, "#set($test = $abc)");
091: }
092:
093: public void testIncludeEventHandlerException() throws Exception {
094: ve = new VelocityEngine();
095: ve.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE,
096: ExceptionGeneratingEventHandler.class.getName());
097: ve.init();
098: assertException(ve, "#include('dummy')");
099: }
100:
101: public void testResourceLoaderException() throws Exception {
102: ve = new VelocityEngine();
103: ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "except");
104: ve.setProperty("except.resource.loader.class",
105: ExceptionGeneratingResourceLoader.class.getName());
106: try {
107: ve.init(); // tries to get the macro file
108: ve.getTemplate("test.txt");
109: fail("Should have thrown RuntimeException");
110: } catch (RuntimeException E) {
111: // do nothing
112: }
113: }
114:
115: public void testDirectiveException() throws Exception {
116: ve = new VelocityEngine();
117: ve.setProperty("userdirective",
118: ExceptionGeneratingDirective.class.getName());
119: ve.init();
120: assertException(ve, "#Exception() test #end");
121: }
122:
123: public void assertException(VelocityEngine ve) throws Exception {
124: Context context = new VelocityContext();
125: context.put("test", "test");
126: assertException(ve, context, "this is a $test");
127: }
128:
129: public void assertException(VelocityEngine ve, String input)
130: throws Exception {
131: Context context = new VelocityContext();
132: context.put("test", "test");
133: assertException(ve, context, input);
134: }
135:
136: public void assertException(VelocityEngine ve, Context context,
137: String input) throws Exception {
138: try {
139: StringWriter writer = new StringWriter();
140: ve.evaluate(context, writer, "test", input);
141: fail("Expected RuntimeException");
142: } catch (RuntimeException E) {
143: // do nothing
144: }
145: }
146:
147: public void assertMethodInvocationException(VelocityEngine ve,
148: Context context, String input) throws Exception {
149: try {
150: StringWriter writer = new StringWriter();
151: ve.evaluate(context, writer, "test", input);
152: fail("Expected MethodInvocationException");
153: } catch (MethodInvocationException E) {
154: // do nothing
155: }
156: }
157:
158: }
|