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.VelocityContext; 28 29 import org.apache.velocity.Template; 30 import org.apache.velocity.app.Velocity; 31 import org.apache.velocity.runtime.RuntimeSingleton; 32 import org.apache.velocity.test.provider.TestProvider; 33 import org.apache.velocity.util.StringUtils; 34 import org.apache.velocity.runtime.VelocimacroFactory; 35 36 import junit.framework.TestCase; 37 38 44 public class MultipleFileResourcePathTest extends BaseTestCase 45 { 46 49 private static final String TMPL_FILE_EXT = "vm"; 50 51 54 private static final String CMP_FILE_EXT = "cmp"; 55 56 59 private static final String RESULT_FILE_EXT = "res"; 60 61 65 private final static String FILE_RESOURCE_LOADER_PATH1 = "../test/multi/path1"; 66 67 71 private final static String FILE_RESOURCE_LOADER_PATH2 = "../test/multi/path2"; 72 73 76 private static final String RESULTS_DIR = "../test/multi/results"; 77 78 81 private static final String COMPARE_DIR = "../test/multi/compare"; 82 83 86 MultipleFileResourcePathTest() 87 { 88 super("MultipleFileResourcePathTest"); 89 90 try 91 { 92 assureResultsDirectoryExists(RESULTS_DIR); 93 94 Velocity.addProperty( 95 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH1); 96 97 Velocity.addProperty( 98 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH2); 99 100 Velocity.init(); 101 } 102 catch (Exception e) 103 { 104 System.err.println("Cannot setup MultipleFileResourcePathTest!"); 105 e.printStackTrace(); 106 System.exit(1); 107 } 108 } 109 110 public static junit.framework.Test suite () 111 { 112 return new MultipleFileResourcePathTest(); 113 } 114 115 118 public void runTest () 119 { 120 try 121 { 122 Template template1 = RuntimeSingleton.getTemplate( 123 getFileName(null, "path1", TMPL_FILE_EXT)); 124 125 Template template2 = RuntimeSingleton.getTemplate( 126 getFileName(null, "path2", TMPL_FILE_EXT)); 127 128 FileOutputStream fos1 = 129 new FileOutputStream ( 130 getFileName(RESULTS_DIR, "path1", RESULT_FILE_EXT)); 131 132 FileOutputStream fos2 = 133 new FileOutputStream ( 134 getFileName(RESULTS_DIR, "path2", RESULT_FILE_EXT)); 135 136 Writer writer1 = new BufferedWriter (new OutputStreamWriter (fos1)); 137 Writer writer2 = new BufferedWriter (new OutputStreamWriter (fos2)); 138 139 142 143 VelocityContext context = new VelocityContext(); 144 145 template1.merge(context, writer1); 146 writer1.flush(); 147 writer1.close(); 148 149 template2.merge(context, writer2); 150 writer2.flush(); 151 writer2.close(); 152 153 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "path1", 154 RESULT_FILE_EXT, CMP_FILE_EXT) || 155 !isMatch(RESULTS_DIR, COMPARE_DIR, "path2", 156 RESULT_FILE_EXT, CMP_FILE_EXT)) 157 { 158 fail("Output incorrect."); 159 } 160 } 161 catch (Exception e) 162 { 163 fail(e.getMessage()); 164 } 165 } 166 } 167 | Popular Tags |