1 package org.apache.velocity.test; 2 3 18 19 import java.io.BufferedWriter ; 20 import java.io.FileOutputStream ; 21 import java.io.OutputStreamWriter ; 22 import java.io.Writer ; 23 24 import org.apache.velocity.Template; 25 import org.apache.velocity.app.Velocity; 26 import org.apache.velocity.VelocityContext; 27 import org.apache.velocity.runtime.RuntimeSingleton; 28 29 36 public class ClasspathResourceTest extends BaseTestCase 37 { 38 41 private static final String TMPL_FILE_EXT = "vm"; 42 43 46 private static final String CMP_FILE_EXT = "cmp"; 47 48 51 private static final String RESULT_FILE_EXT = "res"; 52 53 56 private static final String RESULTS_DIR = "../test/cpload/results"; 57 58 61 private static final String COMPARE_DIR = "../test/cpload/compare"; 62 63 66 public ClasspathResourceTest() 67 { 68 super("ClasspathResourceTest"); 69 70 try 71 { 72 assureResultsDirectoryExists(RESULTS_DIR); 73 74 Velocity.setProperty(Velocity.RESOURCE_LOADER, "classpath"); 75 76 80 81 Velocity.addProperty( 82 "classpath." + Velocity.RESOURCE_LOADER + ".class", 83 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 84 85 Velocity.setProperty( 86 "classpath." + Velocity.RESOURCE_LOADER + ".cache", "false"); 87 88 Velocity.setProperty( 89 "classpath." + Velocity.RESOURCE_LOADER + ".modificationCheckInterval", 90 "2"); 91 92 Velocity.init(); 93 } 94 catch (Exception e) 95 { 96 System.err.println("Cannot setup ClasspathResourceTest!"); 97 e.printStackTrace(); 98 System.exit(1); 99 } 100 } 101 102 public static junit.framework.Test suite () 103 { 104 return new ClasspathResourceTest(); 105 } 106 107 110 public void runTest () 111 { 112 try 113 { 114 117 assureResultsDirectoryExists(RESULTS_DIR); 118 119 Template template1 = RuntimeSingleton.getTemplate( 120 getFileName(null, "template/test1", TMPL_FILE_EXT)); 121 122 Template template2 = RuntimeSingleton.getTemplate( 123 getFileName(null, "template/test2", TMPL_FILE_EXT)); 124 125 FileOutputStream fos1 = 126 new FileOutputStream ( 127 getFileName(RESULTS_DIR, "test1", RESULT_FILE_EXT)); 128 129 FileOutputStream fos2 = 130 new FileOutputStream ( 131 getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT)); 132 133 Writer writer1 = new BufferedWriter (new OutputStreamWriter (fos1)); 134 Writer writer2 = new BufferedWriter (new OutputStreamWriter (fos2)); 135 136 139 140 VelocityContext context = new VelocityContext(); 141 142 template1.merge(context, writer1); 143 writer1.flush(); 144 writer1.close(); 145 146 template2.merge(context, writer2); 147 writer2.flush(); 148 writer2.close(); 149 150 if (!isMatch(RESULTS_DIR,COMPARE_DIR,"test1",RESULT_FILE_EXT,CMP_FILE_EXT) || 151 !isMatch(RESULTS_DIR,COMPARE_DIR,"test2",RESULT_FILE_EXT,CMP_FILE_EXT)) 152 { 153 fail("Output is incorrect!"); 154 } 155 } 156 catch (Exception e) 157 { 158 fail(e.getMessage()); 159 } 160 } 161 } 162 | Popular Tags |