1 package org.apache.velocity.test; 2 3 18 19 import java.io.File ; 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 33 public class BaseTestCase extends TestCase 34 { 35 38 private Perl5Util perl = new Perl5Util(); 39 40 43 public BaseTestCase(String name) 44 { 45 super(name); 46 } 47 48 53 protected static String getFileName (String dir, String base, String ext) 54 { 55 StringBuffer buf = new StringBuffer (); 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 68 protected static void assureResultsDirectoryExists (String resultsDirectory) 69 { 70 File dir = new File (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 errMsg = "Unable to create template results directory"; 81 RuntimeSingleton.warn(errMsg); 82 fail(errMsg); 83 } 84 } 85 } 86 87 88 96 protected String normalizeNewlines (String source) 97 { 98 return perl.substitute("s/\r[\n]/\n/g", source); 99 } 100 101 110 protected boolean isMatch (String resultsDir, 111 String compareDir, 112 String baseFileName, 113 String resultExt, 114 String compareExt) 115 throws Exception 116 { 117 String result = StringUtils.fileContentsToString 118 (getFileName(resultsDir, baseFileName, resultExt)); 119 120 String compare = StringUtils.fileContentsToString 121 (getFileName(compareDir, baseFileName, compareExt)); 122 123 126 127 return normalizeNewlines(result).equals( 128 normalizeNewlines( compare ) ); 129 } 130 131 137 protected static final String getTestCaseName (String s) 138 { 139 StringBuffer name = new StringBuffer (); 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 |