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.util.Map;
024: import java.util.HashMap;
025:
026: import junit.framework.Test;
027: import junit.framework.TestCase;
028: import junit.framework.TestSuite;
029:
030: import org.apache.velocity.VelocityContext;
031: import org.apache.velocity.app.VelocityEngine;
032: import org.apache.velocity.exception.ParseErrorException;
033:
034: /**
035: * More specific parser tests where just templating
036: * isn't enough.
037: *
038: * @author <a href="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
039: * @version $Id: ParserTestCase.java 463298 2006-10-12 16:10:32Z henning $
040: */
041: public class ParserTestCase extends TestCase {
042: public ParserTestCase(String testName) {
043: super (testName);
044: }
045:
046: public static Test suite() {
047: return new TestSuite(ParserTestCase.class);
048: }
049:
050: /**
051: * Test to make sure that using '=' in #if() throws a PEE
052: */
053: public void testEquals() throws Exception {
054: VelocityEngine ve = new VelocityEngine();
055:
056: ve.init();
057:
058: /*
059: * this should parse fine -> uses ==
060: */
061:
062: String template = "#if($a == $b) foo #end";
063:
064: ve.evaluate(new VelocityContext(), new StringWriter(), "foo",
065: template);
066:
067: /*
068: * this should throw an exception
069: */
070:
071: template = "#if($a = $b) foo #end";
072:
073: try {
074: ve.evaluate(new VelocityContext(), new StringWriter(),
075: "foo", template);
076: fail("Could evaluate template with errors!");
077: } catch (ParseErrorException pe) {
078: // Do nothing
079: }
080: }
081:
082: /**
083: * Test to see if we force the first arg to #macro() to be a word
084: */
085: public void testMacro() throws Exception {
086: VelocityEngine ve = new VelocityEngine();
087:
088: ve.init();
089:
090: /*
091: * this should work
092: */
093:
094: String template = "#macro(foo) foo #end";
095:
096: ve.evaluate(new VelocityContext(), new StringWriter(), "foo",
097: template);
098:
099: /*
100: * this should throw an exception
101: */
102:
103: template = "#macro($x) foo #end";
104:
105: try {
106: ve.evaluate(new VelocityContext(), new StringWriter(),
107: "foo", template);
108: fail("Could evaluate macro with errors!");
109: } catch (ParseErrorException pe) {
110: // Do nothing
111: }
112: }
113:
114: /**
115: * Test to see if don't tolerage passing word tokens in anything but the
116: * 0th arg to #macro() and the 1th arg to foreach()
117: */
118: public void testArgs() throws Exception {
119: VelocityEngine ve = new VelocityEngine();
120:
121: ve.init();
122:
123: /*
124: * this should work
125: */
126:
127: String template = "#macro(foo) foo #end";
128:
129: ve.evaluate(new VelocityContext(), new StringWriter(), "foo",
130: template);
131:
132: /*
133: * this should work - spaces intentional
134: */
135:
136: template = "#foreach( $i in $woogie ) end #end";
137:
138: ve.evaluate(new VelocityContext(), new StringWriter(), "foo",
139: template);
140:
141: /*
142: * this should bomb
143: */
144:
145: template = "#macro( foo $a) $a #end #foo(woogie)";
146:
147: try {
148: ve.evaluate(new VelocityContext(), new StringWriter(),
149: "foo", template);
150: fail("Evaluation of macro with errors succeeded!");
151: } catch (ParseErrorException pe) {
152: // Do nothing
153: }
154: }
155:
156: /**
157: * Test to see if we toString is called multiple times on references.
158: */
159: public void testASTReferenceToStringOnlyCalledOnce()
160: throws Exception {
161: VelocityEngine ve = new VelocityEngine();
162:
163: ve.init();
164:
165: String template = "$counter";
166:
167: ToStringCounter counter = new ToStringCounter();
168: Map m = new HashMap();
169: m.put("counter", counter);
170:
171: ve.evaluate(new VelocityContext(m), new StringWriter(), "foo",
172: template);
173:
174: assertEquals(1, counter.timesCalled);
175: }
176:
177: public static class ToStringCounter {
178: public int timesCalled = 0;
179:
180: public String toString() {
181: this .timesCalled++;
182: return "foo";
183: }
184: }
185:
186: }
|