View Javadoc

1   /**
2    * This file Copyright (c) 2010-2013 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 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   * This test class only check in the Tags are correctly called.
56   * The TemplatingFunction method test is done in the Templating module.
57   *
58   * @version $Id$
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         // GIVEN
101         WebRequest request = initTestCase("Root",null);
102 
103         // WHEN
104         response = runner.getResponse(request);
105 
106 
107         // THEN
108         String responseStr = response.getText();
109         //Check the root with nodeTypeName
110         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar/paragraphs\n</div>"));
111         //Check the root with nodeTypeName = ''
112         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/\n</div>"));
113         //Check the root with nodeTypeName = null
114         assertThat(responseStr, containsString("<div id=\"3\">\n    res=/\n</div>"));
115     }
116 
117 
118     @Test
119     public void testAncestors() throws Exception {
120         // GIVEN
121         WebRequest request = initTestCase("Ancestors","/foo/bar");
122 
123         // WHEN
124         response = runner.getResponse(request);
125 
126         // THEN
127         String responseStr = response.getText();
128         //Check the ancestor with nodeType
129         assertThat(responseStr, containsString("<div id=\"1\">\n    res=\n</div>"));
130         //Check the ancestor with nodeType = ''
131         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/foo\n</div>"));
132         //Check the ancestor with nodeType = null
133         assertThat(responseStr, containsString("<div id=\"3\">\n    res=/foo\n</div>"));
134     }
135 
136 
137     @Test
138     public void testAsJCRNode() throws Exception {
139         // GIVEN
140         WebRequest request = initTestCase("AsJCRNode",null);
141 
142         // WHEN
143         response = runner.getResponse(request);
144 
145         // THEN
146         String responseStr = response.getText();
147         //Check the content with repository
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         // GIVEN
155         WebRequest request = initTestCase("Children","/foo/bar");
156 
157         // WHEN
158         response = runner.getResponse(request);
159 
160         // THEN
161         String responseStr = response.getText();
162         //Check the content with repository
163         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar/paragraphs\n</div>"));
164         //Check the content with repository = ''
165         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/foo/bar/MetaData\n</div>"));
166         //Check the content with repository = null
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         // GIVEN
173         WebRequest request = initTestCase("Content",null);
174 
175         // WHEN
176         response = runner.getResponse(request);
177 
178         // THEN
179         String responseStr = response.getText();
180         //Check the content with repository
181         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar/paragraphs\n</div>"));
182         //Check the content with repository = ''
183         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/foo/bar/paragraphs\n</div>"));
184         //Check the content with repository = null
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         // GIVEN
191         WebRequest request = initTestCase("CreateHtmlAttribute",null);
192 
193         // WHEN
194         response = runner.getResponse(request);
195 
196         // THEN
197         String responseStr = response.getText();
198         //Check the inherit with nodeTypeName
199         assertThat(responseStr, containsString("<div id=\"1\">\n    res=a=\"b\"\n</div>"));
200     }
201 
202     @Test
203     public void testDecode() throws Exception {
204         // GIVEN
205         WebRequest request = initTestCase("Decode",null);
206 
207         // WHEN
208         response = runner.getResponse(request);
209 
210         // THEN
211         String responseStr = response.getText();
212         //Check the inherit with nodeTypeName
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         // GIVEN
219         WebRequest request = initTestCase("ExternalLink",null);
220 
221         // WHEN
222         response = runner.getResponse(request);
223 
224         // THEN
225         String responseStr = response.getText();
226         //Check the inherit with nodeTypeName
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         // GIVEN
233         WebRequest request = initTestCase("ExternalLinkTitle",null);
234 
235         // WHEN
236         response = runner.getResponse(request);
237 
238         // THEN
239         String responseStr = response.getText();
240         //Check the inherit with nodeTypeName
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         // GIVEN
247         WebRequest request = initTestCase("Inherit","/foo/bar");
248 
249         // WHEN
250         response = runner.getResponse(request);
251 
252         // THEN
253         String responseStr = response.getText();
254         //Check the inherit with nodeTypeName
255         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar/paragraphs\n</div>"));
256         //Check the inherit with nodeTypeName = ''
257         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/foo/bar\n</div>"));
258         //Check the inherit with nodeTypeName = null
259         assertThat(responseStr, containsString("<div id=\"3\">\n    res=/foo/bar\n</div>"));
260     }
261 
262     @Test
263     public void testInheritList() throws Exception {
264         // GIVEN
265         WebRequest request = initTestCase("InheritList","/foo/bar");
266 
267         // WHEN
268         response = runner.getResponse(request);
269 
270         // THEN
271         String responseStr = response.getText();
272         //Check the LinkForWorkspace
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         // GIVEN
279         WebRequest request = initTestCase("InheritProperty",null);
280 
281         // WHEN
282         response = runner.getResponse(request);
283 
284         // THEN
285         String responseStr = response.getText();
286         //Check the LinkForWorkspace
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         // GIVEN
293         WebRequest request = initTestCase("Is",null);
294 
295         // WHEN
296         response = runner.getResponse(request);
297 
298         // THEN
299         String responseStr = response.getText();
300         //Check the LinkForWorkspace
301         assertThat(responseStr, containsString("<div id=\"2\">\n    res=true\n</div>"));
302     }
303 
304     @Test
305     public void testIsEditMode() throws Exception {
306         // GIVEN
307         WebRequest request = initTestCase("Is",null);
308 
309         // WHEN
310         response = runner.getResponse(request);
311 
312         // THEN
313         String responseStr = response.getText();
314         //Check the LinkForWorkspace
315         assertThat(responseStr, containsString("<div id=\"4\">\n    res=true\n</div>"));
316     }
317 
318     @Test
319     public void testIsFromCurrentPage() throws Exception {
320         // GIVEN
321         WebRequest request = initTestCase("IsFromCurrentPage",null);
322 
323         // WHEN
324         response = runner.getResponse(request);
325 
326         // THEN
327         String responseStr = response.getText();
328         //Check the LinkForWorkspace
329         assertThat(responseStr, containsString("<div id=\"1\">\n    res=true\n</div>"));
330     }
331 
332     @Test
333     public void testIsInherited() throws Exception {
334         // GIVEN
335         WebRequest request = initTestCase("IsInherited",null);
336 
337         // WHEN
338         response = runner.getResponse(request);
339 
340         // THEN
341         String responseStr = response.getText();
342         //Check the LinkForWorkspace
343         assertThat(responseStr, containsString("<div id=\"1\">\n    res=false\n</div>"));
344     }
345 
346     @Test
347     public void testIsPreviewMode() throws Exception {
348         // GIVEN
349         WebRequest request = initTestCase("Is",null);
350 
351         // WHEN
352         response = runner.getResponse(request);
353 
354 
355         // THEN
356         String responseStr = response.getText();
357         //Check the LinkForWorkspace
358         assertThat(responseStr, containsString("<div id=\"3\">\n    res=false\n</div>"));
359     }
360 
361     @Test
362     public void testIsPublicInstance() throws Exception {
363         // GIVEN
364         WebRequest request = initTestCase("Is",null);
365 
366         // WHEN
367         response = runner.getResponse(request);
368 
369 
370         // THEN
371         String responseStr = response.getText();
372         //Check the LinkForWorkspace
373         assertThat(responseStr, containsString("<div id=\"1\">\n    res=false\n</div>"));
374     }
375 
376     @Test
377     public void testLanguage() throws Exception {
378         // GIVEN
379         WebRequest request = initTestCase("Language",null);
380 
381         // WHEN
382         response = runner.getResponse(request);
383 
384 
385         // THEN
386         String responseStr = response.getText();
387         //Check the LinkForWorkspace
388         assertThat(responseStr, containsString("<div id=\"1\">\n    res=en_US\n</div>"));
389     }
390 
391     @Test
392     public void testLink() throws Exception {
393         // GIVEN
394         WebRequest request = initTestCase("Link",null);
395 
396         // WHEN
397         response = runner.getResponse(request);
398 
399 
400         // THEN
401         String responseStr = response.getText();
402         //Check the LinkForWorkspace
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         // GIVEN
409         WebRequest request = initTestCase("LinkForProperty",null);
410 
411         // WHEN
412         response = runner.getResponse(request);
413 
414         // THEN
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         // GIVEN
422         WebRequest request = initTestCase("LinkForWorkspace",null);
423 
424         // WHEN
425         response = runner.getResponse(request);
426 
427         // THEN
428         String responseStr = response.getText();
429         //Check the LinkForWorkspace
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         // GIVEN
436         WebRequest request = initTestCase("Page",null);
437 
438         // WHEN
439         response = runner.getResponse(request);
440 
441         // THEN
442         String responseStr = response.getText();
443         //Check the page
444         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar\n</div>"));
445     }
446 
447     @Test
448     public void testParent() throws Exception {
449         // GIVEN
450         WebRequest request = initTestCase("Parent",null);
451 
452         // WHEN
453         response = runner.getResponse(request);
454 
455         // THEN
456         String responseStr = response.getText();
457         //Check the parent with nodeTypeName
458         assertThat(responseStr, containsString("<div id=\"1\">\n    res=/foo/bar\n</div>"));
459         //Check the parent with nodeTypeName = ''
460         assertThat(responseStr, containsString("<div id=\"2\">\n    res=/foo/bar/paragraphs\n</div>"));
461         //Check the parent with nodeTypeName = null
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         // GIVEN
468         WebRequest request = initTestCase("Siblings",null);
469 
470         // WHEN
471         response = runner.getResponse(request);
472 
473 
474         // THEN
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         // GIVEN
482         WebRequest request = initTestCase("Metadata",null);
483 
484         // WHEN
485         response = runner.getResponse(request);
486 
487 
488         // THEN
489         String responseStr = response.getText();
490         assertThat(responseStr, containsString("<div id=\"1\">\n    res=testParagraph1"));
491     }
492 
493 }