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
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.apache.velocity.Template;
31 import org.apache.velocity.VelocityContext;
32 import org.apache.velocity.app.Velocity;
33 import org.apache.velocity.runtime.RuntimeSingleton;
34 import org.apache.velocity.runtime.log.NullLogChute;
35 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
36
37
38
39
40
41
42
43 public class StringResourceLoaderTestCase extends BaseTestCase
44 {
45
46
47
48 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/stringloader";
49
50
51
52
53 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/stringloader/compare";
54
55
56
57
58 public StringResourceLoaderTestCase(String name)
59 {
60 super(name);
61 }
62
63 public static Test suite()
64 {
65 return new TestSuite(StringResourceLoaderTestCase.class);
66 }
67
68 public void setUp()
69 throws Exception
70 {
71 assureResultsDirectoryExists(RESULTS_DIR);
72
73 Velocity.setProperty(Velocity.RESOURCE_LOADER, "string");
74 Velocity.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
75 Velocity.addProperty("string.resource.loader.modificationCheckInterval", "1");
76
77
78 Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
79
80 Velocity.init();
81 }
82
83 public void testSimpleTemplate()
84 throws Exception
85 {
86 StringResourceLoader.getRepository().putStringResource("simpletemplate.vm", "This is a test for ${foo}");
87
88 Template template = RuntimeSingleton.getTemplate(getFileName(null, "simpletemplate", TMPL_FILE_EXT));
89
90 FileOutputStream fos =
91 new FileOutputStream (
92 getFileName(RESULTS_DIR, "simpletemplate", RESULT_FILE_EXT));
93
94 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
95
96 VelocityContext context = new VelocityContext();
97 context.put("foo", "a foo object");
98
99 template.merge(context, writer);
100 writer.flush();
101 writer.close();
102
103 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "simpletemplate",
104 RESULT_FILE_EXT, CMP_FILE_EXT))
105 {
106 fail("Output incorrect.");
107 }
108 }
109
110 public void testMultipleTemplates()
111 throws Exception
112 {
113 StringResourceLoader.getRepository().putStringResource("multi1.vm", "I am the $first template.");
114 StringResourceLoader.getRepository().putStringResource("multi2.vm", "I am the $second template.");
115
116 Template template1 = RuntimeSingleton.getTemplate(getFileName(null, "multi1", TMPL_FILE_EXT));
117
118 FileOutputStream fos =
119 new FileOutputStream (
120 getFileName(RESULTS_DIR, "multi1", RESULT_FILE_EXT));
121
122 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
123
124 VelocityContext context = new VelocityContext();
125 context.put("first", new Integer(1));
126 context.put("second", "two");
127
128 template1.merge(context, writer);
129 writer.flush();
130 writer.close();
131
132 Template template2 = RuntimeSingleton.getTemplate(getFileName(null, "multi2", TMPL_FILE_EXT));
133
134 fos = new FileOutputStream (
135 getFileName(RESULTS_DIR, "multi2", RESULT_FILE_EXT));
136
137 writer = new BufferedWriter(new OutputStreamWriter(fos));
138
139 template2.merge(context, writer);
140 writer.flush();
141 writer.close();
142
143
144
145 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "multi1",
146 RESULT_FILE_EXT, CMP_FILE_EXT))
147 {
148 fail("Template 1 incorrect.");
149 }
150
151 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "multi2",
152 RESULT_FILE_EXT, CMP_FILE_EXT))
153 {
154 fail("Template 2 incorrect.");
155 }
156 }
157
158 public void testContentChange()
159 throws Exception
160 {
161 StringResourceLoader.getRepository().putStringResource("change.vm", "I am the $first template.");
162
163 Template template = RuntimeSingleton.getTemplate(getFileName(null, "change", TMPL_FILE_EXT));
164
165 FileOutputStream fos =
166 new FileOutputStream (
167 getFileName(RESULTS_DIR, "change1", RESULT_FILE_EXT));
168
169 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
170
171 VelocityContext context = new VelocityContext();
172 context.put("first", new Integer(1));
173 context.put("second", "two");
174
175 template.merge(context, writer);
176 writer.flush();
177 writer.close();
178
179 StringResourceLoader.getRepository().putStringResource("change.vm", "I am the $second template.");
180 Thread.sleep(2000L);
181 template = RuntimeSingleton.getTemplate(getFileName(null, "change", TMPL_FILE_EXT));
182
183 fos = new FileOutputStream (
184 getFileName(RESULTS_DIR, "change2", RESULT_FILE_EXT));
185
186 writer = new BufferedWriter(new OutputStreamWriter(fos));
187
188 template.merge(context, writer);
189 writer.flush();
190 writer.close();
191
192
193
194 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "change1",
195 RESULT_FILE_EXT, CMP_FILE_EXT))
196 {
197 fail("Template 1 incorrect.");
198 }
199
200 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "change2",
201 RESULT_FILE_EXT, CMP_FILE_EXT))
202 {
203 fail("Template 2 incorrect.");
204 }
205 }
206
207 }