1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.model.script; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.Collections; |
24 |
|
|
25 |
|
import org.junit.Assert; |
26 |
|
import org.junit.Before; |
27 |
|
import org.junit.Test; |
28 |
|
import org.slf4j.Logger; |
29 |
|
import org.xwiki.component.manager.ComponentLookupException; |
30 |
|
import org.xwiki.component.manager.ComponentManager; |
31 |
|
import org.xwiki.component.util.ReflectionUtils; |
32 |
|
import org.xwiki.model.EntityType; |
33 |
|
import org.xwiki.model.reference.ClassPropertyReference; |
34 |
|
import org.xwiki.model.reference.DocumentReference; |
35 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
36 |
|
import org.xwiki.model.reference.EntityReference; |
37 |
|
import org.xwiki.model.reference.EntityReferenceResolver; |
38 |
|
import org.xwiki.model.reference.EntityReferenceValueProvider; |
39 |
|
import org.xwiki.model.reference.SpaceReference; |
40 |
|
import org.xwiki.model.reference.WikiReference; |
41 |
|
|
42 |
|
import static org.mockito.Mockito.*; |
43 |
|
|
44 |
|
|
45 |
|
@link |
46 |
|
|
47 |
|
@version |
48 |
|
@since |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (97) |
Complexity: 21 |
Complexity Density: 0.28 |
|
50 |
|
public class ModelScriptServiceTest |
51 |
|
{ |
52 |
|
private ModelScriptService service; |
53 |
|
|
54 |
|
private ComponentManager componentManager; |
55 |
|
|
56 |
|
private DocumentReferenceResolver<EntityReference> resolver; |
57 |
|
|
58 |
|
private EntityReferenceResolver<String> stringEntityReferenceResolver; |
59 |
|
|
60 |
|
private EntityReferenceValueProvider valueProvider; |
61 |
|
|
62 |
|
private Logger logger; |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
|
64 |
20 |
@SuppressWarnings("unchecked")... |
65 |
|
@Before |
66 |
|
public void setUp() |
67 |
|
{ |
68 |
20 |
this.service = new ModelScriptService(); |
69 |
20 |
this.componentManager = mock(ComponentManager.class); |
70 |
20 |
ReflectionUtils.setFieldValue(this.service, "componentManager", this.componentManager); |
71 |
20 |
this.logger = mock(Logger.class); |
72 |
20 |
ReflectionUtils.setFieldValue(this.service, "logger", this.logger); |
73 |
20 |
this.resolver = mock(DocumentReferenceResolver.class); |
74 |
20 |
this.stringEntityReferenceResolver = mock(EntityReferenceResolver.class); |
75 |
20 |
this.valueProvider = mock(EntityReferenceValueProvider.class); |
76 |
|
} |
77 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
78 |
1 |
@Test... |
79 |
|
public void createDocumentReferenceWithSpecifiedHint() throws Exception |
80 |
|
{ |
81 |
1 |
when(this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "default")) |
82 |
|
.thenReturn(this.resolver); |
83 |
1 |
DocumentReference reference = new DocumentReference("wiki", "space", "page"); |
84 |
1 |
when(this.resolver.resolve(reference)).thenReturn(reference); |
85 |
|
|
86 |
1 |
Assert.assertEquals(reference, this.service.createDocumentReference("wiki", "space", "page", "default")); |
87 |
|
} |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
89 |
1 |
@Test... |
90 |
|
public void createDocumentReferenceWithDefaultHint() throws Exception |
91 |
|
{ |
92 |
1 |
when(this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "current")) |
93 |
|
.thenReturn(this.resolver); |
94 |
1 |
DocumentReference reference = new DocumentReference("wiki", "space", "page"); |
95 |
1 |
when(this.resolver.resolve(reference)).thenReturn(reference); |
96 |
|
|
97 |
1 |
Assert.assertEquals(reference, this.service.createDocumentReference("wiki", "space", "page")); |
98 |
|
} |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
100 |
1 |
@Test... |
101 |
|
public void createDocumentReferenceWhenEmptyParameters() throws Exception |
102 |
|
{ |
103 |
1 |
when(this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "default")) |
104 |
|
.thenReturn(this.resolver); |
105 |
1 |
DocumentReference reference = new DocumentReference("defaultwiki", "defaultspace", "defaultpage"); |
106 |
1 |
when(this.resolver.resolve(null)).thenReturn(reference); |
107 |
|
|
108 |
1 |
Assert.assertEquals(reference, this.service.createDocumentReference("", "", "", "default")); |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
111 |
1 |
@Test... |
112 |
|
public void createDocumentReferenceWhenWikiParameterEmpty() throws Exception |
113 |
|
{ |
114 |
1 |
when(this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "default")) |
115 |
|
.thenReturn(this.resolver); |
116 |
1 |
DocumentReference reference = new DocumentReference("defaultwiki", "space", "page"); |
117 |
1 |
when(this.resolver.resolve(new EntityReference("page", EntityType.DOCUMENT, |
118 |
|
new EntityReference("space", EntityType.SPACE)))).thenReturn(reference); |
119 |
|
|
120 |
1 |
Assert.assertEquals(reference, this.service.createDocumentReference("", "space", "page", "default")); |
121 |
|
} |
122 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
123 |
1 |
@Test... |
124 |
|
public void createDocumentReferenceWhenSpaceParameterEmpty() throws Exception |
125 |
|
{ |
126 |
1 |
when(this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "default")) |
127 |
|
.thenReturn(this.resolver); |
128 |
1 |
DocumentReference reference = new DocumentReference("wiki", "defaultspace", "page"); |
129 |
1 |
when(this.resolver.resolve(new EntityReference("page", EntityType.DOCUMENT, |
130 |
|
new EntityReference("wiki", EntityType.WIKI)))).thenReturn(reference); |
131 |
|
|
132 |
1 |
Assert.assertEquals(reference, this.service.createDocumentReference("wiki", "", "page", "default")); |
133 |
1 |
Assert.assertEquals(reference, this.service.createDocumentReference("wiki", Collections.<String>emptyList(), |
134 |
|
"page", "default")); |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
137 |
1 |
@Test... |
138 |
|
public void createDocumentReferenceWhenPageParameterEmpty() throws Exception |
139 |
|
{ |
140 |
1 |
when(this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "default")) |
141 |
|
.thenReturn(this.resolver); |
142 |
1 |
DocumentReference reference = new DocumentReference("wiki", "space", "defaultpage"); |
143 |
1 |
when(this.resolver.resolve(new EntityReference("space", EntityType.SPACE, |
144 |
|
new EntityReference("wiki", EntityType.WIKI)))).thenReturn(reference); |
145 |
|
|
146 |
1 |
Assert.assertEquals(reference, this.service.createDocumentReference("wiki", "space", "", "default")); |
147 |
|
} |
148 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
149 |
1 |
@Test... |
150 |
|
public void createDocumentReferenceWhenWikiAndSpaceParametersEmpty() throws Exception |
151 |
|
{ |
152 |
1 |
when(this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "default")) |
153 |
|
.thenReturn(this.resolver); |
154 |
1 |
DocumentReference reference = new DocumentReference("wiki", "defaultspace", "defaultpage"); |
155 |
1 |
when(this.resolver.resolve(new EntityReference("wiki", EntityType.WIKI))).thenReturn(reference); |
156 |
|
|
157 |
1 |
Assert.assertEquals(reference, this.service.createDocumentReference("wiki", "", "", "default")); |
158 |
1 |
Assert.assertEquals(reference, this.service.createDocumentReference("wiki", Collections.<String>emptyList(), |
159 |
|
"", "default")); |
160 |
|
} |
161 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
162 |
1 |
@Test... |
163 |
|
public void createDocumentReferenceWhenInvalidHint() throws Exception |
164 |
|
{ |
165 |
1 |
when(this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "invalid")) |
166 |
|
.thenThrow(new ComponentLookupException("error")); |
167 |
|
|
168 |
1 |
when(this.componentManager.getInstance(DocumentReferenceResolver.class, "invalid")) |
169 |
|
.thenThrow(new ComponentLookupException("error")); |
170 |
|
|
171 |
1 |
Assert.assertNull(this.service.createDocumentReference("wiki", "space", "page", "invalid")); |
172 |
|
} |
173 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
174 |
1 |
@Test... |
175 |
|
public void createDocumentReferenceWithDeprecatedHint() throws Exception |
176 |
|
{ |
177 |
1 |
when(this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "current/reference")) |
178 |
|
.thenThrow(new ComponentLookupException("error")); |
179 |
1 |
DocumentReference reference = new DocumentReference("wiki", "space", "page"); |
180 |
|
|
181 |
1 |
when(this.componentManager.getInstance(DocumentReferenceResolver.class, "current/reference")) |
182 |
|
.thenReturn(this.resolver); |
183 |
1 |
when(this.resolver.resolve(reference)).thenReturn(reference); |
184 |
|
|
185 |
1 |
Assert.assertEquals(reference, |
186 |
|
this.service.createDocumentReference("wiki", "space", "page", "current/reference")); |
187 |
|
|
188 |
|
|
189 |
1 |
verify(this.logger).warn("Deprecated usage of DocumentReferenceResolver with hint [{}]. " |
190 |
|
+ "Please consider using a DocumentReferenceResolver that takes into account generic types.", |
191 |
|
"current/reference"); |
192 |
|
} |
193 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
194 |
1 |
@Test... |
195 |
|
public void createDocumentReferenceFromPageNameAndSpaceReference() |
196 |
|
{ |
197 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page"); |
198 |
1 |
Assert.assertEquals(documentReference, this.service.createDocumentReference(documentReference.getName(), |
199 |
|
documentReference.getLastSpaceReference())); |
200 |
|
} |
201 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
202 |
1 |
@Test... |
203 |
|
public void getEntityReferenceValue() throws Exception |
204 |
|
{ |
205 |
1 |
when(this.componentManager.getInstance(EntityReferenceValueProvider.class, "current")) |
206 |
|
.thenReturn(this.valueProvider); |
207 |
1 |
when(this.valueProvider.getDefaultValue(EntityType.WIKI)).thenReturn("somewiki"); |
208 |
|
|
209 |
1 |
Assert.assertEquals("somewiki", this.service.getEntityReferenceValue(EntityType.WIKI)); |
210 |
|
} |
211 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
212 |
1 |
@Test... |
213 |
|
public void getEntityReferenceValueWithInvalidHint() throws Exception |
214 |
|
{ |
215 |
1 |
when(this.componentManager.getInstance(EntityReferenceValueProvider.class, "invalid")) |
216 |
|
.thenThrow(new ComponentLookupException("error")); |
217 |
|
|
218 |
1 |
Assert.assertNull(this.service.getEntityReferenceValue(EntityType.WIKI, "invalid")); |
219 |
|
} |
220 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
221 |
1 |
@Test... |
222 |
|
public void getEntityReferenceValueWithNullType() throws Exception |
223 |
|
{ |
224 |
1 |
Assert.assertNull(this.service.getEntityReferenceValue(null)); |
225 |
|
} |
226 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
227 |
1 |
@Test... |
228 |
|
public void createWikiReference() |
229 |
|
{ |
230 |
1 |
Assert.assertEquals(new WikiReference("wiki"), this.service.createWikiReference("wiki")); |
231 |
|
} |
232 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
233 |
1 |
@Test... |
234 |
|
public void createSpaceReference() |
235 |
|
{ |
236 |
1 |
Assert.assertEquals(new SpaceReference("space", new WikiReference("wiki")), |
237 |
|
this.service.createSpaceReference("space", this.service.createWikiReference("wiki"))); |
238 |
|
|
239 |
1 |
SpaceReference spaceReference = |
240 |
|
new SpaceReference("C", new SpaceReference("B", new SpaceReference("A", new WikiReference("wiki")))); |
241 |
1 |
Assert.assertEquals(spaceReference, |
242 |
|
this.service.createSpaceReference(Arrays.asList("A", "B", "C"), this.service.createWikiReference("wiki"))); |
243 |
|
|
244 |
1 |
Assert.assertEquals(spaceReference, |
245 |
|
this.service.createSpaceReference(spaceReference.getName(), (SpaceReference) spaceReference.getParent())); |
246 |
|
} |
247 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
248 |
1 |
@Test... |
249 |
|
public void createEntityReferenceWithoutParent() |
250 |
|
{ |
251 |
1 |
Assert.assertEquals(new EntityReference("page", EntityType.DOCUMENT), |
252 |
|
this.service.createEntityReference("page", EntityType.DOCUMENT)); |
253 |
|
} |
254 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
255 |
1 |
@Test... |
256 |
|
public void createEntityReferenceWithParent() |
257 |
|
{ |
258 |
1 |
Assert.assertEquals(new EntityReference("page", EntityType.DOCUMENT, |
259 |
|
new EntityReference("space", EntityType.SPACE)), |
260 |
|
this.service.createEntityReference("page", EntityType.DOCUMENT, |
261 |
|
this.service.createEntityReference("space", EntityType.SPACE))); |
262 |
|
} |
263 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
264 |
1 |
@Test... |
265 |
|
public void resolveSpace() throws Exception |
266 |
|
{ |
267 |
1 |
when(this.componentManager.getInstance(EntityReferenceResolver.TYPE_STRING, "current")).thenReturn( |
268 |
|
this.stringEntityReferenceResolver); |
269 |
1 |
SpaceReference reference = new SpaceReference("Space", new WikiReference("wiki")); |
270 |
1 |
when(this.stringEntityReferenceResolver.resolve("x", EntityType.SPACE, new Object[] {})).thenReturn(reference); |
271 |
|
|
272 |
1 |
Assert.assertEquals(reference, this.service.resolveSpace("x")); |
273 |
|
} |
274 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
275 |
1 |
@Test... |
276 |
|
public void resolveSpaceWithHintAndParameters() throws Exception |
277 |
|
{ |
278 |
1 |
when(this.componentManager.getInstance(EntityReferenceResolver.TYPE_STRING, "custom")).thenReturn( |
279 |
|
this.stringEntityReferenceResolver); |
280 |
1 |
SpaceReference reference = new SpaceReference("Foo", new WikiReference("bar")); |
281 |
1 |
Object[] parameters = new Object[] {new DocumentReference("wiki", "Space", "Page"), "extra"}; |
282 |
1 |
when(this.stringEntityReferenceResolver.resolve("reference", EntityType.SPACE, parameters)).thenReturn( |
283 |
|
reference); |
284 |
|
|
285 |
1 |
Assert.assertEquals(reference, this.service.resolveSpace("reference", "custom", parameters[0], parameters[1])); |
286 |
|
} |
287 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
288 |
1 |
@Test... |
289 |
|
public void resolveClassPropertyWithHintAndParameters() throws Exception |
290 |
|
{ |
291 |
1 |
when(this.componentManager.getInstance(EntityReferenceResolver.TYPE_STRING, "custom")).thenReturn( |
292 |
|
this.stringEntityReferenceResolver); |
293 |
1 |
ClassPropertyReference reference = |
294 |
|
new ClassPropertyReference("property", new DocumentReference("wiki", "Space", "Class")); |
295 |
1 |
Object[] parameters = new Object[] {new DocumentReference("wiki", "Space", "Page"), "extra"}; |
296 |
1 |
when(this.stringEntityReferenceResolver.resolve("Class^property", EntityType.CLASS_PROPERTY, parameters)) |
297 |
|
.thenReturn(reference); |
298 |
|
|
299 |
1 |
Assert.assertEquals(reference, |
300 |
|
this.service.resolveClassProperty("Class^property", "custom", parameters[0], parameters[1])); |
301 |
|
} |
302 |
|
} |