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.standard; |
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.configuration.ConfigurationSource; |
35 |
|
import org.xwiki.container.Container; |
36 |
|
import org.xwiki.container.servlet.ServletRequest; |
37 |
|
import org.xwiki.environment.Environment; |
38 |
|
import org.xwiki.environment.internal.ServletEnvironment; |
39 |
|
import org.xwiki.model.ModelContext; |
40 |
|
import org.xwiki.model.reference.WikiReference; |
41 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
42 |
|
import org.xwiki.url.ExtendedURL; |
43 |
|
import org.xwiki.url.URLNormalizer; |
44 |
|
|
45 |
|
import static org.junit.Assert.assertEquals; |
46 |
|
import static org.junit.Assert.assertSame; |
47 |
|
import static org.junit.Assert.fail; |
48 |
|
import static org.mockito.Mockito.mock; |
49 |
|
import static org.mockito.Mockito.when; |
50 |
|
|
51 |
|
|
52 |
|
@link |
53 |
|
|
54 |
|
@version |
55 |
|
@since |
56 |
|
|
|
|
| 98.3% |
Uncovered Elements: 1 (60) |
Complexity: 12 |
Complexity Density: 0.24 |
|
57 |
|
public class ContextAndActionURLNormalizerTest |
58 |
|
{ |
59 |
|
private ServletEnvironment environment; |
60 |
|
|
61 |
|
private Container container; |
62 |
|
|
63 |
|
private ServletContext servletContext; |
64 |
|
|
65 |
|
private ConfigurationSource xwikiCfg; |
66 |
|
|
67 |
|
private ModelContext modelContext; |
68 |
|
|
69 |
|
private StandardURLConfiguration urlConfiguration; |
70 |
|
|
71 |
|
private ExtendedURL testURL = new ExtendedURL(Arrays.asList("one", "two")); |
72 |
|
|
73 |
|
@Rule |
74 |
|
public MockitoComponentMockingRule<URLNormalizer<ExtendedURL>> mocker = |
75 |
|
new MockitoComponentMockingRule<URLNormalizer<ExtendedURL>>(ContextAndActionURLNormalizer.class); |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
77 |
9 |
@Before... |
78 |
|
public void configure() throws Exception |
79 |
|
{ |
80 |
|
|
81 |
9 |
this.container = this.mocker.getInstance(Container.class); |
82 |
9 |
this.environment = mock(ServletEnvironment.class); |
83 |
9 |
this.mocker.registerComponent(Environment.class, this.environment); |
84 |
9 |
this.servletContext = mock(ServletContext.class); |
85 |
9 |
when(this.environment.getServletContext()).thenReturn(this.servletContext); |
86 |
9 |
ServletRegistration sr = mock(ServletRegistration.class); |
87 |
9 |
when(this.servletContext.getServletRegistration("action")).thenReturn(sr); |
88 |
9 |
when(sr.getMappings()).thenReturn(Arrays.asList("/bin/*", "/wiki/*", "/testbin/*")); |
89 |
|
|
90 |
|
|
91 |
9 |
this.xwikiCfg = this.mocker.getInstance(ConfigurationSource.class, "xwikicfg"); |
92 |
9 |
this.modelContext = this.mocker.getInstance(ModelContext.class); |
93 |
9 |
this.urlConfiguration = this.mocker.getInstance(StandardURLConfiguration.class); |
94 |
9 |
when(this.urlConfiguration.getEntityPathPrefix()).thenReturn("bin"); |
95 |
9 |
when(this.urlConfiguration.getWikiPathPrefix()).thenReturn("wiki"); |
96 |
|
} |
97 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
98 |
1 |
@Test... |
99 |
|
public void normalizeWithNoKnownContextPathThrowsException() throws Exception |
100 |
|
{ |
101 |
1 |
this.mocker.registerMockComponent(Environment.class); |
102 |
1 |
try { |
103 |
1 |
this.mocker.getComponentUnderTest().normalize(this.testURL); |
104 |
0 |
fail("Should have thrown an exception"); |
105 |
|
} catch (RuntimeException expected) { |
106 |
1 |
assertEquals("Failed to normalize the URL [/one/two] since the application's Servlet context couldn't be " |
107 |
|
+ "computed.", expected.getMessage()); |
108 |
|
} |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
111 |
1 |
@Test... |
112 |
|
public void normalizeUsesTheSpecifiedConfiguration() throws Exception |
113 |
|
{ |
114 |
1 |
when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("good"); |
115 |
1 |
when(this.servletContext.getContextPath()).thenReturn("/bad"); |
116 |
|
|
117 |
1 |
assertEquals("/good/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
118 |
|
} |
119 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
120 |
1 |
@Test... |
121 |
|
public void normalizeRemovesLeadingAndTrailingSlashFromConfiguration() throws Exception |
122 |
|
{ |
123 |
1 |
when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("/xwiki/"); |
124 |
|
|
125 |
1 |
assertEquals("/xwiki/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
126 |
|
} |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
128 |
1 |
@Test... |
129 |
|
public void normalizeWithRootConfiguration() throws Exception |
130 |
|
{ |
131 |
1 |
when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn(""); |
132 |
1 |
when(this.servletContext.getContextPath()).thenReturn("/bad"); |
133 |
|
|
134 |
1 |
assertEquals("/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
137 |
1 |
@Test... |
138 |
|
public void normalizeWithSlashRootConfiguration() throws Exception |
139 |
|
{ |
140 |
1 |
when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("/"); |
141 |
1 |
when(this.servletContext.getContextPath()).thenReturn("/bad"); |
142 |
|
|
143 |
1 |
assertEquals("/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
146 |
1 |
@Test... |
147 |
|
public void normalizeWithNoConfigurationUsesServletContext() throws Exception |
148 |
|
{ |
149 |
1 |
when(this.servletContext.getContextPath()).thenReturn("/xwiki"); |
150 |
|
|
151 |
1 |
assertEquals("/xwiki/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
152 |
|
} |
153 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
154 |
1 |
@Test... |
155 |
|
public void normalizeWithNoConfigurationAndRootServletContext() throws Exception |
156 |
|
{ |
157 |
1 |
when(this.servletContext.getContextPath()).thenReturn(""); |
158 |
|
|
159 |
1 |
assertEquals("/bin/one/two", this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
162 |
1 |
@Test... |
163 |
|
public void normalizeFromVirtualWikiRequestPreservesWikiPath() throws Exception |
164 |
|
{ |
165 |
1 |
when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("xwiki"); |
166 |
|
|
167 |
1 |
HttpServletRequest req = createMockRequest(); |
168 |
1 |
when(req.getServletPath()).thenReturn("/wiki"); |
169 |
1 |
when(this.modelContext.getCurrentEntityReference()).thenReturn(new WikiReference("dev")); |
170 |
|
|
171 |
1 |
assertEquals("/xwiki/wiki/dev/one/two", |
172 |
|
this.mocker.getComponentUnderTest().normalize(this.testURL).serialize()); |
173 |
|
} |
174 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
175 |
1 |
@Test... |
176 |
|
public void normalizePreservesParameters() throws Exception |
177 |
|
{ |
178 |
1 |
when(this.xwikiCfg.getProperty("xwiki.webapppath")).thenReturn("xwiki"); |
179 |
|
|
180 |
1 |
Map<String, List<String>> params = new HashMap<>(); |
181 |
1 |
params.put("age", Arrays.asList("32")); |
182 |
1 |
params.put("colors", Arrays.asList("red", "blue")); |
183 |
1 |
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"), params); |
184 |
|
|
185 |
1 |
assertSame(params, this.mocker.getComponentUnderTest().normalize(extendedURL).getParameters()); |
186 |
|
} |
187 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
188 |
1 |
private HttpServletRequest createMockRequest()... |
189 |
|
{ |
190 |
1 |
ServletRequest request = mock(ServletRequest.class); |
191 |
1 |
when(this.container.getRequest()).thenReturn(request); |
192 |
|
|
193 |
1 |
HttpServletRequest httpRequest = mock(HttpServletRequest.class); |
194 |
1 |
when(request.getHttpServletRequest()).thenReturn(httpRequest); |
195 |
|
|
196 |
1 |
return httpRequest; |
197 |
|
} |
198 |
|
} |