1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.wiki; |
21 |
|
|
22 |
|
import java.lang.reflect.Type; |
23 |
|
import java.util.Collections; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import org.junit.Before; |
28 |
|
import org.junit.Rule; |
29 |
|
import org.junit.Test; |
30 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
31 |
|
import org.xwiki.bridge.SkinAccessBridge; |
32 |
|
import org.xwiki.model.EntityType; |
33 |
|
import org.xwiki.model.reference.AttachmentReference; |
34 |
|
import org.xwiki.model.reference.DocumentReference; |
35 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
36 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
37 |
|
import org.xwiki.rendering.configuration.ExtendedRenderingConfiguration; |
38 |
|
import org.xwiki.rendering.internal.resolver.DefaultResourceReferenceEntityReferenceResolver; |
39 |
|
import org.xwiki.rendering.listener.reference.AttachmentResourceReference; |
40 |
|
import org.xwiki.rendering.listener.reference.DocumentResourceReference; |
41 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
42 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
43 |
|
import org.xwiki.rendering.wiki.WikiModel; |
44 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
45 |
|
|
46 |
|
import static org.junit.Assert.*; |
47 |
|
import static org.mockito.Mockito.*; |
48 |
|
|
49 |
|
|
50 |
|
@link |
51 |
|
|
52 |
|
@version |
53 |
|
@since |
54 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (96) |
Complexity: 16 |
Complexity Density: 0.2 |
|
55 |
|
public class XWikiWikiModelTest |
56 |
|
{ |
57 |
|
@Rule |
58 |
|
public MockitoComponentMockingRule<WikiModel> mocker = new MockitoComponentMockingRule<WikiModel>( |
59 |
|
XWikiWikiModel.class); |
60 |
|
|
61 |
|
private EntityReferenceResolver<ResourceReference> referenceResolver; |
62 |
|
|
63 |
|
private DocumentAccessBridge documentAccessBridge; |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
65 |
14 |
@Before... |
66 |
|
public void configure() throws Exception |
67 |
|
{ |
68 |
14 |
this.referenceResolver = |
69 |
|
this.mocker.getInstance(DefaultResourceReferenceEntityReferenceResolver.TYPE_RESOURCEREFERENCE); |
70 |
14 |
this.documentAccessBridge = this.mocker.getInstance((Type) DocumentAccessBridge.class); |
71 |
|
}; |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
73 |
1 |
@Test... |
74 |
|
public void testGetDocumentEditURLWhenNoQueryStringSpecified() throws Exception |
75 |
|
{ |
76 |
1 |
DocumentResourceReference reference = new DocumentResourceReference("TargetSpace.TargetPage"); |
77 |
1 |
reference.setAnchor("anchor"); |
78 |
|
|
79 |
1 |
EntityReferenceSerializer<String> compactEntityReferenceSerializer = |
80 |
|
this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "compactwiki"); |
81 |
|
|
82 |
|
|
83 |
|
|
84 |
1 |
DocumentReference currentDocumentReference = new DocumentReference("Wiki", "Space", "Page\u20AC"); |
85 |
1 |
DocumentReference documentReference = new DocumentReference("TargetWiki", "TargetSpace", "TargetPage"); |
86 |
|
|
87 |
1 |
when(this.documentAccessBridge.getCurrentDocumentReference()).thenReturn(currentDocumentReference); |
88 |
1 |
when(compactEntityReferenceSerializer.serialize(currentDocumentReference)).thenReturn("Wiki:Space.Page\u20AC"); |
89 |
1 |
when(this.referenceResolver.resolve(reference, EntityType.DOCUMENT)).thenReturn(documentReference); |
90 |
|
|
91 |
1 |
this.mocker.getComponentUnderTest().getDocumentEditURL(reference); |
92 |
|
|
93 |
|
|
94 |
|
|
95 |
1 |
verify(this.documentAccessBridge).getDocumentURL(documentReference, "create", |
96 |
|
"parent=Wiki%3ASpace.Page%E2%82%AC", "anchor"); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
@throws |
103 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
104 |
1 |
@Test... |
105 |
|
public void testGetImageURLWhenBothWidthAndHeightAttributesAreSpecified() throws Exception |
106 |
|
{ |
107 |
1 |
Map<String, String> parameters = new HashMap<String, String>(); |
108 |
1 |
parameters.put("width", "100px"); |
109 |
1 |
parameters.put("height", "50"); |
110 |
|
|
111 |
1 |
testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "width=100&height=50"); |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
@throws |
119 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
120 |
1 |
@Test... |
121 |
|
public void testGetImageURLWhenIncludingImageDimensionsIsForbidden() throws Exception |
122 |
|
{ |
123 |
1 |
Map<String, String> parameters = new HashMap<String, String>(); |
124 |
1 |
parameters.put("width", "101px"); |
125 |
1 |
parameters.put("height", "55px"); |
126 |
|
|
127 |
1 |
testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, false, null); |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
@throws |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
135 |
1 |
@Test... |
136 |
|
public void testGetImageURLWhenBothWidthAndHeightCSSPropertiesAreSpecified() throws Exception |
137 |
|
{ |
138 |
1 |
Map<String, String> parameters = new HashMap<String, String>(); |
139 |
1 |
parameters.put("style", "border: 1px; height: 30px; margin-top: 2em; width: 70px"); |
140 |
|
|
141 |
1 |
testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "width=70&height=30"); |
142 |
|
} |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
@throws |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
149 |
1 |
@Test... |
150 |
|
public void testGetImageURLWhenOnlyWidthAttributeIsSpecified() throws Exception |
151 |
|
{ |
152 |
1 |
Map<String, String> parameters = new HashMap<String, String>(); |
153 |
1 |
parameters.put("width", "150"); |
154 |
1 |
parameters.put("height", "30%"); |
155 |
|
|
156 |
1 |
testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "width=150"); |
157 |
|
} |
158 |
|
|
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
@throws |
163 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
164 |
1 |
@Test... |
165 |
|
public void testGetImageURLWhenOnlyHeightCSSPropertyIsSpecified() throws Exception |
166 |
|
{ |
167 |
1 |
Map<String, String> parameters = new HashMap<String, String>(); |
168 |
1 |
parameters.put("style", "width: 5cm; height: 80px"); |
169 |
|
|
170 |
1 |
testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "height=80"); |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
@throws |
178 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
179 |
1 |
@Test... |
180 |
|
public void testGetImageURLWhenBothWidthAndHeightAreUnspecifiedAndImageSizeIsLimited() throws Exception |
181 |
|
{ |
182 |
1 |
ExtendedRenderingConfiguration configuration = this.mocker.getInstance(ExtendedRenderingConfiguration.class); |
183 |
1 |
when(configuration.getImageWidthLimit()).thenReturn(200); |
184 |
1 |
when(configuration.getImageHeightLimit()).thenReturn(170); |
185 |
1 |
Map<String, String> parameters = Collections.emptyMap(); |
186 |
|
|
187 |
1 |
testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, |
188 |
|
"width=200&height=170&keepAspectRatio=true"); |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
@throws |
196 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
197 |
1 |
@Test... |
198 |
|
public void testGetImageURLWhenBothWidthAndHeightAreUnspecifiedAndOnlyImageWidthIsLimited() throws Exception |
199 |
|
{ |
200 |
1 |
final ExtendedRenderingConfiguration configuration = |
201 |
|
this.mocker.getInstance(ExtendedRenderingConfiguration.class); |
202 |
1 |
when(configuration.getImageWidthLimit()).thenReturn(25); |
203 |
1 |
when(configuration.getImageHeightLimit()).thenReturn(-1); |
204 |
1 |
Map<String, String> parameters = new HashMap<String, String>(); |
205 |
1 |
parameters.put("width", "45%"); |
206 |
1 |
parameters.put("style", "height:10em"); |
207 |
|
|
208 |
1 |
testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "width=25"); |
209 |
|
} |
210 |
|
|
211 |
|
|
212 |
|
|
213 |
|
|
214 |
|
|
215 |
|
@throws |
216 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
217 |
1 |
@Test... |
218 |
|
public void testGetImageURLWhenBothWidthAndHeightAreUnspecifiedAndImageSizeIsNotLimited() throws Exception |
219 |
|
{ |
220 |
1 |
final ExtendedRenderingConfiguration configuration = |
221 |
|
this.mocker.getInstance(ExtendedRenderingConfiguration.class); |
222 |
1 |
when(configuration.getImageWidthLimit()).thenReturn(-1); |
223 |
1 |
when(configuration.getImageHeightLimit()).thenReturn(-1); |
224 |
1 |
Map<String, String> parameters = new HashMap<String, String>(); |
225 |
1 |
parameters.put("style", "bad CSS declaration"); |
226 |
|
|
227 |
1 |
testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, null); |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
@throws |
235 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
236 |
1 |
@Test... |
237 |
|
public void testGetImageURLWhenImageReferenceHasQueryString() throws Exception |
238 |
|
{ |
239 |
1 |
Map<String, String> parameters = new HashMap<String, String>(); |
240 |
1 |
parameters.put("height", "17"); |
241 |
1 |
parameters.put("width", "23"); |
242 |
|
|
243 |
1 |
AttachmentResourceReference reference = new AttachmentResourceReference("attachmentReference"); |
244 |
1 |
reference.setParameter(AttachmentResourceReference.QUERY_STRING, "width=41¶m=value"); |
245 |
1 |
testImageURL(reference, parameters, true, "width=41¶m=value&height=17"); |
246 |
|
} |
247 |
|
|
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
@throws |
252 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
253 |
1 |
@Test... |
254 |
|
public void testGetImageURLWhenBothStyleAndDimensionParametersAreSpecified() throws Exception |
255 |
|
{ |
256 |
1 |
Map<String, String> parameters = new HashMap<String, String>(); |
257 |
1 |
parameters.put("height", "46"); |
258 |
1 |
parameters.put("width", "101px"); |
259 |
1 |
parameters.put("style", "width: 20%; height:75px"); |
260 |
|
|
261 |
|
|
262 |
|
|
263 |
1 |
testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "height=75"); |
264 |
|
} |
265 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
266 |
1 |
@Test... |
267 |
|
public void testGetImageURLWhenIcon() throws Exception |
268 |
|
{ |
269 |
1 |
ResourceReference reference = new ResourceReference("iconname", ResourceType.ICON); |
270 |
1 |
SkinAccessBridge skinAccessBridge = this.mocker.getInstance((Type) SkinAccessBridge.class); |
271 |
1 |
when(skinAccessBridge.getIconURL("iconname")).thenReturn("/path/to/icon"); |
272 |
|
|
273 |
1 |
assertEquals("/path/to/icon", |
274 |
|
this.mocker.getComponentUnderTest().getImageURL(reference, Collections.<String, String>emptyMap())); |
275 |
|
} |
276 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
277 |
1 |
@Test... |
278 |
|
public void testGetDocumentViewURLWhenNoBaseReference() throws Exception |
279 |
|
{ |
280 |
1 |
ResourceReference reference = new ResourceReference("reference", ResourceType.DOCUMENT); |
281 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "space", "page"); |
282 |
1 |
when(this.referenceResolver.resolve(reference, EntityType.DOCUMENT)).thenReturn(documentReference); |
283 |
1 |
when(this.documentAccessBridge.getDocumentURL(documentReference, "view", null, null)).thenReturn("viewurl"); |
284 |
|
|
285 |
1 |
assertEquals("viewurl", this.mocker.getComponentUnderTest().getDocumentViewURL(reference)); |
286 |
|
} |
287 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
288 |
1 |
@Test... |
289 |
|
public void testGetDocumentViewURLWhenBaseReferenceSpecified() throws Exception |
290 |
|
{ |
291 |
1 |
ResourceReference reference = new ResourceReference("reference", ResourceType.DOCUMENT); |
292 |
1 |
reference.addBaseReference("base"); |
293 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "space", "page"); |
294 |
1 |
when(this.referenceResolver.resolve(reference, EntityType.DOCUMENT)).thenReturn(documentReference); |
295 |
1 |
when(this.documentAccessBridge.getDocumentURL(documentReference, "view", null, null)).thenReturn("viewurl"); |
296 |
|
|
297 |
1 |
assertEquals("viewurl", this.mocker.getComponentUnderTest().getDocumentViewURL(reference)); |
298 |
|
} |
299 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
300 |
10 |
private void testImageURL(final ResourceReference imageReference, Map<String, String> parameters,... |
301 |
|
final boolean expectedIsImageDimensionsIncludedInImageURL, final String expectedQueryString) throws Exception |
302 |
|
{ |
303 |
10 |
AttachmentReference attachmentReference = |
304 |
|
new AttachmentReference("image", new DocumentReference("wiki", "space", "page")); |
305 |
10 |
ExtendedRenderingConfiguration configuration = this.mocker.getInstance(ExtendedRenderingConfiguration.class); |
306 |
10 |
when(configuration.isImageDimensionsIncludedInImageURL()).thenReturn( |
307 |
|
expectedIsImageDimensionsIncludedInImageURL); |
308 |
10 |
when(this.referenceResolver.resolve(imageReference, EntityType.ATTACHMENT)).thenReturn(attachmentReference); |
309 |
10 |
when(this.documentAccessBridge.getAttachmentURL(attachmentReference, expectedQueryString, false)).thenReturn( |
310 |
|
"?" + expectedQueryString); |
311 |
|
|
312 |
10 |
assertEquals("?" + expectedQueryString, |
313 |
|
this.mocker.getComponentUnderTest().getImageURL(imageReference, parameters)); |
314 |
|
} |
315 |
|
} |