KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > junit > script > TestScript


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

15 package org.apache.tapestry.junit.script;
16
17 import java.io.IOException JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.hivemind.ClassResolver;
23 import org.apache.hivemind.Resource;
24 import org.apache.hivemind.impl.DefaultClassResolver;
25 import org.apache.hivemind.util.ClasspathResource;
26 import org.apache.tapestry.IRequestCycle;
27 import org.apache.tapestry.IScript;
28 import org.apache.tapestry.engine.RequestCycle;
29 import org.apache.tapestry.junit.TapestryTestCase;
30 import org.apache.tapestry.script.ScriptParser;
31 import org.apache.tapestry.script.ScriptSession;
32 import org.apache.tapestry.script.ScriptSessionImpl;
33 import org.apache.tapestry.services.ExpressionCache;
34 import org.apache.tapestry.services.ExpressionEvaluator;
35 import org.apache.tapestry.services.impl.ExpressionCacheImpl;
36 import org.apache.tapestry.services.impl.ExpressionEvaluatorImpl;
37 import org.apache.tapestry.util.xml.DocumentParseException;
38
39 /**
40  * A collection of tests for Tapestry scripting.
41  *
42  * @author Howard Lewis Ship
43  * @since 2.2
44  */

45
46 public class TestScript extends TapestryTestCase
47 {
48     private MockScriptProcessor _processor = new MockScriptProcessor();
49
50     protected static ExpressionEvaluator createExpressionEvaluator()
51     {
52         ExpressionCache cache = new ExpressionCacheImpl();
53         ExpressionEvaluatorImpl result = new ExpressionEvaluatorImpl();
54         result.setExpressionCache(cache);
55
56         return result;
57     }
58
59     private IScript read(String JavaDoc file) throws IOException JavaDoc, DocumentParseException
60     {
61         ClassResolver resolver = new DefaultClassResolver();
62         ScriptParser parser = new ScriptParser(resolver, createExpressionEvaluator(), null);
63
64         String JavaDoc classAsPath = "/" + getClass().getName().replace('.', '/');
65
66         Resource classLocation = new ClasspathResource(resolver, classAsPath);
67         Resource scriptLocation = classLocation.getRelativeResource(file);
68
69         return parser.parse(scriptLocation);
70     }
71
72     private IScript execute(String JavaDoc file, Map JavaDoc symbols) throws DocumentParseException, IOException JavaDoc
73     {
74         IScript script = read(file);
75
76         IRequestCycle cycle = (IRequestCycle) newMock(IRequestCycle.class);
77
78         replayControls();
79
80         script.execute(cycle, _processor, symbols);
81
82         verifyControls();
83
84         return script;
85     }
86
87     private void assertSymbol(Map JavaDoc symbols, String JavaDoc key, Object JavaDoc expected)
88     {
89         Object JavaDoc actual = symbols.get(key);
90
91         assertEquals(key, expected, actual);
92     }
93
94     /**
95      * Simple test where the body and initialization are static.
96      */

97
98     public void testSimple() throws Exception JavaDoc
99     {
100         execute("simple.script", null);
101
102         assertEquals("body", "\nBODY\n", _processor.getBody());
103         assertEquals("initialization", "\nINITIALIZATION\n", _processor.getInitialization());
104         assertNull(_processor.getExternalScripts());
105     }
106
107     /**
108      * Test the <unique> element, new in the 1.3 DTD
109      *
110      * @since 3.0
111      */

112
113     public void testUnique() throws Exception JavaDoc
114     {
115         IScript script = read("unique.script");
116
117         IRequestCycle cycle = new RequestCycle();
118
119         script.execute(cycle, _processor, null);
120         script.execute(cycle, _processor, null);
121
122         assertEquals("Block1\nBlock2\nNotUnique\n\n\n\nNotUnique", _processor.getBody().trim());
123     }
124
125     /**
126      * Test omitting body and initialization, ensure they return null.
127      */

128
129     public void testEmpty() throws Exception JavaDoc
130     {
131         execute("empty.script", null);
132
133         assertNull("body", _processor.getBody());
134         assertNull("initialization", _processor.getInitialization());
135     }
136
137     /**
138      * Test the ability of the let element to create an output symbol. Also, test the insert
139      * element.
140      */

141
142     public void testLet() throws Exception JavaDoc
143     {
144         String JavaDoc inputSymbol = Long.toHexString(System.currentTimeMillis());
145         Map JavaDoc symbols = new HashMap JavaDoc();
146         symbols.put("inputSymbol", inputSymbol);
147
148         execute("let.script", symbols);
149
150         // Unlike body, the let element trims whitespace.
151

152         String JavaDoc outputSymbol = "output: " + inputSymbol;
153
154         assertEquals("Output symbol", outputSymbol, symbols.get("outputSymbol"));
155     }
156
157     /**
158      * Test the unique attribute on the <let> element. New in the 1.3 DTD
159      *
160      * @since 3.0
161      */

162     public void testUniqueLet() throws Exception JavaDoc
163     {
164         Map JavaDoc symbols = new HashMap JavaDoc();
165
166         execute("unique-let.script", symbols);
167
168         assertSymbol(symbols, "alpha", "Alpha");
169         assertSymbol(symbols, "beta", "Alpha$0");
170         assertSymbol(symbols, "gamma", "Alpha$1");
171     }
172
173     public void testIncludeScript() throws Exception JavaDoc
174     {
175         IScript script = execute("include-script.script", null);
176
177         Resource scriptLocation = script.getScriptResource();
178
179         Resource[] expected = new Resource[]
180         { scriptLocation.getRelativeResource("first"),
181                 scriptLocation.getRelativeResource("second"),
182                 scriptLocation.getRelativeResource("third") };
183
184         assertEquals("included scripts", Arrays.asList(expected), Arrays.asList(_processor
185                 .getExternalScripts()));
186     }
187
188     public void testAntSyntax() throws Exception JavaDoc
189     {
190         Map JavaDoc form = new HashMap JavaDoc();
191
192         form.put("name", "gallahad");
193
194         Map JavaDoc component = new HashMap JavaDoc();
195         component.put("form", form);
196         component.put("name", "lancelot");
197
198         Map JavaDoc symbols = new HashMap JavaDoc();
199         symbols.put("component", component);
200
201         execute("ant-syntax.script", symbols);
202
203         assertSymbol(symbols, "functionName", "gallahad_lancelot");
204         assertSymbol(symbols, "incomplete1", "Incomplete: $");
205         assertSymbol(symbols, "incomplete2", "Incomplete: ${");
206         assertSymbol(symbols, "nopath", "This ${} ends up as literal.");
207         assertSymbol(symbols, "OGNL", "This is a brace: }.");
208     }
209
210     public void testSet() throws Exception JavaDoc
211     {
212         Map JavaDoc symbols = new HashMap JavaDoc();
213
214         execute("set.script", symbols);
215
216         assertSymbol(symbols, "element2", new Character JavaDoc('p'));
217     }
218
219     public void testInvalidKeyLet() throws Exception JavaDoc
220     {
221         try
222         {
223             execute("invalid-key-let.script", new HashMap JavaDoc());
224
225             unreachable();
226         }
227         catch (DocumentParseException ex)
228         {
229             checkException(ex, "key");
230         }
231     }
232
233     public void testInvalidKeySet() throws Exception JavaDoc
234     {
235         try
236         {
237             execute("invalid-key-set.script", new HashMap JavaDoc());
238
239             unreachable();
240         }
241         catch (DocumentParseException ex)
242         {
243             checkException(ex, "key");
244         }
245     }
246
247     public void testInputSymbolClass() throws Exception JavaDoc
248     {
249         try
250         {
251             Map JavaDoc symbols = new HashMap JavaDoc();
252             symbols.put("input", new Integer JavaDoc(20));
253
254             execute("input-symbol-class.script", symbols);
255
256             unreachable();
257         }
258         catch (Exception JavaDoc ex)
259         {
260             checkException(ex, "Integer");
261             checkException(ex, "Long");
262         }
263     }
264
265     public void testInputSymbol() throws Exception JavaDoc
266     {
267         Map JavaDoc symbols = new HashMap JavaDoc();
268         symbols.put("input", new Long JavaDoc(20));
269
270         execute("input-symbol.script", symbols);
271
272         assertSymbol(symbols, "success", "Success");
273     }
274
275     public void testInputSymbolRequired() throws Exception JavaDoc
276     {
277         try
278         {
279             execute("input-symbol-required.script", new HashMap JavaDoc());
280
281             unreachable();
282         }
283         catch (Exception JavaDoc ex)
284         {
285             checkException(ex, "required");
286         }
287     }
288
289     public void testInputSymbolInvalidKey() throws Exception JavaDoc
290     {
291         try
292         {
293             execute("input-symbol-invalid-key.script", new HashMap JavaDoc());
294
295             unreachable();
296         }
297         catch (DocumentParseException ex)
298         {
299             checkException(ex, "key");
300         }
301
302     }
303
304     /** @since 3.0 */
305
306     public void testNameAppend() throws Exception JavaDoc
307     {
308         Map JavaDoc symbols = new HashMap JavaDoc();
309
310         symbols.put("name", "fred");
311         execute("name-append.script", symbols);
312
313         assertSymbol(symbols, "output", "fred$suffix");
314     }
315
316     /**
317      * A bunch of quickies to push up the code coverage numbers.
318      */

319     public void testCheats() throws Exception JavaDoc
320     {
321         IScript script = execute("simple.script", null);
322
323         ScriptSession session = new ScriptSessionImpl(script.getScriptResource(), null, null,
324                 createExpressionEvaluator(), null, null);
325         assertEquals("ScriptSession[" + script.getScriptResource() + "]", session.toString());
326     }
327 }
Popular Tags