Clover icon

Magnolia Imaging Module 3.2.3

  1. Project Clover database Fri Nov 6 2015 14:54:03 CET
  2. Package info.magnolia.imaging.parameters

File ContentParameterProviderFactoryTest.java

 

Code metrics

2
33
12
1
142
84
14
0.42
2.75
12
1.17

Classes

Class Line # Actions
ContentParameterProviderFactoryTest 55 33 0% 14 1
0.978723497.9%
 

Contributing tests

This file is covered by 9 tests. .

Source view

1    /**
2    * This file Copyright (c) 2009-2015 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.imaging.parameters;
35   
36    import static org.junit.Assert.*;
37    import static org.mockito.Mockito.*;
38   
39    import info.magnolia.cms.core.Content;
40    import info.magnolia.cms.core.HierarchyManager;
41    import info.magnolia.cms.util.ContentWrapper;
42    import info.magnolia.context.Context;
43    import info.magnolia.context.MgnlContext;
44    import info.magnolia.imaging.ParameterProvider;
45   
46    import javax.jcr.RepositoryException;
47    import javax.servlet.http.HttpServletRequest;
48   
49    import org.junit.After;
50    import org.junit.Test;
51   
52    /**
53    * Tests for {@link info.magnolia.imaging.parameters.ContentParameterProviderFactory}.
54    */
 
55    public class ContentParameterProviderFactoryTest {
 
56  9 toggle @After
57    public void tearDown() throws Exception {
58  9 MgnlContext.setInstance(null);
59    }
60   
 
61  1 toggle @Test
62    public void testExtensionIsIgnoredIfTrimExtensionIsTrue() throws Exception {
63  1 validPathsTests("/myGenerator/dummyWS/foo/bar/baz.jpg", "dummyWS", "/foo/bar/baz", true);
64    }
65   
 
66  1 toggle @Test
67    public void testExtensionIsNotIgnoredIfTrimExtensionIsFalse() throws Exception {
68  1 validPathsTests("/myGenerator/dummyWS/foo/bar/baz.jpg", "dummyWS", "/foo/bar/baz.jpg", false);
69    }
70   
 
71  1 toggle @Test
72    public void testExtensionIsIgnoredAlsoForTopLevelNodeIfTrimExtensionIsTrue() throws Exception {
73  1 validPathsTests("/myGenerator/dummyWS/foo.jpg", "dummyWS", "/foo", true);
74    }
75   
 
76  1 toggle @Test
77    public void testExtensionIsNotIgnoredAlsoForTopLevelNodeIfTrimExtensionIsFalse() throws Exception {
78  1 validPathsTests("/myGenerator/dummyWS/foo.jpg", "dummyWS", "/foo.jpg", false);
79    }
80   
 
81  1 toggle @Test
82    public void testDeepNodeIsRetrieved() throws Exception {
83  1 validPathsTests("/myGenerator/dummyWS/foo/bar/baz", "dummyWS", "/foo/bar/baz", true);
84    }
85   
 
86  1 toggle @Test
87    public void testNodeAtFirstLevelIsRetrievedToo() throws Exception {
88  1 validPathsTests("/myGenerator/dummyWS/foo", "dummyWS", "/foo", true);
89    }
90   
 
91  1 toggle @Test
92    public void testCanGetTheRootNodeIfNeededButWhoWantsThis() throws Exception {
93  1 validPathsTests("/myGenerator/dummyWS", "dummyWS", "/", true);
94    }
95   
 
96  7 toggle private void validPathsTests(final String pathInfo, final String expectedWorkspaceName, final String expectedNodePath, final boolean trimExtension) throws RepositoryException {
97  7 final Context ctx = mock(Context.class);
98  7 final HierarchyManager hm = mock(HierarchyManager.class);
99  7 final Content mockNode = mock(Content.class);
100  7 final HttpServletRequest req = mock(HttpServletRequest.class);
101  7 MgnlContext.setInstance(ctx);
102   
103  7 when(req.getPathInfo()).thenReturn(pathInfo);
104  7 when(ctx.getHierarchyManager(expectedWorkspaceName)).thenReturn(hm);
105  7 when(hm.getContent(expectedNodePath)).thenReturn(mockNode);
106  7 when(mockNode.getHandle()).thenReturn("/does/not/matter"); // see SimpleEqualityContentWrapper
107   
108  7 final ContentParameterProviderFactory f = new ContentParameterProviderFactory();
109  7 f.setTrimExtension(trimExtension);
110  7 final ParameterProvider<Content> pp = f.newParameterProviderFor(req);
111  7 final Content result = pp.getParameter();
112    // we're only checking if the ParameterProvider indeed returns our mock, just
113    // so we can ensure it wasn't tempered with (i.e that the ParameterProvider did not call any unexpected method on it)
114    // now that we know its wrapped, we also check that, and assertEquals against the unwrapped instance
115  7 assertTrue(result instanceof SimpleEqualityContentWrapper);
116  7 assertEquals(mockNode, ((ContentWrapper) result).getWrappedContent());
117    }
118   
 
119  1 toggle @Test
120    public void testFailsIfNoPathInfo() throws Exception {
121  1 failureTestForIncompletePaths(null, "Can't determine node, no pathInfo is available for uri /chalala");
122    }
123   
 
124  1 toggle @Test
125    public void testFailsIfWorkspaceNorNodePath() throws Exception {
126  1 failureTestForIncompletePaths("/dummy", "Can't determine node from pathInfo: /dummy");
127    }
128   
 
129  2 toggle private void failureTestForIncompletePaths(final String pathInfo, final String expectedExceptionMsg) {
130  2 final HttpServletRequest req = mock(HttpServletRequest.class);
131  2 when(req.getPathInfo()).thenReturn(pathInfo);
132  2 when(req.getRequestURI()).thenReturn("/chalala" + (pathInfo == null ? "" : pathInfo));
133   
134  2 final ContentParameterProviderFactory f = new ContentParameterProviderFactory();
135  2 try {
136  2 f.newParameterProviderFor(req);
137  0 fail("should have failed");
138    } catch (IllegalArgumentException e) {
139  2 assertEquals(expectedExceptionMsg, e.getMessage());
140    }
141    }
142    }