1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.url; |
21 |
|
|
22 |
|
import java.net.URI; |
23 |
|
import java.net.URL; |
24 |
|
import java.util.Arrays; |
25 |
|
import java.util.LinkedHashMap; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Map; |
28 |
|
|
29 |
|
import org.junit.Test; |
30 |
|
import org.xwiki.resource.CreateResourceReferenceException; |
31 |
|
|
32 |
|
import static org.hamcrest.CoreMatchers.hasItem; |
33 |
|
import static org.hamcrest.CoreMatchers.hasItems; |
34 |
|
import static org.junit.Assert.*; |
35 |
|
|
36 |
|
|
37 |
|
@link |
38 |
|
|
39 |
|
@version |
40 |
|
@since |
41 |
|
|
|
|
| 96.9% |
Uncovered Elements: 2 (64) |
Complexity: 13 |
Complexity Density: 0.25 |
|
42 |
|
public class ExtendedURLTest |
43 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
44 |
1 |
@Test... |
45 |
|
public void withoutPrefix() throws Exception |
46 |
|
{ |
47 |
1 |
URL url = new URL("http://localhost:8080/some/path"); |
48 |
1 |
ExtendedURL extendedURL = new ExtendedURL(url, null); |
49 |
1 |
assertEquals(new URI("http://localhost:8080/some/path"), extendedURL.getURI()); |
50 |
1 |
assertEquals(2, extendedURL.getSegments().size()); |
51 |
1 |
assertThat(extendedURL.getSegments(), hasItems("some", "path")); |
52 |
|
} |
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
54 |
1 |
@Test... |
55 |
|
public void withPrefix() throws Exception |
56 |
|
{ |
57 |
1 |
URL url = new URL("http://localhost:8080/xwiki/path"); |
58 |
1 |
ExtendedURL extendedURL = new ExtendedURL(url, "xwiki"); |
59 |
1 |
assertEquals(new URI("http://localhost:8080/xwiki/path"), extendedURL.getURI()); |
60 |
1 |
assertEquals(1, extendedURL.getSegments().size()); |
61 |
1 |
assertThat(extendedURL.getSegments(), hasItems("path")); |
62 |
|
} |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
64 |
1 |
@Test... |
65 |
|
public void withPrefixAndNoSlashAfter() throws Exception |
66 |
|
{ |
67 |
1 |
URL url = new URL("http://localhost:8080/xwiki"); |
68 |
1 |
ExtendedURL extendedURL = new ExtendedURL(url, "xwiki"); |
69 |
1 |
assertEquals(new URI("http://localhost:8080/xwiki"), extendedURL.getURI()); |
70 |
1 |
assertEquals(0, extendedURL.getSegments().size()); |
71 |
1 |
assertTrue(extendedURL.getSegments().isEmpty()); |
72 |
|
} |
73 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
74 |
1 |
@Test... |
75 |
|
public void withPrefixStartingWithSlash() throws Exception |
76 |
|
{ |
77 |
1 |
URL url = new URL("http://localhost:8080/xwiki/path"); |
78 |
1 |
ExtendedURL extendedURL = new ExtendedURL(url, "/xwiki"); |
79 |
1 |
assertEquals(new URI("http://localhost:8080/xwiki/path"), extendedURL.getURI()); |
80 |
1 |
assertEquals(1, extendedURL.getSegments().size()); |
81 |
1 |
assertThat(extendedURL.getSegments(), hasItems("path")); |
82 |
|
} |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
84 |
1 |
@Test... |
85 |
|
public void withPrefixStartingWithSlashAndNoSlashAfter() throws Exception |
86 |
|
{ |
87 |
1 |
URL url = new URL("http://localhost:8080/xwiki"); |
88 |
1 |
ExtendedURL extendedURL = new ExtendedURL(url, "/xwiki"); |
89 |
1 |
assertEquals(new URI("http://localhost:8080/xwiki"), extendedURL.getURI()); |
90 |
1 |
assertEquals(0, extendedURL.getSegments().size()); |
91 |
1 |
assertTrue(extendedURL.getSegments().isEmpty()); |
92 |
|
} |
93 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
94 |
1 |
@Test... |
95 |
|
public void withInvalidPrefix() throws Exception |
96 |
|
{ |
97 |
1 |
URL url = new URL("http://localhost:8080/some/path"); |
98 |
1 |
try { |
99 |
1 |
new ExtendedURL(url, "/xwiki"); |
100 |
0 |
fail("Should have thrown an exception here"); |
101 |
|
} catch (CreateResourceReferenceException e) { |
102 |
1 |
assertEquals("URL Path [/some/path] doesn't start with [/xwiki]", e.getMessage()); |
103 |
|
} |
104 |
|
} |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
106 |
1 |
@Test... |
107 |
|
public void equality() throws Exception |
108 |
|
{ |
109 |
1 |
ExtendedURL extendedURL1 = new ExtendedURL(new URL("http://localhost:8080/some/path"), null); |
110 |
1 |
ExtendedURL extendedURL2 = new ExtendedURL(new URL("http://localhost:8080/some/path"), null); |
111 |
1 |
assertEquals(extendedURL1, extendedURL2); |
112 |
|
} |
113 |
|
|
|
|
| 75% |
Uncovered Elements: 1 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
1PASS
|
|
114 |
1 |
@Test... |
115 |
|
public void invalidURL() throws Exception |
116 |
|
{ |
117 |
1 |
try { |
118 |
|
|
119 |
1 |
new ExtendedURL(new URL("http://host/xwiki/bin/view/space/page name"), null); |
120 |
0 |
fail("Should have thrown an exception here"); |
121 |
|
} catch (CreateResourceReferenceException expected) { |
122 |
1 |
assertEquals("Invalid URL [http://host/xwiki/bin/view/space/page name]", expected.getMessage()); |
123 |
|
} |
124 |
|
} |
125 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
126 |
1 |
@Test... |
127 |
|
public void withEncodedChars() throws Exception |
128 |
|
{ |
129 |
1 |
ExtendedURL extendedURL = |
130 |
|
new ExtendedURL(new URL("http://host/xwiki/bin/view/space/page%20name?param=%2D"), null); |
131 |
1 |
assertThat(extendedURL.getSegments(), hasItem("page name")); |
132 |
1 |
assertEquals("param=-", extendedURL.getURI().getQuery()); |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
138 |
1 |
@Test... |
139 |
|
public void ignoreSpecialPathParameters() throws Exception |
140 |
|
{ |
141 |
1 |
ExtendedURL extendedURL = new ExtendedURL( |
142 |
|
new URL("http://host/xwiki/bin/view/space;param1=value1/page;param2=value2"), "xwiki"); |
143 |
1 |
assertThat(extendedURL.getSegments(), hasItem("space")); |
144 |
1 |
assertThat(extendedURL.getSegments(), hasItem("page")); |
145 |
|
|
146 |
|
|
147 |
|
|
148 |
1 |
extendedURL = new ExtendedURL(new URL("http://host/xwiki/bin/view/space/my%3Bpage"), "xwiki"); |
149 |
1 |
assertThat(extendedURL.getSegments(), hasItem("my;page")); |
150 |
|
} |
151 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
152 |
1 |
@Test... |
153 |
|
public void serialize() throws Exception |
154 |
|
{ |
155 |
|
|
156 |
1 |
ExtendedURL extendedURL = new ExtendedURL(Arrays.asList("one", "with/slash", "with space")); |
157 |
1 |
assertEquals("/one/with%2Fslash/with+space", extendedURL.serialize()); |
158 |
1 |
assertEquals("/one/with%2Fslash/with+space", extendedURL.toString()); |
159 |
|
|
160 |
|
|
161 |
1 |
Map<String, List<String>> parameters = new LinkedHashMap<>(); |
162 |
1 |
parameters.put("key1", Arrays.asList("value1")); |
163 |
1 |
parameters.put("key2", Arrays.asList("value2", "value3")); |
164 |
1 |
extendedURL = new ExtendedURL(Arrays.asList("one", "with/slash", "with space"), parameters); |
165 |
1 |
assertEquals("/one/with%2Fslash/with+space?key1=value1&key2=value2&key2=value3", extendedURL.serialize()); |
166 |
|
} |
167 |
|
} |