1. Project Clover database Fri Apr 29 2016 13:24:33 CEST
  2. Package info.magnolia.module.blossom.support

File IncludeRequestWrapperTest.java

 

Code metrics

0
80
12
1
235
136
12
0.15
6.67
12
1

Classes

Class Line # Actions
IncludeRequestWrapperTest 59 80 0% 12 2
0.978260997.8%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    /**
2    * This file Copyright (c) 2010-2016 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.module.blossom.support;
35   
36    import static org.junit.Assert.assertEquals;
37    import static org.junit.Assert.assertFalse;
38    import static org.junit.Assert.assertTrue;
39   
40    import java.io.IOException;
41    import java.util.concurrent.atomic.AtomicBoolean;
42    import java.util.concurrent.atomic.AtomicReference;
43   
44    import javax.servlet.RequestDispatcher;
45    import javax.servlet.ServletException;
46    import javax.servlet.ServletRequest;
47    import javax.servlet.ServletResponse;
48   
49    import org.junit.Test;
50    import org.springframework.mock.web.MockHttpServletRequest;
51    import org.springframework.mock.web.MockHttpServletResponse;
52    import org.springframework.web.util.WebUtils;
53   
54    /**
55    * Test case for IncludeRequestWrapper.
56    *
57    * @since 1.2
58    */
 
59    public class IncludeRequestWrapperTest {
60   
 
61  1 toggle @Test
62    public void testSetsIncludeAttributes() {
63   
64    // GIVEN
65  1 MockHttpServletRequest mock = new MockHttpServletRequest();
66  1 IncludeRequestWrapper wrapper = new IncludeRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path", "q=1");
67   
68    // THEN
69  1 assertEquals("/context/servlet/path", wrapper.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE));
70  1 assertEquals("/context", wrapper.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE));
71  1 assertEquals("/servlet", wrapper.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE));
72  1 assertEquals("/path", wrapper.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE));
73    }
74   
 
75  1 toggle @Test
76    public void testHidesPreviousIncludeAttributes() {
77   
78    // GIVEN
79  1 MockHttpServletRequest mock = new MockHttpServletRequest();
80  1 mock.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/foo/bar/zed");
81  1 mock.setAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE, "/foo");
82  1 mock.setAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE, "/bar");
83  1 mock.setAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE, "/zed");
84  1 mock.setAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE, "p=1");
85  1 IncludeRequestWrapper wrapper = new IncludeRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path", "q=1");
86   
87    // THEN
88  1 assertEquals("/context/servlet/path", wrapper.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE));
89  1 assertEquals("/context", wrapper.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE));
90  1 assertEquals("/servlet", wrapper.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE));
91  1 assertEquals("/path", wrapper.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE));
92  1 assertEquals("q=1", wrapper.getAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE));
93    }
94   
 
95  1 toggle @Test
96    public void testOverridesPreviousIncludeWrapper() {
97   
98    // GIVEN
99  1 MockHttpServletRequest mock = new MockHttpServletRequest();
100  1 mock.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/foo/bar/zed");
101  1 mock.setAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE, "/foo");
102  1 mock.setAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE, "/bar");
103  1 mock.setAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE, "/zed");
104  1 mock.setAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE, "p=1");
105  1 IncludeRequestWrapper wrapper = new IncludeRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path", "q=1");
106   
107    // WHEN
108  1 wrapper = new IncludeRequestWrapper(wrapper, "/newcontext/newservlet/newpath", "/newcontext", "/newservlet", "/newpath", "newq=1");
109   
110    // THEN
111  1 assertEquals("/newcontext/newservlet/newpath", wrapper.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE));
112  1 assertEquals("/newcontext", wrapper.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE));
113  1 assertEquals("/newservlet", wrapper.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE));
114  1 assertEquals("/newpath", wrapper.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE));
115  1 assertEquals("newq=1", wrapper.getAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE));
116    }
117   
 
118  1 toggle @Test
119    public void testHidesAttributesWhenPerformingInclude() throws ServletException, IOException {
120   
121    // GIVEN
122  1 final AtomicBoolean assertsWereRun = new AtomicBoolean(false);
123  1 final AtomicReference<IncludeRequestWrapper> wrapperHolder = new AtomicReference<IncludeRequestWrapper>();
124  1 MockHttpServletRequest mock = new MockHttpServletRequest() {
 
125  1 toggle @Override
126    public RequestDispatcher getRequestDispatcher(String path) {
127  1 return new RequestDispatcher() {
128   
 
129  0 toggle @Override
130    public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException {
131    }
132   
 
133  1 toggle @Override
134    public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
135   
136  1 IncludeRequestWrapper wrapper = wrapperHolder.get();
137   
138  1 setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/foo/bar/zed");
139  1 setAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE, "/foo");
140  1 setAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE, "/bar");
141  1 setAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE, "/zed");
142  1 setAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE, "p=1");
143   
144    // WHEN
145  1 assertEquals("/foo/bar/zed", wrapper.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE));
146  1 assertEquals("/foo", wrapper.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE));
147  1 assertEquals("/bar", wrapper.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE));
148  1 assertEquals("/zed", wrapper.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE));
149  1 assertEquals("p=1", wrapper.getAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE));
150  1 assertFalse(wrapper.isEnabled());
151   
152  1 assertsWereRun.set(true);
153    }
154    };
155    }
156    };
157  1 IncludeRequestWrapper wrapper = new IncludeRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path", "q=1");
158  1 wrapperHolder.set(wrapper);
159   
160    // THEN
161  1 wrapper.getRequestDispatcher("/include").include(wrapper, new MockHttpServletResponse());
162   
163    // WHEN
164  1 assertTrue(assertsWereRun.get());
165    }
166   
 
167  1 toggle @Test
168    public void testHidesAttributesWhenPerformingForward() throws ServletException, IOException {
169   
170    // GIVEN
171  1 final AtomicBoolean assertsWereRun = new AtomicBoolean(false);
172  1 final AtomicReference<IncludeRequestWrapper> wrapperHolder = new AtomicReference<IncludeRequestWrapper>();
173  1 MockHttpServletRequest mock = new MockHttpServletRequest() {
 
174  1 toggle @Override
175    public RequestDispatcher getRequestDispatcher(String path) {
176  1 return new RequestDispatcher() {
177   
 
178  1 toggle @Override
179    public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException {
180   
181  1 IncludeRequestWrapper wrapper = wrapperHolder.get();
182   
183  1 setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/foo/bar/zed");
184  1 setAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE, "/foo");
185  1 setAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE, "/bar");
186  1 setAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE, "/zed");
187  1 setAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE, "p=1");
188   
189    // WHEN
190  1 assertEquals("/foo/bar/zed", wrapper.getAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE));
191  1 assertEquals("/foo", wrapper.getAttribute(WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE));
192  1 assertEquals("/bar", wrapper.getAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE));
193  1 assertEquals("/zed", wrapper.getAttribute(WebUtils.INCLUDE_PATH_INFO_ATTRIBUTE));
194  1 assertEquals("p=1", wrapper.getAttribute(WebUtils.INCLUDE_QUERY_STRING_ATTRIBUTE));
195  1 assertFalse(wrapper.isEnabled());
196   
197  1 assertsWereRun.set(true);
198    }
199   
 
200  0 toggle @Override
201    public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
202    }
203    };
204    }
205    };
206  1 IncludeRequestWrapper wrapper = new IncludeRequestWrapper(mock, "/context/servlet/path", "/context", "/servlet", "/path", "q=1");
207  1 wrapperHolder.set(wrapper);
208   
209    // THEN
210  1 wrapper.getRequestDispatcher("/forward").forward(wrapper, new MockHttpServletResponse());
211   
212    // WHEN
213  1 assertTrue(assertsWereRun.get());
214    }
215   
 
216  1 toggle @Test
217    public void testDisablesPreviousIncludeWrapperAndRestoresItAfterwards() {
218   
219    // GIVEN
220  1 MockHttpServletRequest mock = new MockHttpServletRequest();
221  1 IncludeRequestWrapper previousWrapper = new IncludeRequestWrapper(mock, "/previousContext/previousServlet/previousPath", "/previousContext", "/previousServlet", "/previousPath", null);
222  1 assertTrue(previousWrapper.isEnabled());
223   
224    // WHEN
225  1 IncludeRequestWrapper wrapper = new IncludeRequestWrapper(previousWrapper, "/context/servlet/path", "/context", "/servlet", "/path", null);
226   
227    // THEN
228  1 assertFalse(previousWrapper.isEnabled());
229   
230    // WHEN
231  1 wrapper.restore();
232   
233    // THEN
234  1 assertTrue(previousWrapper.isEnabled());
235    }}