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 |
|
|
29 |
|
import org.junit.Before; |
30 |
|
import org.junit.Rule; |
31 |
|
import org.junit.Test; |
32 |
|
import org.xwiki.configuration.ConfigurationSource; |
33 |
|
import org.xwiki.environment.Environment; |
34 |
|
import org.xwiki.environment.internal.ServletEnvironment; |
35 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
36 |
|
import org.xwiki.url.ExtendedURL; |
37 |
|
import org.xwiki.url.URLNormalizer; |
38 |
|
|
39 |
|
import static org.junit.Assert.assertEquals; |
40 |
|
import static org.junit.Assert.assertSame; |
41 |
|
import static org.junit.Assert.fail; |
42 |
|
import static org.mockito.Mockito.mock; |
43 |
|
import static org.mockito.Mockito.when; |
44 |
|
|
45 |
|
|
46 |
|
@link |
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
|
|
| 97.7% |
Uncovered Elements: 1 (43) |
Complexity: 9 |
Complexity Density: 0.26 |
|
51 |
|
public class ExtendedURLURLNormalizerTest |
52 |
|
{ |
53 |
|
private ConfigurationSource configurationSource; |
54 |
|
|
55 |
|
@Rule |
56 |
|
public MockitoComponentMockingRule<URLNormalizer<ExtendedURL>> mocker = |
57 |
|
new MockitoComponentMockingRule<URLNormalizer<ExtendedURL>>(ExtendedURLURLNormalizer.class); |
58 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
59 |
7 |
@Before... |
60 |
|
public void configure() throws Exception |
61 |
|
{ |
62 |
7 |
this.configurationSource = this.mocker.getInstance(ConfigurationSource.class, "xwikicfg"); |
63 |
|
} |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
65 |
1 |
@Test... |
66 |
|
public void normalizeWhenConfigurationPropertyDefined() throws Exception |
67 |
|
{ |
68 |
1 |
when(this.configurationSource.getProperty("xwiki.webapppath")).thenReturn("xwiki"); |
69 |
|
|
70 |
1 |
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two")); |
71 |
1 |
assertEquals("/xwiki/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize()); |
72 |
|
} |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
74 |
1 |
@Test... |
75 |
|
public void normalizeWhenConfigurationPropertyDefinedButWithLeadingAndTrailingSlash() throws Exception |
76 |
|
{ |
77 |
1 |
when(this.configurationSource.getProperty("xwiki.webapppath")).thenReturn("/xwiki/"); |
78 |
|
|
79 |
1 |
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two")); |
80 |
1 |
assertEquals("/xwiki/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize()); |
81 |
|
} |
82 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
83 |
1 |
@Test... |
84 |
|
public void normalizeWhenNoConfigurationPropertyButEnvironment() throws Exception |
85 |
|
{ |
86 |
1 |
ServletContext sc = mock(ServletContext.class); |
87 |
1 |
ServletEnvironment environment = mock(ServletEnvironment.class); |
88 |
1 |
this.mocker.registerComponent(Environment.class, environment); |
89 |
1 |
when(environment.getServletContext()).thenReturn(sc); |
90 |
1 |
when(sc.getContextPath()).thenReturn("/xwiki"); |
91 |
|
|
92 |
1 |
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two")); |
93 |
1 |
assertEquals("/xwiki/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize()); |
94 |
|
} |
95 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
96 |
1 |
@Test... |
97 |
|
public void normalizeWhenNoConfigurationPropertyButEnvironmentWithRootContext() throws Exception |
98 |
|
{ |
99 |
1 |
ServletContext sc = mock(ServletContext.class); |
100 |
1 |
ServletEnvironment environment = mock(ServletEnvironment.class); |
101 |
1 |
this.mocker.registerComponent(Environment.class, environment); |
102 |
1 |
when(environment.getServletContext()).thenReturn(sc); |
103 |
1 |
when(sc.getContextPath()).thenReturn(""); |
104 |
|
|
105 |
1 |
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two")); |
106 |
1 |
assertEquals("/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize()); |
107 |
|
} |
108 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
109 |
1 |
@Test... |
110 |
|
public void normalizeWhenNoConfigurationPropertyAndNoServletEnvironment() throws Exception |
111 |
|
{ |
112 |
1 |
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two")); |
113 |
1 |
try { |
114 |
1 |
this.mocker.getComponentUnderTest().normalize(extendedURL); |
115 |
0 |
fail("Should have thrown an exception"); |
116 |
|
} catch (RuntimeException expected) { |
117 |
1 |
assertEquals("Failed to normalize the URL [/one/two] since the application's Servlet context couldn't be " |
118 |
|
+ "computed.", expected.getMessage()); |
119 |
|
} |
120 |
|
} |
121 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
122 |
1 |
@Test... |
123 |
|
public void normalizeExtendedURLWithParameters() throws Exception |
124 |
|
{ |
125 |
1 |
when(this.configurationSource.getProperty("xwiki.webapppath")).thenReturn("xwiki"); |
126 |
|
|
127 |
1 |
Map<String, List<String>> params = new HashMap<>(); |
128 |
1 |
params.put("age", Arrays.asList("32")); |
129 |
1 |
params.put("colors", Arrays.asList("red", "blue")); |
130 |
|
|
131 |
1 |
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two"), params); |
132 |
1 |
assertSame(params, this.mocker.getComponentUnderTest().normalize(extendedURL).getParameters()); |
133 |
|
} |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
135 |
1 |
@Test... |
136 |
|
public void normalizeWhenRootWebapp() throws Exception |
137 |
|
{ |
138 |
1 |
when(this.configurationSource.getProperty("xwiki.webapppath")).thenReturn(""); |
139 |
|
|
140 |
1 |
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "two")); |
141 |
1 |
assertEquals("/one/two", this.mocker.getComponentUnderTest().normalize(extendedURL).serialize()); |
142 |
|
} |
143 |
|
} |