1 package org.apache.anakia.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
24
25 /**
26 * Miscellaneous routines to help with testing
27 *
28 * @version $Id: TestUtil.java 524478 2007-03-31 20:51:49Z wglass $
29 */
30 public class TestUtil
31 {
32
33 /**
34 * Do not instantiate.
35 */
36 private TestUtil()
37 {
38 }
39
40 /**
41 * Assures that the results directory exists. If the results directory
42 * cannot be created, fails the test.
43 */
44 public static void assureResultsDirectoryExists (String resultsDirectory)
45 {
46 File dir = new File(resultsDirectory);
47 if (!dir.exists())
48 {
49 Log.log("Template results directory does not exist");
50 if (dir.mkdirs())
51 {
52 Log.log("Created template results directory");
53 }
54 else
55 {
56 String errMsg = "Unable to create template results directory";
57 Log.log(errMsg);
58 throw new RuntimeException(errMsg);
59 }
60 }
61 }
62
63 /**
64 * Normalizes lines to account for platform differences. Macs use
65 * a single \r, DOS derived operating systems use \r\n, and Unix
66 * uses \n. Replace each with a single \n.
67 *
68 * @author <a href="mailto:rubys@us.ibm.com">Sam Ruby</a>
69 * @return source with all line terminations changed to Unix style
70 */
71 public static String normalizeNewlines (String source)
72 {
73 return source.replaceAll("\r[\r]?[\n]","\n");
74 }
75
76 /**
77 * Returns whether the processed template matches the
78 * content of the provided comparison file.
79 *
80 * @return Whether the output matches the contents
81 * of the comparison file.
82 *
83 * @exception Exception Test failure condition.
84 */
85 public static boolean compareFiles(String compareFileName, String resultsFileName)
86 throws Exception
87 {
88 Log.log("Comparing result file '" + resultsFileName + "' with compare file '" + compareFileName + "'");
89 String resultText = org.apache.velocity.util.StringUtils.fileContentsToString(resultsFileName);
90
91 return compareTextWithFile(resultText, compareFileName);
92 }
93
94
95 /**
96 * Returns whether the processed template matches the
97 * content of the provided comparison file.
98 *
99 * @return Whether the output matches the contents
100 * of the comparison file.
101 *
102 * @exception Exception Test failure condition.
103 */
104 public static boolean compareTextWithFile(String resultText, String compareFileName)
105 throws Exception
106 {
107 String compareText = org.apache.velocity.util.StringUtils.fileContentsToString(compareFileName);
108
109 /*
110 * normalize each wrt newline
111 */
112
113 String normalizedResultText = normalizeNewlines(resultText);
114 String normalizedCompareText = normalizeNewlines(compareText);
115 return normalizedResultText.equals( normalizedCompareText );
116 }
117
118
119 }