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

File SpecialAttributeRequestWrapperTest.java

 

Code metrics

4
111
17
1
350
192
22
0.2
6.53
17
1.29

Classes

Class Line # Actions
SpecialAttributeRequestWrapperTest 56 111 0% 22 3
0.9772727597.7%
 

Contributing tests

This file is covered by 14 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 java.util.ArrayList;
37    import java.util.Enumeration;
38    import java.util.List;
39    import java.util.NoSuchElementException;
40   
41    import org.junit.Test;
42    import org.springframework.mock.web.MockHttpServletRequest;
43    import org.springframework.web.util.WebUtils;
44    import static org.junit.Assert.assertEquals;
45    import static org.junit.Assert.assertFalse;
46    import static org.junit.Assert.assertNotNull;
47    import static org.junit.Assert.assertNull;
48    import static org.junit.Assert.assertTrue;
49    import static org.junit.Assert.fail;
50   
51    /**
52    * Test case for SpecialAttributeRequestWrapper.
53    *
54    * @since 1.2
55    */
 
56    public class SpecialAttributeRequestWrapperTest {
57   
58    private static final String TEST_ATTRIBUTES_COMMON_PREFIX = "javax.servlet.include.";
59    private static final String TEST_ATTRIBUTE = WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE;
60    private static final String TEST_ATTRIBUTE2 = WebUtils.INCLUDE_CONTEXT_PATH_ATTRIBUTE;
61   
 
62  1 toggle @Test
63    public void testAddedSingleAttributeIsVisible() {
64   
65    // GIVEN
66  1 MockHttpServletRequest mock = new MockHttpServletRequest();
67  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
68   
69    // WHEN
70  1 setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "value");
71   
72    // THEN
73  1 assertEquals("value", wrapper.getAttribute(TEST_ATTRIBUTE));
74    }
75   
 
76  1 toggle @Test
77    public void testOverridesSingleAttribute() {
78   
79    // GIVEN
80  1 MockHttpServletRequest mock = new MockHttpServletRequest();
81  1 mock.setAttribute(TEST_ATTRIBUTE, "value");
82  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
83   
84    // WHEN
85  1 setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "overriddenValue");
86   
87    // THEN
88  1 assertEquals("overriddenValue", wrapper.getAttribute(TEST_ATTRIBUTE));
89    }
90   
 
91  1 toggle @Test
92    public void testOverriddenAttributeIsPresentInAttributeNamesEnumeration() {
93    // GIVEN
94  1 MockHttpServletRequest mock = new MockHttpServletRequest();
95  1 mock.setAttribute(WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE, "value");
96  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
97   
98    // WHEN
99  1 setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "value");
100   
101    // THEN
102  1 assertTrue(contains(wrapper.getAttributeNames(), TEST_ATTRIBUTE));
103  1 assertTrue(contains(wrapper.getAttributeNames(), WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE));
104    }
105   
 
106  1 toggle @Test
107    public void testDoesNotAllowChangingOverriddenAttribute() {
108   
109    // GIVEN
110  1 MockHttpServletRequest mock = new MockHttpServletRequest();
111  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
112  1 setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "value");
113   
114    // WHEN
115  1 wrapper.setAttribute(TEST_ATTRIBUTE, "newValue");
116   
117    // THEN
118  1 assertEquals("value", wrapper.getAttribute(TEST_ATTRIBUTE));
119    }
120   
 
121  1 toggle @Test
122    public void testAllowsChangingNonOverriddenAttribute() {
123   
124    // GIVEN
125  1 MockHttpServletRequest mock = new MockHttpServletRequest();
126  1 mock.setAttribute(TEST_ATTRIBUTE, "value");
127  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
128  1 setNoOverriddenAttribute(wrapper);
129   
130    // WHEN
131  1 wrapper.setAttribute(TEST_ATTRIBUTE, "newValue");
132   
133    // THEN
134  1 assertEquals("newValue", wrapper.getAttribute(TEST_ATTRIBUTE));
135    }
136   
 
137  1 toggle @Test
138    public void testDoesNotAllowRemovingOverriddenAttribute() {
139   
140    // GIVEN
141  1 MockHttpServletRequest mock = new MockHttpServletRequest();
142  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
143  1 setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "value");
144   
145    // WHEN
146  1 wrapper.removeAttribute(TEST_ATTRIBUTE);
147   
148    // THEN
149  1 assertNotNull(wrapper.getAttribute(TEST_ATTRIBUTE));
150    }
151   
 
152  1 toggle @Test
153    public void testAllowsRemovingNonOverriddenAttribute() {
154   
155    // GIVEN
156  1 MockHttpServletRequest mock = new MockHttpServletRequest();
157  1 mock.setAttribute(TEST_ATTRIBUTE, "value");
158  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
159  1 setNoOverriddenAttribute(wrapper);
160   
161    // WHEN
162  1 wrapper.removeAttribute(TEST_ATTRIBUTE);
163   
164    // THEN
165  1 assertNull(wrapper.getAttribute(TEST_ATTRIBUTE));
166    }
167   
 
168  1 toggle @Test
169    public void testHidesOverriddenValuesWhenDisabled() {
170   
171    // GIVEN
172  1 MockHttpServletRequest mock = new MockHttpServletRequest();
173  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
174  1 setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "value");
175   
176    // WHEN
177  1 wrapper.setEnabled(false);
178   
179    // THEN
180  1 assertNull(wrapper.getAttribute(TEST_ATTRIBUTE));
181  1 assertFalse(contains(wrapper.getAttributeNames(), TEST_ATTRIBUTE));
182   
183    // WHEN
184  1 wrapper.setEnabled(true);
185   
186    // THEN
187  1 assertNotNull(wrapper.getAttribute(TEST_ATTRIBUTE));
188  1 assertTrue(contains(wrapper.getAttributeNames(), TEST_ATTRIBUTE));
189    }
190   
 
191  1 toggle @Test
192    public void testIsOverrideAttributes() {
193   
194    // GIVEN
195  1 MockHttpServletRequest mock = new MockHttpServletRequest();
196  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
197   
198    // WHEN
199  1 wrapper.setEnabled(false);
200   
201    // THEN
202  1 assertFalse(wrapper.isEnabled());
203   
204    // WHEN
205  1 wrapper.setEnabled(true);
206   
207    // THEN
208  1 assertTrue(wrapper.isEnabled());
209    }
210   
 
211  1 toggle @Test
212    public void testOverridesWithNullValue() {
213   
214    // GIVEN
215  1 MockHttpServletRequest mock = new MockHttpServletRequest();
216  1 mock.setAttribute(TEST_ATTRIBUTE, "value");
217  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
218  1 setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, null);
219   
220    // THEN
221  1 assertNull(wrapper.getAttribute(TEST_ATTRIBUTE));
222  1 assertFalse(contains(wrapper.getAttributeNames(), TEST_ATTRIBUTE));
223    }
224   
 
225  1 toggle @Test
226    public void testAttributeNamesNextElementWithExistingAttributes() {
227   
228    // GIVEN
229  1 MockHttpServletRequest mock = new MockHttpServletRequest();
230  1 mock.setAttribute(TEST_ATTRIBUTE, "value");
231  1 mock.setAttribute(TEST_ATTRIBUTE2, "value2");
232   
233    // WHEN
234  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
235  1 setNoOverriddenAttribute(wrapper);
236   
237    // THEN
238  1 Enumeration enumeration = wrapper.getAttributeNames();
239  1 List<Object> attributeNames = new ArrayList<Object>();
240  1 attributeNames.add(enumeration.nextElement());
241  1 attributeNames.add(enumeration.nextElement());
242   
243    // just to trigger rest of code path and get coverage
244  1 try {
245  1 enumeration.nextElement();
246  0 fail();
247    } catch (NoSuchElementException expected) {
248    // expected
249    }
250   
251  1 assertFalse(enumeration.hasMoreElements());
252  1 assertTrue(attributeNames.contains(TEST_ATTRIBUTE));
253  1 assertTrue(attributeNames.contains(TEST_ATTRIBUTE2));
254    }
255   
 
256  1 toggle @Test
257    public void testAttributeNamesNextElementWithOverriddenAttributes() {
258   
259    // GIVEN
260  1 MockHttpServletRequest mock = new MockHttpServletRequest();
261   
262    // WHEN
263  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
264  1 wrapper.setOverriddenAttributes(
265    TEST_ATTRIBUTES_COMMON_PREFIX,
266    new String[]{TEST_ATTRIBUTE, TEST_ATTRIBUTE2},
267    new Object[]{"value", "value2"});
268   
269    // THEN
270  1 Enumeration enumeration = wrapper.getAttributeNames();
271  1 List<Object> attributeNames = new ArrayList<Object>();
272  1 attributeNames.add(enumeration.nextElement());
273  1 attributeNames.add(enumeration.nextElement());
274   
275  1 assertFalse(enumeration.hasMoreElements());
276  1 assertTrue(attributeNames.contains(TEST_ATTRIBUTE));
277  1 assertTrue(attributeNames.contains(TEST_ATTRIBUTE2));
278    }
279   
 
280  1 toggle @Test
281    public void testAttributeNamesNextElementSkipsOverriddenNullValues() {
282   
283    // GIVEN
284  1 MockHttpServletRequest mock = new MockHttpServletRequest();
285   
286    // WHEN
287  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
288  1 wrapper.setOverriddenAttributes(
289    TEST_ATTRIBUTES_COMMON_PREFIX,
290    new String[]{TEST_ATTRIBUTE, TEST_ATTRIBUTE2},
291    new Object[]{"value", null});
292   
293    // THEN
294  1 Enumeration enumeration = wrapper.getAttributeNames();
295  1 List<Object> attributeNames = new ArrayList<Object>();
296  1 attributeNames.add(enumeration.nextElement());
297   
298    // just to trigger rest of code path and get coverage
299  1 try {
300  1 enumeration.nextElement();
301  0 fail();
302    } catch (NoSuchElementException expected) {
303    // expected
304    }
305   
306  1 assertFalse(enumeration.hasMoreElements());
307  1 assertTrue(attributeNames.contains(TEST_ATTRIBUTE));
308  1 assertFalse(attributeNames.contains(TEST_ATTRIBUTE2));
309    }
310   
 
311  1 toggle @Test
312    public void testEnumeratesThrowsNoSuchElementException() {
313   
314    // GIVEN
315  1 MockHttpServletRequest mock = new MockHttpServletRequest();
316  1 mock.setAttribute(TEST_ATTRIBUTE, "value");
317   
318    // WHEN
319  1 SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock);
320  1 setNoOverriddenAttribute(wrapper);
321   
322    // THEN
323  1 Enumeration attributeNames = wrapper.getAttributeNames();
324  1 assertEquals(TEST_ATTRIBUTE, attributeNames.nextElement());
325  1 try {
326  1 attributeNames.nextElement();
327  0 fail();
328    } catch (NoSuchElementException expected) {
329    // expected
330    }
331    }
332   
 
333  5 toggle private boolean contains(Enumeration enumeration, String value) {
334  6 while (enumeration.hasMoreElements()) {
335  4 Object o = enumeration.nextElement();
336  4 if (o.equals(value)) {
337  3 return true;
338    }
339    }
340  2 return false;
341    }
342   
 
343  7 toggle private void setOverriddenAttribute(SpecialAttributeRequestWrapper wrapper, String name, Object value) {
344  7 wrapper.setOverriddenAttributes(TEST_ATTRIBUTES_COMMON_PREFIX, new String[]{name}, new Object[]{value});
345    }
346   
 
347  4 toggle private void setNoOverriddenAttribute(SpecialAttributeRequestWrapper wrapper) {
348  4 wrapper.setOverriddenAttributes("", new String[]{}, new Object[]{});
349    }
350    }