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 import java.io.File ; 24 25 import java.util.Properties ; 26 27 import org.apache.velocity.Template; 28 import org.apache.velocity.app.Velocity; 29 import org.apache.velocity.VelocityContext; 30 import org.apache.velocity.test.provider.TestProvider; 31 import org.apache.velocity.util.StringUtils; 32 import org.apache.velocity.runtime.VelocimacroFactory; 33 34 import junit.framework.TestCase; 35 36 43 public class MultiLoaderTestCase extends BaseTestCase 44 { 45 48 private static final String TMPL_FILE_EXT = "vm"; 49 50 53 private static final String CMP_FILE_EXT = "cmp"; 54 55 58 private static final String RESULT_FILE_EXT = "res"; 59 60 63 private static final String RESULTS_DIR = "../test/multiloader/results"; 64 65 69 private final static String FILE_RESOURCE_LOADER_PATH = "../test/multiloader"; 70 71 74 private static final String COMPARE_DIR = "../test/multiloader/compare"; 75 76 79 public MultiLoaderTestCase() 80 { 81 super("MultiLoaderTestCase"); 82 83 try 84 { 85 assureResultsDirectoryExists(RESULTS_DIR); 86 87 90 91 Velocity.setProperty(Velocity.RESOURCE_LOADER, "file"); 92 93 Velocity.setProperty( 94 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH); 95 96 Velocity.addProperty(Velocity.RESOURCE_LOADER, "classpath"); 97 98 Velocity.addProperty(Velocity.RESOURCE_LOADER, "jar"); 99 100 103 104 Velocity.setProperty( 105 "classpath." + Velocity.RESOURCE_LOADER + ".class", 106 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 107 108 Velocity.setProperty( 109 "classpath." + Velocity.RESOURCE_LOADER + ".cache", "false"); 110 111 Velocity.setProperty( 112 "classpath." + Velocity.RESOURCE_LOADER + ".modificationCheckInterval", 113 "2"); 114 115 118 119 Velocity.setProperty( 120 "jar." + Velocity.RESOURCE_LOADER + ".class", 121 "org.apache.velocity.runtime.resource.loader.JarResourceLoader"); 122 123 Velocity.setProperty( "jar." + Velocity.RESOURCE_LOADER + ".path", 124 "jar:file:" + FILE_RESOURCE_LOADER_PATH + "/test2.jar" ); 125 126 Velocity.init(); 127 } 128 catch (Exception e) 129 { 130 System.err.println("Cannot setup MultiLoaderTestCase!"); 131 e.printStackTrace(); 132 System.exit(1); 133 } 134 } 135 136 public static junit.framework.Test suite () 137 { 138 return new MultiLoaderTestCase(); 139 } 140 141 144 public void runTest () 145 { 146 try 147 { 148 151 assureResultsDirectoryExists(RESULTS_DIR); 152 153 156 Template template1 = Velocity.getTemplate( 157 getFileName(null, "path1", TMPL_FILE_EXT)); 158 159 162 Template template2 = Velocity.getTemplate( 163 getFileName(null, "template/test1", TMPL_FILE_EXT)); 164 165 168 Template template3 = Velocity.getTemplate( 169 getFileName(null, "template/test2", TMPL_FILE_EXT)); 170 171 174 175 FileOutputStream fos1 = 176 new FileOutputStream ( 177 getFileName(RESULTS_DIR, "path1", RESULT_FILE_EXT)); 178 179 FileOutputStream fos2 = 180 new FileOutputStream ( 181 getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT)); 182 183 FileOutputStream fos3 = 184 new FileOutputStream ( 185 getFileName(RESULTS_DIR, "test3", RESULT_FILE_EXT)); 186 187 Writer writer1 = new BufferedWriter (new OutputStreamWriter (fos1)); 188 Writer writer2 = new BufferedWriter (new OutputStreamWriter (fos2)); 189 Writer writer3 = new BufferedWriter (new OutputStreamWriter (fos3)); 190 191 194 195 VelocityContext context = new VelocityContext(); 196 197 template1.merge(context, writer1); 198 writer1.flush(); 199 writer1.close(); 200 201 template2.merge(context, writer2); 202 writer2.flush(); 203 writer2.close(); 204 205 template3.merge(context, writer3); 206 writer3.flush(); 207 writer3.close(); 208 209 if (!isMatch(RESULTS_DIR,COMPARE_DIR,"path1",RESULT_FILE_EXT,CMP_FILE_EXT)) 210 { 211 fail("Output incorrect for FileResourceLoader test."); 212 } 213 214 if (!isMatch(RESULTS_DIR,COMPARE_DIR,"test2",RESULT_FILE_EXT,CMP_FILE_EXT) ) 215 { 216 fail("Output incorrect for ClasspathResourceLoader test."); 217 } 218 219 if( !isMatch(RESULTS_DIR,COMPARE_DIR,"test3",RESULT_FILE_EXT,CMP_FILE_EXT)) 220 { 221 fail("Output incorrect for JarResourceLoader test."); 222 } 223 } 224 catch (Exception e) 225 { 226 fail(e.getMessage()); 227 } 228 } 229 } 230 | Popular Tags |