1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.url.internal.standard.entity

File BinEntityResourceReferenceResolverTest.java

 

Code metrics

6
77
9
1
287
187
12
0.16
8.56
9
1.33

Classes

Class Line # Actions
BinEntityResourceReferenceResolverTest 53 77 0% 12 1
0.9891304498.9%
 

Contributing tests

This file is covered by 5 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.url.internal.standard.entity;
21   
22    import java.net.URL;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.LinkedHashMap;
26    import java.util.List;
27    import java.util.Map;
28   
29    import org.junit.Before;
30    import org.junit.Test;
31    import org.xwiki.component.util.ReflectionUtils;
32    import org.xwiki.model.EntityType;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.EntityReferenceResolver;
35    import org.xwiki.model.reference.WikiReference;
36    import org.xwiki.resource.ResourceType;
37    import org.xwiki.resource.entity.EntityResourceReference;
38    import org.xwiki.resource.ResourceReference;
39    import org.xwiki.resource.internal.entity.EntityResourceActionLister;
40    import org.xwiki.url.ExtendedURL;
41    import org.xwiki.url.internal.standard.StandardURLConfiguration;
42    import org.xwiki.url.internal.standard.WikiReferenceExtractor;
43   
44    import static org.junit.Assert.assertEquals;
45    import static org.mockito.Mockito.*;
46   
47    /**
48    * Unit tests for {@link BinEntityResourceReferenceResolver}.
49    *
50    * @version $Id: 6ef55faab2e53ab3324fcefd9b0bf2d6325c96a1 $
51    * @since 6.1M2
52    */
 
53    public class BinEntityResourceReferenceResolverTest
54    {
55    private BinEntityResourceReferenceResolver resolver;
56   
57    private WikiReference wikiReference = new WikiReference("wiki");
58   
59    private WikiReferenceExtractor wikiReferenceExtractor;
60   
61    private EntityReferenceResolver<EntityReference> entityReferenceResolver;
62   
63    private EntityResourceActionLister entityResourceActionLister;
64   
65    private StandardURLConfiguration configuration;
66   
 
67  5 toggle @Before
68    public void setUp() throws Exception
69    {
70  5 this.resolver = new BinEntityResourceReferenceResolver();
71   
72  5 this.wikiReferenceExtractor = mock(WikiReferenceExtractor.class);
73  5 when(this.wikiReferenceExtractor.extract(any(ExtendedURL.class))).thenReturn(this.wikiReference);
74  5 ReflectionUtils.setFieldValue(this.resolver, "wikiExtractor", this.wikiReferenceExtractor);
75   
76  5 this.entityReferenceResolver = mock(EntityReferenceResolver.class);
77  5 ReflectionUtils.setFieldValue(this.resolver, "defaultReferenceEntityReferenceResolver",
78    this.entityReferenceResolver);
79   
80  5 this.configuration = mock(StandardURLConfiguration.class);
81  5 ReflectionUtils.setFieldValue(this.resolver, "configuration", this.configuration);
82   
83  5 this.entityResourceActionLister = mock(EntityResourceActionLister.class);
84  5 when(this.entityResourceActionLister.listActions()).thenReturn(Arrays.asList("view", "download"));
85  5 ReflectionUtils.setFieldValue(this.resolver, "entityResourceActionLister", this.entityResourceActionLister);
86    }
87   
 
88  1 toggle @Test
89    public void createResourceWhenViewActionHidden() throws Exception
90    {
91  1 when(this.configuration.isViewActionHidden()).thenReturn(true);
92   
93  1 EntityReference fullSingleSpaceReference = buildEntityReference("wiki", Arrays.asList("space"), "page");
94  1 EntityReference fullTwoSpacesReference =
95    buildEntityReference("wiki", Arrays.asList("space1", "space2"), "page");
96   
97    // Test when no segment
98  1 testCreateResource("http://localhost/bin", "view", this.wikiReference, fullSingleSpaceReference,
99    EntityType.DOCUMENT);
100   
101    // Test when no segment but trailing slash
102  1 testCreateResource("http://localhost/bin/", "view", this.wikiReference, fullSingleSpaceReference,
103    EntityType.DOCUMENT);
104   
105    // Test when single space segment, to be Nested Document friendly.
106    // Normally the last segment is always the page name but we want to handle a special case when we
107    // have "/view/something" and we wish in this case to consider that "something" is the space. This
108    // is to handle Nested Documents, so that the user can have a top level Nested Document
109    // (something.WebHome) and access it from /view/something. If we didn't handle this special case
110    // the user would get Main.something and thus wouldn't be able to access something.WebHome. He'd
111    // need to use /view/something/ which is not natural in the Nested Document mode.
112  1 testCreateResource("http://localhost/bin/space", "view",
113    buildEntityReference("wiki", Arrays.asList("space"), null), fullSingleSpaceReference, EntityType.DOCUMENT);
114   
115    // Test when 1 space segment and trailing slash
116  1 testCreateResource("http://localhost/bin/space/", "view",
117    buildEntityReference("wiki", Arrays.asList("space"), null), fullSingleSpaceReference, EntityType.DOCUMENT);
118   
119    // Test when 2 space segments and trailing slash
120  1 testCreateResource("http://localhost/bin/space1/space2/", "view",
121    buildEntityReference("wiki", Arrays.asList("space1", "space2"), null), fullTwoSpacesReference,
122    EntityType.DOCUMENT);
123   
124    // Test when 1 space and page segments
125  1 testCreateResource("http://localhost/bin/space/page", "view", fullSingleSpaceReference,
126    fullSingleSpaceReference, EntityType.DOCUMENT);
127   
128    // Test when 2 spaces and page segments
129  1 testCreateResource("http://localhost/bin/space1/space2/page", "view", fullTwoSpacesReference,
130    fullTwoSpacesReference, EntityType.DOCUMENT);
131   
132    // Test when space segment is called "view"
133  1 testCreateResource("http://localhost/bin/view/space/page", "view", fullSingleSpaceReference,
134    fullSingleSpaceReference, EntityType.DOCUMENT);
135  1 EntityReference viewTwoSpacesReference =
136    buildEntityReference("wiki", Arrays.asList("view", "space2"), "page");
137  1 testCreateResource("http://localhost/bin/view/view/space2/page", "view", viewTwoSpacesReference,
138    viewTwoSpacesReference, EntityType.DOCUMENT);
139   
140    // Test when space segment is called "download"
141  1 EntityReference downloadTwoSpacesReference =
142    buildEntityReference("wiki", Arrays.asList("download", "space2"), "page");
143  1 testCreateResource("http://localhost/bin/view/download/space2/page", "view", downloadTwoSpacesReference,
144    downloadTwoSpacesReference, EntityType.DOCUMENT);
145   
146    // Test when download action
147  1 testCreateResource("http://localhost/bin/download/space/page", "download", fullSingleSpaceReference,
148    fullSingleSpaceReference, EntityType.DOCUMENT);
149    }
150   
 
151  1 toggle @Test
152    public void createResourceWhenViewActionShown() throws Exception
153    {
154  1 when(this.configuration.isViewActionHidden()).thenReturn(false);
155   
156  1 EntityReference fullSingleSpaceReference = buildEntityReference("wiki", Arrays.asList("space"), "page");
157  1 EntityReference fullTwoSpacesReference =
158    buildEntityReference("wiki", Arrays.asList("space1", "space2"), "page");
159   
160    // Test when 1 space segment to be Nested Document friendly (see the test above for more explanations)
161  1 testCreateResource("http://localhost/bin/view/space", "view",
162    buildEntityReference("wiki", Arrays.asList("space"), null), fullSingleSpaceReference, EntityType.DOCUMENT);
163   
164    // Test when 1 space segment and trailing slash
165  1 testCreateResource("http://localhost/bin/view/space/", "view",
166    buildEntityReference("wiki", Arrays.asList("space"), null), fullSingleSpaceReference, EntityType.DOCUMENT);
167   
168    // Test when 2 space segments and trailing slash
169  1 testCreateResource("http://localhost/bin/view/space1/space2/", "view",
170    buildEntityReference("wiki", Arrays.asList("space1", "space2"), null), fullTwoSpacesReference,
171    EntityType.DOCUMENT);
172   
173    // Test when 1 space and page segments
174  1 testCreateResource("http://localhost/bin/view/space/page", "view", fullSingleSpaceReference,
175    fullSingleSpaceReference, EntityType.DOCUMENT);
176   
177    // Test when 2 spaces and page segments
178  1 testCreateResource("http://localhost/bin/view/space1/space2/page", "view", fullTwoSpacesReference,
179    fullTwoSpacesReference, EntityType.DOCUMENT);
180   
181    // Test when no "view" specified and space that is not an actio name
182  1 testCreateResource("http://localhost/bin/space/page", "view", fullSingleSpaceReference,
183    fullSingleSpaceReference, EntityType.DOCUMENT);
184   
185    }
186   
 
187  1 toggle @Test
188    public void createResourceWhenSpaceAndPageNamesContainDots() throws Exception
189    {
190  1 EntityReference reference = buildEntityReference("wiki", Arrays.asList("space.with.dots"), "page.with.dots");
191  1 testCreateResource("http://localhost/bin/view/space.with.dots/page.with.dots", "view",
192    reference, reference, EntityType.DOCUMENT);
193    }
194   
 
195  1 toggle @Test
196    public void createResourceWhenFileActionAction() throws Exception
197    {
198  1 EntityReference singleSpaceReference =
199    buildEntityReference("wiki", Arrays.asList("space"), "page", "attachment.ext");
200  1 EntityReference twoSpaceReference =
201    buildEntityReference("wiki", Arrays.asList("space1", "space2"), "page", "attachment.ext");
202   
203  1 testCreateResource("http://localhost/bin/download/space/page/attachment.ext", "download", singleSpaceReference,
204    singleSpaceReference, EntityType.ATTACHMENT);
205  1 testCreateResource("http://localhost/bin/download/space1/space2/page/attachment.ext", "download",
206    twoSpaceReference, twoSpaceReference, EntityType.ATTACHMENT);
207   
208  1 testCreateResource("http://localhost/bin/delattachment/space/page/attachment.ext", "delattachment",
209    singleSpaceReference, singleSpaceReference, EntityType.ATTACHMENT);
210  1 testCreateResource("http://localhost/bin/delattachment/space1/space2/page/attachment.ext", "delattachment",
211    twoSpaceReference, twoSpaceReference, EntityType.ATTACHMENT);
212   
213  1 testCreateResource("http://localhost/bin/viewattachrev/space/page/attachment.ext", "viewattachrev",
214    singleSpaceReference, singleSpaceReference, EntityType.ATTACHMENT);
215  1 testCreateResource("http://localhost/bin/viewattachrev/space1/space2/page/attachment.ext", "viewattachrev",
216    twoSpaceReference, twoSpaceReference, EntityType.ATTACHMENT);
217   
218  1 testCreateResource("http://localhost/bin/downloadrev/space/page/attachment.ext", "downloadrev",
219    singleSpaceReference, singleSpaceReference, EntityType.ATTACHMENT);
220  1 testCreateResource("http://localhost/bin/downloadrev/space1/space2/page/attachment.ext", "downloadrev",
221    twoSpaceReference, twoSpaceReference, EntityType.ATTACHMENT);
222    }
223   
 
224  1 toggle @Test
225    public void createResourceWhenURLHasParameters() throws Exception
226    {
227  1 EntityReference fullReference = buildEntityReference("wiki", Arrays.asList("space"), "page");
228  1 ResourceReference resource =
229    testCreateResource("http://localhost/bin/view/space/page?param1=value1&param2=value2", "view",
230    fullReference, fullReference, EntityType.DOCUMENT);
231   
232    // Assert parameters
233    // Note: the parameters order are the same as the order specified in the URL.
234  1 Map<String, List<String>> expectedMap = new LinkedHashMap<String, List<String>>();
235  1 expectedMap.put("param1", Arrays.asList("value1"));
236  1 expectedMap.put("param2", Arrays.asList("value2"));
237  1 assertEquals(expectedMap, resource.getParameters());
238   
239    // Also verify it works when there's a param with no value.
240  1 resource = testCreateResource("http://localhost/bin/view/space/page?param",
241    "view", fullReference, fullReference, EntityType.DOCUMENT);
242  1 expectedMap = new LinkedHashMap<>();
243  1 expectedMap.put("param", Collections.<String>emptyList());
244  1 assertEquals(expectedMap, resource.getParameters());
245    }
246   
 
247  28 toggle private ResourceReference testCreateResource(String testURL, String expectedActionName,
248    EntityReference expectedReference, EntityReference returnedReference, EntityType expectedEntityType)
249    throws Exception
250    {
251  28 when(this.entityReferenceResolver.resolve(expectedReference, expectedEntityType)).thenReturn(returnedReference);
252  28 ExtendedURL extendedURL = new ExtendedURL(new URL(testURL), null);
253    // Remove the resource type segment since this is what gets passed to specific Reference Resolvers.
254  28 extendedURL.getSegments().remove(0);
255  28 EntityResourceReference entityResource = this.resolver.resolve(extendedURL,
256    new ResourceType("bin"), Collections.<String, Object>emptyMap());
257   
258  28 assertEquals(expectedActionName, entityResource.getAction().getActionName());
259  28 assertEquals(returnedReference, entityResource.getEntityReference());
260   
261  28 return entityResource;
262    }
263   
 
264  14 toggle private EntityReference buildEntityReference(String wiki, List<String> spaces, String page)
265    {
266  14 return buildEntityReference(wiki, spaces, page, null);
267    }
268   
 
269  16 toggle private EntityReference buildEntityReference(String wiki, List<String> spaces, String page, String attachment)
270    {
271  16 EntityReference entityReference = new WikiReference(wiki);
272  16 if (spaces != null) {
273  16 EntityReference parent = entityReference;
274  16 for (String space : spaces) {
275  23 entityReference = new EntityReference(space, EntityType.SPACE, parent);
276  23 parent = entityReference;
277    }
278    }
279  16 if (page != null) {
280  10 entityReference = new EntityReference(page, EntityType.DOCUMENT, entityReference);
281    }
282  16 if (attachment != null) {
283  2 entityReference = new EntityReference(attachment, EntityType.ATTACHMENT, entityReference);
284    }
285  16 return entityReference;
286    }
287    }