Source Code Cross Referenced for BuiltInEventHandlerTestCase.java in  » Template-Engine » Velocity » org » apache » velocity » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Template Engine » Velocity » org.apache.velocity.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.BufferedWriter;
023:        import java.io.FileOutputStream;
024:        import java.io.OutputStreamWriter;
025:        import java.io.StringWriter;
026:        import java.io.Writer;
027:        import java.util.ArrayList;
028:        import java.util.List;
029:
030:        import junit.framework.Test;
031:        import junit.framework.TestSuite;
032:
033:        import org.apache.velocity.Template;
034:        import org.apache.velocity.VelocityContext;
035:        import org.apache.velocity.app.VelocityEngine;
036:        import org.apache.velocity.app.event.EventCartridge;
037:        import org.apache.velocity.app.event.implement.EscapeHtmlReference;
038:        import org.apache.velocity.app.event.implement.EscapeJavaScriptReference;
039:        import org.apache.velocity.app.event.implement.EscapeReference;
040:        import org.apache.velocity.app.event.implement.EscapeSqlReference;
041:        import org.apache.velocity.app.event.implement.EscapeXmlReference;
042:        import org.apache.velocity.app.event.implement.InvalidReferenceInfo;
043:        import org.apache.velocity.app.event.implement.ReportInvalidReferences;
044:        import org.apache.velocity.context.Context;
045:        import org.apache.velocity.runtime.RuntimeConstants;
046:
047:        /**
048:         * Tests the operation of the built in event handlers.
049:         *
050:         * @author <a href="mailto:wglass@forio.com">Will Glass-Husain</a>
051:         * @version $Id: BuiltInEventHandlerTestCase.java 463298 2006-10-12 16:10:32Z henning $
052:         */
053:        public class BuiltInEventHandlerTestCase extends BaseTestCase {
054:
055:            /**
056:             * VTL file extension.
057:             */
058:            private static final String TMPL_FILE_EXT = "vm";
059:
060:            /**
061:             * Comparison file extension.
062:             */
063:            private static final String CMP_FILE_EXT = "cmp";
064:
065:            /**
066:             * Comparison file extension.
067:             */
068:            private static final String RESULT_FILE_EXT = "res";
069:
070:            /**
071:             * Path for templates. This property will override the
072:             * value in the default velocity properties file.
073:             */
074:            private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR
075:                    + "/includeevent";
076:
077:            /**
078:             * Results relative to the build directory.
079:             */
080:            private static final String RESULTS_DIR = TEST_RESULT_DIR
081:                    + "/includeevent";
082:
083:            /**
084:             * Results relative to the build directory.
085:             */
086:            private static final String COMPARE_DIR = TEST_COMPARE_DIR
087:                    + "/includeevent/compare";
088:
089:            /**
090:             * Default constructor.
091:             */
092:            public BuiltInEventHandlerTestCase(String name) {
093:                super (name);
094:            }
095:
096:            public void setUp() {
097:                assureResultsDirectoryExists(RESULTS_DIR);
098:            }
099:
100:            public static Test suite() {
101:                return new TestSuite(BuiltInEventHandlerTestCase.class);
102:            }
103:
104:            /**
105:             * Test reporting of invalid syntax
106:             * @throws Exception
107:             */
108:            public void testReportInvalidReferences1() throws Exception {
109:                VelocityEngine ve = new VelocityEngine();
110:                ReportInvalidReferences reporter = new ReportInvalidReferences();
111:                ve.init();
112:
113:                VelocityContext context = new VelocityContext();
114:                EventCartridge ec = new EventCartridge();
115:                ec.addEventHandler(reporter);
116:                ec.attachToContext(context);
117:
118:                context.put("a1", "test");
119:                context.put("b1", "test");
120:                Writer writer = new StringWriter();
121:
122:                ve.evaluate(context, writer, "test",
123:                        "$a1 $c1 $a1.length() $a1.foobar()");
124:
125:                List errors = reporter.getInvalidReferences();
126:                assertEquals(2, errors.size());
127:                assertEquals("$c1", ((InvalidReferenceInfo) errors.get(0))
128:                        .getInvalidReference());
129:                assertEquals("$a1.foobar()", ((InvalidReferenceInfo) errors
130:                        .get(1)).getInvalidReference());
131:            }
132:
133:            public void testReportInvalidReferences2() throws Exception {
134:                VelocityEngine ve = new VelocityEngine();
135:                ve.setProperty("eventhandler.invalidreference.exception",
136:                        "true");
137:                ReportInvalidReferences reporter = new ReportInvalidReferences();
138:                ve.init();
139:
140:                VelocityContext context = new VelocityContext();
141:                EventCartridge ec = new EventCartridge();
142:                ec.addEventHandler(reporter);
143:                ec.attachToContext(context);
144:
145:                context.put("a1", "test");
146:                context.put("b1", "test");
147:                Writer writer = new StringWriter();
148:
149:                ve.evaluate(context, writer, "test", "$a1 no problem");
150:
151:                try {
152:                    ve.evaluate(context, writer, "test",
153:                            "$a1 $c1 $a1.length() $a1.foobar()");
154:                    fail("Expected exception.");
155:                } catch (RuntimeException E) {
156:                }
157:
158:            }
159:
160:            /**
161:             * Test escaping
162:             * @throws Exception
163:             */
164:            public void testEscapeHtml() throws Exception {
165:                EscapeReference esc = new EscapeHtmlReference();
166:                assertEquals(
167:                        "test string&amp;another&lt;b&gt;bold&lt;/b&gt;test",
168:                        esc.referenceInsert("",
169:                                "test string&another<b>bold</b>test"));
170:                assertEquals("&lt;&quot;&gt;", esc.referenceInsert("", "<\">"));
171:                assertEquals("test string", esc.referenceInsert("",
172:                        "test string"));
173:            }
174:
175:            /**
176:             * Test escaping
177:             * @throws Exception
178:             */
179:            public void testEscapeXml() throws Exception {
180:                EscapeReference esc = new EscapeXmlReference();
181:                assertEquals(
182:                        "test string&amp;another&lt;b&gt;bold&lt;/b&gt;test",
183:                        esc.referenceInsert("",
184:                                "test string&another<b>bold</b>test"));
185:                assertEquals("&lt;&quot;&gt;", esc.referenceInsert("", "<\">"));
186:                assertEquals("&apos;", esc.referenceInsert("", "'"));
187:                assertEquals("test string", esc.referenceInsert("",
188:                        "test string"));
189:            }
190:
191:            /**
192:             * Test escaping
193:             * @throws Exception
194:             */
195:            public void testEscapeSql() throws Exception {
196:                EscapeReference esc = new EscapeSqlReference();
197:                assertEquals("Jimmy''s Pizza", esc.referenceInsert("",
198:                        "Jimmy's Pizza"));
199:                assertEquals("test string", esc.referenceInsert("",
200:                        "test string"));
201:            }
202:
203:            /**
204:             * Test escaping
205:             * @throws Exception
206:             */
207:            public void testEscapeJavaScript() throws Exception {
208:                EscapeReference esc = new EscapeJavaScriptReference();
209:                assertEquals("Jimmy\\'s Pizza", esc.referenceInsert("",
210:                        "Jimmy's Pizza"));
211:                assertEquals("test string", esc.referenceInsert("",
212:                        "test string"));
213:            }
214:
215:            /**
216:             * test that escape reference handler works with no match restrictions
217:             * @throws Exception
218:             */
219:            public void testEscapeReferenceMatchAll() throws Exception {
220:                VelocityEngine ve = new VelocityEngine();
221:                ve
222:                        .setProperty(
223:                                RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION,
224:                                "org.apache.velocity.app.event.implement.EscapeHtmlReference");
225:                ve.init();
226:
227:                Context context;
228:                Writer writer;
229:
230:                // test normal reference
231:                context = new VelocityContext();
232:                writer = new StringWriter();
233:                context.put("bold", "<b>");
234:                ve.evaluate(context, writer, "test", "$bold test & test");
235:                assertEquals("&lt;b&gt; test & test", writer.toString());
236:
237:                // test method reference
238:                context = new VelocityContext();
239:                writer = new StringWriter();
240:                context.put("bold", "<b>");
241:                ve.evaluate(context, writer, "test", "$bold.substring(0,1)");
242:                assertEquals("&lt;", writer.toString());
243:            }
244:
245:            /**
246:             * test that escape reference handler works with match restrictions
247:             * @throws Exception
248:             */
249:            public void testEscapeReferenceMatch() throws Exception {
250:                // set up HTML match on everything, JavaScript match on _js*
251:                VelocityEngine ve = new VelocityEngine();
252:                ve
253:                        .setProperty(
254:                                RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION,
255:                                "org.apache.velocity.app.event.implement.EscapeHtmlReference,org.apache.velocity.app.event.implement.EscapeJavaScriptReference");
256:                ve.setProperty("eventhandler.escape.javascript.match",
257:                        "/.*_js.*/");
258:                ve.init();
259:
260:                Writer writer;
261:
262:                // Html no JavaScript
263:                writer = new StringWriter();
264:                ve.evaluate(newEscapeContext(), writer, "test", "$test1");
265:                assertEquals("Jimmy's &lt;b&gt;pizza&lt;/b&gt;", writer
266:                        .toString());
267:
268:                // JavaScript and HTML
269:                writer = new StringWriter();
270:                ve.evaluate(newEscapeContext(), writer, "test", "$test1_js");
271:                assertEquals("Jimmy\\'s &lt;b&gt;pizza&lt;/b&gt;", writer
272:                        .toString());
273:
274:                // JavaScript and HTML
275:                writer = new StringWriter();
276:                ve.evaluate(newEscapeContext(), writer, "test",
277:                        "$test1_js_test");
278:                assertEquals("Jimmy\\'s &lt;b&gt;pizza&lt;/b&gt;", writer
279:                        .toString());
280:
281:                // JavaScript and HTML (method call)
282:                writer = new StringWriter();
283:                ve.evaluate(newEscapeContext(), writer, "test",
284:                        "$test1_js.substring(0,7)");
285:                assertEquals("Jimmy\\'s", writer.toString());
286:            }
287:
288:            private Context newEscapeContext() {
289:                Context context = new VelocityContext();
290:                context.put("test1", "Jimmy's <b>pizza</b>");
291:                context.put("test1_js", "Jimmy's <b>pizza</b>");
292:                context.put("test1_js_test", "Jimmy's <b>pizza</b>");
293:                return context;
294:            }
295:
296:            public void testPrintExceptionHandler() throws Exception {
297:                VelocityEngine ve1 = new VelocityEngine();
298:                ve1
299:                        .setProperty(
300:                                RuntimeConstants.EVENTHANDLER_METHODEXCEPTION,
301:                                "org.apache.velocity.app.event.implement.PrintExceptions");
302:                ve1.init();
303:
304:                VelocityEngine ve2 = new VelocityEngine();
305:                ve2
306:                        .setProperty(
307:                                RuntimeConstants.EVENTHANDLER_METHODEXCEPTION,
308:                                "org.apache.velocity.app.event.implement.PrintExceptions");
309:                ve2.setProperty("eventhandler.methodexception.message", "true");
310:                ve2.init();
311:
312:                VelocityEngine ve3 = new VelocityEngine();
313:                ve3
314:                        .setProperty(
315:                                RuntimeConstants.EVENTHANDLER_METHODEXCEPTION,
316:                                "org.apache.velocity.app.event.implement.PrintExceptions");
317:                ve3.setProperty("eventhandler.methodexception.stacktrace",
318:                        "true");
319:                ve3.init();
320:
321:                Context context;
322:                StringWriter writer;
323:
324:                context = new VelocityContext();
325:                context.put("list", new ArrayList());
326:
327:                // exception only
328:                writer = new StringWriter();
329:                ve1.evaluate(context, writer, "test", "$list.get(0)");
330:                assertTrue(writer.toString().indexOf(
331:                        "IndexOutOfBoundsException") != -1);
332:                assertTrue(writer.toString().indexOf("Index: 0, Size: 0") == -1);
333:                assertTrue(writer.toString().indexOf("ArrayList") == -1);
334:
335:                // message
336:                writer = new StringWriter();
337:                ve2.evaluate(context, writer, "test", "$list.get(0)");
338:                assertTrue(writer.toString().indexOf(
339:                        "IndexOutOfBoundsException") != -1);
340:                assertTrue(writer.toString().indexOf("Index: 0, Size: 0") != -1);
341:                assertTrue(writer.toString().indexOf("ArrayList") == -1);
342:
343:                // stack trace
344:                writer = new StringWriter();
345:                ve3.evaluate(context, writer, "test", "$list.get(0)");
346:                assertTrue(writer.toString().indexOf(
347:                        "IndexOutOfBoundsException") != -1);
348:                assertTrue(writer.toString().indexOf("ArrayList") != -1);
349:            }
350:
351:            public void testIncludeNotFound() throws Exception {
352:                VelocityEngine ve = new VelocityEngine();
353:                ve
354:                        .setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE,
355:                                "org.apache.velocity.app.event.implement.IncludeNotFound");
356:                ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
357:                        FILE_RESOURCE_LOADER_PATH);
358:                ve.init();
359:
360:                Template template;
361:                FileOutputStream fos;
362:                Writer fwriter;
363:                Context context;
364:
365:                template = ve.getTemplate(getFileName(null, "test6",
366:                        TMPL_FILE_EXT));
367:
368:                fos = new FileOutputStream(getFileName(RESULTS_DIR, "test6",
369:                        RESULT_FILE_EXT));
370:
371:                fwriter = new BufferedWriter(new OutputStreamWriter(fos));
372:
373:                context = new VelocityContext();
374:                template.merge(context, fwriter);
375:                fwriter.flush();
376:                fwriter.close();
377:
378:                if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test6",
379:                        RESULT_FILE_EXT, CMP_FILE_EXT)) {
380:                    fail("Output incorrect.");
381:                }
382:            }
383:
384:            public void testIncludeRelativePath() throws Exception {
385:                VelocityEngine ve = new VelocityEngine();
386:                ve
387:                        .setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE,
388:                                "org.apache.velocity.app.event.implement.IncludeRelativePath");
389:                ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
390:                        FILE_RESOURCE_LOADER_PATH);
391:                ve.init();
392:
393:                Template template;
394:                FileOutputStream fos;
395:                Writer fwriter;
396:                Context context;
397:
398:                template = ve.getTemplate(getFileName(null, "subdir/test2",
399:                        TMPL_FILE_EXT));
400:
401:                fos = new FileOutputStream(getFileName(RESULTS_DIR, "test2",
402:                        RESULT_FILE_EXT));
403:
404:                fwriter = new BufferedWriter(new OutputStreamWriter(fos));
405:
406:                context = new VelocityContext();
407:                template.merge(context, fwriter);
408:                fwriter.flush();
409:                fwriter.close();
410:
411:                if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test2",
412:                        RESULT_FILE_EXT, CMP_FILE_EXT)) {
413:                    fail("Output incorrect.");
414:                }
415:            }
416:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.