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.log.NullLogChute;
34
35
36
37
38
39
40
41
42 public class MultiLoaderTestCase extends BaseTestCase
43 {
44
45
46
47 private static final String TMPL_FILE_EXT = "vm";
48
49
50
51
52 private static final String CMP_FILE_EXT = "cmp";
53
54
55
56
57 private static final String RESULT_FILE_EXT = "res";
58
59
60
61
62 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/multiloader";
63
64
65
66
67
68 private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/multiloader";
69
70
71
72
73 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/multiloader/compare";
74
75
76
77
78 public MultiLoaderTestCase(String name)
79 {
80 super(name);
81 }
82
83 public void setUp()
84 throws Exception
85 {
86 assureResultsDirectoryExists(RESULTS_DIR);
87
88
89
90
91
92 Velocity.setProperty(Velocity.RESOURCE_LOADER, "file");
93
94 Velocity.setProperty(
95 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
96
97 Velocity.addProperty(Velocity.RESOURCE_LOADER, "classpath");
98
99 Velocity.addProperty(Velocity.RESOURCE_LOADER, "jar");
100
101
102
103
104
105 Velocity.setProperty(
106 "classpath." + Velocity.RESOURCE_LOADER + ".class",
107 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
108
109 Velocity.setProperty(
110 "classpath." + Velocity.RESOURCE_LOADER + ".cache", "false");
111
112 Velocity.setProperty(
113 "classpath." + Velocity.RESOURCE_LOADER + ".modificationCheckInterval",
114 "2");
115
116
117
118
119
120 Velocity.setProperty(
121 "jar." + Velocity.RESOURCE_LOADER + ".class",
122 "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
123
124 Velocity.setProperty( "jar." + Velocity.RESOURCE_LOADER + ".path",
125 "jar:file:" + FILE_RESOURCE_LOADER_PATH + "/test2.jar" );
126
127
128 Velocity.setProperty(
129 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
130
131 Velocity.init();
132 }
133
134 public static Test suite ()
135 {
136 return new TestSuite(MultiLoaderTestCase.class);
137 }
138
139
140
141
142 public void testMultiLoader ()
143 throws Exception
144 {
145
146
147
148 assureResultsDirectoryExists(RESULTS_DIR);
149
150
151
152
153 Template template1 = Velocity.getTemplate(
154 getFileName(null, "path1", TMPL_FILE_EXT));
155
156
157
158
159 Template template2 = Velocity.getTemplate("template/test1." + TMPL_FILE_EXT);
160
161
162
163
164 Template template3 = Velocity.getTemplate("template/test2." + TMPL_FILE_EXT);
165
166
167
168
169
170 FileOutputStream fos1 =
171 new FileOutputStream (
172 getFileName(RESULTS_DIR, "path1", RESULT_FILE_EXT));
173
174 FileOutputStream fos2 =
175 new FileOutputStream (
176 getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));
177
178 FileOutputStream fos3 =
179 new FileOutputStream (
180 getFileName(RESULTS_DIR, "test3", RESULT_FILE_EXT));
181
182 Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
183 Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
184 Writer writer3 = new BufferedWriter(new OutputStreamWriter(fos3));
185
186
187
188
189
190 VelocityContext context = new VelocityContext();
191
192 template1.merge(context, writer1);
193 writer1.flush();
194 writer1.close();
195
196 template2.merge(context, writer2);
197 writer2.flush();
198 writer2.close();
199
200 template3.merge(context, writer3);
201 writer3.flush();
202 writer3.close();
203
204 if (!isMatch(RESULTS_DIR,COMPARE_DIR,"path1",RESULT_FILE_EXT,CMP_FILE_EXT))
205 {
206 fail("Output incorrect for FileResourceLoader test.");
207 }
208
209 if (!isMatch(RESULTS_DIR,COMPARE_DIR,"test2",RESULT_FILE_EXT,CMP_FILE_EXT) )
210 {
211 fail("Output incorrect for ClasspathResourceLoader test.");
212 }
213
214 if( !isMatch(RESULTS_DIR,COMPARE_DIR,"test3",RESULT_FILE_EXT,CMP_FILE_EXT))
215 {
216 fail("Output incorrect for JarResourceLoader test.");
217 }
218 }
219 }