001: package org.apache.velocity.test.issues;
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.test.BaseTestCase;
036:
037: /**
038: * Test Case for <a href="https://issues.apache.org/jira/browse/VELOCITY-285">Velocity Issue 285</a>.
039: */
040: public class Velocity285TestCase extends BaseTestCase {
041: /**
042: * Comparison file extension.
043: */
044: private static final String CMP_FILE_EXT = "cmp";
045:
046: /**
047: * Comparison file extension.
048: */
049: private static final String RESULT_FILE_EXT = "res";
050:
051: /**
052: * Results relative to the build directory.
053: */
054: private static final String RESULTS_DIR = TEST_RESULT_DIR
055: + "/issues/velocity-285";
056:
057: /**
058: * Template Directory
059: */
060: private static final String TEMPLATE_DIR = TEST_COMPARE_DIR
061: + "/issues/velocity-285/templates";
062:
063: /**
064: * Results relative to the build directory.
065: */
066: private static final String COMPARE_DIR = TEST_COMPARE_DIR
067: + "/issues/velocity-285/compare";
068:
069: public Velocity285TestCase(final String name) throws Exception {
070: super (name);
071: }
072:
073: public static Test suite() {
074: return new TestSuite(Velocity285TestCase.class);
075: }
076:
077: public void setUp() throws Exception {
078:
079: assureResultsDirectoryExists(RESULTS_DIR);
080:
081: Velocity.addProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
082: TEMPLATE_DIR);
083:
084: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
085: NullLogChute.class.getName());
086:
087: Velocity.init();
088: }
089:
090: public void testVelocity285() throws Exception {
091: executeTest("velocity285.vm");
092: }
093:
094: protected Template executeTest(final String templateName)
095: throws Exception {
096: Template template = RuntimeSingleton.getTemplate(templateName);
097:
098: FileOutputStream fos = new FileOutputStream(getFileName(
099: RESULTS_DIR, templateName, RESULT_FILE_EXT));
100:
101: Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
102:
103: VelocityContext context = new VelocityContext();
104:
105: template.merge(context, writer);
106: writer.flush();
107: writer.close();
108:
109: if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName,
110: RESULT_FILE_EXT, CMP_FILE_EXT)) {
111: fail("Output incorrect for Template: " + templateName);
112: }
113:
114: return template;
115: }
116: }
|