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