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.standard.entity; |
21 |
|
|
22 |
|
import java.net.URL; |
23 |
|
import java.util.Arrays; |
24 |
|
import java.util.Collections; |
25 |
|
|
26 |
|
import org.junit.Before; |
27 |
|
import org.junit.Test; |
28 |
|
import org.xwiki.component.util.ReflectionUtils; |
29 |
|
import org.xwiki.model.EntityType; |
30 |
|
import org.xwiki.model.reference.DocumentReference; |
31 |
|
import org.xwiki.model.reference.EntityReference; |
32 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
33 |
|
import org.xwiki.model.reference.WikiReference; |
34 |
|
import org.xwiki.resource.ResourceReference; |
35 |
|
import org.xwiki.resource.ResourceType; |
36 |
|
import org.xwiki.resource.entity.EntityResourceReference; |
37 |
|
import org.xwiki.resource.internal.entity.EntityResourceActionLister; |
38 |
|
import org.xwiki.url.ExtendedURL; |
39 |
|
import org.xwiki.url.internal.standard.StandardURLConfiguration; |
40 |
|
import org.xwiki.url.internal.standard.WikiReferenceExtractor; |
41 |
|
|
42 |
|
import static org.junit.Assert.assertEquals; |
43 |
|
import static org.mockito.Mockito.*; |
44 |
|
|
45 |
|
|
46 |
|
@link |
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
|
|
| 90.9% |
Uncovered Elements: 4 (44) |
Complexity: 8 |
Complexity Density: 0.24 |
|
51 |
|
public class WikiEntityResourceReferenceResolverTest |
52 |
|
{ |
53 |
|
private WikiEntityResourceReferenceResolver resolver; |
54 |
|
|
55 |
|
private WikiReference wikiReference = new WikiReference("somewiki"); |
56 |
|
|
57 |
|
private WikiReferenceExtractor wikiReferenceExtractor; |
58 |
|
|
59 |
|
private EntityReferenceResolver<EntityReference> entityReferenceResolver; |
60 |
|
|
61 |
|
private EntityResourceActionLister entityResourceActionLister; |
62 |
|
|
63 |
|
private StandardURLConfiguration configuration; |
64 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
|
65 |
1 |
@Before... |
66 |
|
public void setUp() throws Exception |
67 |
|
{ |
68 |
1 |
this.resolver = new WikiEntityResourceReferenceResolver(); |
69 |
|
|
70 |
1 |
this.wikiReferenceExtractor = mock(WikiReferenceExtractor.class); |
71 |
1 |
ReflectionUtils.setFieldValue(this.resolver, "wikiExtractor", this.wikiReferenceExtractor); |
72 |
|
|
73 |
1 |
this.entityReferenceResolver = mock(EntityReferenceResolver.class); |
74 |
1 |
ReflectionUtils.setFieldValue(this.resolver, "defaultReferenceEntityReferenceResolver", |
75 |
|
this.entityReferenceResolver); |
76 |
|
|
77 |
1 |
this.configuration = mock(StandardURLConfiguration.class); |
78 |
1 |
when(this.configuration.isViewActionHidden()).thenReturn(false); |
79 |
1 |
ReflectionUtils.setFieldValue(this.resolver, "configuration", this.configuration); |
80 |
|
|
81 |
1 |
this.entityResourceActionLister = mock(EntityResourceActionLister.class); |
82 |
1 |
when(this.entityResourceActionLister.listActions()).thenReturn(Arrays.asList("view", "download")); |
83 |
1 |
ReflectionUtils.setFieldValue(this.resolver, "entityResourceActionLister", this.entityResourceActionLister); |
84 |
|
} |
85 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
86 |
1 |
@Test... |
87 |
|
public void resolve() throws Exception |
88 |
|
{ |
89 |
1 |
ExtendedURL url = new ExtendedURL(new URL("http://localhost/wiki/somewiki/view/space/page"), null); |
90 |
1 |
url.getSegments().remove(0); |
91 |
1 |
when(wikiReferenceExtractor.extract(url)).thenReturn(this.wikiReference); |
92 |
|
|
93 |
1 |
EntityReference reference = buildEntityReference("somewiki", "space", "page"); |
94 |
1 |
EntityResourceReference result = (EntityResourceReference) testCreateResource( |
95 |
|
"http://localhost/wiki/somewiki/view/space/page", "view", reference, reference, EntityType.DOCUMENT); |
96 |
|
|
97 |
1 |
assertEquals(new DocumentReference("somewiki", "space", "page"), result.getEntityReference()); |
98 |
|
} |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
100 |
1 |
private ResourceReference testCreateResource(String testURL, String expectedActionName,... |
101 |
|
EntityReference expectedReference, EntityReference returnedReference, EntityType expectedEntityType) |
102 |
|
throws Exception |
103 |
|
{ |
104 |
1 |
when(this.entityReferenceResolver.resolve(expectedReference, expectedEntityType)).thenReturn( |
105 |
|
returnedReference); |
106 |
1 |
ExtendedURL extendedURL = new ExtendedURL(new URL(testURL), null); |
107 |
|
|
108 |
1 |
extendedURL.getSegments().remove(0); |
109 |
1 |
EntityResourceReference entityResource = |
110 |
|
this.resolver.resolve(extendedURL, new ResourceType("wiki"), Collections.<String, Object>emptyMap()); |
111 |
|
|
112 |
1 |
assertEquals(expectedActionName, entityResource.getAction().getActionName()); |
113 |
1 |
assertEquals(returnedReference, entityResource.getEntityReference()); |
114 |
|
|
115 |
1 |
return entityResource; |
116 |
|
} |
117 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
118 |
1 |
private EntityReference buildEntityReference(String wiki, String space, String page)... |
119 |
|
{ |
120 |
1 |
return buildEntityReference(wiki, space, page, null); |
121 |
|
} |
122 |
|
|
|
|
| 71.4% |
Uncovered Elements: 4 (14) |
Complexity: 4 |
Complexity Density: 0.5 |
|
123 |
1 |
private EntityReference buildEntityReference(String wiki, String space, String page, String attachment)... |
124 |
|
{ |
125 |
1 |
EntityReference entityReference = new WikiReference(wiki); |
126 |
1 |
if (space != null) { |
127 |
1 |
entityReference = new EntityReference(space, EntityType.SPACE, entityReference); |
128 |
|
} |
129 |
1 |
if (page != null) { |
130 |
1 |
entityReference = new EntityReference(page, EntityType.DOCUMENT, entityReference); |
131 |
|
} |
132 |
1 |
if (attachment != null) { |
133 |
0 |
entityReference = new EntityReference(attachment, EntityType.ATTACHMENT, entityReference); |
134 |
|
} |
135 |
1 |
return entityReference; |
136 |
|
} |
137 |
|
} |