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
36
37
38
39
40
41
42
43 public class ClasspathResourceTestCase extends BaseTestCase
44 {
45
46
47
48 private static final String TMPL_FILE_EXT = "vm";
49
50
51
52
53 private static final String CMP_FILE_EXT = "cmp";
54
55
56
57
58 private static final String RESULT_FILE_EXT = "res";
59
60
61
62
63 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/cpload";
64
65
66
67
68 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/cpload/compare";
69
70
71
72
73 public ClasspathResourceTestCase(String name)
74 {
75 super(name);
76 }
77
78 public void setUp()
79 throws Exception
80 {
81 assureResultsDirectoryExists(RESULTS_DIR);
82
83 Velocity.setProperty(Velocity.RESOURCE_LOADER, "classpath");
84
85
86
87
88
89
90 Velocity.addProperty(
91 "classpath." + Velocity.RESOURCE_LOADER + ".class",
92 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
93
94 Velocity.setProperty(
95 "classpath." + Velocity.RESOURCE_LOADER + ".cache", "false");
96
97 Velocity.setProperty(
98 "classpath." + Velocity.RESOURCE_LOADER + ".modificationCheckInterval",
99 "2");
100
101 Velocity.setProperty(
102 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, NullLogChute.class.getName());
103
104 Velocity.init();
105 }
106
107 public static Test suite ()
108 {
109 return new TestSuite(ClasspathResourceTestCase.class);
110 }
111
112
113
114
115 public void testClasspathResource ()
116 throws Exception
117 {
118
119
120
121 assureResultsDirectoryExists(RESULTS_DIR);
122
123 Template template1 = RuntimeSingleton.getTemplate("template/test1." + TMPL_FILE_EXT);
124
125
126
127
128
129 FileOutputStream fos1 =
130 new FileOutputStream (
131 getFileName(RESULTS_DIR, "test1", RESULT_FILE_EXT));
132
133
134
135
136
137
138 Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
139
140
141
142
143
144
145
146 VelocityContext context = new VelocityContext();
147
148 template1.merge(context, writer1);
149 writer1.flush();
150 writer1.close();
151
152
153
154
155
156
157 if (!isMatch(RESULTS_DIR,COMPARE_DIR,"test1",RESULT_FILE_EXT,CMP_FILE_EXT)
158
159
160 )
161 {
162 fail("Output is incorrect!");
163 }
164 }
165 }