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: import java.io.Writer;
024:
025: import junit.framework.Test;
026: import junit.framework.TestSuite;
027:
028: import org.apache.velocity.Template;
029: import org.apache.velocity.VelocityContext;
030: import org.apache.velocity.app.VelocityEngine;
031: import org.apache.velocity.exception.ParseErrorException;
032:
033: /**
034: * Test parser exception is generated with appropriate info.
035: *
036: * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
037: * @version $Id: ParseExceptionTestCase.java 471372 2006-11-05 06:02:20Z wglass $
038: */
039: public class ParseExceptionTestCase extends BaseTestCase {
040: /**
041: * Path for templates. This property will override the
042: * value in the default velocity properties file.
043: */
044: private final static String FILE_RESOURCE_LOADER_PATH = "test/parseexception";
045:
046: /**
047: * Default constructor.
048: * @param name name of test
049: */
050: public ParseExceptionTestCase(String name) {
051: super (name);
052: }
053:
054: public void setUp() throws Exception {
055:
056: }
057:
058: public static Test suite() {
059: return new TestSuite(ParseExceptionTestCase.class);
060: }
061:
062: /**
063: * Tests that parseException has useful info when called by template.marge()
064: * @throws Exception
065: */
066: public void testParseExceptionFromTemplate() throws Exception {
067:
068: VelocityEngine ve = new VelocityEngine();
069:
070: ve.setProperty("file.resource.loader.cache", "true");
071: ve.setProperty("file.resource.loader.path",
072: FILE_RESOURCE_LOADER_PATH);
073: ve.init();
074:
075: Writer writer = new StringWriter();
076:
077: VelocityContext context = new VelocityContext();
078:
079: try {
080: Template template = ve.getTemplate("badtemplate.vm");
081: template.merge(context, writer);
082: fail("Should have thown a ParseErrorException");
083: } catch (ParseErrorException e) {
084: assertEquals("badtemplate.vm", e.getTemplateName());
085: assertEquals(5, e.getLineNumber());
086: assertEquals(9, e.getColumnNumber());
087: } finally {
088: if (writer != null) {
089: writer.close();
090: }
091: }
092: }
093:
094: /**
095: * Tests that parseException has useful info when thrown in VelocityEngine.evaluate()
096: * @throws Exception
097: */
098: public void testParseExceptionFromEval() throws Exception {
099:
100: VelocityEngine ve = new VelocityEngine();
101: ve.init();
102:
103: VelocityContext context = new VelocityContext();
104:
105: Writer writer = new StringWriter();
106:
107: try {
108: ve.evaluate(context, writer, "test", " #set($abc) ");
109: fail("Should have thown a ParseErrorException");
110: } catch (ParseErrorException e) {
111: assertEquals("test", e.getTemplateName());
112: assertEquals(1, e.getLineNumber());
113: assertEquals(13, e.getColumnNumber());
114: } finally {
115: if (writer != null) {
116: writer.close();
117: }
118: }
119: }
120:
121: /**
122: * Tests that parseException has useful info when thrown in VelocityEngine.evaluate()
123: * and the problem comes from a macro definition
124: * @throws Exception
125: */
126: public void testParseExceptionFromMacroDef() throws Exception {
127: VelocityEngine ve = new VelocityEngine();
128: ve.init();
129:
130: VelocityContext context = new VelocityContext();
131:
132: Writer writer = new StringWriter();
133:
134: try {
135: ve.evaluate(context, writer, "testMacro",
136: "#macro($blarg) foo #end");
137: fail("Should have thown a ParseErrorException");
138: } catch (ParseErrorException e) {
139: assertEquals("testMacro", e.getTemplateName());
140: assertEquals(1, e.getLineNumber());
141: assertEquals(7, e.getColumnNumber());
142: } finally {
143: if (writer != null) {
144: writer.close();
145: }
146: }
147: }
148:
149: /**
150: * Tests that parseException has useful info when thrown in VelocityEngine.evaluate()
151: * and the problem comes from a macro invocation
152: * @throws Exception
153: */
154: public void testParseExceptionFromMacroInvoke() throws Exception {
155: VelocityEngine ve = new VelocityEngine();
156: ve.init();
157:
158: VelocityContext context = new VelocityContext();
159:
160: Writer writer = new StringWriter();
161:
162: try {
163: ve.evaluate(context, writer, "testMacroInvoke",
164: "#macro( foo $a) $a #end #foo(woogie)");
165: fail("Should have thown a ParseErrorException");
166: } catch (ParseErrorException e) {
167: assertEquals("testMacroInvoke", e.getTemplateName());
168: assertEquals(1, e.getLineNumber());
169: assertEquals(31, e.getColumnNumber());
170: } finally {
171: if (writer != null) {
172: writer.close();
173: }
174: }
175: }
176:
177: /**
178: * Tests that parseException has useful info with macro calls with
179: * invalid number of arguments
180: * @throws Exception
181: */
182: public void testParseExceptionMacroInvalidArgumentCount()
183: throws Exception {
184: VelocityEngine ve = new VelocityEngine();
185: ve.setProperty("velocimacro.arguments.strict", "true");
186: ve.init();
187:
188: VelocityContext context = new VelocityContext();
189:
190: Writer writer = new StringWriter();
191:
192: try {
193: ve.evaluate(context, writer, "testMacroInvoke",
194: "#macro(foo $a) $a #end #foo('test1' 'test2')");
195: fail("Should have thown a ParseErrorException");
196: } catch (ParseErrorException e) {
197: assertEquals("testMacroInvoke", e.getTemplateName());
198: assertEquals(1, e.getLineNumber());
199: assertEquals(24, e.getColumnNumber());
200: } finally {
201: if (writer != null) {
202: writer.close();
203: }
204: }
205: }
206:
207: /**
208: * Tests that parseException has useful info with macro calls with
209: * invalid number of arguments
210: * @throws Exception
211: */
212: public void testParseExceptionMacroInvalidArgumentCountNoException()
213: throws Exception {
214: VelocityEngine ve = new VelocityEngine();
215: ve.init();
216:
217: VelocityContext context = new VelocityContext();
218:
219: Writer writer = new StringWriter();
220:
221: // will not throw an exception
222: try {
223: ve.evaluate(context, writer, "testMacroInvoke",
224: "#macro(foo $a) $a #end #foo('test1' 'test2')");
225: } finally {
226: if (writer != null) {
227: writer.close();
228: }
229: }
230: }
231:
232: }
|