1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
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 |
|
|
53 |
|
|
54 |
|
@since |
55 |
|
|
|
|
| 97.7% |
Uncovered Elements: 3 (132) |
Complexity: 22 |
Complexity Density: 0.2 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
62 |
1 |
@Test... |
63 |
|
public void testAddedSingleAttributeIsVisible() { |
64 |
|
|
65 |
|
|
66 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
67 |
1 |
SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock); |
68 |
|
|
69 |
|
|
70 |
1 |
setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "value"); |
71 |
|
|
72 |
|
|
73 |
1 |
assertEquals("value", wrapper.getAttribute(TEST_ATTRIBUTE)); |
74 |
|
} |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
76 |
1 |
@Test... |
77 |
|
public void testOverridesSingleAttribute() { |
78 |
|
|
79 |
|
|
80 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
81 |
1 |
mock.setAttribute(TEST_ATTRIBUTE, "value"); |
82 |
1 |
SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock); |
83 |
|
|
84 |
|
|
85 |
1 |
setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "overriddenValue"); |
86 |
|
|
87 |
|
|
88 |
1 |
assertEquals("overriddenValue", wrapper.getAttribute(TEST_ATTRIBUTE)); |
89 |
|
} |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
91 |
1 |
@Test... |
92 |
|
public void testOverriddenAttributeIsPresentInAttributeNamesEnumeration() { |
93 |
|
|
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 |
|
|
99 |
1 |
setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "value"); |
100 |
|
|
101 |
|
|
102 |
1 |
assertTrue(contains(wrapper.getAttributeNames(), TEST_ATTRIBUTE)); |
103 |
1 |
assertTrue(contains(wrapper.getAttributeNames(), WebUtils.INCLUDE_SERVLET_PATH_ATTRIBUTE)); |
104 |
|
} |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
106 |
1 |
@Test... |
107 |
|
public void testDoesNotAllowChangingOverriddenAttribute() { |
108 |
|
|
109 |
|
|
110 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
111 |
1 |
SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock); |
112 |
1 |
setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "value"); |
113 |
|
|
114 |
|
|
115 |
1 |
wrapper.setAttribute(TEST_ATTRIBUTE, "newValue"); |
116 |
|
|
117 |
|
|
118 |
1 |
assertEquals("value", wrapper.getAttribute(TEST_ATTRIBUTE)); |
119 |
|
} |
120 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
121 |
1 |
@Test... |
122 |
|
public void testAllowsChangingNonOverriddenAttribute() { |
123 |
|
|
124 |
|
|
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 |
|
|
131 |
1 |
wrapper.setAttribute(TEST_ATTRIBUTE, "newValue"); |
132 |
|
|
133 |
|
|
134 |
1 |
assertEquals("newValue", wrapper.getAttribute(TEST_ATTRIBUTE)); |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
137 |
1 |
@Test... |
138 |
|
public void testDoesNotAllowRemovingOverriddenAttribute() { |
139 |
|
|
140 |
|
|
141 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
142 |
1 |
SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock); |
143 |
1 |
setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "value"); |
144 |
|
|
145 |
|
|
146 |
1 |
wrapper.removeAttribute(TEST_ATTRIBUTE); |
147 |
|
|
148 |
|
|
149 |
1 |
assertNotNull(wrapper.getAttribute(TEST_ATTRIBUTE)); |
150 |
|
} |
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
152 |
1 |
@Test... |
153 |
|
public void testAllowsRemovingNonOverriddenAttribute() { |
154 |
|
|
155 |
|
|
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 |
|
|
162 |
1 |
wrapper.removeAttribute(TEST_ATTRIBUTE); |
163 |
|
|
164 |
|
|
165 |
1 |
assertNull(wrapper.getAttribute(TEST_ATTRIBUTE)); |
166 |
|
} |
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
168 |
1 |
@Test... |
169 |
|
public void testHidesOverriddenValuesWhenDisabled() { |
170 |
|
|
171 |
|
|
172 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
173 |
1 |
SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock); |
174 |
1 |
setOverriddenAttribute(wrapper, TEST_ATTRIBUTE, "value"); |
175 |
|
|
176 |
|
|
177 |
1 |
wrapper.setEnabled(false); |
178 |
|
|
179 |
|
|
180 |
1 |
assertNull(wrapper.getAttribute(TEST_ATTRIBUTE)); |
181 |
1 |
assertFalse(contains(wrapper.getAttributeNames(), TEST_ATTRIBUTE)); |
182 |
|
|
183 |
|
|
184 |
1 |
wrapper.setEnabled(true); |
185 |
|
|
186 |
|
|
187 |
1 |
assertNotNull(wrapper.getAttribute(TEST_ATTRIBUTE)); |
188 |
1 |
assertTrue(contains(wrapper.getAttributeNames(), TEST_ATTRIBUTE)); |
189 |
|
} |
190 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
191 |
1 |
@Test... |
192 |
|
public void testIsOverrideAttributes() { |
193 |
|
|
194 |
|
|
195 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
196 |
1 |
SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock); |
197 |
|
|
198 |
|
|
199 |
1 |
wrapper.setEnabled(false); |
200 |
|
|
201 |
|
|
202 |
1 |
assertFalse(wrapper.isEnabled()); |
203 |
|
|
204 |
|
|
205 |
1 |
wrapper.setEnabled(true); |
206 |
|
|
207 |
|
|
208 |
1 |
assertTrue(wrapper.isEnabled()); |
209 |
|
} |
210 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
211 |
1 |
@Test... |
212 |
|
public void testOverridesWithNullValue() { |
213 |
|
|
214 |
|
|
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 |
|
|
221 |
1 |
assertNull(wrapper.getAttribute(TEST_ATTRIBUTE)); |
222 |
1 |
assertFalse(contains(wrapper.getAttributeNames(), TEST_ATTRIBUTE)); |
223 |
|
} |
224 |
|
|
|
|
| 93.3% |
Uncovered Elements: 1 (15) |
Complexity: 2 |
Complexity Density: 0.13 |
1PASS
|
|
225 |
1 |
@Test... |
226 |
|
public void testAttributeNamesNextElementWithExistingAttributes() { |
227 |
|
|
228 |
|
|
229 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
230 |
1 |
mock.setAttribute(TEST_ATTRIBUTE, "value"); |
231 |
1 |
mock.setAttribute(TEST_ATTRIBUTE2, "value2"); |
232 |
|
|
233 |
|
|
234 |
1 |
SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock); |
235 |
1 |
setNoOverriddenAttribute(wrapper); |
236 |
|
|
237 |
|
|
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 |
|
|
244 |
1 |
try { |
245 |
1 |
enumeration.nextElement(); |
246 |
0 |
fail(); |
247 |
|
} catch (NoSuchElementException expected) { |
248 |
|
|
249 |
|
} |
250 |
|
|
251 |
1 |
assertFalse(enumeration.hasMoreElements()); |
252 |
1 |
assertTrue(attributeNames.contains(TEST_ATTRIBUTE)); |
253 |
1 |
assertTrue(attributeNames.contains(TEST_ATTRIBUTE2)); |
254 |
|
} |
255 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
256 |
1 |
@Test... |
257 |
|
public void testAttributeNamesNextElementWithOverriddenAttributes() { |
258 |
|
|
259 |
|
|
260 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
261 |
|
|
262 |
|
|
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 |
|
|
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 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 2 |
Complexity Density: 0.17 |
1PASS
|
|
280 |
1 |
@Test... |
281 |
|
public void testAttributeNamesNextElementSkipsOverriddenNullValues() { |
282 |
|
|
283 |
|
|
284 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
285 |
|
|
286 |
|
|
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 |
|
|
294 |
1 |
Enumeration enumeration = wrapper.getAttributeNames(); |
295 |
1 |
List<Object> attributeNames = new ArrayList<Object>(); |
296 |
1 |
attributeNames.add(enumeration.nextElement()); |
297 |
|
|
298 |
|
|
299 |
1 |
try { |
300 |
1 |
enumeration.nextElement(); |
301 |
0 |
fail(); |
302 |
|
} catch (NoSuchElementException expected) { |
303 |
|
|
304 |
|
} |
305 |
|
|
306 |
1 |
assertFalse(enumeration.hasMoreElements()); |
307 |
1 |
assertTrue(attributeNames.contains(TEST_ATTRIBUTE)); |
308 |
1 |
assertFalse(attributeNames.contains(TEST_ATTRIBUTE2)); |
309 |
|
} |
310 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1PASS
|
|
311 |
1 |
@Test... |
312 |
|
public void testEnumeratesThrowsNoSuchElementException() { |
313 |
|
|
314 |
|
|
315 |
1 |
MockHttpServletRequest mock = new MockHttpServletRequest(); |
316 |
1 |
mock.setAttribute(TEST_ATTRIBUTE, "value"); |
317 |
|
|
318 |
|
|
319 |
1 |
SpecialAttributeRequestWrapper wrapper = new SpecialAttributeRequestWrapper(mock); |
320 |
1 |
setNoOverriddenAttribute(wrapper); |
321 |
|
|
322 |
|
|
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 |
|
|
330 |
|
} |
331 |
|
} |
332 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
333 |
5 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
343 |
7 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
347 |
4 |
private void setNoOverriddenAttribute(SpecialAttributeRequestWrapper wrapper) {... |
348 |
4 |
wrapper.setOverriddenAttributes("", new String[]{}, new Object[]{}); |
349 |
|
} |
350 |
|
} |