1 package org.apache.velocity.test; 2 3 18 19 import java.io.BufferedWriter ; 20 import java.io.File ; 21 import java.io.FileOutputStream ; 22 import java.io.OutputStreamWriter ; 23 import java.io.Writer ; 24 25 import java.util.ArrayList ; 26 import java.util.Hashtable ; 27 import java.util.HashMap ; 28 import java.util.Vector ; 29 30 import org.apache.velocity.VelocityContext; 31 import org.apache.velocity.Template; 32 import org.apache.velocity.runtime.RuntimeSingleton; 33 import org.apache.velocity.test.provider.TestProvider; 34 import org.apache.velocity.test.provider.BoolObj; 35 import org.apache.velocity.util.StringUtils; 36 37 import org.apache.velocity.app.FieldMethodizer; 38 39 import junit.framework.TestCase; 40 41 67 public class TemplateTestCase extends BaseTestCase implements TemplateTestBase 68 { 69 73 protected String baseFileName; 74 75 private TestProvider provider; 76 private ArrayList al; 77 private Hashtable h; 78 private VelocityContext context; 79 private VelocityContext context1; 80 private VelocityContext context2; 81 private Vector vec; 82 83 89 public TemplateTestCase (String baseFileName) 90 { 91 super(getTestCaseName(baseFileName)); 92 this.baseFileName = baseFileName; 93 } 94 95 public static junit.framework.Test suite() 96 { 97 return new TemplateTestSuite(); 98 } 99 100 103 protected void setUp () 104 { 105 provider = new TestProvider(); 106 al = provider.getCustomers(); 107 h = new Hashtable (); 108 109 h.put("Bar", "this is from a hashtable!"); 110 h.put("Foo", "this is from a hashtable too!"); 111 112 115 116 vec = new Vector (); 117 118 vec.addElement(new String ("string1")); 119 vec.addElement(new String ("string2")); 120 121 125 126 context2 = new VelocityContext(); 127 context1 = new VelocityContext( context2 ); 128 context = new VelocityContext( context1 ); 129 130 context.put("provider", provider); 131 context1.put("name", "jason"); 132 context2.put("providers", provider.getCustomers2()); 133 context.put("list", al); 134 context1.put("hashtable", h); 135 context2.put("hashmap", new HashMap ()); 136 context2.put("search", provider.getSearch()); 137 context.put("relatedSearches", provider.getRelSearches()); 138 context1.put("searchResults", provider.getRelSearches()); 139 context2.put("stringarray", provider.getArray()); 140 context.put("vector", vec ); 141 context.put("mystring", new String ()); 142 context.put("runtime", new FieldMethodizer( "org.apache.velocity.runtime.RuntimeSingleton" )); 143 context.put("fmprov", new FieldMethodizer( provider )); 144 context.put("Floog", "floogie woogie"); 145 context.put("boolobj", new BoolObj() ); 146 147 151 152 Object [] oarr = { "a","b","c","d" } ; 153 int intarr[] = { 10, 20, 30, 40, 50 }; 154 155 context.put( "collection", vec ); 156 context2.put("iterator", vec.iterator()); 157 context1.put("map", h ); 158 context.put("obarr", oarr ); 159 context.put("enumerator", vec.elements()); 160 context.put("intarr", intarr ); 161 } 162 163 166 public void runTest () 167 { 168 try 169 { 170 Template template = RuntimeSingleton.getTemplate 171 (getFileName(null, baseFileName, TMPL_FILE_EXT)); 172 173 assureResultsDirectoryExists(RESULT_DIR); 174 175 176 FileOutputStream fos = 177 new FileOutputStream (getFileName( 178 RESULT_DIR, baseFileName, RESULT_FILE_EXT)); 179 180 Writer writer = new BufferedWriter (new OutputStreamWriter (fos)); 181 182 183 template.merge( context, writer); 184 185 186 writer.flush(); 187 writer.close(); 188 189 if (!isMatch(RESULT_DIR,COMPARE_DIR,baseFileName, 190 RESULT_FILE_EXT,CMP_FILE_EXT)) 191 { 192 fail("Processed template did not match expected output"); 193 } 194 } 195 catch (Exception e) 196 { 197 System.out.println("EXCEPTION : " + e ); 198 199 fail(e.getMessage()); 200 } 201 } 202 } 203 | Popular Tags |