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.StringWriter;
23
24 import junit.framework.TestCase;
25
26 import org.apache.velocity.Template;
27 import org.apache.velocity.VelocityContext;
28 import org.apache.velocity.app.Velocity;
29 import org.apache.velocity.app.VelocityEngine;
30 import org.apache.velocity.runtime.RuntimeSingleton;
31 import org.apache.velocity.runtime.log.SystemLogChute;
32 import org.apache.velocity.runtime.resource.loader.StringResourceLoader;
33 import org.apache.velocity.runtime.resource.util.StringResourceRepository;
34 import org.apache.velocity.runtime.resource.util.StringResourceRepositoryImpl;
35
36
37
38
39
40
41
42 public class StringResourceLoaderRepositoryTestCase extends TestCase
43 {
44 private VelocityContext context;
45
46 public StringResourceLoaderRepositoryTestCase(String name)
47 {
48 super(name);
49 }
50
51 public void setUp() throws Exception
52 {
53 Velocity.setProperty(Velocity.RESOURCE_LOADER, "string");
54 Velocity.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
55 Velocity.addProperty("string.resource.loader.modificationCheckInterval", "1");
56 Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, SystemLogChute.class.getName());
57 Velocity.init();
58
59 StringResourceRepository repo = getRepo(null, null);
60
61
62
63 if (repo != null)
64 {
65 repo.putStringResource("foo", "This is $foo");
66 repo.putStringResource("bar", "This is $bar");
67 }
68
69 context = new VelocityContext();
70 context.put("foo", "wonderful!");
71 context.put("bar", "horrible!");
72 context.put("woogie", "a woogie");
73 }
74
75
76 protected VelocityEngine newStringEngine(String repoName, boolean isStatic)
77 {
78 VelocityEngine engine = new VelocityEngine();
79 engine.setProperty(Velocity.RESOURCE_LOADER, "string");
80 engine.addProperty("string.resource.loader.class", StringResourceLoader.class.getName());
81 if (repoName != null)
82 {
83 engine.addProperty("string.resource.loader.repository.name", repoName);
84 }
85 if (!isStatic)
86 {
87 engine.addProperty("string.resource.loader.repository.static", "false");
88 }
89 engine.addProperty("string.resource.loader.modificationCheckInterval", "1");
90 engine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, SystemLogChute.class.getName());
91 return engine;
92 }
93
94 protected StringResourceRepository getRepo(String name, VelocityEngine engine)
95 {
96 if (engine == null)
97 {
98 if (name == null)
99 {
100 return StringResourceLoader.getRepository();
101 }
102 else
103 {
104 return StringResourceLoader.getRepository(name);
105 }
106 }
107 else
108 {
109 if (name == null)
110 {
111 return (StringResourceRepository)engine.getApplicationAttribute(StringResourceLoader.REPOSITORY_NAME_DEFAULT);
112 }
113 else
114 {
115 return (StringResourceRepository)engine.getApplicationAttribute(name);
116 }
117 }
118 }
119
120 protected String render(Template template) throws Exception
121 {
122 StringWriter out = new StringWriter();
123 template.merge(this.context, out);
124 return out.toString();
125 }
126
127
128 public void testSharedRepo() throws Exception
129 {
130
131
132 VelocityEngine engine = newStringEngine(null, true);
133
134
135 String engineOut = render(engine.getTemplate("foo"));
136 String singletonOut = render(RuntimeSingleton.getTemplate("foo"));
137
138
139 assertEquals(engineOut, singletonOut);
140 }
141
142 public void testAlternateStaticRepo() throws Exception
143 {
144 VelocityEngine engine = newStringEngine("alternate.repo", true);
145
146 StringResourceRepository repo = getRepo("alternate.repo", null);
147 assertNull(repo);
148 engine.init();
149
150 repo = getRepo("alternate.repo", null);
151 assertNotNull(repo);
152 repo.putStringResource("foo", "This is NOT $foo");
153
154
155 String engineOut = render(engine.getTemplate("foo"));
156 String singletonOut = render(RuntimeSingleton.getTemplate("foo"));
157
158
159 assertFalse(engineOut.equals(singletonOut));
160 }
161
162 public void testPreCreatedStaticRepo() throws Exception
163 {
164 VelocityEngine engine = newStringEngine("my.repo", true);
165 MyRepo repo = new MyRepo();
166 repo.put("bar", "This is NOT $bar");
167 StringResourceLoader.setRepository("my.repo", repo);
168
169 String out = render(engine.getTemplate("bar"));
170 assertEquals(out, "This is NOT horrible!");
171 }
172
173 public void testAppRepo() throws Exception
174 {
175 VelocityEngine engine = newStringEngine(null, false);
176 engine.init();
177
178 StringResourceRepository repo = getRepo(null, engine);
179 assertNotNull(repo);
180 repo.putStringResource("woogie", "What is $woogie?");
181
182 String out = render(engine.getTemplate("woogie"));
183 assertEquals(out, "What is a woogie?");
184 }
185
186 public void testAlternateAppRepo() throws Exception
187 {
188 VelocityEngine engine = newStringEngine("alternate.app.repo", false);
189 engine.init();
190
191 StringResourceRepository repo = getRepo("alternate.app.repo", engine);
192 assertNotNull(repo);
193 repo.putStringResource("you/foo.vm", "You look $foo");
194
195 String out = render(engine.getTemplate("you/foo.vm"));
196 assertEquals(out, "You look wonderful!");
197 }
198
199 public void testPreCreatedAppRepo() throws Exception
200 {
201 VelocityEngine engine = newStringEngine("my.app.repo", false);
202 MyRepo repo = new MyRepo();
203 repo.put("you/bar.vm", "You look $bar");
204 engine.setApplicationAttribute("my.app.repo", repo);
205
206 String out = render(engine.getTemplate("you/bar.vm"));
207 assertEquals(out, "You look horrible!");
208 }
209
210 public static class MyRepo extends StringResourceRepositoryImpl
211 {
212 public void put(String name, String template)
213 {
214 putStringResource(name, template);
215 }
216 }
217
218 }