1 52 53 package freemarker.testcase; 54 55 import java.io.BufferedReader ; 56 import java.io.File ; 57 import java.io.FileOutputStream ; 58 import java.io.IOException ; 59 import java.io.InputStream ; 60 import java.io.InputStreamReader ; 61 import java.io.OutputStreamWriter ; 62 import java.io.Reader ; 63 import java.io.StringReader ; 64 import java.io.StringWriter ; 65 66 import freemarker.template.Configuration; 67 import freemarker.template.SimpleHash; 68 import freemarker.template.Template; 69 import freemarker.template.TemplateException; 70 import freemarker.template.utility.ClassUtil; 71 72 115 public abstract class AbstractTestCase extends junit.framework.TestCase { 116 117 protected Configuration config; 118 private static final int TEST_BUFFER_SIZE = 1024; 119 120 static { 121 try { 122 Class log4jbc = ClassUtil.forName("org.apache.log4j.BasicConfigurator"); 123 log4jbc.getMethod("configure", null).invoke(null, null); 124 } 125 catch(Exception e) { 126 throw new RuntimeException ("Failed to initialize test case class:" + e); 127 } 128 } 129 130 protected String referenceText; 131 protected String filename; 132 protected SimpleHash root = new SimpleHash(); 133 134 138 public AbstractTestCase( String aTestName ) { 139 super( aTestName ); 140 try { 141 config = new Configuration(); 142 config.setClassForTemplateLoading(AbstractTestCase.class, "/freemarker/testcase/template"); 143 config.setDefaultEncoding("UTF-8"); 144 } 145 catch(Exception e) { 146 throw new RuntimeException ("Failed to initialize test case intance:" + e); 147 } 148 } 149 150 public void runTest() throws TemplateException, IOException { 151 StringWriter sw = new StringWriter (); 152 Template template = config.getTemplate(filename); 153 template.process(root, sw); 154 showTestResults( referenceText, sw.toString() ); 155 } 156 157 165 protected void setUpFiles( String aFilename ) { 166 try { 167 referenceText = getReferenceText( aFilename ); 168 } catch( IOException e ) { 169 referenceText = ""; 170 } 171 filename = aFilename; 172 } 173 174 180 protected String getTextFromFile( String aFilename ) throws IOException { 181 Class thisClass = this.getClass(); 182 InputStream cStream; 183 Reader cReader; 184 StringBuffer cBuffer = new StringBuffer (); 185 char[] aBuffer = new char[ TEST_BUFFER_SIZE ]; 186 int nLength; 187 188 cStream = thisClass.getResourceAsStream( aFilename ); 189 190 if( cStream == null ) { 191 throw new IOException ( "Could not find stream " + aFilename ); 192 } 193 cReader = new InputStreamReader ( cStream, "UTF-8" ); 194 196 nLength = cReader.read( aBuffer ); 197 198 while( nLength > 0 ) { 199 cBuffer.append( aBuffer, 0, nLength ); 200 nLength = cReader.read( aBuffer ); 201 } 202 return cBuffer.toString(); 203 } 204 205 208 protected String getReferenceText( String aFilename ) throws IOException { 209 210 return getTextFromFile( "reference/" + aFilename ); 211 } 212 213 216 protected String getTemplateText( String aFilename ) throws IOException { 217 218 return getTextFromFile( "template/" + aFilename ); 219 } 220 221 225 protected void isTextIdentical( String aReference, String aOutput ) throws TestCaseException { 226 BufferedReader r1 = new BufferedReader (new StringReader (aReference)); 227 BufferedReader r2 = new BufferedReader (new StringReader (aOutput)); 228 int line = 0; 229 try 230 { 231 for(;;) 232 { 233 String l1 = r1.readLine(); 234 String l2 = r2.readLine(); 235 if(l1 == null) 236 { 237 if(l2 == null) 238 { 239 return; 240 } 241 throw new TestCaseException("Output text is " + (aOutput.length() - aReference.length()) + " characters longer than reference text."); 242 } 243 if(l2 == null) 244 { 245 throw new TestCaseException("Output text is " + (aReference.length() - aOutput.length()) + " characters shorter than reference text."); 246 } 247 ++line; 248 if(!l1.equals(l2)) { 249 int l = Math.min(l1.length(), l2.length()); 250 for(int i = 0; i < l; ++i) { 251 if(l1.charAt(i) != l2.charAt(i)) { 252 throw new TestCaseException( "Difference encountered at line " + 253 line + ", character " + (i + 1) + "." ); 254 } 255 } 256 throw new TestCaseException( "Difference encountered at line " + 257 line + ", character " + l + "." ); 258 } 259 } 260 } 261 catch(IOException e) 262 { 263 throw new Error (); 265 } 266 } 267 268 277 protected void showTestResults( String aReference, String aOutput ) { 278 279 try { 280 isTextIdentical( referenceText, aOutput ); 281 } catch( TestCaseException error ) { 282 try { 283 writeText( filename, aOutput ); 284 } catch( IOException writeException ) { 285 fail( writeException.getMessage() ); 286 } 287 assertTrue( error.getMessage(), false ); 288 } 289 } 290 291 295 protected void writeText( String aFilename, String aText ) throws IOException { 296 FileOutputStream cStream = new FileOutputStream ( aFilename ); 297 OutputStreamWriter cWriter = new OutputStreamWriter ( cStream, "UTF-8" ); 298 299 cWriter.write( aText ); 300 cWriter.flush(); 301 cWriter.close(); 302 cStream.close(); 303 } 304 305 310 protected String getTestcasePath() { 311 java.net.URL url = getClass().getResource("AbstractTestCase.class"); 312 return new File (url.getFile()).getParent(); 313 } 314 315 } 316 | Popular Tags |