KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > ParserTestCase


1 package org.apache.velocity.test;
2 /*
3  * Copyright 2002,2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 import junit.framework.Test;
19 import junit.framework.TestCase;
20 import junit.framework.TestSuite;
21 import org.apache.velocity.app.VelocityEngine;
22 import org.apache.velocity.VelocityContext;
23 import org.apache.velocity.exception.ParseErrorException;
24
25 import java.io.StringWriter JavaDoc;
26
27 /**
28  * More specific parser tests where just templating
29  * isn't enough.
30  *
31  * @author <a HREF="mailto:geirm@apache.org">Geir Magnusson Jr.</a>
32  * @version $Id: ParserTestCase.java,v 1.1.4.1 2004/03/03 23:23:04 geirm Exp $
33  */

34 public class ParserTestCase extends TestCase
35 {
36     public ParserTestCase(String JavaDoc testName)
37     {
38         super(testName);
39     }
40
41     public static Test suite()
42     {
43        return new TestSuite(ParserTestCase.class);
44     }
45
46     /**
47      * Test to make sure that using '=' in #if() throws a PEE
48      */

49     public void testEquals()
50         throws Exception JavaDoc
51     {
52         VelocityEngine ve = new VelocityEngine();
53
54         ve.init();
55
56         /*
57          * this should parse fine -> uses ==
58          */

59
60         String JavaDoc template = "#if($a == $b) foo #end";
61
62         ve.evaluate(new VelocityContext(), new StringWriter JavaDoc(), "foo", template);
63
64         /*
65          * this should throw an exception
66          */

67
68         template = "#if($a = $b) foo #end";
69
70         try
71         {
72             ve.evaluate(new VelocityContext(), new StringWriter JavaDoc(), "foo", template);
73             assertTrue(false);
74         }
75         catch(ParseErrorException pe)
76         {
77         }
78     }
79
80     /**
81      * Test to see if we force the first arg to #macro() to be a word
82      */

83     public void testMacro()
84         throws Exception JavaDoc
85     {
86         VelocityEngine ve = new VelocityEngine();
87
88         ve.init();
89
90         /*
91          * this should work
92          */

93
94         String JavaDoc template = "#macro(foo) foo #end";
95
96         ve.evaluate(new VelocityContext(), new StringWriter JavaDoc(), "foo", template);
97
98          /*
99          * this should throw an exception
100          */

101
102         template = "#macro($x) foo #end";
103
104         try
105         {
106             ve.evaluate(new VelocityContext(), new StringWriter JavaDoc(), "foo", template);
107             assertTrue(false);
108         }
109         catch(ParseErrorException pe)
110         {
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()
119         throws Exception JavaDoc
120     {
121         VelocityEngine ve = new VelocityEngine();
122
123         ve.init();
124
125         /*
126          * this should work
127          */

128
129         String JavaDoc template = "#macro(foo) foo #end";
130
131         ve.evaluate(new VelocityContext(), new StringWriter JavaDoc(), "foo", template);
132
133          /*
134          * this should work - spaces intentional
135          */

136
137         template = "#foreach( $i in $woogie ) end #end";
138
139         ve.evaluate(new VelocityContext(), new StringWriter JavaDoc(), "foo", template);
140
141         /*
142         * this should bomb
143         */

144
145        template = "#macro( foo $a) $a #end #foo(woogie)";
146
147         try
148         {
149             ve.evaluate(new VelocityContext(), new StringWriter JavaDoc(), "foo", template);
150             assertTrue(false);
151         }
152         catch(ParseErrorException pe)
153         {
154             System.out.println("Caught pee!");
155         }
156     }
157
158 }
159
Popular Tags