1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.url.internal.container; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.servlet.ServletContext; |
28 |
|
import javax.servlet.ServletRegistration; |
29 |
|
import javax.servlet.http.HttpServletRequest; |
30 |
|
|
31 |
|
import org.junit.Before; |
32 |
|
import org.junit.Rule; |
33 |
|
import org.junit.Test; |
34 |
|
import org.xwiki.container.Container; |
35 |
|
import org.xwiki.container.servlet.ServletRequest; |
36 |
|
import org.xwiki.environment.Environment; |
37 |
|
import org.xwiki.environment.internal.ServletEnvironment; |
38 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
39 |
|
import org.xwiki.url.ExtendedURL; |
40 |
|
import org.xwiki.url.URLNormalizer; |
41 |
|
|
42 |
|
import static org.junit.Assert.assertEquals; |
43 |
|
import static org.junit.Assert.assertSame; |
44 |
|
import static org.junit.Assert.fail; |
45 |
|
import static org.mockito.Mockito.mock; |
46 |
|
import static org.mockito.Mockito.when; |
47 |
|
|
48 |
|
|
49 |
|
@link |
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
|
|
| 98% |
Uncovered Elements: 1 (50) |
Complexity: 10 |
Complexity Density: 0.24 |
|
54 |
|
public class ContextAndActionURLNormalizerTest |
55 |
|
{ |
56 |
|
private Container container; |
57 |
|
|
58 |
|
private ServletEnvironment environment; |
59 |
|
|
60 |
|
private ServletContext servletContext; |
61 |
|
|
62 |
|
private ExtendedURL testURL = new ExtendedURL(Arrays.asList("one", "two")); |
63 |
|
|
64 |
|
@Rule |
65 |
|
public MockitoComponentMockingRule<URLNormalizer<ExtendedURL>> mocker = |
66 |
|
new MockitoComponentMockingRule<URLNormalizer<ExtendedURL>>(ContextAndActionURLNormalizer.class); |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
68 |
7 |
@Before... |
69 |
|
public void configure() throws Exception |
70 |
|
{ |
71 |
7 |
this.container = this.mocker.getInstance(Container.class); |
72 |
|
|
73 |
7 |
this.environment = mock(ServletEnvironment.class); |
74 |
7 |
this.mocker.registerComponent(Environment.class, this.environment); |
75 |
|
|
76 |
7 |
this.servletContext = mock(ServletContext.class); |
77 |
7 |
when(this.environment.getServletContext()).thenReturn(this.servletContext); |
78 |
|
|
79 |
7 |
ServletRegistration sr = mock(ServletRegistration.class); |
80 |
7 |
when(this.servletContext.getServletRegistration("action")).thenReturn(sr); |
81 |
7 |
when(sr.getMappings()).thenReturn(Arrays.asList("/bin/*", "/wiki/*", "/testbin/*")); |
82 |
|
} |
83 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
84 |
1 |
@Test... |
85 |
|
public void normalizeWithNoKnownContextPathThrowsException() throws Exception |
86 |
|
{ |
87 |
1 |
this.mocker.registerMockComponent(Environment.class); |
88 |
1 |
try { |
89 |
1 |
this.mocker.getComponentUnderTest().normalize(this.testURL); |
90 |
0 |
fail("Should have thrown an exception"); |
91 |
|
} catch (RuntimeException expected) { |
92 |
1 |
assertEquals("Failed to normalize the URL [/one/two] since the application's Servlet context couldn't be " |
93 |
|
+ "computed.", expected.getMessage()); |
94 |
|
} |
95 |
|
} |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
97 |
1 |
@Test... |
98 |
|
public void normalizeUsesServletContext() throws Exception |
99 |
|
{ |
100 |
1 |
when(this.servletContext.getContextPath()).thenReturn("/xwiki"); |
101 |
|
|
102 |
1 |
assertEquals("/xwiki/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
103 |
|
} |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
105 |
1 |
@Test... |
106 |
|
public void normalizeWithRootServletContext() throws Exception |
107 |
|
{ |
108 |
1 |
when(this.servletContext.getContextPath()).thenReturn(""); |
109 |
|
|
110 |
1 |
assertEquals("/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
111 |
|
} |
112 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
113 |
1 |
@Test... |
114 |
|
public void normalizeFromAlternateServletMappingPreservesServletMapping() throws Exception |
115 |
|
{ |
116 |
1 |
when(this.servletContext.getContextPath()).thenReturn("/xwiki"); |
117 |
|
|
118 |
1 |
HttpServletRequest req = createMockRequest(); |
119 |
1 |
when(req.getServletPath()).thenReturn("/testbin"); |
120 |
|
|
121 |
1 |
assertEquals("/xwiki/testbin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
122 |
|
} |
123 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
124 |
1 |
@Test... |
125 |
|
public void normalizeFromRootServletContextAndServletMapping() throws Exception |
126 |
|
{ |
127 |
1 |
when(this.servletContext.getContextPath()).thenReturn("/"); |
128 |
|
|
129 |
1 |
when(this.servletContext.getServletRegistration("action").getMappings()) |
130 |
|
.thenReturn(Arrays.asList("/*", "/bin/*", "/wiki/*", "/testbin/*")); |
131 |
|
|
132 |
1 |
HttpServletRequest req = createMockRequest(); |
133 |
1 |
when(req.getServletPath()).thenReturn("/"); |
134 |
|
|
135 |
1 |
assertEquals("/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
136 |
|
} |
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
138 |
1 |
@Test... |
139 |
|
public void normalizeFromNonActionRequestUsesDefaultServletMapping() throws Exception |
140 |
|
{ |
141 |
1 |
when(this.servletContext.getContextPath()).thenReturn("/xwiki"); |
142 |
|
|
143 |
1 |
HttpServletRequest req = createMockRequest(); |
144 |
1 |
when(req.getServletPath()).thenReturn("/rest"); |
145 |
|
|
146 |
1 |
assertEquals("/xwiki/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
149 |
1 |
@Test... |
150 |
|
public void normalizePreservesParameters() throws Exception |
151 |
|
{ |
152 |
1 |
when(this.servletContext.getContextPath()).thenReturn("/xwiki"); |
153 |
|
|
154 |
1 |
Map<String, List<String>> params = new HashMap<>(); |
155 |
1 |
params.put("age", Arrays.asList("32")); |
156 |
1 |
params.put("colors", Arrays.asList("red", "blue")); |
157 |
1 |
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"), params); |
158 |
|
|
159 |
1 |
assertSame(params, this.mocker.getComponentUnderTest().normalize(extendedURL).getParameters()); |
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
162 |
3 |
private HttpServletRequest createMockRequest()... |
163 |
|
{ |
164 |
3 |
ServletRequest request = mock(ServletRequest.class); |
165 |
3 |
when(this.container.getRequest()).thenReturn(request); |
166 |
|
|
167 |
3 |
HttpServletRequest httpRequest = mock(HttpServletRequest.class); |
168 |
3 |
when(request.getHttpServletRequest()).thenReturn(httpRequest); |
169 |
|
|
170 |
3 |
return httpRequest; |
171 |
|
} |
172 |
|
} |