1 package org.apache.velocity.test;
2
3 /*
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
19 * under the License.
20 */
21
22 import java.io.File;
23 import java.io.IOException;
24
25 import junit.framework.TestCase;
26
27 import org.apache.oro.text.perl.Perl5Util;
28 import org.apache.velocity.runtime.RuntimeSingleton;
29 import org.apache.velocity.util.StringUtils;
30
31 /**
32 * Base test case that provides a few utility methods for
33 * the rest of the tests.
34 *
35 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
36 * @version $Id: BaseTestCase.java 689111 2008-08-26 15:27:06Z nbubna $
37 */
38 public abstract class BaseTestCase
39 extends TestCase
40 implements TemplateTestBase
41 {
42 /**
43 * used for nomalization of output and compare data
44 */
45 private Perl5Util perl = new Perl5Util();
46
47 /**
48 * Default constructor.
49 */
50 public BaseTestCase(String name)
51 {
52 super(name);
53 }
54
55 /**
56 * Concatenates the file name parts together appropriately.
57 *
58 * @return The full path to the file.
59 */
60 protected static String getFileName (final String dir, final String base, final String ext)
61 {
62 return getFileName(dir, base, ext, false);
63 }
64
65 protected static String getFileName (final String dir, final String base, final String ext, final boolean mustExist)
66 {
67 StringBuffer buf = new StringBuffer();
68
69 try
70 {
71 File baseFile = new File(base);
72
73 if (dir != null)
74 {
75 if (!baseFile.isAbsolute())
76 {
77 baseFile = new File(dir, base);
78 }
79
80 buf.append(baseFile.getCanonicalPath());
81 }
82 else
83 {
84 buf.append(baseFile.getPath());
85 }
86
87 if (org.apache.commons.lang.StringUtils.isNotEmpty(ext))
88 {
89 buf.append('.').append(ext);
90 }
91
92 if (mustExist)
93 {
94 File testFile = new File(buf.toString());
95
96 if (!testFile.exists())
97 {
98 fail("getFileName() result " + testFile.getPath() + " does not exist!");
99 }
100
101 if (!testFile.isFile())
102 {
103 fail("getFileName() result " + testFile.getPath() + " is not a file!");
104 }
105 }
106 }
107 catch (IOException e)
108 {
109 fail("IO Exception while running getFileName(" + dir + ", " + base + ", "+ ext + ", " + mustExist + "): " + e.getMessage());
110 }
111
112 return buf.toString();
113 }
114
115 /**
116 * Assures that the results directory exists. If the results directory
117 * cannot be created, fails the test.
118 */
119 protected static void assureResultsDirectoryExists (String resultsDirectory)
120 {
121 File dir = new File(resultsDirectory);
122 if (!dir.exists())
123 {
124 String msg = "Template results directory ("+resultsDirectory+")does not exist";
125 RuntimeSingleton.getLog().info(msg);
126 if (dir.mkdirs())
127 {
128 RuntimeSingleton.getLog().info("Created template results directory");
129 //caveman hack to get gump to give more info
130 System.out.println("Created template results directory: "+resultsDirectory);
131 }
132 else
133 {
134 String errMsg = "Unable to create template results directory";
135 RuntimeSingleton.getLog().warn(errMsg);
136 //caveman hack to get gump to give more info
137 System.out.println(errMsg);
138 fail(errMsg);
139 }
140 }
141 }
142
143
144 /**
145 * Normalizes lines to account for platform differences. Macs use
146 * a single \r, DOS derived operating systems use \r\n, and Unix
147 * uses \n. Replace each with a single \n.
148 *
149 * @author <a href="mailto:rubys@us.ibm.com">Sam Ruby</a>
150 * @return source with all line terminations changed to Unix style
151 */
152 protected String normalizeNewlines (String source)
153 {
154 return perl.substitute("s/\r[\r]?[\n]/\n/g", source);
155 }
156
157 /**
158 * Returns whether the processed template matches the
159 * content of the provided comparison file.
160 *
161 * @return Whether the output matches the contents
162 * of the comparison file.
163 *
164 * @exception Exception Test failure condition.
165 */
166 protected boolean isMatch (String resultsDir,
167 String compareDir,
168 String baseFileName,
169 String resultExt,
170 String compareExt)
171 throws Exception
172 {
173 String result = StringUtils.fileContentsToString
174 (getFileName(resultsDir, baseFileName, resultExt, true));
175
176 return isMatch(result,compareDir,baseFileName,compareExt);
177 }
178
179
180 protected String getFileContents(String dir, String baseFileName, String ext)
181 {
182 return StringUtils
183 .fileContentsToString(getFileName(dir, baseFileName, ext, true));
184 }
185
186 /**
187 * Returns whether the processed template matches the
188 * content of the provided comparison file.
189 *
190 * @return Whether the output matches the contents
191 * of the comparison file.
192 *
193 * @exception Exception Test failure condition.
194 */
195 protected boolean isMatch (
196 String result,
197 String compareDir,
198 String baseFileName,
199 String compareExt)
200 throws Exception
201 {
202 String compare = StringUtils.fileContentsToString
203 (getFileName(compareDir, baseFileName, compareExt, true));
204
205 /*
206 * normalize each wrt newline
207 */
208
209 return normalizeNewlines(result).equals(
210 normalizeNewlines( compare ) );
211 }
212
213 /**
214 * Turns a base file name into a test case name.
215 *
216 * @param s The base file name.
217 * @return The test case name.
218 */
219 protected static final String getTestCaseName (String s)
220 {
221 StringBuffer name = new StringBuffer();
222 name.append(Character.toTitleCase(s.charAt(0)));
223 name.append(s.substring(1, s.length()).toLowerCase());
224 return name.toString();
225 }
226 }