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 package info.magnolia.templating.jsp.cmsfn;
35
36
37 import static org.hamcrest.CoreMatchers.containsString;
38 import static org.junit.Assert.assertThat;
39 import static org.mockito.Mockito.mock;
40
41 import info.magnolia.cms.core.AggregationState;
42 import info.magnolia.cms.i18n.DefaultI18nContentSupport;
43 import info.magnolia.cms.i18n.I18nContentSupport;
44 import info.magnolia.context.MgnlContext;
45 import info.magnolia.jcr.util.ContentMap;
46 import info.magnolia.objectfactory.guice.GuiceUtils;
47 import info.magnolia.rendering.template.type.TemplateTypeHelper;
48 import info.magnolia.templating.functions.SiblingsHelper;
49 import info.magnolia.templating.functions.TemplatingFunctions;
50 import info.magnolia.templating.jsp.AbstractTagTestCase;
51 import info.magnolia.test.ComponentsTestUtil;
52
53 import javax.inject.Provider;
54
55 import org.junit.Before;
56 import org.junit.Test;
57
58 import com.meterware.httpunit.GetMethodWebRequest;
59 import com.meterware.httpunit.WebRequest;
60 import com.meterware.httpunit.WebResponse;
61
62
63
64
65
66 public class JspTemplatingFunctionTagTest extends AbstractTagTestCase {
67
68 public WebResponse response;
69
70 @Override
71 @Before
72 public void setUp() throws Exception {
73 super.setUp();
74
75 Provider<AggregationState> aggregationProvider = new Provider<AggregationState>() {
76 @Override
77 public AggregationState get() {
78 return MgnlContext.getAggregationState();
79 }
80 };
81
82 final I18nContentSupport i18nContentSupport = new DefaultI18nContentSupport();
83 final TemplatingFunctions templatingFunctions = new TemplatingFunctions(aggregationProvider, mock(TemplateTypeHelper.class), GuiceUtils.providerForInstance(i18nContentSupport));
84
85 ComponentsTestUtil.setInstance(TemplatingFunctions.class, templatingFunctions);
86 ComponentsTestUtil.setInstance(I18nContentSupport.class, i18nContentSupport);
87 }
88
89 private WebRequest initTestCase(String methodToTest, String nodePath) throws Exception {
90 final String jspPath = getClass().getName().replace('.', '/') + methodToTest + ".jsp";
91 final String jspUrl = "http://localhost" + CONTEXT + "/" + jspPath;
92
93 final WebRequest request = new GetMethodWebRequest(jspUrl);
94 ContentMap content;
95 if (nodePath == null) {
96 content = new ContentMap(getSession().getNode("/foo/bar/paragraphs/1"));
97 } else {
98 content = new ContentMap(getSession().getNode(nodePath));
99 }
100
101 runner.getSession(true).getServletContext().setAttribute("content", content);
102 return request;
103 }
104
105
106 @Test
107 public void testRoot() throws Exception {
108
109 WebRequest request = initTestCase("Root", null);
110
111
112 response = runner.getResponse(request);
113
114
115 String responseStr = response.getText();
116
117 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar/paragraphs\n</div>"));
118
119 assertThat(responseStr, containsString("<div id=\"2\">\n res=/\n</div>"));
120
121 assertThat(responseStr, containsString("<div id=\"3\">\n res=/\n</div>"));
122 }
123
124
125 @Test
126 public void testAncestors() throws Exception {
127
128 WebRequest request = initTestCase("Ancestors", "/foo/bar");
129
130
131 response = runner.getResponse(request);
132
133
134 String responseStr = response.getText();
135
136 assertThat(responseStr, containsString("<div id=\"1\">\n res=\n</div>"));
137
138 assertThat(responseStr, containsString("<div id=\"2\">\n res=/foo\n</div>"));
139
140 assertThat(responseStr, containsString("<div id=\"3\">\n res=/foo\n</div>"));
141 }
142
143
144 @Test
145 public void testAsJCRNode() throws Exception {
146
147 WebRequest request = initTestCase("AsJCRNode", null);
148
149
150 response = runner.getResponse(request);
151
152
153 String responseStr = response.getText();
154
155 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar/paragraphs/1\n</div>"));
156 }
157
158
159 @Test
160 public void testChildren() throws Exception {
161
162 WebRequest request = initTestCase("Children", "/foo/bar");
163
164
165 response = runner.getResponse(request);
166
167
168 String responseStr = response.getText();
169
170 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar/paragraphs\n</div>"));
171
172 assertThat(responseStr, containsString("<div id=\"2\">\n res=/foo/bar/paragraphs\n</div>"));
173
174 assertThat(responseStr, containsString("<div id=\"3\">\n res=/foo/bar/paragraphs\n</div>"));
175 }
176
177 @Test
178 public void testContent() throws Exception {
179
180 WebRequest request = initTestCase("Content", null);
181
182
183 response = runner.getResponse(request);
184
185
186 String responseStr = response.getText();
187
188 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar/paragraphs\n</div>"));
189
190 assertThat(responseStr, containsString("<div id=\"2\">\n res=/foo/bar/paragraphs\n</div>"));
191
192 assertThat(responseStr, containsString("<div id=\"3\">\n res=/foo/bar/paragraphs\n</div>"));
193 }
194
195 @Test
196 public void testCreateHtmlAttribute() throws Exception {
197
198 WebRequest request = initTestCase("CreateHtmlAttribute", null);
199
200
201 response = runner.getResponse(request);
202
203
204 String responseStr = response.getText();
205
206 assertThat(responseStr, containsString("<div id=\"1\">\n res=a=\"b\"\n</div>"));
207 }
208
209 @Test
210 public void testDecode() throws Exception {
211
212 WebRequest request = initTestCase("Decode", null);
213
214
215 response = runner.getResponse(request);
216
217
218 String responseStr = response.getText();
219
220 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar/paragraphs/1\n</div>"));
221 }
222
223 @Test
224 public void testExternalLink() throws Exception {
225
226 WebRequest request = initTestCase("ExternalLink", null);
227
228
229 response = runner.getResponse(request);
230
231
232 String responseStr = response.getText();
233
234 assertThat(responseStr, containsString("<div id=\"1\">\n res=http://hello 1\n</div>"));
235 }
236
237 @Test
238 public void testExternalLinkTitle() throws Exception {
239
240 WebRequest request = initTestCase("ExternalLinkTitle", null);
241
242
243 response = runner.getResponse(request);
244
245
246 String responseStr = response.getText();
247
248 assertThat(responseStr, containsString("<div id=\"1\">\n res=http://hello 1\n</div>"));
249 }
250
251 @Test
252 public void testInherit() throws Exception {
253
254 WebRequest request = initTestCase("Inherit", "/foo/bar");
255
256
257 response = runner.getResponse(request);
258
259
260 String responseStr = response.getText();
261
262 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar/paragraphs\n</div>"));
263
264 assertThat(responseStr, containsString("<div id=\"2\">\n res=/foo/bar\n</div>"));
265
266 assertThat(responseStr, containsString("<div id=\"3\">\n res=/foo/bar\n</div>"));
267 }
268
269 @Test
270 public void testInheritList() throws Exception {
271
272 WebRequest request = initTestCase("InheritList", "/foo/bar");
273
274
275 response = runner.getResponse(request);
276
277
278 String responseStr = response.getText();
279
280 assertThat(responseStr, containsString("<div id=\"1\">\n res=[info.magnolia.jcr.util.ContentMap@"));
281 }
282
283 @Test
284 public void testInheritProperty() throws Exception {
285
286 WebRequest request = initTestCase("InheritProperty", null);
287
288
289 response = runner.getResponse(request);
290
291
292 String responseStr = response.getText();
293
294 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar/paragraphs/1/text: MockValue [type=String, value=hello 1]"));
295 }
296
297 @Test
298 public void testIsAuthorInstance() throws Exception {
299
300 WebRequest request = initTestCase("Is", null);
301
302
303 response = runner.getResponse(request);
304
305
306 String responseStr = response.getText();
307
308 assertThat(responseStr, containsString("<div id=\"2\">\n res=true\n</div>"));
309 }
310
311 @Test
312 public void testIsEditMode() throws Exception {
313
314 WebRequest request = initTestCase("Is", null);
315
316
317 response = runner.getResponse(request);
318
319
320 String responseStr = response.getText();
321
322 assertThat(responseStr, containsString("<div id=\"4\">\n res=true\n</div>"));
323 }
324
325 @Test
326 public void testIsFromCurrentPage() throws Exception {
327
328 WebRequest request = initTestCase("IsFromCurrentPage", null);
329
330
331 response = runner.getResponse(request);
332
333
334 String responseStr = response.getText();
335
336 assertThat(responseStr, containsString("<div id=\"1\">\n res=true\n</div>"));
337 }
338
339 @Test
340 public void testIsInherited() throws Exception {
341
342 WebRequest request = initTestCase("IsInherited", null);
343
344
345 response = runner.getResponse(request);
346
347
348 String responseStr = response.getText();
349
350 assertThat(responseStr, containsString("<div id=\"1\">\n res=false\n</div>"));
351 }
352
353 @Test
354 public void testIsPreviewMode() throws Exception {
355
356 WebRequest request = initTestCase("Is", null);
357
358
359 response = runner.getResponse(request);
360
361
362 String responseStr = response.getText();
363
364 assertThat(responseStr, containsString("<div id=\"3\">\n res=false\n</div>"));
365 }
366
367 @Test
368 public void testIsPublicInstance() throws Exception {
369
370 WebRequest request = initTestCase("Is", null);
371
372
373 response = runner.getResponse(request);
374
375
376
377 String responseStr = response.getText();
378
379 assertThat(responseStr, containsString("<div id=\"1\">\n res=false\n</div>"));
380 }
381
382 @Test
383 public void testLanguage() throws Exception {
384
385 WebRequest request = initTestCase("Language", null);
386
387
388 response = runner.getResponse(request);
389
390
391
392 String responseStr = response.getText();
393
394 assertThat(responseStr, containsString("<div id=\"1\">\n res=en_US\n</div>"));
395 }
396
397 @Test
398 public void testLink() throws Exception {
399
400 WebRequest request = initTestCase("Link", null);
401
402
403 response = runner.getResponse(request);
404
405
406
407 String responseStr = response.getText();
408
409 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar/paragraphs/1\n</div>"));
410 }
411
412 @Test
413 public void testLinkForProperty() throws Exception {
414
415 WebRequest request = initTestCase("LinkForProperty", null);
416
417
418 response = runner.getResponse(request);
419
420
421 String responseStr = response.getText();
422 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar/paragraphs/1/image/file.jpg\n</div>"));
423 }
424
425 @Test
426 public void testLinkForWorkspace() throws Exception {
427
428 WebRequest request = initTestCase("LinkForWorkspace", null);
429
430
431 response = runner.getResponse(request);
432
433
434 String responseStr = response.getText();
435
436 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar/paragraphs/0\n</div>"));
437 }
438
439 @Test
440 public void testPage() throws Exception {
441
442 WebRequest request = initTestCase("Page", null);
443
444
445 response = runner.getResponse(request);
446
447
448 String responseStr = response.getText();
449
450 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar\n</div>"));
451 }
452
453 @Test
454 public void testParent() throws Exception {
455
456 WebRequest request = initTestCase("Parent", null);
457
458
459 response = runner.getResponse(request);
460
461
462 String responseStr = response.getText();
463
464 assertThat(responseStr, containsString("<div id=\"1\">\n res=/foo/bar\n</div>"));
465
466 assertThat(responseStr, containsString("<div id=\"2\">\n res=/foo/bar/paragraphs\n</div>"));
467
468 assertThat(responseStr, containsString("<div id=\"3\">\n res=/foo/bar/paragraphs\n</div>"));
469 }
470
471 @Test
472 public void testSiblings() throws Exception {
473
474 WebRequest request = initTestCase("Siblings", null);
475
476
477 response = runner.getResponse(request);
478
479
480 String responseStr = response.getText();
481 assertThat(responseStr, containsString("<div id=\"1\">\n res=" + SiblingsHelper.class.getName()));
482 }
483
484 @Test
485 public void testMetadata() throws Exception {
486
487 WebRequest request = initTestCase("Metadata", null);
488
489
490 response = runner.getResponse(request);
491
492
493 String responseStr = response.getText();
494 assertThat(responseStr, containsString("<div id=\"1\">\n res=test:testParagraph1"));
495 }
496
497 }