001: package org.apache.anakia.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:
024: /**
025: * Miscellaneous routines to help with testing
026: *
027: * @version $Id: TestUtil.java 524478 2007-03-31 20:51:49Z wglass $
028: */
029: public class TestUtil {
030:
031: /**
032: * Do not instantiate.
033: */
034: private TestUtil() {
035: }
036:
037: /**
038: * Assures that the results directory exists. If the results directory
039: * cannot be created, fails the test.
040: */
041: public static void assureResultsDirectoryExists(
042: String resultsDirectory) {
043: File dir = new File(resultsDirectory);
044: if (!dir.exists()) {
045: Log.log("Template results directory does not exist");
046: if (dir.mkdirs()) {
047: Log.log("Created template results directory");
048: } else {
049: String errMsg = "Unable to create template results directory";
050: Log.log(errMsg);
051: throw new RuntimeException(errMsg);
052: }
053: }
054: }
055:
056: /**
057: * Normalizes lines to account for platform differences. Macs use
058: * a single \r, DOS derived operating systems use \r\n, and Unix
059: * uses \n. Replace each with a single \n.
060: *
061: * @author <a href="mailto:rubys@us.ibm.com">Sam Ruby</a>
062: * @return source with all line terminations changed to Unix style
063: */
064: public static String normalizeNewlines(String source) {
065: return source.replaceAll("\r[\r]?[\n]", "\n");
066: }
067:
068: /**
069: * Returns whether the processed template matches the
070: * content of the provided comparison file.
071: *
072: * @return Whether the output matches the contents
073: * of the comparison file.
074: *
075: * @exception Exception Test failure condition.
076: */
077: public static boolean compareFiles(String compareFileName,
078: String resultsFileName) throws Exception {
079: Log.log("Comparing result file '" + resultsFileName
080: + "' with compare file '" + compareFileName + "'");
081: String resultText = org.apache.velocity.util.StringUtils
082: .fileContentsToString(resultsFileName);
083:
084: return compareTextWithFile(resultText, compareFileName);
085: }
086:
087: /**
088: * Returns whether the processed template matches the
089: * content of the provided comparison file.
090: *
091: * @return Whether the output matches the contents
092: * of the comparison file.
093: *
094: * @exception Exception Test failure condition.
095: */
096: public static boolean compareTextWithFile(String resultText,
097: String compareFileName) throws Exception {
098: String compareText = org.apache.velocity.util.StringUtils
099: .fileContentsToString(compareFileName);
100:
101: /*
102: * normalize each wrt newline
103: */
104:
105: String normalizedResultText = normalizeNewlines(resultText);
106: String normalizedCompareText = normalizeNewlines(compareText);
107: return normalizedResultText.equals(normalizedCompareText);
108: }
109:
110: }
|