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.VelocityEngine;
033: import org.apache.velocity.context.Context;
034: import org.apache.velocity.runtime.RuntimeConstants;
035:
036: /**
037: * Test that an instance of a ResourceLoader can be successfully passed in.
038: *
039: * @author <a href="mailto:wglass@apache.org">Will Glass-Husain</a>
040: * @version $Id: SetTestCase.java 463298 2006-10-12 16:10:32Z henning $
041: */
042: public class SetTestCase extends BaseTestCase {
043: /**
044: * VTL file extension.
045: */
046: private static final String TMPL_FILE_EXT = "vm";
047:
048: /**
049: * Comparison file extension.
050: */
051: private static final String CMP_FILE_EXT = "cmp";
052:
053: /**
054: * Comparison file extension.
055: */
056: private static final String RESULT_FILE_EXT = "res";
057:
058: /**
059: * Path for templates. This property will override the
060: * value in the default velocity properties file.
061: */
062: private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR
063: + "/set";
064:
065: /**
066: * Results relative to the build directory.
067: */
068: private static final String RESULTS_DIR = TEST_RESULT_DIR + "/set";
069:
070: /**
071: * Results relative to the build directory.
072: */
073: private static final String COMPARE_DIR = TEST_COMPARE_DIR
074: + "/set/compare";
075:
076: /**
077: * Default constructor.
078: */
079: public SetTestCase(String name) {
080: super (name);
081: }
082:
083: public void setUp() throws Exception {
084: assureResultsDirectoryExists(RESULTS_DIR);
085: }
086:
087: public static Test suite() {
088: return new TestSuite(SetTestCase.class);
089: }
090:
091: /**
092: * Runs the test.
093: */
094: public void testSetNull() throws Exception {
095: /**
096: * Check that #set does not accept nulls
097: */
098:
099: VelocityEngine ve = new VelocityEngine();
100: ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
101: FILE_RESOURCE_LOADER_PATH);
102: ve.init();
103:
104: checkTemplate(ve, "set1");
105:
106: /**
107: * Check that setting the property is the same as the default
108: */
109: ve = new VelocityEngine();
110: ve.addProperty(RuntimeConstants.SET_NULL_ALLOWED, "false");
111: ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
112: FILE_RESOURCE_LOADER_PATH);
113: ve.init();
114:
115: checkTemplate(ve, "set1");
116:
117: /**
118: * Check that #set can accept nulls
119: */
120: ve = new VelocityEngine();
121: ve.addProperty(RuntimeConstants.SET_NULL_ALLOWED, "true");
122: ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
123: FILE_RESOURCE_LOADER_PATH);
124: ve.init();
125:
126: checkTemplate(ve, "set2");
127: }
128:
129: public void checkTemplate(VelocityEngine ve, String templateName)
130: throws Exception {
131: Template template;
132: FileOutputStream fos;
133: Writer fwriter;
134: Context context;
135:
136: template = ve.getTemplate(getFileName(null, templateName,
137: TMPL_FILE_EXT));
138:
139: fos = new FileOutputStream(getFileName(RESULTS_DIR,
140: templateName, RESULT_FILE_EXT));
141:
142: fwriter = new BufferedWriter(new OutputStreamWriter(fos));
143:
144: context = new VelocityContext();
145: template.merge(context, fwriter);
146: fwriter.flush();
147: fwriter.close();
148:
149: if (!isMatch(RESULTS_DIR, COMPARE_DIR, templateName,
150: RESULT_FILE_EXT, CMP_FILE_EXT)) {
151: fail("Output incorrect.");
152: }
153: }
154:
155: }
|