KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > BaseTestCase


1 package org.apache.velocity.test;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.File JavaDoc;
20 import org.apache.velocity.runtime.RuntimeSingleton;
21 import org.apache.velocity.util.StringUtils;
22
23 import junit.framework.TestCase;
24 import org.apache.oro.text.perl.Perl5Util;
25
26 /**
27  * Base test case that provides a few utility methods for
28  * the rest of the tests.
29  *
30  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
31  * @version $Id: BaseTestCase.java,v 1.12.4.1 2004/03/03 23:23:04 geirm Exp $
32  */

33 public class BaseTestCase extends TestCase
34 {
35     /**
36      * used for nomalization of output and compare data
37      */

38     private Perl5Util perl = new Perl5Util();
39
40     /**
41      * Default constructor.
42      */

43     public BaseTestCase(String JavaDoc name)
44     {
45         super(name);
46     }
47
48     /**
49      * Concatenates the file name parts together appropriately.
50      *
51      * @return The full path to the file.
52      */

53     protected static String JavaDoc getFileName (String JavaDoc dir, String JavaDoc base, String JavaDoc ext)
54     {
55         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
56         if (dir != null)
57         {
58             buf.append(dir).append('/');
59         }
60         buf.append(base).append('.').append(ext);
61         return buf.toString();
62     }
63
64     /**
65      * Assures that the results directory exists. If the results directory
66      * cannot be created, fails the test.
67      */

68     protected static void assureResultsDirectoryExists (String JavaDoc resultsDirectory)
69     {
70         File JavaDoc dir = new File JavaDoc(resultsDirectory);
71         if (!dir.exists())
72         {
73             RuntimeSingleton.info("Template results directory does not exist");
74             if (dir.mkdirs())
75             {
76                 RuntimeSingleton.info("Created template results directory");
77             }
78             else
79             {
80                 String JavaDoc errMsg = "Unable to create template results directory";
81                 RuntimeSingleton.warn(errMsg);
82                 fail(errMsg);
83             }
84         }
85     }
86
87
88     /**
89      * Normalizes lines to account for platform differences. Macs use
90      * a single \r, DOS derived operating systems use \r\n, and Unix
91      * uses \n. Replace each with a single \n.
92      *
93      * @author <a HREF="mailto:rubys@us.ibm.com">Sam Ruby</a>
94      * @return source with all line terminations changed to Unix style
95      */

96     protected String JavaDoc normalizeNewlines (String JavaDoc source)
97     {
98         return perl.substitute("s/\r[\n]/\n/g", source);
99     }
100
101     /**
102      * Returns whether the processed template matches the
103      * content of the provided comparison file.
104      *
105      * @return Whether the output matches the contents
106      * of the comparison file.
107      *
108      * @exception Exception Test failure condition.
109      */

110     protected boolean isMatch (String JavaDoc resultsDir,
111                                String JavaDoc compareDir,
112                                String JavaDoc baseFileName,
113                                String JavaDoc resultExt,
114                                String JavaDoc compareExt)
115         throws Exception JavaDoc
116     {
117         String JavaDoc result = StringUtils.fileContentsToString
118             (getFileName(resultsDir, baseFileName, resultExt));
119             
120         String JavaDoc compare = StringUtils.fileContentsToString
121              (getFileName(compareDir, baseFileName, compareExt));
122
123         /*
124          * normalize each wrt newline
125          */

126
127         return normalizeNewlines(result).equals(
128                            normalizeNewlines( compare ) );
129     }
130
131     /**
132      * Turns a base file name into a test case name.
133      *
134      * @param s The base file name.
135      * @return The test case name.
136      */

137     protected static final String JavaDoc getTestCaseName (String JavaDoc s)
138     {
139         StringBuffer JavaDoc name = new StringBuffer JavaDoc();
140         name.append(Character.toTitleCase(s.charAt(0)));
141         name.append(s.substring(1, s.length()).toLowerCase());
142         return name.toString();
143     }
144 }
145
Popular Tags