Source Code Cross Referenced for IncludeEventHandlingTestCase.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.Writer;
026:
027:        import junit.framework.Test;
028:        import junit.framework.TestSuite;
029:
030:        import org.apache.velocity.Template;
031:        import org.apache.velocity.VelocityContext;
032:        import org.apache.velocity.app.Velocity;
033:        import org.apache.velocity.app.event.EventCartridge;
034:        import org.apache.velocity.app.event.IncludeEventHandler;
035:        import org.apache.velocity.context.Context;
036:        import org.apache.velocity.runtime.RuntimeServices;
037:        import org.apache.velocity.runtime.RuntimeSingleton;
038:        import org.apache.velocity.runtime.log.NullLogChute;
039:        import org.apache.velocity.util.RuntimeServicesAware;
040:
041:        /**
042:         *  Tests event handling
043:         *
044:         * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
045:         * @version $Id: IncludeEventHandlingTestCase.java 477002 2006-11-20 01:07:43Z henning $
046:         */
047:        public class IncludeEventHandlingTestCase extends BaseTestCase
048:                implements  IncludeEventHandler, RuntimeServicesAware {
049:
050:            /**
051:             * VTL file extension.
052:             */
053:            private static final String TMPL_FILE_EXT = "vm";
054:
055:            /**
056:             * Comparison file extension.
057:             */
058:            private static final String CMP_FILE_EXT = "cmp";
059:
060:            /**
061:             * Comparison file extension.
062:             */
063:            private static final String RESULT_FILE_EXT = "res";
064:
065:            /**
066:             * Path for templates. This property will override the
067:             * value in the default velocity properties file.
068:             */
069:            private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR
070:                    + "/includeevent";
071:
072:            /**
073:             * Results relative to the build directory.
074:             */
075:            private static final String RESULTS_DIR = TEST_RESULT_DIR
076:                    + "/includeevent";
077:
078:            /**
079:             * Results relative to the build directory.
080:             */
081:            private static final String COMPARE_DIR = TEST_COMPARE_DIR
082:                    + "/includeevent/compare";
083:
084:            private static final int PASS_THROUGH = 0;
085:            private static final int RELATIVE_PATH = 1;
086:            private static final int BLOCK = 2;
087:
088:            private int EventHandlerBehavior = PASS_THROUGH;
089:
090:            /**
091:             * Default constructor.
092:             */
093:            public IncludeEventHandlingTestCase(String name) {
094:                super (name);
095:            }
096:
097:            public void setUp() throws Exception {
098:                assureResultsDirectoryExists(RESULTS_DIR);
099:
100:                Velocity.addProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
101:                        FILE_RESOURCE_LOADER_PATH);
102:
103:                Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
104:                        NullLogChute.class.getName());
105:
106:                Velocity.init();
107:
108:            }
109:
110:            public static Test suite() {
111:                return new TestSuite(IncludeEventHandlingTestCase.class);
112:            }
113:
114:            /**
115:             * Runs the test.
116:             */
117:            public void testIncludeEventHandling() throws Exception {
118:                Template template1 = RuntimeSingleton.getTemplate(getFileName(
119:                        null, "test1", TMPL_FILE_EXT));
120:
121:                Template template2 = RuntimeSingleton.getTemplate(getFileName(
122:                        null, "subdir/test2", TMPL_FILE_EXT));
123:
124:                Template template3 = RuntimeSingleton.getTemplate(getFileName(
125:                        null, "test3", TMPL_FILE_EXT));
126:
127:                FileOutputStream fos1 = new FileOutputStream(getFileName(
128:                        RESULTS_DIR, "test1", RESULT_FILE_EXT));
129:
130:                FileOutputStream fos2 = new FileOutputStream(getFileName(
131:                        RESULTS_DIR, "test2", RESULT_FILE_EXT));
132:
133:                FileOutputStream fos3 = new FileOutputStream(getFileName(
134:                        RESULTS_DIR, "test3", RESULT_FILE_EXT));
135:
136:                Writer writer1 = new BufferedWriter(
137:                        new OutputStreamWriter(fos1));
138:                Writer writer2 = new BufferedWriter(
139:                        new OutputStreamWriter(fos2));
140:                Writer writer3 = new BufferedWriter(
141:                        new OutputStreamWriter(fos3));
142:
143:                /*
144:                 *  lets make a Context and add the event cartridge
145:                 */
146:
147:                Context context = new VelocityContext();
148:
149:                /*
150:                 *  Now make an event cartridge, register the
151:                 *  input event handler and attach it to the
152:                 *  Context
153:                 */
154:
155:                EventCartridge ec = new EventCartridge();
156:                ec.addEventHandler(this );
157:                ec.attachToContext(context);
158:
159:                // BEHAVIOR A: pass through #input and #parse with no change
160:                EventHandlerBehavior = PASS_THROUGH;
161:
162:                template1.merge(context, writer1);
163:                writer1.flush();
164:                writer1.close();
165:
166:                // BEHAVIOR B: pass through #input and #parse with using a relative path
167:                EventHandlerBehavior = RELATIVE_PATH;
168:
169:                template2.merge(context, writer2);
170:                writer2.flush();
171:                writer2.close();
172:
173:                // BEHAVIOR C: refuse to pass through #input and #parse
174:                EventHandlerBehavior = BLOCK;
175:
176:                template3.merge(context, writer3);
177:                writer3.flush();
178:                writer3.close();
179:
180:                if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test1",
181:                        RESULT_FILE_EXT, CMP_FILE_EXT)
182:                        || !isMatch(RESULTS_DIR, COMPARE_DIR, "test2",
183:                                RESULT_FILE_EXT, CMP_FILE_EXT)
184:                        || !isMatch(RESULTS_DIR, COMPARE_DIR, "test3",
185:                                RESULT_FILE_EXT, CMP_FILE_EXT)) {
186:                    fail("Output incorrect.");
187:                }
188:            }
189:
190:            public void setRuntimeServices(RuntimeServices rs) {
191:            }
192:
193:            /**
194:             * Sample handler with different behaviors for the different tests.
195:             */
196:            public String includeEvent(String includeResourcePath,
197:                    String currentResourcePath, String directiveName) {
198:                if (EventHandlerBehavior == PASS_THROUGH)
199:                    return includeResourcePath;
200:
201:                // treat as relative path
202:                else if (EventHandlerBehavior == RELATIVE_PATH) {
203:                    // if the resource name starts with a slash, it's not a relative path
204:                    if (includeResourcePath.startsWith("/")
205:                            || includeResourcePath.startsWith("\\")) {
206:                        return includeResourcePath;
207:                    }
208:
209:                    int lastslashpos = Math.max(currentResourcePath
210:                            .lastIndexOf("/"), currentResourcePath
211:                            .lastIndexOf("\\"));
212:
213:                    // root of resource tree
214:                    if ((lastslashpos == -1))
215:                        return includeResourcePath;
216:
217:                    // prepend path to the input path
218:                    else
219:                        return currentResourcePath.substring(0, lastslashpos)
220:                                + "/" + includeResourcePath;
221:
222:                } else if (EventHandlerBehavior == BLOCK)
223:                    return null;
224:
225:                // should never happen
226:                else
227:                    return null;
228:
229:            }
230:
231:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.