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: * Tests if the VM template-locality is working.
038: *
039: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
040: * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
041: * @version $Id: InlineScopeVMTestCase.java 477002 2006-11-20 01:07:43Z henning $
042: */
043: public class InlineScopeVMTestCase extends BaseTestCase implements
044: TemplateTestBase {
045: public InlineScopeVMTestCase(String name) {
046: super (name);
047: }
048:
049: public void setUp() throws Exception {
050: /*
051: * do our properties locally, and just override the ones we want
052: * changed
053: */
054:
055: Velocity.setProperty(
056: Velocity.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, "true");
057:
058: Velocity.setProperty(Velocity.VM_PERM_INLINE_LOCAL, "true");
059:
060: Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
061: FILE_RESOURCE_LOADER_PATH);
062:
063: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
064: NullLogChute.class.getName());
065:
066: Velocity.init();
067: }
068:
069: public static Test suite() {
070: return new TestSuite(InlineScopeVMTestCase.class);
071: }
072:
073: /**
074: * Runs the test.
075: */
076: public void testInlineScopeVM() throws Exception {
077: assureResultsDirectoryExists(RESULT_DIR);
078:
079: /*
080: * Get the template and the output. Do them backwards.
081: * vm_test2 uses a local VM and vm_test1 doesn't
082: */
083:
084: Template template2 = RuntimeSingleton.getTemplate(getFileName(
085: null, "vm_test2", TMPL_FILE_EXT));
086:
087: Template template1 = RuntimeSingleton.getTemplate(getFileName(
088: null, "vm_test1", TMPL_FILE_EXT));
089:
090: FileOutputStream fos1 = new FileOutputStream(getFileName(
091: RESULT_DIR, "vm_test1", RESULT_FILE_EXT));
092:
093: FileOutputStream fos2 = new FileOutputStream(getFileName(
094: RESULT_DIR, "vm_test2", RESULT_FILE_EXT));
095:
096: Writer writer1 = new BufferedWriter(
097: new OutputStreamWriter(fos1));
098: Writer writer2 = new BufferedWriter(
099: new OutputStreamWriter(fos2));
100:
101: /*
102: * put the Vector into the context, and merge both
103: */
104:
105: VelocityContext context = new VelocityContext();
106:
107: template1.merge(context, writer1);
108: writer1.flush();
109: writer1.close();
110:
111: template2.merge(context, writer2);
112: writer2.flush();
113: writer2.close();
114:
115: if (!isMatch(RESULT_DIR, COMPARE_DIR, "vm_test1",
116: RESULT_FILE_EXT, CMP_FILE_EXT)
117: || !isMatch(RESULT_DIR, COMPARE_DIR, "vm_test2",
118: RESULT_FILE_EXT, CMP_FILE_EXT)) {
119: fail("Output incorrect.");
120: }
121: }
122: }
|