1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.model.script

File ModelScriptServiceTest.java

 

Code metrics

0
83
22
1
332
249
22
0.27
3.77
22
1

Classes

Class Line # Actions
ModelScriptServiceTest 55 83 0% 22 0
1.0100%
 

Contributing tests

No tests hitting this source file were found.

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