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: * Multiple paths in the file resource loader.
038: *
039: * @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
040: * @version $Id: MultipleFileResourcePathTestCase.java 477002 2006-11-20 01:07:43Z henning $
041: */
042: public class MultipleFileResourcePathTestCase extends BaseTestCase {
043:
044: /**
045: * Path for templates. This property will override the
046: * value in the default velocity properties file.
047: */
048: private final static String FILE_RESOURCE_LOADER_PATH1 = TEST_COMPARE_DIR
049: + "/multi/path1";
050:
051: /**
052: * Path for templates. This property will override the
053: * value in the default velocity properties file.
054: */
055: private final static String FILE_RESOURCE_LOADER_PATH2 = TEST_COMPARE_DIR
056: + "/multi/path2";
057:
058: /**
059: * Results relative to the build directory.
060: */
061: private static final String RESULTS_DIR = TEST_RESULT_DIR
062: + "/multi";
063:
064: /**
065: * Results relative to the build directory.
066: */
067: private static final String COMPARE_DIR = TEST_COMPARE_DIR
068: + "/multi/compare";
069:
070: /**
071: * Default constructor.
072: */
073: public MultipleFileResourcePathTestCase(String name) {
074: super (name);
075: }
076:
077: public static Test suite() {
078: return new TestSuite(MultipleFileResourcePathTestCase.class);
079: }
080:
081: public void setUp() throws Exception {
082: assureResultsDirectoryExists(RESULTS_DIR);
083:
084: Velocity.addProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
085: FILE_RESOURCE_LOADER_PATH1);
086:
087: Velocity.addProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
088: FILE_RESOURCE_LOADER_PATH2);
089:
090: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
091: NullLogChute.class.getName());
092:
093: Velocity.init();
094: }
095:
096: /**
097: * Runs the test.
098: */
099: public void testMultipleFileResources() throws Exception {
100: Template template1 = RuntimeSingleton.getTemplate(getFileName(
101: null, "path1", TMPL_FILE_EXT));
102:
103: Template template2 = RuntimeSingleton.getTemplate(getFileName(
104: null, "path2", TMPL_FILE_EXT));
105:
106: FileOutputStream fos1 = new FileOutputStream(getFileName(
107: RESULTS_DIR, "path1", RESULT_FILE_EXT));
108:
109: FileOutputStream fos2 = new FileOutputStream(getFileName(
110: RESULTS_DIR, "path2", RESULT_FILE_EXT));
111:
112: Writer writer1 = new BufferedWriter(
113: new OutputStreamWriter(fos1));
114: Writer writer2 = new BufferedWriter(
115: new OutputStreamWriter(fos2));
116:
117: /*
118: * put the Vector into the context, and merge both
119: */
120:
121: VelocityContext context = new VelocityContext();
122:
123: template1.merge(context, writer1);
124: writer1.flush();
125: writer1.close();
126:
127: template2.merge(context, writer2);
128: writer2.flush();
129: writer2.close();
130:
131: if (!isMatch(RESULTS_DIR, COMPARE_DIR, "path1",
132: RESULT_FILE_EXT, CMP_FILE_EXT)
133: || !isMatch(RESULTS_DIR, COMPARE_DIR, "path2",
134: RESULT_FILE_EXT, CMP_FILE_EXT)) {
135: fail("Output incorrect.");
136: }
137: }
138: }
|