View Javadoc
1   /**
2    * This file Copyright (c) 2010-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * This test class only checks if the Tags are correctly called.
64   * The TemplatingFunction method test is done in the Templating module.
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         // GIVEN
109         WebRequest request = initTestCase("Root", null);
110 
111         // WHEN
112         response = runner.getResponse(request);
113 
114         // THEN
115         String responseStr = response.getText();
116         //Check the root with nodeTypeName
117         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar/paragraphs\n</div>"));
118         //Check the root with nodeTypeName = ''
119         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/\n</div>"));
120         //Check the root with nodeTypeName = null
121         assertThat(responseStr, containsString("<div id=\"3\">\n    res=/\n</div>"));
122     }
123 
124 
125     @Test
126     public void testAncestors() throws Exception {
127         // GIVEN
128         WebRequest request = initTestCase("Ancestors", "/foo/bar");
129 
130         // WHEN
131         response = runner.getResponse(request);
132 
133         // THEN
134         String responseStr = response.getText();
135         //Check the ancestor with nodeType
136         assertThat(responseStr, containsString("<div id=\"1\">\n    res=\n</div>"));
137         //Check the ancestor with nodeType = ''
138         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/foo\n</div>"));
139         //Check the ancestor with nodeType = null
140         assertThat(responseStr, containsString("<div id=\"3\">\n    res=/foo\n</div>"));
141     }
142 
143 
144     @Test
145     public void testAsJCRNode() throws Exception {
146         // GIVEN
147         WebRequest request = initTestCase("AsJCRNode", null);
148 
149         // WHEN
150         response = runner.getResponse(request);
151 
152         // THEN
153         String responseStr = response.getText();
154         //Check the content with repository
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         // GIVEN
162         WebRequest request = initTestCase("Children", "/foo/bar");
163 
164         // WHEN
165         response = runner.getResponse(request);
166 
167         // THEN
168         String responseStr = response.getText();
169         //Check the content with repository
170         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar/paragraphs\n</div>"));
171         //Check the content with repository = ''
172         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/foo/bar/paragraphs\n</div>"));
173         //Check the content with repository = null
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         // GIVEN
180         WebRequest request = initTestCase("Content", null);
181 
182         // WHEN
183         response = runner.getResponse(request);
184 
185         // THEN
186         String responseStr = response.getText();
187         //Check the content with repository
188         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar/paragraphs\n</div>"));
189         //Check the content with repository = ''
190         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/foo/bar/paragraphs\n</div>"));
191         //Check the content with repository = null
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         // GIVEN
198         WebRequest request = initTestCase("CreateHtmlAttribute", null);
199 
200         // WHEN
201         response = runner.getResponse(request);
202 
203         // THEN
204         String responseStr = response.getText();
205         //Check the inherit with nodeTypeName
206         assertThat(responseStr, containsString("<div id=\"1\">\n    res=a=\"b\"\n</div>"));
207     }
208 
209     @Test
210     public void testDecode() throws Exception {
211         // GIVEN
212         WebRequest request = initTestCase("Decode", null);
213 
214         // WHEN
215         response = runner.getResponse(request);
216 
217         // THEN
218         String responseStr = response.getText();
219         //Check the inherit with nodeTypeName
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         // GIVEN
226         WebRequest request = initTestCase("ExternalLink", null);
227 
228         // WHEN
229         response = runner.getResponse(request);
230 
231         // THEN
232         String responseStr = response.getText();
233         //Check the inherit with nodeTypeName
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         // GIVEN
240         WebRequest request = initTestCase("ExternalLinkTitle", null);
241 
242         // WHEN
243         response = runner.getResponse(request);
244 
245         // THEN
246         String responseStr = response.getText();
247         //Check the inherit with nodeTypeName
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         // GIVEN
254         WebRequest request = initTestCase("Inherit", "/foo/bar");
255 
256         // WHEN
257         response = runner.getResponse(request);
258 
259         // THEN
260         String responseStr = response.getText();
261         //Check the inherit with nodeTypeName
262         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar/paragraphs\n</div>"));
263         //Check the inherit with nodeTypeName = ''
264         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/foo/bar\n</div>"));
265         //Check the inherit with nodeTypeName = null
266         assertThat(responseStr, containsString("<div id=\"3\">\n    res=/foo/bar\n</div>"));
267     }
268 
269     @Test
270     public void testInheritList() throws Exception {
271         // GIVEN
272         WebRequest request = initTestCase("InheritList", "/foo/bar");
273 
274         // WHEN
275         response = runner.getResponse(request);
276 
277         // THEN
278         String responseStr = response.getText();
279         //Check the LinkForWorkspace
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         // GIVEN
286         WebRequest request = initTestCase("InheritProperty", null);
287 
288         // WHEN
289         response = runner.getResponse(request);
290 
291         // THEN
292         String responseStr = response.getText();
293         //Check the LinkForWorkspace
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         // GIVEN
300         WebRequest request = initTestCase("Is", null);
301 
302         // WHEN
303         response = runner.getResponse(request);
304 
305         // THEN
306         String responseStr = response.getText();
307         //Check the LinkForWorkspace
308         assertThat(responseStr, containsString("<div id=\"2\">\n    res=true\n</div>"));
309     }
310 
311     @Test
312     public void testIsEditMode() throws Exception {
313         // GIVEN
314         WebRequest request = initTestCase("Is", null);
315 
316         // WHEN
317         response = runner.getResponse(request);
318 
319         // THEN
320         String responseStr = response.getText();
321         //Check the LinkForWorkspace
322         assertThat(responseStr, containsString("<div id=\"4\">\n    res=true\n</div>"));
323     }
324 
325     @Test
326     public void testIsFromCurrentPage() throws Exception {
327         // GIVEN
328         WebRequest request = initTestCase("IsFromCurrentPage", null);
329 
330         // WHEN
331         response = runner.getResponse(request);
332 
333         // THEN
334         String responseStr = response.getText();
335         //Check the LinkForWorkspace
336         assertThat(responseStr, containsString("<div id=\"1\">\n    res=true\n</div>"));
337     }
338 
339     @Test
340     public void testIsInherited() throws Exception {
341         // GIVEN
342         WebRequest request = initTestCase("IsInherited", null);
343 
344         // WHEN
345         response = runner.getResponse(request);
346 
347         // THEN
348         String responseStr = response.getText();
349         //Check the LinkForWorkspace
350         assertThat(responseStr, containsString("<div id=\"1\">\n    res=false\n</div>"));
351     }
352 
353     @Test
354     public void testIsPreviewMode() throws Exception {
355         // GIVEN
356         WebRequest request = initTestCase("Is", null);
357 
358         // WHEN
359         response = runner.getResponse(request);
360 
361         // THEN
362         String responseStr = response.getText();
363         //Check the LinkForWorkspace
364         assertThat(responseStr, containsString("<div id=\"3\">\n    res=false\n</div>"));
365     }
366 
367     @Test
368     public void testIsPublicInstance() throws Exception {
369         // GIVEN
370         WebRequest request = initTestCase("Is", null);
371 
372         // WHEN
373         response = runner.getResponse(request);
374 
375 
376         // THEN
377         String responseStr = response.getText();
378         //Check the LinkForWorkspace
379         assertThat(responseStr, containsString("<div id=\"1\">\n    res=false\n</div>"));
380     }
381 
382     @Test
383     public void testLanguage() throws Exception {
384         // GIVEN
385         WebRequest request = initTestCase("Language", null);
386 
387         // WHEN
388         response = runner.getResponse(request);
389 
390 
391         // THEN
392         String responseStr = response.getText();
393         //Check the LinkForWorkspace
394         assertThat(responseStr, containsString("<div id=\"1\">\n    res=en_US\n</div>"));
395     }
396 
397     @Test
398     public void testLink() throws Exception {
399         // GIVEN
400         WebRequest request = initTestCase("Link", null);
401 
402         // WHEN
403         response = runner.getResponse(request);
404 
405 
406         // THEN
407         String responseStr = response.getText();
408         //Check the LinkForWorkspace
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         // GIVEN
415         WebRequest request = initTestCase("LinkForProperty", null);
416 
417         // WHEN
418         response = runner.getResponse(request);
419 
420         // THEN
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         // GIVEN
428         WebRequest request = initTestCase("LinkForWorkspace", null);
429 
430         // WHEN
431         response = runner.getResponse(request);
432 
433         // THEN
434         String responseStr = response.getText();
435         //Check the LinkForWorkspace
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         // GIVEN
442         WebRequest request = initTestCase("Page", null);
443 
444         // WHEN
445         response = runner.getResponse(request);
446 
447         // THEN
448         String responseStr = response.getText();
449         //Check the page
450         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar\n</div>"));
451     }
452 
453     @Test
454     public void testParent() throws Exception {
455         // GIVEN
456         WebRequest request = initTestCase("Parent", null);
457 
458         // WHEN
459         response = runner.getResponse(request);
460 
461         // THEN
462         String responseStr = response.getText();
463         //Check the parent with nodeTypeName
464         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar\n</div>"));
465         //Check the parent with nodeTypeName = ''
466         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/foo/bar/paragraphs\n</div>"));
467         //Check the parent with nodeTypeName = null
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         // GIVEN
474         WebRequest request = initTestCase("Siblings", null);
475 
476         // WHEN
477         response = runner.getResponse(request);
478 
479         // THEN
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         // GIVEN
487         WebRequest request = initTestCase("Metadata", null);
488 
489         // WHEN
490         response = runner.getResponse(request);
491 
492         // THEN
493         String responseStr = response.getText();
494         assertThat(responseStr, containsString("<div id=\"1\">\n    res=test:testParagraph1"));
495     }
496 
497 }