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.Template;
028: import org.apache.velocity.VelocityContext;
029: import org.apache.velocity.app.Velocity;
030: import org.apache.velocity.app.VelocityEngine;
031: import org.apache.velocity.context.Context;
032: import org.apache.velocity.exception.ParseErrorException;
033: import org.apache.velocity.exception.ResourceNotFoundException;
034:
035: /**
036: * Test that #parse and #include pass errors to calling code.
037: * Specifically checking against VELOCITY-95 and VELOCITY-96.
038: *
039: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
040: * @version $Id: IncludeErrorTestCase.java 463298 2006-10-12 16:10:32Z henning $
041: */
042: public class IncludeErrorTestCase extends BaseTestCase implements
043: TemplateTestBase {
044: VelocityEngine ve;
045:
046: /**
047: * Default constructor.
048: */
049: public IncludeErrorTestCase(String name) {
050: super (name);
051: }
052:
053: public static Test suite() {
054: return new TestSuite(IncludeErrorTestCase.class);
055: }
056:
057: public void setUp() throws Exception {
058: ve = new VelocityEngine();
059: ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
060: "test/includeerror");
061:
062: ve.init();
063: }
064:
065: public void testMissingParseError() throws Exception {
066: checkException("missingparse.vm",
067: ResourceNotFoundException.class);
068: }
069:
070: public void testMissingIncludeError() throws Exception {
071: checkException("missinginclude.vm",
072: ResourceNotFoundException.class);
073: }
074:
075: public void testParseError() throws Exception {
076: checkException("parsemain.vm", ParseErrorException.class);
077: }
078:
079: public void testParseError2() throws Exception {
080: checkException("parsemain2.vm", ParseErrorException.class);
081: }
082:
083: /**
084: * Check that an exception is thrown for the given template
085: * @param templateName
086: * @param exceptionClass
087: * @throws Exception
088: */
089: private void checkException(String templateName,
090: Class exceptionClass) throws Exception {
091: Context context = new VelocityContext();
092: StringWriter writer = new StringWriter();
093: Template template = ve.getTemplate(templateName, "UTF-8");
094:
095: try {
096: template.merge(context, writer);
097: writer.flush();
098: fail("File should have thrown an exception");
099: } catch (Exception E) {
100: assertTrue(exceptionClass.isAssignableFrom(E.getClass()));
101: } finally {
102: writer.close();
103: }
104:
105: }
106:
107: }
|