1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki

File XWikiMockitoTest.java

 

Code metrics

6
218
17
1
563
408
20
0.09
12.82
17
1.18

Classes

Class Line # Actions
XWikiMockitoTest 102 218 0% 20 3
0.9875518798.8%
 

Contributing tests

This file is covered by 12 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 com.xpn.xwiki;
21   
22    import java.sql.Timestamp;
23    import java.util.Arrays;
24    import java.util.Date;
25    import java.util.HashMap;
26    import java.util.Locale;
27    import java.util.Map;
28    import java.util.concurrent.ConcurrentHashMap;
29   
30    import javax.inject.Named;
31   
32    import org.junit.jupiter.api.BeforeEach;
33    import org.junit.jupiter.api.Test;
34    import org.mockito.ArgumentMatcher;
35    import org.mockito.invocation.InvocationOnMock;
36    import org.mockito.stubbing.Answer;
37    import org.xwiki.bridge.event.DocumentDeletedEvent;
38    import org.xwiki.bridge.event.DocumentDeletingEvent;
39    import org.xwiki.bridge.event.DocumentRolledBackEvent;
40    import org.xwiki.bridge.event.DocumentRollingBackEvent;
41    import org.xwiki.bridge.event.DocumentUpdatedEvent;
42    import org.xwiki.bridge.event.DocumentUpdatingEvent;
43    import org.xwiki.configuration.ConfigurationSource;
44    import org.xwiki.context.Execution;
45    import org.xwiki.context.ExecutionContext;
46    import org.xwiki.context.internal.DefaultExecution;
47    import org.xwiki.environment.Environment;
48    import org.xwiki.localization.ContextualLocalizationManager;
49    import org.xwiki.model.internal.reference.EntityReferenceFactory;
50    import org.xwiki.model.reference.AttachmentReference;
51    import org.xwiki.model.reference.DocumentReference;
52    import org.xwiki.model.reference.ObjectReference;
53    import org.xwiki.model.reference.PageObjectReference;
54    import org.xwiki.model.reference.PageReference;
55    import org.xwiki.model.reference.SpaceReference;
56    import org.xwiki.model.reference.WikiReference;
57    import org.xwiki.observation.ObservationManager;
58    import org.xwiki.refactoring.internal.batch.DefaultBatchOperationExecutor;
59    import org.xwiki.rendering.syntax.Syntax;
60    import org.xwiki.resource.ResourceReferenceManager;
61    import org.xwiki.test.annotation.ComponentList;
62    import org.xwiki.test.junit5.mockito.ComponentTest;
63    import org.xwiki.test.junit5.mockito.InjectComponentManager;
64    import org.xwiki.test.junit5.mockito.MockComponent;
65    import org.xwiki.test.mockito.MockitoComponentManager;
66    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
67   
68    import com.xpn.xwiki.doc.DocumentRevisionProvider;
69    import com.xpn.xwiki.doc.XWikiAttachment;
70    import com.xpn.xwiki.doc.XWikiDocument;
71    import com.xpn.xwiki.internal.store.StoreConfiguration;
72    import com.xpn.xwiki.objects.BaseObject;
73    import com.xpn.xwiki.store.AttachmentRecycleBinStore;
74    import com.xpn.xwiki.store.XWikiRecycleBinStoreInterface;
75    import com.xpn.xwiki.store.XWikiStoreInterface;
76    import com.xpn.xwiki.store.XWikiVersioningStoreInterface;
77    import com.xpn.xwiki.test.reference.ReferenceComponentList;
78    import com.xpn.xwiki.web.Utils;
79    import com.xpn.xwiki.web.XWikiURLFactory;
80   
81    import static org.junit.jupiter.api.Assertions.assertEquals;
82    import static org.junit.jupiter.api.Assertions.assertTrue;
83    import static org.mockito.ArgumentMatchers.any;
84    import static org.mockito.ArgumentMatchers.anyString;
85    import static org.mockito.ArgumentMatchers.argThat;
86    import static org.mockito.ArgumentMatchers.eq;
87    import static org.mockito.ArgumentMatchers.same;
88    import static org.mockito.Mockito.mock;
89    import static org.mockito.Mockito.never;
90    import static org.mockito.Mockito.verify;
91    import static org.mockito.Mockito.verifyNoMoreInteractions;
92    import static org.mockito.Mockito.when;
93   
94    /**
95    * Unit tests for {@link XWiki}.
96    *
97    * @version $Id: 9eb26100de8c531df44cb60e375a47a516ebdc0b $
98    */
99    @ComponentTest
100    @ComponentList({ DefaultBatchOperationExecutor.class, DefaultExecution.class })
101    @ReferenceComponentList
 
102    public class XWikiMockitoTest
103    {
104    @MockComponent
105    private EntityReferenceFactory entityReferenceFactory;
106   
107    @MockComponent
108    private DocumentRevisionProvider documentRevisionProvider;
109   
110    @MockComponent
111    @Named("xwikicfg")
112    private ConfigurationSource xwikiCfgConfigurationSource;
113   
114    @InjectComponentManager
115    private MockitoComponentManager componentManager;
116   
117    protected Map<DocumentReference, XWikiDocument> documents = new ConcurrentHashMap<>();
118   
119    /**
120    * The object being tested.
121    */
122    private XWiki xwiki;
123   
124    /**
125    * A mock {@link XWikiContext};
126    */
127    private XWikiContext context = new XWikiContext();
128   
129    private XWikiStoreInterface store;
130   
 
131  12 toggle @BeforeEach
132    public void beforeEach() throws Exception
133    {
134  12 this.componentManager.registerMockComponent(ResourceReferenceManager.class);
135  12 this.componentManager.registerMockComponent(Environment.class);
136  12 this.componentManager.registerMockComponent(ObservationManager.class);
137  12 this.componentManager.registerMockComponent(StoreConfiguration.class);
138  12 this.componentManager.registerMockComponent(WikiDescriptorManager.class);
139   
140  12 when(this.entityReferenceFactory.getReference(any())).thenAnswer((invocation) -> invocation.getArgument(0));
141   
142  12 Utils.setComponentManager(this.componentManager);
143  12 xwiki = new XWiki();
144  12 this.context.setWiki(this.xwiki);
145   
146  12 this.store = mock(XWikiStoreInterface.class);
147  12 xwiki.setStore(store);
148   
149  12 XWikiVersioningStoreInterface versioningStore = mock(XWikiVersioningStoreInterface.class);
150  12 xwiki.setVersioningStore(versioningStore);
151   
152  12 Execution execution = this.componentManager.getInstance(Execution.class);
153  12 ExecutionContext executionContext = new ExecutionContext();
154  12 execution.setContext(executionContext);
155   
156  12 when(this.store.loadXWikiDoc(any(XWikiDocument.class), any(XWikiContext.class)))
157    .thenAnswer(new Answer<XWikiDocument>()
158    {
 
159  12 toggle @Override
160    public XWikiDocument answer(InvocationOnMock invocation) throws Throwable
161    {
162  12 XWikiDocument target = invocation.getArgument(0);
163  12 DocumentReference reference = target.getDocumentReference();
164   
165  12 XWikiDocument document = documents.get(reference);
166   
167  12 if (document == null) {
168  8 document = new XWikiDocument(reference, target.getLocale());
169  8 document.setSyntax(Syntax.PLAIN_1_0);
170  8 document.setOriginalDocument(document.clone());
171    }
172   
173  12 return document;
174    }
175    });
176    }
177   
178    /**
179    * Verify that attachment versions are not incremented when a document is copied.
180    *
181    * @see <a href="https://jira.xwiki.org/browse/XWIKI-8157">XWIKI-8157: The "Copy Page" action adds an extra version
182    * to the attached file</a>
183    */
 
184  1 toggle @Test
185    public void copyDocumentPreservesAttachmentsVersion() throws Exception
186    {
187  1 DocumentReference targetReference = new DocumentReference("bar", "Space", "Target");
188  1 XWikiDocument target = mock(XWikiDocument.class);
189  1 when(target.isNew()).thenReturn(true);
190  1 when(target.getDocumentReference()).thenReturn(targetReference);
191   
192  1 DocumentReference sourceReference = new DocumentReference("foo", "Space", "Source");
193  1 XWikiDocument source = mock(XWikiDocument.class);
194  1 when(source.copyDocument(targetReference, context)).thenReturn(target);
195   
196  1 when(xwiki.getStore().loadXWikiDoc(any(XWikiDocument.class), same(context))).thenReturn(source, target);
197   
198  1 assertTrue(xwiki.copyDocument(sourceReference, targetReference, context));
199   
200    // The target document needs to be new in order for the attachment version to be preserved on save.
201  1 verify(target).setNew(true);
202   
203  1 verify(xwiki.getStore()).saveXWikiDoc(target, context);
204    }
205   
206    /**
207    * Verify that {@link XWiki#rollback(XWikiDocument, String, XWikiContext)} fires the right events.
208    */
 
209  1 toggle @Test
210    public void rollbackFiresEvents() throws Exception
211    {
212  1 ObservationManager observationManager = this.componentManager.getInstance(ObservationManager.class);
213   
214  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
215  1 XWikiDocument document = mock(XWikiDocument.class);
216  1 when(document.getDocumentReference()).thenReturn(documentReference);
217   
218  1 XWikiDocument originalDocument = mock(XWikiDocument.class);
219    // Mark the document as existing so that the roll-back method will fire an update event.
220  1 when(originalDocument.isNew()).thenReturn(false);
221   
222  1 XWikiDocument result = mock(XWikiDocument.class);
223  1 when(result.clone()).thenReturn(result);
224  1 when(result.getDocumentReference()).thenReturn(documentReference);
225  1 when(result.getOriginalDocument()).thenReturn(originalDocument);
226   
227  1 String revision = "3.5";
228  1 when(this.documentRevisionProvider.getRevision(document, revision)).thenReturn(result);
229   
230  1 this.componentManager.registerMockComponent(ContextualLocalizationManager.class);
231   
232  1 xwiki.rollback(document, revision, context);
233   
234  1 verify(observationManager).notify(new DocumentRollingBackEvent(documentReference, revision), result, context);
235  1 verify(observationManager).notify(new DocumentUpdatingEvent(documentReference), result, context);
236  1 verify(observationManager).notify(new DocumentUpdatedEvent(documentReference), result, context);
237  1 verify(observationManager).notify(new DocumentRolledBackEvent(documentReference, revision), result, context);
238    }
239   
240    /**
241    * @see "XWIKI-9399: Attachment version is incremented when a document is rolled back even if the attachment did not
242    * change"
243    */
 
244  1 toggle @Test
245    public void rollbackDoesNotSaveUnchangedAttachment() throws Exception
246    {
247  1 String version = "1.1";
248  1 String fileName = "logo.png";
249  1 Date date = new Date();
250  1 XWikiAttachment currentAttachment = mock(XWikiAttachment.class);
251  1 when(currentAttachment.getAttachmentRevision(version, context)).thenReturn(currentAttachment);
252  1 when(currentAttachment.getDate()).thenReturn(new Timestamp(date.getTime()));
253  1 when(currentAttachment.getVersion()).thenReturn(version);
254  1 when(currentAttachment.getFilename()).thenReturn(fileName);
255   
256  1 XWikiAttachment oldAttachment = mock(XWikiAttachment.class);
257  1 when(oldAttachment.getFilename()).thenReturn(fileName);
258  1 when(oldAttachment.getVersion()).thenReturn(version);
259  1 when(oldAttachment.getDate()).thenReturn(date);
260   
261  1 DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
262  1 XWikiDocument document = mock(XWikiDocument.class);
263  1 when(document.getDocumentReference()).thenReturn(documentReference);
264  1 when(document.getAttachmentList()).thenReturn(Arrays.asList(currentAttachment));
265  1 when(document.getAttachment(fileName)).thenReturn(currentAttachment);
266   
267  1 XWikiDocument result = mock(XWikiDocument.class);
268  1 when(result.clone()).thenReturn(result);
269  1 when(result.getDocumentReference()).thenReturn(documentReference);
270  1 when(result.getAttachmentList()).thenReturn(Arrays.asList(oldAttachment));
271  1 when(result.getAttachment(fileName)).thenReturn(oldAttachment);
272   
273  1 String revision = "3.5";
274  1 when(this.documentRevisionProvider.getRevision(document, revision)).thenReturn(result);
275   
276  1 AttachmentRecycleBinStore attachmentRecycleBinStore = mock(AttachmentRecycleBinStore.class);
277  1 xwiki.setAttachmentRecycleBinStore(attachmentRecycleBinStore);
278   
279  1 XWikiDocument emptyDocument = new XWikiDocument(document.getDocumentReference());
280  1 this.componentManager.registerMockComponent(ContextualLocalizationManager.class);
281  1 when(xwiki.getStore().loadXWikiDoc(any(XWikiDocument.class), same(context))).thenReturn(emptyDocument);
282   
283  1 xwiki.rollback(document, revision, context);
284   
285  1 verify(attachmentRecycleBinStore, never()).saveToRecycleBin(same(currentAttachment), any(String.class),
286    any(Date.class), same(context), eq(true));
287  1 verify(oldAttachment, never()).setMetaDataDirty(true);
288    }
289   
 
290  1 toggle @Test
291    public void deleteAllDocumentsAndWithoutSendingToTrash() throws Exception
292    {
293  1 XWiki xwiki = new XWiki();
294   
295  1 XWikiDocument document = mock(XWikiDocument.class);
296  1 DocumentReference reference = new DocumentReference("wiki", "space", "page");
297  1 when(document.getDocumentReference()).thenReturn(reference);
298   
299    // Make sure we have a trash for the test.
300  1 XWikiRecycleBinStoreInterface recycleBinStoreInterface = mock(XWikiRecycleBinStoreInterface.class);
301  1 xwiki.setRecycleBinStore(recycleBinStoreInterface);
302  1 when(xwikiCfgConfigurationSource.getProperty("xwiki.recyclebin", "1")).thenReturn("1");
303   
304    // Configure the mocked Store to later verify if it's called
305  1 XWikiStoreInterface storeInterface = mock(XWikiStoreInterface.class);
306  1 xwiki.setStore(storeInterface);
307  1 XWikiContext xwikiContext = mock(XWikiContext.class);
308   
309  1 xwiki.deleteAllDocuments(document, false, xwikiContext);
310   
311    // Verify that saveToRecycleBin is never called since otherwise it would mean the doc has been saved in the
312    // trash
313  1 verify(recycleBinStoreInterface, never()).saveToRecycleBin(any(XWikiDocument.class), any(String.class),
314    any(Date.class), any(XWikiContext.class), any(Boolean.class));
315   
316    // Verify that deleteXWikiDoc() is called
317  1 verify(storeInterface).deleteXWikiDoc(document, xwikiContext);
318    }
319   
 
320  1 toggle @Test
321    public void deleteDocument() throws Exception
322    {
323  1 final DocumentReference documentReference = new DocumentReference("wiki", "Space", "Page");
324  1 XWikiDocument document = mock(XWikiDocument.class);
325  1 when(document.getDocumentReference()).thenReturn(documentReference);
326   
327  1 final XWikiDocument originalDocument = mock(XWikiDocument.class);
328  1 when(document.getOriginalDocument()).thenReturn(originalDocument);
329   
330  1 this.xwiki.deleteDocument(document, this.context);
331   
332  1 ObservationManager observation = this.componentManager.getInstance(ObservationManager.class);
333   
334  1 ArgumentMatcher<XWikiDocument> matcher = new ArgumentMatcher<XWikiDocument>()
335    {
 
336  4 toggle @Override
337    public boolean matches(XWikiDocument argument)
338    {
339  4 return argument.getDocumentReference().equals(documentReference)
340    && argument.getOriginalDocument() == originalDocument;
341    }
342    };
343   
344    // Make sure the right events have been sent
345  1 verify(observation).notify(eq(new DocumentDeletingEvent(documentReference)), argThat(matcher),
346    same(this.context));
347  1 verify(observation).notify(eq(new DocumentDeletedEvent(documentReference)), argThat(matcher),
348    same(this.context));
349   
350  1 verifyNoMoreInteractions(observation);
351    }
352   
 
353  1 toggle @Test
354    public void getPlainUserName() throws XWikiException
355    {
356  1 XWikiDocument document = mock(XWikiDocument.class);
357  1 DocumentReference userReference = new DocumentReference("wiki", "XWiki", "user");
358  1 when(document.getDocumentReference()).thenReturn(userReference);
359  1 when(this.store.loadXWikiDoc(any(XWikiDocument.class), any(XWikiContext.class))).thenReturn(document);
360  1 BaseObject userObject = mock(BaseObject.class);
361  1 when(document.getObject("XWiki.XWikiUsers")).thenReturn(userObject);
362   
363  1 when(userObject.getStringValue("first_name")).thenReturn("first<name");
364  1 when(userObject.getStringValue("last_name")).thenReturn("last'name");
365  1 assertEquals("first<name last'name", xwiki.getPlainUserName(userReference, context));
366   
367  1 when(userObject.getStringValue("first_name")).thenReturn("first<name");
368  1 when(userObject.getStringValue("last_name")).thenReturn("");
369  1 assertEquals("first<name", xwiki.getPlainUserName(userReference, context));
370   
371  1 when(userObject.getStringValue("first_name")).thenReturn("");
372  1 when(userObject.getStringValue("last_name")).thenReturn("last'name");
373  1 assertEquals("last'name", xwiki.getPlainUserName(userReference, context));
374    }
375   
 
376  1 toggle @Test
377    public void getURLWithDotsAndBackslashInSpaceName() throws Exception
378    {
379  1 XWikiURLFactory urlFactory = mock(XWikiURLFactory.class);
380  1 context.setURLFactory(urlFactory);
381   
382  1 DocumentReference reference = new DocumentReference("wiki", Arrays.asList("space.withdot.and\\and:"), "page");
383   
384  1 this.xwiki.getURL(reference, "view", null, null, context);
385   
386  1 verify(urlFactory).createURL("space\\.withdot\\.and\\\\and\\:", "page", "view", null, null, "wiki", context);
387    }
388   
 
389  1 toggle @Test
390    public void getURLWithLocale() throws Exception
391    {
392  1 XWikiURLFactory urlFactory = mock(XWikiURLFactory.class);
393  1 context.setURLFactory(urlFactory);
394   
395  1 DocumentReference reference = new DocumentReference("wiki", "Space", "Page", Locale.FRENCH);
396   
397  1 this.xwiki.getURL(reference, "view", null, null, context);
398  1 verify(urlFactory).createURL("Space", "Page", "view", "language=fr", null, "wiki", context);
399   
400  1 this.xwiki.getURL(reference, "view", "language=ro", null, context);
401  1 verify(urlFactory).createURL("Space", "Page", "view", "language=ro&language=fr", null, "wiki", context);
402    }
403   
 
404  1 toggle @Test
405    public void getEntityURLWithDefaultAction() throws Exception
406    {
407  1 DocumentReference documentReference = new DocumentReference("tennis", Arrays.asList("Path", "To"), "Success");
408  1 AttachmentReference attachmentReference = new AttachmentReference("image.png", documentReference);
409   
410  1 XWikiURLFactory urlFactory = mock(XWikiURLFactory.class);
411  1 context.setURLFactory(urlFactory);
412   
413  1 this.xwiki.getURL(documentReference, this.context);
414  1 verify(urlFactory).createURL("Path.To", "Success", "view", null, null, "tennis", this.context);
415   
416  1 this.xwiki.getURL(attachmentReference, this.context);
417  1 verify(urlFactory).createAttachmentURL("image.png", "Path.To", "Success", "download", null, "tennis",
418    this.context);
419    }
420   
 
421  1 toggle @Test
422    public void getSpacePreference() throws Exception
423    {
424  1 this.componentManager.registerMockComponent(ConfigurationSource.class, "wiki");
425  1 ConfigurationSource spaceConfiguration =
426    this.componentManager.registerMockComponent(ConfigurationSource.class, "space");
427   
428  1 when(this.xwikiCfgConfigurationSource.getProperty(any(), anyString())).then(new Answer<String>()
429    {
 
430  14 toggle @Override
431    public String answer(InvocationOnMock invocation) throws Throwable
432    {
433  14 return invocation.getArgument(1);
434    }
435    });
436   
437  1 WikiReference wikiReference = new WikiReference("wiki");
438  1 SpaceReference space1Reference = new SpaceReference("space1", wikiReference);
439  1 SpaceReference space2Reference = new SpaceReference("space2", space1Reference);
440   
441    // Without preferences and current doc
442   
443  1 assertEquals("", this.xwiki.getSpacePreference("pref", this.context));
444  1 assertEquals("defaultvalue", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
445  1 assertEquals("", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
446  1 assertEquals("defaultvalue",
447    this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));
448   
449    // Without preferences but with current doc
450   
451  1 this.context.setDoc(new XWikiDocument(new DocumentReference("document", space2Reference)));
452   
453  1 assertEquals("", this.xwiki.getSpacePreference("pref", this.context));
454  1 assertEquals("defaultvalue", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
455  1 assertEquals("", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
456  1 assertEquals("defaultvalue",
457    this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));
458   
459    // With preferences
460   
461  1 final Map<String, Map<String, String>> spacesPreferences = new HashMap<>();
462  1 Map<String, String> space1Preferences = new HashMap<>();
463  1 space1Preferences.put("pref", "prefvalue1");
464  1 space1Preferences.put("pref1", "pref1value1");
465  1 Map<String, String> space2Preferences = new HashMap<>();
466  1 space2Preferences.put("pref", "prefvalue2");
467  1 space2Preferences.put("pref2", "pref2value2");
468  1 spacesPreferences.put(space1Reference.getName(), space1Preferences);
469  1 spacesPreferences.put(space2Reference.getName(), space2Preferences);
470   
471  1 when(spaceConfiguration.getProperty(any(), same(String.class))).then(new Answer<String>()
472    {
 
473  24 toggle @Override
474    public String answer(InvocationOnMock invocation) throws Throwable
475    {
476  24 if (context.getDoc() != null) {
477  24 Map<String, String> spacePreferences =
478    spacesPreferences.get(context.getDoc().getDocumentReference().getParent().getName());
479  24 if (spacePreferences != null) {
480  24 return spacePreferences.get(invocation.getArgument(0));
481    }
482    }
483   
484  0 return null;
485    }
486    });
487   
488  1 this.context.setDoc(new XWikiDocument(new DocumentReference("document", space1Reference)));
489  1 assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", this.context));
490  1 assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
491  1 assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", this.context));
492  1 assertEquals("", this.xwiki.getSpacePreference("pref2", this.context));
493   
494  1 this.context.setDoc(new XWikiDocument(new DocumentReference("document", space2Reference)));
495  1 assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", this.context));
496  1 assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", "defaultvalue", this.context));
497  1 assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", this.context));
498  1 assertEquals("pref2value2", this.xwiki.getSpacePreference("pref2", this.context));
499   
500  1 assertEquals("", this.xwiki.getSpacePreference("nopref", space1Reference, this.context));
501  1 assertEquals("defaultvalue",
502    this.xwiki.getSpacePreference("nopref", space1Reference, "defaultvalue", this.context));
503  1 assertEquals("prefvalue1", this.xwiki.getSpacePreference("pref", space1Reference, this.context));
504  1 assertEquals("prefvalue1",
505    this.xwiki.getSpacePreference("pref", space1Reference, "defaultvalue", this.context));
506  1 assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", space1Reference, this.context));
507  1 assertEquals("", this.xwiki.getSpacePreference("pref2", space1Reference, this.context));
508   
509  1 assertEquals("", this.xwiki.getSpacePreference("nopref", space2Reference, this.context));
510  1 assertEquals("defaultvalue",
511    this.xwiki.getSpacePreference("nopref", space2Reference, "defaultvalue", this.context));
512  1 assertEquals("prefvalue2", this.xwiki.getSpacePreference("pref", space2Reference, this.context));
513  1 assertEquals("prefvalue2",
514    this.xwiki.getSpacePreference("pref", space2Reference, "defaultvalue", this.context));
515  1 assertEquals("pref1value1", this.xwiki.getSpacePreference("pref1", space2Reference, this.context));
516  1 assertEquals("pref2value2", this.xwiki.getSpacePreference("pref2", space2Reference, this.context));
517    }
518   
 
519  1 toggle @Test
520    public void getDocumentWithEntityReference() throws Exception
521    {
522  1 DocumentReference documentReference = new DocumentReference("wiki", "Main", "WebHome");
523   
524  1 assertEquals(documentReference,
525    this.xwiki.getDocument(new WikiReference("wiki"), this.context).getDocumentReference());
526   
527  1 assertEquals(documentReference, this.xwiki
528    .getDocument(new ObjectReference("object", documentReference), this.context).getDocumentReference());
529    }
530   
 
531  1 toggle @Test
532    public void getDocumentWithPageReference() throws Exception
533    {
534  1 PageReference pageReference = new PageReference("wiki", "Main", "Space");
535  1 PageObjectReference pageObjectReference = new PageObjectReference("object", pageReference);
536  1 DocumentReference webhomeDocumentReference =
537    new DocumentReference("wiki", Arrays.asList("Main", "Space"), "WebHome");
538  1 DocumentReference finalDocumentReference = new DocumentReference("wiki", "Main", "Space");
539   
540  1 assertEquals(webhomeDocumentReference,
541    this.xwiki.getDocument(pageReference, this.context).getDocumentReference());
542  1 assertEquals(webhomeDocumentReference,
543    this.xwiki.getDocument(pageObjectReference, this.context).getDocumentReference());
544   
545  1 XWikiDocument finalDocument = new XWikiDocument(finalDocumentReference);
546  1 finalDocument.setNew(false);
547  1 this.documents.put(finalDocument.getDocumentReference(), finalDocument);
548   
549  1 assertEquals(finalDocumentReference,
550    this.xwiki.getDocument(pageReference, this.context).getDocumentReference());
551  1 assertEquals(finalDocumentReference,
552    this.xwiki.getDocument(pageObjectReference, this.context).getDocumentReference());
553   
554  1 XWikiDocument webhomeDocument = new XWikiDocument(webhomeDocumentReference);
555  1 webhomeDocument.setNew(false);
556  1 this.documents.put(webhomeDocument.getDocumentReference(), webhomeDocument);
557   
558  1 assertEquals(webhomeDocumentReference,
559    this.xwiki.getDocument(pageReference, this.context).getDocumentReference());
560  1 assertEquals(webhomeDocumentReference,
561    this.xwiki.getDocument(pageObjectReference, this.context).getDocumentReference());
562    }
563    }