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.RuntimeSingleton;
034: import org.apache.velocity.runtime.log.NullLogChute;
035: import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
036:
037: /**
038: * Multiple paths in the file resource loader.
039: *
040: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
041: * @version $Id: StringResourceLoaderTestCase.java 479058 2006-11-25 00:26:32Z henning $
042: */
043: public class StringResourceLoaderTestCase extends BaseTestCase {
044: /**
045: * Results relative to the build directory.
046: */
047: private static final String RESULTS_DIR = TEST_RESULT_DIR
048: + "/stringloader";
049:
050: /**
051: * Results relative to the build directory.
052: */
053: private static final String COMPARE_DIR = TEST_COMPARE_DIR
054: + "/stringloader/compare";
055:
056: /**
057: * Default constructor.
058: */
059: public StringResourceLoaderTestCase(String name) {
060: super (name);
061: }
062:
063: public static Test suite() {
064: return new TestSuite(StringResourceLoaderTestCase.class);
065: }
066:
067: public void setUp() throws Exception {
068: assureResultsDirectoryExists(RESULTS_DIR);
069:
070: Velocity.setProperty(Velocity.RESOURCE_LOADER, "string");
071: Velocity.addProperty("string.resource.loader.class",
072: StringResourceLoader.class.getName());
073: Velocity
074: .addProperty(
075: "string.resource.loader.modificationCheckInterval",
076: "1");
077:
078: // Silence the logger.
079: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
080: NullLogChute.class.getName());
081:
082: Velocity.init();
083: }
084:
085: public void testSimpleTemplate() throws Exception {
086: StringResourceLoader.getRepository().putStringResource(
087: "simpletemplate.vm", "This is a test for ${foo}");
088:
089: Template template = RuntimeSingleton.getTemplate(getFileName(
090: null, "simpletemplate", TMPL_FILE_EXT));
091:
092: FileOutputStream fos = new FileOutputStream(getFileName(
093: RESULTS_DIR, "simpletemplate", RESULT_FILE_EXT));
094:
095: Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
096:
097: VelocityContext context = new VelocityContext();
098: context.put("foo", "a foo object");
099:
100: template.merge(context, writer);
101: writer.flush();
102: writer.close();
103:
104: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "simpletemplate",
105: RESULT_FILE_EXT, CMP_FILE_EXT)) {
106: fail("Output incorrect.");
107: }
108: }
109:
110: public void testMultipleTemplates() throws Exception {
111: StringResourceLoader.getRepository().putStringResource(
112: "multi1.vm", "I am the $first template.");
113: StringResourceLoader.getRepository().putStringResource(
114: "multi2.vm", "I am the $second template.");
115:
116: Template template1 = RuntimeSingleton.getTemplate(getFileName(
117: null, "multi1", TMPL_FILE_EXT));
118:
119: FileOutputStream fos = new FileOutputStream(getFileName(
120: RESULTS_DIR, "multi1", RESULT_FILE_EXT));
121:
122: Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
123:
124: VelocityContext context = new VelocityContext();
125: context.put("first", new Integer(1));
126: context.put("second", "two");
127:
128: template1.merge(context, writer);
129: writer.flush();
130: writer.close();
131:
132: Template template2 = RuntimeSingleton.getTemplate(getFileName(
133: null, "multi2", TMPL_FILE_EXT));
134:
135: fos = new FileOutputStream(getFileName(RESULTS_DIR, "multi2",
136: RESULT_FILE_EXT));
137:
138: writer = new BufferedWriter(new OutputStreamWriter(fos));
139:
140: template2.merge(context, writer);
141: writer.flush();
142: writer.close();
143:
144: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "multi1",
145: RESULT_FILE_EXT, CMP_FILE_EXT)) {
146: fail("Template 1 incorrect.");
147: }
148:
149: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "multi2",
150: RESULT_FILE_EXT, CMP_FILE_EXT)) {
151: fail("Template 2 incorrect.");
152: }
153: }
154:
155: public void testContentChange() throws Exception {
156: StringResourceLoader.getRepository().putStringResource(
157: "change.vm", "I am the $first template.");
158:
159: Template template = RuntimeSingleton.getTemplate(getFileName(
160: null, "change", TMPL_FILE_EXT));
161:
162: FileOutputStream fos = new FileOutputStream(getFileName(
163: RESULTS_DIR, "change1", RESULT_FILE_EXT));
164:
165: Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
166:
167: VelocityContext context = new VelocityContext();
168: context.put("first", new Integer(1));
169: context.put("second", "two");
170:
171: template.merge(context, writer);
172: writer.flush();
173: writer.close();
174:
175: StringResourceLoader.getRepository().putStringResource(
176: "change.vm", "I am the $second template.");
177: Thread.sleep(2000L);
178: template = RuntimeSingleton.getTemplate(getFileName(null,
179: "change", TMPL_FILE_EXT));
180:
181: fos = new FileOutputStream(getFileName(RESULTS_DIR, "change2",
182: RESULT_FILE_EXT));
183:
184: writer = new BufferedWriter(new OutputStreamWriter(fos));
185:
186: template.merge(context, writer);
187: writer.flush();
188: writer.close();
189:
190: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "change1",
191: RESULT_FILE_EXT, CMP_FILE_EXT)) {
192: fail("Template 1 incorrect.");
193: }
194:
195: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "change2",
196: RESULT_FILE_EXT, CMP_FILE_EXT)) {
197: fail("Template 2 incorrect.");
198: }
199: }
200:
201: }
|