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:
036: /**
037: * Load templates from the Classpath.
038: *
039: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
040: * @author <a href="mailto:daveb@miceda-data.com">Dave Bryson</a>
041: * @version $Id: ClasspathResourceTestCase.java 477002 2006-11-20 01:07:43Z henning $
042: */
043: public class ClasspathResourceTestCase extends BaseTestCase {
044: /**
045: * VTL file extension.
046: */
047: private static final String TMPL_FILE_EXT = "vm";
048:
049: /**
050: * Comparison file extension.
051: */
052: private static final String CMP_FILE_EXT = "cmp";
053:
054: /**
055: * Comparison file extension.
056: */
057: private static final String RESULT_FILE_EXT = "res";
058:
059: /**
060: * Results relative to the build directory.
061: */
062: private static final String RESULTS_DIR = TEST_RESULT_DIR
063: + "/cpload";
064:
065: /**
066: * Results relative to the build directory.
067: */
068: private static final String COMPARE_DIR = TEST_COMPARE_DIR
069: + "/cpload/compare";
070:
071: /**
072: * Default constructor.
073: */
074: public ClasspathResourceTestCase(String name) {
075: super (name);
076: }
077:
078: public void setUp() throws Exception {
079: assureResultsDirectoryExists(RESULTS_DIR);
080:
081: Velocity.setProperty(Velocity.RESOURCE_LOADER, "classpath");
082:
083: /*
084: * I don't think I should have to do this, these should
085: * be in the default config file.
086: */
087:
088: Velocity
089: .addProperty("classpath." + Velocity.RESOURCE_LOADER
090: + ".class",
091: "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
092:
093: Velocity.setProperty("classpath." + Velocity.RESOURCE_LOADER
094: + ".cache", "false");
095:
096: Velocity.setProperty("classpath." + Velocity.RESOURCE_LOADER
097: + ".modificationCheckInterval", "2");
098:
099: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
100: NullLogChute.class.getName());
101:
102: Velocity.init();
103: }
104:
105: public static Test suite() {
106: return new TestSuite(ClasspathResourceTestCase.class);
107: }
108:
109: /**
110: * Runs the test.
111: */
112: public void testClasspathResource() throws Exception {
113: /*
114: * lets ensure the results directory exists
115: */
116: assureResultsDirectoryExists(RESULTS_DIR);
117:
118: Template template1 = RuntimeSingleton
119: .getTemplate("template/test1." + TMPL_FILE_EXT);
120:
121: // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
122: // Template template2 = RuntimeSingleton.getTemplate(
123: // getFileName(null, "template/test2", TMPL_FILE_EXT));
124:
125: FileOutputStream fos1 = new FileOutputStream(getFileName(
126: RESULTS_DIR, "test1", RESULT_FILE_EXT));
127:
128: // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
129: // FileOutputStream fos2 =
130: // new FileOutputStream (
131: // getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));
132:
133: Writer writer1 = new BufferedWriter(
134: new OutputStreamWriter(fos1));
135: // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
136: // Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
137:
138: /*
139: * put the Vector into the context, and merge both
140: */
141:
142: VelocityContext context = new VelocityContext();
143:
144: template1.merge(context, writer1);
145: writer1.flush();
146: writer1.close();
147:
148: // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
149: // template2.merge(context, writer2);
150: // writer2.flush();
151: // writer2.close();
152:
153: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test1",
154: RESULT_FILE_EXT, CMP_FILE_EXT)
155: // Uncomment when http://jira.codehaus.org/browse/MPTEST-57 has been resolved
156: // || !isMatch(RESULTS_DIR,COMPARE_DIR,"test2",RESULT_FILE_EXT,CMP_FILE_EXT)
157: ) {
158: fail("Output is incorrect!");
159: }
160: }
161: }
|