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.test.misc.UberspectTestException;
033: import org.apache.velocity.util.introspection.Info;
034:
035: /**
036: * Test that the Info class in the Introspector holds the correct information.
037: *
038: * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
039: * @author <a href="mailto:isidore@setgame.com">Llewellyn Falco</a>
040: * @version $Id: InfoTestCase.java 463298 2006-10-12 16:10:32Z henning $
041: */
042: public class InfoTestCase extends BaseTestCase implements
043: TemplateTestBase {
044: VelocityEngine ve;
045:
046: /**
047: * Default constructor.
048: */
049: public InfoTestCase(String name) {
050: super (name);
051: }
052:
053: public static Test suite() {
054: return new TestSuite(InfoTestCase.class);
055: }
056:
057: public void setUp() throws Exception {
058: ve = new VelocityEngine();
059: ve.setProperty("runtime.introspector.uberspect",
060: "org.apache.velocity.test.misc.UberspectTestImpl");
061:
062: ve.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, "test/info");
063:
064: ve.init();
065: }
066:
067: public void testInfoProperty() throws Exception {
068: // check property
069: checkInfo("info1.vm", 1, 7);
070: }
071:
072: public void testInfoMethod() throws Exception {
073: // check method
074: checkInfo("info2.vm", 1, 7);
075: }
076:
077: public void checkInfo(String templateName, int expectedLine,
078: int expectedCol) throws Exception {
079: Context context = new VelocityContext();
080: StringWriter writer = new StringWriter();
081: Template template = ve.getTemplate(templateName, "UTF-8");
082: Info info = null;
083:
084: context.put("main", this );
085:
086: try {
087: template.merge(context, writer);
088: writer.flush();
089: fail("Uberspect should have thrown an exception");
090: } catch (UberspectTestException E) {
091: info = E.getInfo();
092: } finally {
093: writer.close();
094: }
095: assertInfoEqual(info, templateName, expectedLine, expectedCol);
096:
097: }
098:
099: private void assertInfoEqual(Info i, String name, int line,
100: int column) {
101: assertEquals("Template Name", name, i.getTemplateName());
102: assertEquals("Template Line", line, i.getLine());
103: assertEquals("Template Column", column, i.getColumn());
104: }
105:
106: }
|