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 java.util.Vector ; 25 26 import org.apache.velocity.VelocityContext; 27 28 import org.apache.velocity.Template; 29 import org.apache.velocity.app.Velocity; 30 import org.apache.velocity.runtime.RuntimeSingleton; 31 32 45 public class ContextSafetyTestCase extends BaseTestCase implements TemplateTestBase 46 { 47 public ContextSafetyTestCase() 48 { 49 super("ContextSafetyTestCase"); 50 51 try 52 { 53 Velocity.setProperty( 54 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH); 55 56 Velocity.init(); 57 } 58 catch (Exception e) 59 { 60 System.err.println("Cannot setup ContextSafetyTestCase!"); 61 e.printStackTrace(); 62 System.exit(1); 63 } 64 } 65 66 public static junit.framework.Test suite() 67 { 68 return new ContextSafetyTestCase(); 69 } 70 71 74 public void runTest () 75 { 76 80 Vector v = new Vector (); 81 82 v.addElement( new String ("vector hello 1") ); 83 v.addElement( new String ("vector hello 2") ); 84 v.addElement( new String ("vector hello 3") ); 85 86 String strArray[] = new String [3]; 87 88 strArray[0] = "array hello 1"; 89 strArray[1] = "array hello 2"; 90 strArray[2] = "array hello 3"; 91 92 VelocityContext context = new VelocityContext(); 93 94 try 95 { 96 assureResultsDirectoryExists(RESULT_DIR); 97 98 101 102 Template template = RuntimeSingleton.getTemplate( 103 getFileName(null, "context_safety", TMPL_FILE_EXT)); 104 105 FileOutputStream fos1 = 106 new FileOutputStream ( 107 getFileName(RESULT_DIR, "context_safety1", RESULT_FILE_EXT)); 108 109 FileOutputStream fos2 = 110 new FileOutputStream ( 111 getFileName(RESULT_DIR, "context_safety2", RESULT_FILE_EXT)); 112 113 Writer writer1 = new BufferedWriter (new OutputStreamWriter (fos1)); 114 Writer writer2 = new BufferedWriter (new OutputStreamWriter (fos2)); 115 116 119 120 context.put("vector", v); 121 template.merge(context, writer1); 122 writer1.flush(); 123 writer1.close(); 124 125 128 129 context.put("vector", strArray); 130 template.merge(context, writer2); 131 writer2.flush(); 132 writer2.close(); 133 134 if (!isMatch(RESULT_DIR,COMPARE_DIR,"context_safety1", 135 RESULT_FILE_EXT,CMP_FILE_EXT) || 136 !isMatch(RESULT_DIR,COMPARE_DIR,"context_safety2", 137 RESULT_FILE_EXT,CMP_FILE_EXT)) 138 { 139 fail("Output incorrect."); 140 } 141 } 142 catch (Exception e) 143 { 144 fail(e.getMessage()); 145 } 146 } 147 } 148 | Popular Tags |