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: import java.util.Vector;
027:
028: import junit.framework.Test;
029: import junit.framework.TestSuite;
030:
031: import org.apache.velocity.Template;
032: import org.apache.velocity.VelocityContext;
033: import org.apache.velocity.app.Velocity;
034: import org.apache.velocity.runtime.RuntimeSingleton;
035: import org.apache.velocity.runtime.log.NullLogChute;
036:
037: /**
038: * Tests if we are context safe : can we switch objects in the context
039: * and re-merge the template safely.
040: *
041: * NOTE:
042: * This class should not extend RuntimeTestCase because this test
043: * is run from the VelocityTestSuite which in effect a runtime
044: * test suite and the test suite initializes the Runtime. Extending
045: * RuntimeTestCase causes the Runtime to be initialized twice.
046: *
047: * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
048: * @version $Id: ContextSafetyTestCase.java 477002 2006-11-20 01:07:43Z henning $
049: */
050: public class ContextSafetyTestCase extends BaseTestCase implements
051: TemplateTestBase {
052: public ContextSafetyTestCase(String name) {
053: super (name);
054: }
055:
056: public void setUp() throws Exception {
057: Velocity.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH,
058: FILE_RESOURCE_LOADER_PATH);
059:
060: Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS,
061: NullLogChute.class.getName());
062:
063: Velocity.init();
064: }
065:
066: public static Test suite() {
067: return new TestSuite(ContextSafetyTestCase.class);
068: }
069:
070: /**
071: * Runs the test.
072: */
073: public void testContextSafety() throws Exception {
074: /*
075: * make a Vector and String array because
076: * they are treated differently in Foreach()
077: */
078: Vector v = new Vector();
079:
080: v.addElement(new String("vector hello 1"));
081: v.addElement(new String("vector hello 2"));
082: v.addElement(new String("vector hello 3"));
083:
084: String strArray[] = new String[3];
085:
086: strArray[0] = "array hello 1";
087: strArray[1] = "array hello 2";
088: strArray[2] = "array hello 3";
089:
090: VelocityContext context = new VelocityContext();
091:
092: assureResultsDirectoryExists(RESULT_DIR);
093:
094: /*
095: * get the template and the output
096: */
097:
098: Template template = RuntimeSingleton.getTemplate(getFileName(
099: null, "context_safety", TMPL_FILE_EXT));
100:
101: FileOutputStream fos1 = new FileOutputStream(getFileName(
102: RESULT_DIR, "context_safety1", RESULT_FILE_EXT));
103:
104: FileOutputStream fos2 = new FileOutputStream(getFileName(
105: RESULT_DIR, "context_safety2", RESULT_FILE_EXT));
106:
107: Writer writer1 = new BufferedWriter(
108: new OutputStreamWriter(fos1));
109: Writer writer2 = new BufferedWriter(
110: new OutputStreamWriter(fos2));
111:
112: /*
113: * put the Vector into the context, and merge
114: */
115:
116: context.put("vector", v);
117: template.merge(context, writer1);
118: writer1.flush();
119: writer1.close();
120:
121: /*
122: * now put the string array into the context, and merge
123: */
124:
125: context.put("vector", strArray);
126: template.merge(context, writer2);
127: writer2.flush();
128: writer2.close();
129:
130: if (!isMatch(RESULT_DIR, COMPARE_DIR, "context_safety1",
131: RESULT_FILE_EXT, CMP_FILE_EXT)
132: || !isMatch(RESULT_DIR, COMPARE_DIR, "context_safety2",
133: RESULT_FILE_EXT, CMP_FILE_EXT)) {
134: fail("Output incorrect.");
135: }
136: }
137: }
|