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.FileResourceLoader;
036: import org.apache.velocity.runtime.resource.loader.ResourceLoader;
037:
038: /**
039: * Test that an instance of a ResourceLoader can be successfully passed in.
040: *
041: * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
042: * @version $Id: ResourceLoaderInstanceTestCase.java 477002 2006-11-20 01:07:43Z henning $
043: */
044: public class ResourceLoaderInstanceTestCase extends BaseTestCase {
045: /**
046: * VTL file extension.
047: */
048: private static final String TMPL_FILE_EXT = "vm";
049:
050: /**
051: * Comparison file extension.
052: */
053: private static final String CMP_FILE_EXT = "cmp";
054:
055: /**
056: * Comparison file extension.
057: */
058: private static final String RESULT_FILE_EXT = "res";
059:
060: /**
061: * Path for templates. This property will override the
062: * value in the default velocity properties file.
063: */
064: private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR
065: + "/resourceinstance";
066:
067: /**
068: * Results relative to the build directory.
069: */
070: private static final String RESULTS_DIR = TEST_RESULT_DIR
071: + "/resourceinstance";
072:
073: /**
074: * Results relative to the build directory.
075: */
076: private static final String COMPARE_DIR = TEST_COMPARE_DIR
077: + "/resourceinstance/compare";
078:
079: /**
080: * Default constructor.
081: */
082: public ResourceLoaderInstanceTestCase(String name) {
083: super (name);
084: }
085:
086: public void setUp() throws Exception {
087:
088: ResourceLoader rl = new FileResourceLoader();
089:
090: // pass in an instance to Velocity
091: Velocity.addProperty("resource.loader", "testrl");
092: Velocity.setProperty("testrl.resource.loader.instance", rl);
093: Velocity.setProperty("testrl.resource.loader.path",
094: FILE_RESOURCE_LOADER_PATH);
095:
096: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
097: NullLogChute.class.getName());
098:
099: Velocity.init();
100: }
101:
102: public static Test suite() {
103: return new TestSuite(ResourceLoaderInstanceTestCase.class);
104: }
105:
106: /**
107: * Runs the test.
108: */
109: public void testResourceLoaderInstance() throws Exception {
110: assureResultsDirectoryExists(RESULTS_DIR);
111:
112: Template template = RuntimeSingleton.getTemplate(getFileName(
113: null, "testfile", TMPL_FILE_EXT));
114:
115: FileOutputStream fos = new FileOutputStream(getFileName(
116: RESULTS_DIR, "testfile", RESULT_FILE_EXT));
117:
118: Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
119:
120: /*
121: * put the Vector into the context, and merge both
122: */
123:
124: VelocityContext context = new VelocityContext();
125:
126: template.merge(context, writer);
127: writer.flush();
128: writer.close();
129:
130: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "testfile",
131: RESULT_FILE_EXT, CMP_FILE_EXT)) {
132: fail("Output incorrect.");
133: }
134: }
135: }
|