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.runtime.log.NullLogChute;
034:
035: /**
036: * Load templates from the Classpath.
037: *
038: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
039: * @author <a href="mailto:daveb@miceda-data.com">Dave Bryson</a>
040: * @version $Id: MultiLoaderTestCase.java 477002 2006-11-20 01:07:43Z henning $
041: */
042: public class MultiLoaderTestCase extends BaseTestCase {
043: /**
044: * VTL file extension.
045: */
046: private static final String TMPL_FILE_EXT = "vm";
047:
048: /**
049: * Comparison file extension.
050: */
051: private static final String CMP_FILE_EXT = "cmp";
052:
053: /**
054: * Comparison file extension.
055: */
056: private static final String RESULT_FILE_EXT = "res";
057:
058: /**
059: * Results relative to the build directory.
060: */
061: private static final String RESULTS_DIR = TEST_RESULT_DIR
062: + "/multiloader";
063:
064: /**
065: * Path for templates. This property will override the
066: * value in the default velocity properties file.
067: */
068: private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR
069: + "/multiloader";
070:
071: /**
072: * Results relative to the build directory.
073: */
074: private static final String COMPARE_DIR = TEST_COMPARE_DIR
075: + "/multiloader/compare";
076:
077: /**
078: * Default constructor.
079: */
080: public MultiLoaderTestCase(String name) {
081: super (name);
082: }
083:
084: public void setUp() throws Exception {
085: assureResultsDirectoryExists(RESULTS_DIR);
086:
087: /*
088: * Set up the file loader.
089: */
090:
091: Velocity.setProperty(Velocity.RESOURCE_LOADER, "file");
092:
093: Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
094: FILE_RESOURCE_LOADER_PATH);
095:
096: Velocity.addProperty(Velocity.RESOURCE_LOADER, "classpath");
097:
098: Velocity.addProperty(Velocity.RESOURCE_LOADER, "jar");
099:
100: /*
101: * Set up the classpath loader.
102: */
103:
104: Velocity
105: .setProperty("classpath." + Velocity.RESOURCE_LOADER
106: + ".class",
107: "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
108:
109: Velocity.setProperty("classpath." + Velocity.RESOURCE_LOADER
110: + ".cache", "false");
111:
112: Velocity.setProperty("classpath." + Velocity.RESOURCE_LOADER
113: + ".modificationCheckInterval", "2");
114:
115: /*
116: * setup the Jar loader
117: */
118:
119: Velocity
120: .setProperty("jar." + Velocity.RESOURCE_LOADER
121: + ".class",
122: "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
123:
124: Velocity.setProperty("jar." + Velocity.RESOURCE_LOADER
125: + ".path", "jar:file:" + FILE_RESOURCE_LOADER_PATH
126: + "/test2.jar");
127:
128: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
129: NullLogChute.class.getName());
130:
131: Velocity.init();
132: }
133:
134: public static Test suite() {
135: return new TestSuite(MultiLoaderTestCase.class);
136: }
137:
138: /**
139: * Runs the test.
140: */
141: public void testMultiLoader() throws Exception {
142: /*
143: * lets ensure the results directory exists
144: */
145: assureResultsDirectoryExists(RESULTS_DIR);
146:
147: /*
148: * Template to find with the file loader.
149: */
150: Template template1 = Velocity.getTemplate(getFileName(null,
151: "path1", TMPL_FILE_EXT));
152:
153: /*
154: * Template to find with the classpath loader.
155: */
156: Template template2 = Velocity.getTemplate("template/test1."
157: + TMPL_FILE_EXT);
158:
159: /*
160: * Template to find with the jar loader
161: */
162: Template template3 = Velocity.getTemplate("template/test2."
163: + TMPL_FILE_EXT);
164:
165: /*
166: * and the results files
167: */
168:
169: FileOutputStream fos1 = new FileOutputStream(getFileName(
170: RESULTS_DIR, "path1", RESULT_FILE_EXT));
171:
172: FileOutputStream fos2 = new FileOutputStream(getFileName(
173: RESULTS_DIR, "test2", RESULT_FILE_EXT));
174:
175: FileOutputStream fos3 = new FileOutputStream(getFileName(
176: RESULTS_DIR, "test3", RESULT_FILE_EXT));
177:
178: Writer writer1 = new BufferedWriter(
179: new OutputStreamWriter(fos1));
180: Writer writer2 = new BufferedWriter(
181: new OutputStreamWriter(fos2));
182: Writer writer3 = new BufferedWriter(
183: new OutputStreamWriter(fos3));
184:
185: /*
186: * put the Vector into the context, and merge both
187: */
188:
189: VelocityContext context = new VelocityContext();
190:
191: template1.merge(context, writer1);
192: writer1.flush();
193: writer1.close();
194:
195: template2.merge(context, writer2);
196: writer2.flush();
197: writer2.close();
198:
199: template3.merge(context, writer3);
200: writer3.flush();
201: writer3.close();
202:
203: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "path1",
204: RESULT_FILE_EXT, CMP_FILE_EXT)) {
205: fail("Output incorrect for FileResourceLoader test.");
206: }
207:
208: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test2",
209: RESULT_FILE_EXT, CMP_FILE_EXT)) {
210: fail("Output incorrect for ClasspathResourceLoader test.");
211: }
212:
213: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test3",
214: RESULT_FILE_EXT, CMP_FILE_EXT)) {
215: fail("Output incorrect for JarResourceLoader test.");
216: }
217: }
218: }
|