1
2
3
4
5
6
7
8 /
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public class VelocityTemplatingTest extends LedgeTestCase
69 {
70 private Templating templating;
71
72 protected void setUp() throws Exception
73 {
74 super.setUp();
75 FileSystemProvider lfs = new LocalFileSystemProvider("local", "src/test/resources");
76 FileSystemProvider cfs = new ClasspathFileSystemProvider("classpath",
77 getClass().getClassLoader());
78 FileSystem fs = new FileSystem(new FileSystemProvider[] { lfs, cfs }, 4096, 4096);
79 try
80 {
81 InputSource source = new InputSource(fs.getInputStream(
82 "config/org.objectledge.logging.LoggingConfigurator.xml"));
83 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
84 Document logConfig = builder.parse(source);
85 LedgeDOMConfigurator configurator = new LedgeDOMConfigurator(fs);
86 configurator.doConfigure(logConfig.getDocumentElement(),
87 LogManager.getLoggerRepository());
88
89 Configuration config = getConfig(fs,
90 "config/org.objectledge.templating.velocity.VelocityTemplating.xml");
91
92 Logger logger = Logger.getLogger(VelocityTemplating.class);
93 templating = new VelocityTemplating(config, new Log4JLogger(logger), fs);
94 }
95 catch(Exception e)
96 {
97 throw new RuntimeException(e);
98 }
99 }
100
101 /***
102 * CreateContext method test.
103 */
104 public void testCreateContext()
105 {
106 assertNotNull(templating);
107 TemplatingContext context = templating.createContext();
108 assertNotNull(context);
109 context.put("foo","bar");
110 assertEquals("bar",context.get("foo"));
111 assertTrue(context.containsKey("foo"));
112 assertEquals(1, context.getKeys().length);
113 context.remove("foo");
114 assertNull(context.get("foo"));
115 assertEquals(0, context.getKeys().length);
116 }
117
118 /***
119 * CreateContext method test.
120 */
121 public void testTemplateExists()
122 {
123 assertEquals(templating.templateExists("foo"), false);
124 assertEquals(templating.templateExists("bar"), true);
125 }
126
127 /***
128 * CreateContext method test.
129 */
130 public void testGetTemplate()
131 {
132 try
133 {
134 assertNotNull(templating.getTemplate("bar"));
135
136 assertNotNull(templating.getTemplate("bar"));
137 }
138 catch(TemplateNotFoundException e)
139 {
140 fail(e.getMessage());
141 }
142 try
143 {
144 templating.getTemplate("foo");
145 fail("Should throw TemplateNotFoundException");
146 }
147 catch(TemplateNotFoundException e)
148 {
149
150 }
151 }
152
153 /***
154 * CreateContext method test.
155 */
156 public void testEvaluate()
157 {
158 try
159 {
160 TemplatingContext context = templating.createContext();
161 context.put("foo","bar");
162 StringWriter target = new StringWriter();
163 StringReader source = new StringReader("foo $foo");
164 templating.merge(context,source,target,"test");
165 assertEquals("foo bar",target.toString());
166 }
167 catch(MergingException e)
168 {
169 fail(e.getMessage());
170 }
171 }
172
173 /***
174 * CreateContext method test.
175 */
176 public void testMerge()
177 {
178 try
179 {
180 Template template = templating.getTemplate("bar");
181 TemplatingContext context = templating.createContext();
182 context.put("foo","bar");
183 assertEquals("foo bar",template.merge(context));
184 }
185 catch(TemplateNotFoundException e)
186 {
187 fail(e.getMessage());
188 }
189 catch(MergingException e)
190 {
191 fail(e.getMessage());
192 }
193 }
194
195 /***
196 * CreateContext method test.
197 */
198 public void testGetTemplateEncoding()
199 {
200 assertEquals("UTF-8", templating.getTemplateEncoding());
201 }
202
203 public void testPipelineComponents()
204 {
205 try
206 {
207 Context context = new Context();
208 Valve[] valves = new Valve[3];
209 ContextTools contextTools = new ContextTools(new ContextToolFactory[0]);
210 valves[0] = new TemplatingContextLoaderValve(templating);
211 valves[1] = new ContextToolPopulatorValve(contextTools);
212 valves[2] = new ContextToolRecyclerValve(contextTools);
213
214 Valve pipe = new SimplePipeline(valves);
215 pipe.process(context);
216 }
217 catch(Exception e)
218 {
219 fail(e.getMessage());
220 }
221 }
222 }