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

File DefaultModelBridgeTest.java

 

Code metrics

0
124
14
1
325
234
14
0.11
8.86
14
1

Classes

Class Line # Actions
DefaultModelBridgeTest 65 124 0% 14 0
1.0100%
 

Contributing tests

This file is covered by 13 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.refactoring.internal;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.Locale;
25   
26    import javax.inject.Provider;
27   
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.job.event.status.JobProgressManager;
32    import org.xwiki.model.EntityType;
33    import org.xwiki.model.reference.DocumentReference;
34    import org.xwiki.model.reference.DocumentReferenceResolver;
35    import org.xwiki.model.reference.EntityReferenceProvider;
36    import org.xwiki.model.reference.EntityReferenceSerializer;
37    import org.xwiki.model.reference.SpaceReference;
38    import org.xwiki.query.Query;
39    import org.xwiki.query.QueryManager;
40    import org.xwiki.test.mockito.MockitoComponentMockingRule;
41   
42    import com.xpn.xwiki.XWiki;
43    import com.xpn.xwiki.XWikiContext;
44    import com.xpn.xwiki.doc.XWikiDocument;
45    import com.xpn.xwiki.internal.parentchild.ParentChildConfiguration;
46    import com.xpn.xwiki.objects.BaseObject;
47   
48    import static org.junit.Assert.assertEquals;
49    import static org.junit.Assert.assertTrue;
50    import static org.mockito.ArgumentMatchers.any;
51    import static org.mockito.ArgumentMatchers.anyBoolean;
52    import static org.mockito.ArgumentMatchers.anyInt;
53    import static org.mockito.ArgumentMatchers.anyString;
54    import static org.mockito.ArgumentMatchers.eq;
55    import static org.mockito.Mockito.mock;
56    import static org.mockito.Mockito.never;
57    import static org.mockito.Mockito.verify;
58    import static org.mockito.Mockito.when;
59   
60    /**
61    * Unit tests for {@link DefaultModelBridge}.
62    *
63    * @version $Id: b574005793efc02473c64b6a1a5239f265ab3f30 $
64    */
 
65    public class DefaultModelBridgeTest
66    {
67    @Rule
68    public MockitoComponentMockingRule<ModelBridge> mocker = new MockitoComponentMockingRule<ModelBridge>(
69    DefaultModelBridge.class);
70   
71    private XWikiContext xcontext = mock(XWikiContext.class);
72   
 
73  13 toggle @Before
74    public void configure() throws Exception
75    {
76  13 XWiki xwiki = mock(XWiki.class);
77  13 when(this.xcontext.getWiki()).thenReturn(xwiki);
78   
79  13 Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
80  13 when(xcontextProvider.get()).thenReturn(this.xcontext);
81   
82  13 EntityReferenceProvider entityReferenceProvider = this.mocker.getInstance(EntityReferenceProvider.class);
83  13 when(entityReferenceProvider.getDefaultReference(EntityType.DOCUMENT)).thenReturn(
84    new DocumentReference("what", "ever", "WebHome"));
85  13 when(entityReferenceProvider.getDefaultReference(EntityType.SPACE)).thenReturn(
86    new SpaceReference("whatever", "Main"));
87    }
88   
 
89  1 toggle @Test
90    public void create() throws Exception
91    {
92  1 XWikiDocument document = mock(XWikiDocument.class);
93  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
94  1 when(this.xcontext.getWiki().getDocument(documentReference, this.xcontext)).thenReturn(document);
95   
96  1 this.mocker.getComponentUnderTest().create(documentReference);
97   
98  1 verify(this.xcontext.getWiki()).saveDocument(document, this.xcontext);
99  1 verify(this.mocker.getMockedLogger()).info("Document [{}] has been created.", documentReference);
100    }
101   
 
102  1 toggle @Test
103    public void copy() throws Exception
104    {
105  1 DocumentReference sourceReference = new DocumentReference("wiki", "Space", "Page", Locale.FRENCH);
106  1 DocumentReference copyReference = new DocumentReference("wiki", "Space", "Copy");
107   
108  1 when(this.xcontext.getWiki().copyDocument(sourceReference, copyReference, "fr", false, true, true,
109    this.xcontext)).thenReturn(true);
110   
111  1 assertTrue(this.mocker.getComponentUnderTest().copy(sourceReference, copyReference));
112   
113  1 verify(this.mocker.getMockedLogger()).info("Document [{}] has been copied to [{}].", sourceReference,
114    copyReference);
115    }
116   
 
117  1 toggle @Test
118    public void deleteTranslation() throws Exception
119    {
120  1 XWikiDocument sourceDocument = mock(XWikiDocument.class);
121  1 DocumentReference sourceReference = new DocumentReference("wiki", "Space", "Page", Locale.FRENCH);
122  1 when(this.xcontext.getWiki().getDocument(sourceReference, this.xcontext)).thenReturn(sourceDocument);
123  1 when(sourceDocument.getTranslation()).thenReturn(1);
124   
125  1 this.mocker.getComponentUnderTest().delete(sourceReference);
126   
127  1 verify(this.xcontext.getWiki()).deleteDocument(sourceDocument, this.xcontext);
128  1 verify(this.mocker.getMockedLogger()).info("Document [{}] has been deleted.", sourceReference);
129    }
130   
 
131  1 toggle @Test
132    public void deleteAllTranslations() throws Exception
133    {
134  1 DocumentReference sourceReference = new DocumentReference("wiki", "Space", "Page");
135   
136  1 XWikiDocument sourceDocument = mock(XWikiDocument.class);
137  1 when(this.xcontext.getWiki().getDocument(sourceReference, this.xcontext)).thenReturn(sourceDocument);
138  1 when(sourceDocument.getTranslation()).thenReturn(0);
139   
140  1 this.mocker.getComponentUnderTest().delete(sourceReference);
141   
142  1 verify(this.xcontext.getWiki()).deleteAllDocuments(sourceDocument, this.xcontext);
143  1 verify(this.mocker.getMockedLogger()).info("Document [{}] has been deleted with all its translations.",
144    sourceReference);
145    }
146   
 
147  1 toggle @Test
148    public void createRedirect() throws Exception
149    {
150  1 DocumentReference oldReference = new DocumentReference("wiki", "Space", "Old");
151  1 DocumentReference newReference = new DocumentReference("wiki", "Space", "New");
152   
153  1 DocumentReference redirectClassReference = new DocumentReference("wiki", "XWiki", "RedirectClass");
154  1 when(this.xcontext.getWiki().exists(redirectClassReference, this.xcontext)).thenReturn(true);
155   
156  1 XWikiDocument oldDocument = mock(XWikiDocument.class);
157  1 when(this.xcontext.getWiki().getDocument(oldReference, this.xcontext)).thenReturn(oldDocument);
158  1 when(oldDocument.getXObject(eq(redirectClassReference), anyInt())).thenReturn(mock(BaseObject.class));
159   
160  1 this.mocker.getComponentUnderTest().createRedirect(oldReference, newReference);
161   
162  1 verify(oldDocument).setHidden(true);
163  1 verify(this.xcontext.getWiki()).saveDocument(oldDocument, "Create automatic redirect.", this.xcontext);
164  1 verify(this.mocker.getMockedLogger()).info("Created automatic redirect from [{}] to [{}].", oldReference,
165    newReference);
166    }
167   
 
168  1 toggle @Test
169    public void getDocumentReferences() throws Exception
170    {
171  1 SpaceReference spaceReference = new SpaceReference("wiki", "Space");
172   
173  1 Query query = mock(Query.class);
174  1 QueryManager queryManager = this.mocker.getInstance(QueryManager.class);
175  1 when(queryManager.createQuery(any(), any())).thenReturn(query);
176   
177  1 EntityReferenceSerializer<String> localEntityReferenceSerializer =
178    this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
179  1 when(localEntityReferenceSerializer.serialize(spaceReference)).thenReturn("Space");
180   
181  1 when(query.execute()).thenReturn(Arrays.<Object>asList("Page"));
182   
183  1 DocumentReferenceResolver<String> explicitDocumentReferenceResolver =
184    this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "explicit");
185  1 DocumentReference documentReference = new DocumentReference("Page", spaceReference);
186  1 when(explicitDocumentReferenceResolver.resolve("Page", spaceReference)).thenReturn(documentReference);
187   
188  1 assertEquals(Arrays.asList(documentReference),
189    this.mocker.getComponentUnderTest().getDocumentReferences(spaceReference));
190   
191  1 verify(query).setWiki(spaceReference.getWikiReference().getName());
192  1 verify(query).bindValue("space", "Space");
193  1 verify(query).bindValue("spacePrefix", "Space.%");
194    }
195   
 
196  1 toggle @Test
197    public void updateParentFields() throws Exception
198    {
199  1 DocumentReference oldParentReference = new DocumentReference("wiki", "Space", "Old");
200  1 DocumentReference newParentReference = new DocumentReference("wiki", "Space", "New");
201   
202  1 XWikiDocument oldParentDocument = mock(XWikiDocument.class);
203  1 when(this.xcontext.getWiki().getDocument(oldParentReference, this.xcontext)).thenReturn(oldParentDocument);
204   
205  1 DocumentReference child1Reference = new DocumentReference("wiki", "Space", "Child1");
206  1 DocumentReference child2Reference = new DocumentReference("wiki", "Space", "Child2");
207  1 when(oldParentDocument.getChildrenReferences(this.xcontext)).thenReturn(
208    Arrays.asList(child1Reference, child2Reference));
209   
210  1 JobProgressManager mockProgressManager = mocker.getInstance(JobProgressManager.class);
211   
212  1 XWikiDocument child1Document = mock(XWikiDocument.class);
213  1 when(this.xcontext.getWiki().getDocument(child1Reference, this.xcontext)).thenReturn(child1Document);
214  1 XWikiDocument child2Document = mock(XWikiDocument.class);
215  1 when(this.xcontext.getWiki().getDocument(child2Reference, this.xcontext)).thenReturn(child2Document);
216   
217  1 this.mocker.getComponentUnderTest().updateParentField(oldParentReference, newParentReference);
218   
219  1 verify(mockProgressManager).pushLevelProgress(2, this.mocker.getComponentUnderTest());
220   
221  1 verify(child1Document).setParentReference(newParentReference);
222  1 verify(this.xcontext.getWiki()).saveDocument(child1Document, "Updated parent field.", true, this.xcontext);
223   
224  1 verify(child2Document).setParentReference(newParentReference);
225  1 verify(this.xcontext.getWiki()).saveDocument(child1Document, "Updated parent field.", true, this.xcontext);
226    }
227   
 
228  1 toggle @Test
229    public void updateParentFieldsNoChildren() throws Exception
230    {
231  1 DocumentReference oldParentReference = new DocumentReference("wiki", "Space", "Old");
232  1 DocumentReference newParentReference = new DocumentReference("wiki", "Space", "New");
233   
234  1 XWikiDocument oldParentDocument = mock(XWikiDocument.class);
235  1 when(this.xcontext.getWiki().getDocument(oldParentReference, this.xcontext)).thenReturn(oldParentDocument);
236   
237  1 when(oldParentDocument.getChildrenReferences(this.xcontext)).thenReturn(
238    Collections.<DocumentReference>emptyList());
239   
240  1 JobProgressManager mockProgressManager = mocker.getInstance(JobProgressManager.class);
241   
242  1 this.mocker.getComponentUnderTest().updateParentField(oldParentReference, newParentReference);
243   
244  1 verify(mockProgressManager, never()).pushLevelProgress(anyInt(), any());
245  1 verify(this.xcontext.getWiki(), never()).saveDocument(any(XWikiDocument.class), eq("Updated parent field."),
246    eq(true), eq(this.xcontext));
247    }
248   
 
249  1 toggle @Test
250    public void updateTitle() throws Exception
251    {
252  1 DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Page");
253  1 XWikiDocument document = mock(XWikiDocument.class);
254  1 when(this.xcontext.getWiki().getDocument(documentReference, xcontext)).thenReturn(document);
255   
256  1 this.mocker.getComponentUnderTest().update(documentReference, Collections.singletonMap("title", "foo"));
257   
258  1 verify(document).setTitle("foo");
259  1 verify(this.xcontext.getWiki()).saveDocument(document, "Update document after refactoring.", true, xcontext);
260  1 verify(this.mocker.getMockedLogger()).info("Document [{}] has been updated.", documentReference);
261    }
262   
 
263  1 toggle @Test
264    public void updateParentWhenPageIsTerminal() throws Exception
265    {
266  1 DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Page");
267  1 XWikiDocument document = mock(XWikiDocument.class);
268  1 when(this.xcontext.getWiki().getDocument(documentReference, xcontext)).thenReturn(document);
269  1 when(document.getParentReference()).thenReturn(new DocumentReference("wiki", "What", "Ever"));
270   
271  1 this.mocker.getComponentUnderTest().update(documentReference, Collections.emptyMap());
272   
273  1 verify(document).setParentReference(new DocumentReference("wiki", Arrays.asList("Path", "To"), "WebHome"));
274  1 verify(this.xcontext.getWiki()).saveDocument(document, "Update document after refactoring.", true, xcontext);
275  1 verify(this.mocker.getMockedLogger()).info("Document [{}] has been updated.", documentReference);
276    }
277   
 
278  1 toggle @Test
279    public void updateParentWhenPageIsNested() throws Exception
280    {
281  1 DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "WebHome");
282  1 XWikiDocument document = mock(XWikiDocument.class);
283  1 when(this.xcontext.getWiki().getDocument(documentReference, xcontext)).thenReturn(document);
284  1 when(document.getParentReference()).thenReturn(new DocumentReference("wiki", "What", "Ever"));
285   
286  1 this.mocker.getComponentUnderTest().update(documentReference, Collections.emptyMap());
287   
288  1 verify(document).setParentReference(new DocumentReference("wiki", "Path", "WebHome"));
289  1 verify(this.xcontext.getWiki()).saveDocument(document, "Update document after refactoring.", true, xcontext);
290  1 verify(this.mocker.getMockedLogger()).info("Document [{}] has been updated.", documentReference);
291    }
292   
 
293  1 toggle @Test
294    public void updateParentWhenPageIsTopLevel() throws Exception
295    {
296  1 DocumentReference documentReference = new DocumentReference("wiki", "Path", "WebHome");
297  1 XWikiDocument document = mock(XWikiDocument.class);
298  1 when(this.xcontext.getWiki().getDocument(documentReference, xcontext)).thenReturn(document);
299  1 when(document.getParentReference()).thenReturn(new DocumentReference("wiki", "What", "Ever"));
300   
301  1 this.mocker.getComponentUnderTest().update(documentReference, Collections.emptyMap());
302   
303  1 verify(document).setParentReference(new DocumentReference("wiki", "Main", "WebHome"));
304  1 verify(this.xcontext.getWiki()).saveDocument(document, "Update document after refactoring.", true, xcontext);
305  1 verify(this.mocker.getMockedLogger()).info("Document [{}] has been updated.", documentReference);
306    }
307   
 
308  1 toggle @Test
309    public void dontUpdateParentWhenLegacyMode() throws Exception
310    {
311  1 DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Page");
312  1 XWikiDocument document = mock(XWikiDocument.class);
313  1 when(this.xcontext.getWiki().getDocument(documentReference, xcontext)).thenReturn(document);
314  1 when(document.getParentReference()).thenReturn(new DocumentReference("wiki", "What", "Ever"));
315   
316  1 ParentChildConfiguration parentChildConfiguration = mocker.getInstance(ParentChildConfiguration.class);
317  1 when(parentChildConfiguration.isParentChildMechanismEnabled()).thenReturn(true);
318   
319  1 this.mocker.getComponentUnderTest().update(documentReference, Collections.emptyMap());
320   
321  1 verify(document, never()).setParentReference(any(DocumentReference.class));
322  1 verify(this.xcontext.getWiki(), never()).saveDocument(any(XWikiDocument.class), anyString(), anyBoolean(),
323    any(XWikiContext.class));
324    }
325    }