1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.model.script

File ModelScriptServiceTest.java

 

Code metrics

0
76
21
1
302
227
21
0.28
3.62
21
1

Classes

Class Line # Actions
ModelScriptServiceTest 50 76 0% 21 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.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    * Unit tests for {@link org.xwiki.model.script.ModelScriptService}.
46    *
47    * @version $Id: 6e815fa20c00d2d063b3210bfd81ec4d806e8ba9 $
48    * @since 2.3M1
49    */
 
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   
 
64  20 toggle @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   
 
78  1 toggle @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   
 
89  1 toggle @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  1 toggle @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   
 
111  1 toggle @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   
 
123  1 toggle @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   
 
137  1 toggle @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   
 
149  1 toggle @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   
 
162  1 toggle @Test
163    public void createDocumentReferenceWhenInvalidHint() throws Exception
164    {
165  1 when(this.componentManager.getInstance(DocumentReferenceResolver.TYPE_REFERENCE, "invalid"))
166    .thenThrow(new ComponentLookupException("error"));
167    // Make sure backward compatibility is preserved.
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   
 
174  1 toggle @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    // Make sure backward compatibility is preserved.
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    // Verify that we log a warning!
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   
 
194  1 toggle @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   
 
202  1 toggle @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   
 
212  1 toggle @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   
 
221  1 toggle @Test
222    public void getEntityReferenceValueWithNullType() throws Exception
223    {
224  1 Assert.assertNull(this.service.getEntityReferenceValue(null));
225    }
226   
 
227  1 toggle @Test
228    public void createWikiReference()
229    {
230  1 Assert.assertEquals(new WikiReference("wiki"), this.service.createWikiReference("wiki"));
231    }
232   
 
233  1 toggle @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   
 
248  1 toggle @Test
249    public void createEntityReferenceWithoutParent()
250    {
251  1 Assert.assertEquals(new EntityReference("page", EntityType.DOCUMENT),
252    this.service.createEntityReference("page", EntityType.DOCUMENT));
253    }
254   
 
255  1 toggle @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   
 
264  1 toggle @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   
 
275  1 toggle @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   
 
288  1 toggle @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    }