1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedWriter;
23 import java.io.FileOutputStream;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26 import java.util.Vector;
27
28 import junit.framework.Test;
29 import junit.framework.TestSuite;
30
31 import org.apache.velocity.Template;
32 import org.apache.velocity.VelocityContext;
33 import org.apache.velocity.app.Velocity;
34 import org.apache.velocity.runtime.RuntimeSingleton;
35 import org.apache.velocity.runtime.log.NullLogChute;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class ContextSafetyTestCase extends BaseTestCase implements TemplateTestBase
51 {
52 public ContextSafetyTestCase(String name)
53 {
54 super(name);
55 }
56
57 public void setUp()
58 throws Exception
59 {
60 Velocity.setProperty(
61 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
62
63 Velocity.setProperty(
64 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
65
66 Velocity.init();
67 }
68
69 public static Test suite()
70 {
71 return new TestSuite(ContextSafetyTestCase.class);
72 }
73
74
75
76
77 public void testContextSafety ()
78 throws Exception
79 {
80
81
82
83
84 Vector v = new Vector();
85
86 v.addElement( new String("vector hello 1") );
87 v.addElement( new String("vector hello 2") );
88 v.addElement( new String("vector hello 3") );
89
90 String strArray[] = new String[3];
91
92 strArray[0] = "array hello 1";
93 strArray[1] = "array hello 2";
94 strArray[2] = "array hello 3";
95
96 VelocityContext context = new VelocityContext();
97
98 assureResultsDirectoryExists(RESULT_DIR);
99
100
101
102
103
104 Template template = RuntimeSingleton.getTemplate(
105 getFileName(null, "context_safety", TMPL_FILE_EXT));
106
107 FileOutputStream fos1 =
108 new FileOutputStream (
109 getFileName(RESULT_DIR, "context_safety1", RESULT_FILE_EXT));
110
111 FileOutputStream fos2 =
112 new FileOutputStream (
113 getFileName(RESULT_DIR, "context_safety2", RESULT_FILE_EXT));
114
115 Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
116 Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
117
118
119
120
121
122 context.put("vector", v);
123 template.merge(context, writer1);
124 writer1.flush();
125 writer1.close();
126
127
128
129
130
131 context.put("vector", strArray);
132 template.merge(context, writer2);
133 writer2.flush();
134 writer2.close();
135
136 if (!isMatch(RESULT_DIR,COMPARE_DIR,"context_safety1",
137 RESULT_FILE_EXT,CMP_FILE_EXT) ||
138 !isMatch(RESULT_DIR,COMPARE_DIR,"context_safety2",
139 RESULT_FILE_EXT,CMP_FILE_EXT))
140 {
141 fail("Output incorrect.");
142 }
143 }
144 }