1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.wiki

File XWikiWikiModelTest.java

 

Code metrics

0
80
16
1
315
188
16
0.2
5
16
1

Classes

Class Line # Actions
XWikiWikiModelTest 55 80 0% 16 0
1.0100%
 

Contributing tests

This file is covered by 14 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
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    * Unit tests for {@link XWikiWikiModel}.
51    *
52    * @version $Id: 2e322fbd44220b02237d7d1154a07d7bd4c8e248 $
53    * @since 2.0M1
54    */
 
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   
 
65  14 toggle @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   
 
73  1 toggle @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    // Note: we use a character that needs to be encoded in the current document's page name to make sure the
83    // generate query string is encoded.
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    // Verify that getDocumentURL is called with the query string already encoded since getDocumentURL() doesn't
94    // encode it.
95  1 verify(this.documentAccessBridge).getDocumentURL(documentReference, "create",
96    "parent=Wiki%3ASpace.Page%E2%82%AC", "anchor");
97    }
98   
99    /**
100    * Tests that the proper image URL is generated when both the width and the height image parameters are specified.
101    *
102    * @throws Exception if an exception occurs while running the test
103    */
 
104  1 toggle @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    * Tests that the proper image URL is generated when both the width and the height image parameters are specified
116    * but including them in the image URL is forbidden from the rendering configuration.
117    *
118    * @throws Exception if an exception occurs while running the test
119    */
 
120  1 toggle @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    * Tests that the proper image URL is generated when both the width and the height CSS properties are specified.
132    *
133    * @throws Exception if an exception occurs while running the test
134    */
 
135  1 toggle @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    * Tests that the proper image URL is generated when only the width image parameter is specified.
146    *
147    * @throws Exception if an exception occurs while running the test
148    */
 
149  1 toggle @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    * Tests that the proper image URL is generated when only the height CSS property is specified.
161    *
162    * @throws Exception if an exception occurs while running the test
163    */
 
164  1 toggle @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    * Tests that the proper image URL is generated when both the width and the height are unspecified and image size is
175    * limited in the configuration.
176    *
177    * @throws Exception if an exception occurs while running the test
178    */
 
179  1 toggle @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    * Tests that the proper image URL is generated when both the width and the height are unspecified and only the
193    * image width is limited in the configuration.
194    *
195    * @throws Exception if an exception occurs while running the test
196    */
 
197  1 toggle @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    * Tests that the proper image URL is generated when both the width and the height are unspecified and the image
213    * size is not limited in the configuration.
214    *
215    * @throws Exception if an exception occurs while running the test
216    */
 
217  1 toggle @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    * Tests that the proper image URL is generated when the attachment URL has a query string and a fragment
232    * identifier.
233    *
234    * @throws Exception if an exception occurs while running the test
235    */
 
236  1 toggle @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&param=value");
245  1 testImageURL(reference, parameters, true, "width=41&param=value&height=17");
246    }
247   
248    /**
249    * Tests that the proper image URL is generated when both the style and the dimension parameters are specified.
250    *
251    * @throws Exception if an exception occurs while running the test
252    */
 
253  1 toggle @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    // Note that the style parameter take precedence over the dimension parameters and the width is actually 20% but
262    // we can't use it for resizing the image on the server side so it's omitted from the query string.
263  1 testImageURL(new AttachmentResourceReference("attachmentReference"), parameters, true, "height=75");
264    }
265   
 
266  1 toggle @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   
 
277  1 toggle @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   
 
288  1 toggle @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   
 
300  10 toggle 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    }