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: }
|