1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
33
34
35
36
37
38 public abstract class BaseTestCase
39 extends TestCase
40 implements TemplateTestBase
41 {
42
43
44
45 private Perl5Util perl = new Perl5Util();
46
47
48
49
50 public BaseTestCase(String name)
51 {
52 super(name);
53 }
54
55
56
57
58
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
117
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
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
137 System.out.println(errMsg);
138 fail(errMsg);
139 }
140 }
141 }
142
143
144
145
146
147
148
149
150
151
152 protected String normalizeNewlines (String source)
153 {
154 return perl.substitute("s/\r[\r]?[\n]/\n/g", source);
155 }
156
157
158
159
160
161
162
163
164
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
188
189
190
191
192
193
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
207
208
209 return normalizeNewlines(result).equals(
210 normalizeNewlines( compare ) );
211 }
212
213
214
215
216
217
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 }