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