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.File;
023: import java.io.IOException;
024:
025: import junit.framework.TestCase;
026:
027: import org.apache.oro.text.perl.Perl5Util;
028: import org.apache.velocity.runtime.RuntimeSingleton;
029: import org.apache.velocity.util.StringUtils;
030:
031: /**
032: * Base test case that provides a few utility methods for
033: * the rest of the tests.
034: *
035: * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
036: * @version $Id: BaseTestCase.java 490718 2006-12-28 13:35:49Z wglass $
037: */
038: public abstract class BaseTestCase extends TestCase implements
039: TemplateTestBase {
040: /**
041: * used for nomalization of output and compare data
042: */
043: private Perl5Util perl = new Perl5Util();
044:
045: /**
046: * Default constructor.
047: */
048: public BaseTestCase(String name) {
049: super (name);
050: }
051:
052: /**
053: * Concatenates the file name parts together appropriately.
054: *
055: * @return The full path to the file.
056: */
057: protected static String getFileName(final String dir,
058: final String base, final String ext) {
059: return getFileName(dir, base, ext, false);
060: }
061:
062: protected static String getFileName(final String dir,
063: final String base, final String ext, final boolean mustExist) {
064: StringBuffer buf = new StringBuffer();
065:
066: try {
067: File baseFile = new File(base);
068:
069: if (dir != null) {
070: if (!baseFile.isAbsolute()) {
071: baseFile = new File(dir, base);
072: }
073:
074: buf.append(baseFile.getCanonicalPath());
075: } else {
076: buf.append(baseFile.getPath());
077: }
078:
079: if (org.apache.commons.lang.StringUtils.isNotEmpty(ext)) {
080: buf.append('.').append(ext);
081: }
082:
083: if (mustExist) {
084: File testFile = new File(buf.toString());
085:
086: if (!testFile.exists()) {
087: fail("getFileName() result " + testFile.getPath()
088: + " does not exist!");
089: }
090:
091: if (!testFile.isFile()) {
092: fail("getFileName() result " + testFile.getPath()
093: + " is not a file!");
094: }
095: }
096: } catch (IOException e) {
097: fail("IO Exception while running getFileName(" + dir + ", "
098: + base + ", " + ext + ", " + mustExist + "): "
099: + e.getMessage());
100: }
101:
102: return buf.toString();
103: }
104:
105: /**
106: * Assures that the results directory exists. If the results directory
107: * cannot be created, fails the test.
108: */
109: protected static void assureResultsDirectoryExists(
110: String resultsDirectory) {
111: File dir = new File(resultsDirectory);
112: if (!dir.exists()) {
113: RuntimeSingleton.getLog().info(
114: "Template results directory does not exist");
115: if (dir.mkdirs()) {
116: RuntimeSingleton.getLog().info(
117: "Created template results directory");
118: } else {
119: String errMsg = "Unable to create template results directory";
120: RuntimeSingleton.getLog().warn(errMsg);
121: fail(errMsg);
122: }
123: }
124: }
125:
126: /**
127: * Normalizes lines to account for platform differences. Macs use
128: * a single \r, DOS derived operating systems use \r\n, and Unix
129: * uses \n. Replace each with a single \n.
130: *
131: * @author <a href="mailto:rubys@us.ibm.com">Sam Ruby</a>
132: * @return source with all line terminations changed to Unix style
133: */
134: protected String normalizeNewlines(String source) {
135: return perl.substitute("s/\r[\r]?[\n]/\n/g", source);
136: }
137:
138: /**
139: * Returns whether the processed template matches the
140: * content of the provided comparison file.
141: *
142: * @return Whether the output matches the contents
143: * of the comparison file.
144: *
145: * @exception Exception Test failure condition.
146: */
147: protected boolean isMatch(String resultsDir, String compareDir,
148: String baseFileName, String resultExt, String compareExt)
149: throws Exception {
150: String result = StringUtils.fileContentsToString(getFileName(
151: resultsDir, baseFileName, resultExt, true));
152:
153: return isMatch(result, compareDir, baseFileName, compareExt);
154: }
155:
156: /**
157: * Returns whether the processed template matches the
158: * content of the provided comparison file.
159: *
160: * @return Whether the output matches the contents
161: * of the comparison file.
162: *
163: * @exception Exception Test failure condition.
164: */
165: protected boolean isMatch(String result, String compareDir,
166: String baseFileName, String compareExt) throws Exception {
167: String compare = StringUtils.fileContentsToString(getFileName(
168: compareDir, baseFileName, compareExt, true));
169:
170: /*
171: * normalize each wrt newline
172: */
173:
174: return normalizeNewlines(result).equals(
175: normalizeNewlines(compare));
176: }
177:
178: /**
179: * Turns a base file name into a test case name.
180: *
181: * @param s The base file name.
182: * @return The test case name.
183: */
184: protected static final String getTestCaseName(String s) {
185: StringBuffer name = new StringBuffer();
186: name.append(Character.toTitleCase(s.charAt(0)));
187: name.append(s.substring(1, s.length()).toLowerCase());
188: return name.toString();
189: }
190: }
|