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

File MockitoOldcore.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
41% of files have more coverage

Code metrics

92
265
48
1
886
632
98
0.37
5.52
48
2.04

Classes

Class Line # Actions
MockitoOldcore 100 265 0% 98 46
0.886419888.6%
 

Contributing tests

This file is covered by 334 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.test;
21   
22    import java.io.File;
23    import java.util.ArrayList;
24    import java.util.Date;
25    import java.util.List;
26    import java.util.Locale;
27    import java.util.Map;
28    import java.util.concurrent.ConcurrentHashMap;
29   
30    import javax.inject.Provider;
31    import javax.script.ScriptContext;
32    import javax.script.SimpleScriptContext;
33    import javax.servlet.ServletContext;
34   
35    import org.apache.commons.lang3.StringUtils;
36    import org.mockito.internal.util.MockUtil;
37    import org.mockito.invocation.InvocationOnMock;
38    import org.mockito.stubbing.Answer;
39    import org.xwiki.bridge.event.DocumentCreatedEvent;
40    import org.xwiki.bridge.event.DocumentDeletedEvent;
41    import org.xwiki.bridge.event.DocumentUpdatedEvent;
42    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
43    import org.xwiki.component.manager.ComponentLookupException;
44    import org.xwiki.component.manager.ComponentManager;
45    import org.xwiki.configuration.ConfigurationSource;
46    import org.xwiki.configuration.internal.MemoryConfigurationSource;
47    import org.xwiki.context.Execution;
48    import org.xwiki.context.ExecutionContext;
49    import org.xwiki.context.ExecutionContextManager;
50    import org.xwiki.environment.Environment;
51    import org.xwiki.environment.internal.ServletEnvironment;
52    import org.xwiki.model.internal.reference.EntityReferenceFactory;
53    import org.xwiki.model.reference.DocumentReference;
54    import org.xwiki.model.reference.DocumentReferenceResolver;
55    import org.xwiki.model.reference.LocalDocumentReference;
56    import org.xwiki.model.reference.WikiReference;
57    import org.xwiki.observation.ObservationManager;
58    import org.xwiki.query.QueryManager;
59    import org.xwiki.rendering.syntax.Syntax;
60    import org.xwiki.script.ScriptContextManager;
61    import org.xwiki.script.internal.ScriptExecutionContextInitializer;
62    import org.xwiki.security.authorization.AuthorizationManager;
63    import org.xwiki.security.authorization.ContextualAuthorizationManager;
64    import org.xwiki.test.annotation.AllComponents;
65    import org.xwiki.test.internal.MockConfigurationSource;
66    import org.xwiki.test.mockito.MockitoComponentManager;
67    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
68   
69    import com.xpn.xwiki.CoreConfiguration;
70    import com.xpn.xwiki.XWiki;
71    import com.xpn.xwiki.XWikiContext;
72    import com.xpn.xwiki.doc.XWikiAttachment;
73    import com.xpn.xwiki.doc.XWikiDocument;
74    import com.xpn.xwiki.internal.XWikiCfgConfigurationSource;
75    import com.xpn.xwiki.objects.classes.BaseClass;
76    import com.xpn.xwiki.store.XWikiHibernateStore;
77    import com.xpn.xwiki.store.XWikiStoreInterface;
78    import com.xpn.xwiki.store.XWikiVersioningStoreInterface;
79    import com.xpn.xwiki.user.api.XWikiGroupService;
80    import com.xpn.xwiki.user.api.XWikiRightService;
81    import com.xpn.xwiki.util.XWikiStubContextProvider;
82    import com.xpn.xwiki.web.Utils;
83   
84    import static com.xpn.xwiki.test.mockito.OldcoreMatchers.anyXWikiContext;
85    import static com.xpn.xwiki.test.mockito.OldcoreMatchers.anyXWikiDocument;
86    import static org.mockito.ArgumentMatchers.any;
87    import static org.mockito.ArgumentMatchers.anyBoolean;
88    import static org.mockito.Mockito.doAnswer;
89    import static org.mockito.Mockito.doReturn;
90    import static org.mockito.Mockito.mock;
91    import static org.mockito.Mockito.spy;
92    import static org.mockito.Mockito.when;
93   
94    /**
95    * Test setup to initialize and manipulate various oldcore APIs.
96    *
97    * @version $Id: 44e5837fe1f9b6b86c8c9447a29f591b4992fbe6 $
98    * @since 10.2
99    */
 
100    public class MockitoOldcore
101    {
102    public static final LocalDocumentReference USER_CLASS = new LocalDocumentReference("XWiki", "XWikiUsers");
103   
104    public static final LocalDocumentReference GROUP_CLASS = new LocalDocumentReference("XWiki", "XWikiGroups");
105   
106    private final MockitoComponentManager componentManager;
107   
108    private XWikiContext context;
109   
110    private XWiki spyXWiki;
111   
112    protected File permanentDirectory;
113   
114    protected File temporaryDirectory;
115   
116    private XWikiHibernateStore mockHibernateStore;
117   
118    private XWikiVersioningStoreInterface mockVersioningStore;
119   
120    private XWikiRightService mockRightService;
121   
122    private XWikiGroupService mockGroupService;
123   
124    private AuthorizationManager mockAuthorizationManager;
125   
126    private ContextualAuthorizationManager mockContextualAuthorizationManager;
127   
128    private QueryManager queryManager;
129   
130    private WikiDescriptorManager wikiDescriptorManager;
131   
132    protected Map<DocumentReference, XWikiDocument> documents = new ConcurrentHashMap<>();
133   
134    private boolean notifyDocumentCreatedEvent;
135   
136    private boolean notifyDocumentUpdatedEvent;
137   
138    private boolean notifyDocumentDeletedEvent;
139   
140    private MemoryConfigurationSource configurationSource;
141   
142    private MemoryConfigurationSource xwikicfgConfigurationSource;
143   
144    private MemoryConfigurationSource wikiConfigurationSource;
145   
146    private MemoryConfigurationSource spaceConfigurationSource;
147   
148    private ScriptContext scriptContext;
149   
150    private Environment environment;
151   
 
152  453 toggle public MockitoOldcore(MockitoComponentManager componentManager)
153    {
154  453 this.componentManager = componentManager;
155    }
156   
 
157  8936 toggle public MockitoComponentManager getMocker()
158    {
159  8936 return this.componentManager;
160    }
161   
 
162  17 toggle public void notifyDocumentCreatedEvent(boolean notifyDocumentCreatedEvent)
163    {
164  17 this.notifyDocumentCreatedEvent = notifyDocumentCreatedEvent;
165    }
166   
 
167  17 toggle public void notifyDocumentUpdatedEvent(boolean notifyDocumentUpdatedEvent)
168    {
169  17 this.notifyDocumentUpdatedEvent = notifyDocumentUpdatedEvent;
170    }
171   
 
172  13 toggle public void notifyDocumentDeletedEvent(boolean notifyDocumentDeletedEvent)
173    {
174  13 this.notifyDocumentDeletedEvent = notifyDocumentDeletedEvent;
175    }
176   
177    /**
178    * Enabled notification of component descriptor registration/unregistration.
179    *
180    * @throws ComponentLookupException when failing to lookup {@link ObservationManager} component
181    */
 
182  5 toggle public void notifyComponentDescriptorEvent() throws ComponentLookupException
183    {
184  5 getMocker().notifyComponentDescriptorEvent();
185    }
186   
 
187  453 toggle public void before(Class<?> testClass) throws Exception
188    {
189    // Statically store the component manager in {@link Utils} to be able to access it without
190    // the context.
191  453 Utils.setComponentManager(getMocker());
192   
193  453 this.context = new XWikiContext();
194   
195  453 getXWikiContext().setWikiId("xwiki");
196  453 getXWikiContext().setMainXWiki("xwiki");
197   
198  453 this.spyXWiki = spy(new XWiki());
199  453 getXWikiContext().setWiki(this.spyXWiki);
200   
201  453 this.mockHibernateStore = mock(XWikiHibernateStore.class);
202  453 this.mockVersioningStore = mock(XWikiVersioningStoreInterface.class);
203  453 this.mockRightService = mock(XWikiRightService.class);
204  453 this.mockGroupService = mock(XWikiGroupService.class);
205   
206  453 doReturn(this.mockHibernateStore).when(this.spyXWiki).getStore();
207  453 doReturn(this.mockHibernateStore).when(this.spyXWiki).getHibernateStore();
208  453 doReturn(this.mockVersioningStore).when(this.spyXWiki).getVersioningStore();
209  453 doReturn(this.mockRightService).when(this.spyXWiki).getRightService();
210  453 doReturn(this.mockGroupService).when(this.spyXWiki).getGroupService(getXWikiContext());
211   
212    // We need to initialize the Component Manager so that the components can be looked up
213  453 getXWikiContext().put(ComponentManager.class.getName(), getMocker());
214   
215  453 if (testClass.getAnnotation(AllComponents.class) != null) {
216    // If @AllComponents is enabled force mocking AuthorizationManager and ContextualAuthorizationManager if not
217    // already mocked
218  70 this.mockAuthorizationManager = getMocker().registerMockComponent(AuthorizationManager.class, false);
219  70 this.mockContextualAuthorizationManager =
220    getMocker().registerMockComponent(ContextualAuthorizationManager.class, false);
221    } else {
222    // Make sure an AuthorizationManager and a ContextualAuthorizationManager is available
223  383 if (!getMocker().hasComponent(AuthorizationManager.class)) {
224  370 this.mockAuthorizationManager = getMocker().registerMockComponent(AuthorizationManager.class);
225    }
226  383 if (!getMocker().hasComponent(ContextualAuthorizationManager.class)) {
227  381 this.mockContextualAuthorizationManager =
228    getMocker().registerMockComponent(ContextualAuthorizationManager.class);
229    }
230    }
231   
232    // Make sure to provide a EntityReferenceFactory
233  453 if (!getMocker().hasComponent(EntityReferenceFactory.class)) {
234  373 EntityReferenceFactory factory = getMocker().registerMockComponent(EntityReferenceFactory.class);
235  373 when(factory.getReference(any())).thenAnswer((invocation) -> invocation.getArgument(0));
236    }
237   
238    // Make sure a default ConfigurationSource is available
239  453 if (!getMocker().hasComponent(ConfigurationSource.class)) {
240  453 this.configurationSource = getMocker().registerMemoryConfigurationSource();
241    }
242   
243    // Make sure a "xwikicfg" ConfigurationSource is available
244  453 if (!getMocker().hasComponent(ConfigurationSource.class, XWikiCfgConfigurationSource.ROLEHINT)) {
245  383 this.xwikicfgConfigurationSource = new MockConfigurationSource();
246  383 getMocker().registerComponent(MockConfigurationSource.getDescriptor(XWikiCfgConfigurationSource.ROLEHINT),
247    this.xwikicfgConfigurationSource);
248    }
249    // Make sure a "wiki" ConfigurationSource is available
250  453 if (!getMocker().hasComponent(ConfigurationSource.class, "wiki")) {
251  447 this.wikiConfigurationSource = new MockConfigurationSource();
252  447 getMocker().registerComponent(MockConfigurationSource.getDescriptor("wiki"), this.wikiConfigurationSource);
253    }
254   
255    // Make sure a "space" ConfigurationSource is available
256  453 if (!getMocker().hasComponent(ConfigurationSource.class, "space")) {
257  452 this.spaceConfigurationSource = new MockConfigurationSource();
258  452 getMocker().registerComponent(MockConfigurationSource.getDescriptor("space"),
259    this.spaceConfigurationSource);
260    }
261   
262    // Since the oldcore module draws the Servlet Environment in its dependencies we need to ensure it's set up
263    // correctly with a Servlet Context.
264  453 if (getMocker().hasComponent(Environment.class)
265    && getMocker().getInstance(Environment.class) instanceof ServletEnvironment) {
266  60 ServletEnvironment servletEnvironment = getMocker().getInstance(Environment.class);
267   
268  60 ServletContext servletContextMock = mock(ServletContext.class);
269  60 servletEnvironment.setServletContext(servletContextMock);
270  60 when(servletContextMock.getAttribute("javax.servlet.context.tempdir"))
271    .thenReturn(new File(System.getProperty("java.io.tmpdir")));
272   
273  60 initEnvironmentDirectories();
274   
275  60 servletEnvironment.setTemporaryDirectory(this.temporaryDirectory);
276  60 servletEnvironment.setPermanentDirectory(this.permanentDirectory);
277   
278  60 this.environment = servletEnvironment;
279    }
280   
281    // Initialize the Execution Context
282  453 if (this.componentManager.hasComponent(ExecutionContextManager.class)) {
283  76 ExecutionContextManager ecm = this.componentManager.getInstance(ExecutionContextManager.class);
284  76 ExecutionContext ec = new ExecutionContext();
285  76 ecm.initialize(ec);
286    }
287   
288    // Bridge with old XWiki Context, required for old code.
289  453 Execution execution;
290  453 if (this.componentManager.hasComponent(Execution.class)) {
291  77 execution = this.componentManager.getInstance(Execution.class);
292    } else {
293  376 execution = this.componentManager.registerMockComponent(Execution.class);
294    }
295  453 ExecutionContext econtext;
296  453 if (MockUtil.isMock(execution)) {
297  377 econtext = new ExecutionContext();
298  377 when(execution.getContext()).thenReturn(econtext);
299    } else {
300  76 econtext = execution.getContext();
301    }
302   
303    // Set a few standard things in the ExecutionContext
304  453 econtext.setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, this.context);
305  453 this.scriptContext = (ScriptContext) econtext.getProperty(ScriptExecutionContextInitializer.SCRIPT_CONTEXT_ID);
306  453 if (this.scriptContext == null) {
307  377 this.scriptContext = new SimpleScriptContext();
308  377 econtext.setProperty(ScriptExecutionContextInitializer.SCRIPT_CONTEXT_ID, this.scriptContext);
309    }
310   
311  453 if (!this.componentManager.hasComponent(ScriptContextManager.class)) {
312  376 ScriptContextManager scriptContextManager =
313    this.componentManager.registerMockComponent(ScriptContextManager.class);
314  376 when(scriptContextManager.getCurrentScriptContext()).thenReturn(this.scriptContext);
315  376 when(scriptContextManager.getScriptContext()).thenReturn(this.scriptContext);
316    }
317   
318    // Initialize XWikiContext provider
319  453 if (!this.componentManager.hasComponent(XWikiContext.TYPE_PROVIDER)) {
320  369 Provider<XWikiContext> xcontextProvider =
321    this.componentManager.registerMockComponent(XWikiContext.TYPE_PROVIDER);
322  369 when(xcontextProvider.get()).thenReturn(this.context);
323    } else {
324  84 Provider<XWikiContext> xcontextProvider = this.componentManager.getInstance(XWikiContext.TYPE_PROVIDER);
325  84 if (MockUtil.isMock(xcontextProvider)) {
326  14 when(xcontextProvider.get()).thenReturn(this.context);
327    }
328    }
329   
330    // Initialize readonly XWikiContext provider
331  453 if (!this.componentManager.hasComponent(XWikiContext.TYPE_PROVIDER, "readonly")) {
332  383 Provider<XWikiContext> xcontextProvider =
333    this.componentManager.registerMockComponent(XWikiContext.TYPE_PROVIDER, "readonly");
334  383 when(xcontextProvider.get()).thenReturn(this.context);
335    } else {
336  70 Provider<XWikiContext> xcontextProvider = this.componentManager.getInstance(XWikiContext.TYPE_PROVIDER);
337  70 if (MockUtil.isMock(xcontextProvider)) {
338  0 when(xcontextProvider.get()).thenReturn(this.context);
339    }
340    }
341   
342    // Initialize stub context provider
343  453 if (this.componentManager.hasComponent(XWikiStubContextProvider.class)) {
344  70 XWikiStubContextProvider stubContextProvider =
345    this.componentManager.getInstance(XWikiStubContextProvider.class);
346  70 if (!MockUtil.isMock(stubContextProvider)) {
347  70 stubContextProvider.initialize(this.context);
348    }
349    }
350   
351    // Make sure to have a mocked CoreConfiguration (even if one already exist)
352  453 if (!this.componentManager.hasComponent(CoreConfiguration.class)) {
353  383 CoreConfiguration coreConfigurationMock =
354    this.componentManager.registerMockComponent(CoreConfiguration.class);
355  383 when(coreConfigurationMock.getDefaultDocumentSyntax()).thenReturn(Syntax.XWIKI_2_1);
356    } else {
357  70 CoreConfiguration coreConfiguration =
358    this.componentManager.registerMockComponent(CoreConfiguration.class, false);
359  70 if (MockUtil.isMock(coreConfiguration)) {
360  70 when(coreConfiguration.getDefaultDocumentSyntax()).thenReturn(Syntax.XWIKI_2_1);
361    }
362    }
363   
364    // Set a context ComponentManager if none exist
365  453 if (!this.componentManager.hasComponent(ComponentManager.class, "context")) {
366  383 DefaultComponentDescriptor<ComponentManager> componentManagerDescriptor =
367    new DefaultComponentDescriptor<>();
368  383 componentManagerDescriptor.setRoleHint("context");
369  383 componentManagerDescriptor.setRoleType(ComponentManager.class);
370  383 this.componentManager.registerComponent(componentManagerDescriptor, this.componentManager);
371    }
372   
373    // XWiki
374   
375  453 doAnswer(new Answer<XWikiDocument>()
376    {
 
377  1 toggle @Override
378    public XWikiDocument answer(InvocationOnMock invocation) throws Throwable
379    {
380  1 XWikiDocument doc = invocation.getArgument(0);
381  1 String revision = invocation.getArgument(1);
382   
383  1 if (StringUtils.equals(revision, doc.getVersion())) {
384  0 return doc;
385    }
386   
387    // TODO: implement version store mocking
388  1 return new XWikiDocument(doc.getDocumentReference());
389    }
390    }).when(getSpyXWiki()).getDocument(anyXWikiDocument(), any(), anyXWikiContext());
391  453 doAnswer(new Answer<XWikiDocument>()
392    {
 
393  2388 toggle @Override
394    public XWikiDocument answer(InvocationOnMock invocation) throws Throwable
395    {
396  2388 DocumentReference target = invocation.getArgument(0);
397   
398  2388 if (target.getLocale() == null) {
399  1050 target = new DocumentReference(target, Locale.ROOT);
400    }
401   
402  2388 XWikiDocument document = documents.get(target);
403   
404  2388 if (document == null) {
405  1717 document = new XWikiDocument(target, target.getLocale());
406  1717 document.setSyntax(Syntax.PLAIN_1_0);
407  1717 document.setOriginalDocument(document.clone());
408    }
409   
410  2388 return document;
411    }
412    }).when(getSpyXWiki()).getDocument(any(DocumentReference.class), anyXWikiContext());
413  453 doAnswer(new Answer<XWikiDocument>()
414    {
 
415  10 toggle @Override
416    public XWikiDocument answer(InvocationOnMock invocation) throws Throwable
417    {
418  10 XWikiDocument target = invocation.getArgument(0);
419   
420  10 return getSpyXWiki().getDocument(target.getDocumentReferenceWithLocale(), invocation.getArgument(1));
421    }
422    }).when(getSpyXWiki()).getDocument(anyXWikiDocument(), any(XWikiContext.class));
423  453 doAnswer(new Answer<Boolean>()
424    {
 
425  20 toggle @Override
426    public Boolean answer(InvocationOnMock invocation) throws Throwable
427    {
428  20 DocumentReference target = (DocumentReference) invocation.getArguments()[0];
429   
430  20 if (target.getLocale() == null) {
431  20 target = new DocumentReference(target, Locale.ROOT);
432    }
433   
434  20 return documents.containsKey(target);
435    }
436    }).when(getSpyXWiki()).exists(any(DocumentReference.class), anyXWikiContext());
437  453 doAnswer(new Answer<Void>()
438    {
 
439  538 toggle @Override
440    public Void answer(InvocationOnMock invocation) throws Throwable
441    {
442  538 XWikiDocument document = invocation.getArgument(0);
443  538 String comment = invocation.getArgument(1);
444  538 boolean minorEdit = invocation.getArgument(2);
445   
446  538 boolean isNew = document.isNew();
447   
448  538 document.setComment(StringUtils.defaultString(comment));
449  538 document.setMinorEdit(minorEdit);
450   
451  538 if (document.isContentDirty() || document.isMetaDataDirty()) {
452  515 document.setDate(new Date());
453  515 if (document.isContentDirty()) {
454  476 document.setContentUpdateDate(new Date());
455  476 document.setContentAuthorReference(document.getAuthorReference());
456    }
457  515 document.incrementVersion();
458   
459  515 document.setContentDirty(false);
460  515 document.setMetaDataDirty(false);
461    }
462  538 document.setNew(false);
463  538 document.setStore(getMockStore());
464   
465  538 XWikiDocument previousDocument = documents.get(document.getDocumentReferenceWithLocale());
466   
467  538 if (previousDocument != null && previousDocument != document) {
468  5 for (XWikiAttachment attachment : document.getAttachmentList()) {
469  0 if (!attachment.isContentDirty()) {
470  0 attachment.setAttachment_content(
471    previousDocument.getAttachment(attachment.getFilename()).getAttachment_content());
472    }
473    }
474    }
475   
476  538 XWikiDocument originalDocument = document.getOriginalDocument();
477  538 if (originalDocument == null) {
478  451 originalDocument = spyXWiki.getDocument(document.getDocumentReferenceWithLocale(), context);
479  451 document.setOriginalDocument(originalDocument);
480    }
481   
482  538 XWikiDocument savedDocument = document.clone();
483   
484  538 documents.put(document.getDocumentReferenceWithLocale(), savedDocument);
485   
486  538 if (isNew) {
487  484 if (notifyDocumentCreatedEvent) {
488  33 getObservationManager().notify(new DocumentCreatedEvent(document.getDocumentReference()),
489    document, getXWikiContext());
490    }
491    } else {
492  54 if (notifyDocumentUpdatedEvent) {
493  21 getObservationManager().notify(new DocumentUpdatedEvent(document.getDocumentReference()),
494    document, getXWikiContext());
495    }
496    }
497   
498    // Set the document as it's original document
499  538 savedDocument.setOriginalDocument(savedDocument.clone());
500   
501  538 return null;
502    }
503    }).when(getSpyXWiki()).saveDocument(anyXWikiDocument(), any(String.class), anyBoolean(), anyXWikiContext());
504  453 doAnswer(new Answer<Void>()
505    {
 
506  72 toggle @Override
507    public Void answer(InvocationOnMock invocation) throws Throwable
508    {
509  72 XWikiDocument document = invocation.getArgument(0);
510   
511  72 documents.remove(document.getDocumentReferenceWithLocale());
512   
513  72 if (notifyDocumentDeletedEvent) {
514  1 getObservationManager().notify(new DocumentDeletedEvent(document.getDocumentReference()), document,
515    getXWikiContext());
516    }
517   
518  72 return null;
519    }
520    }).when(getSpyXWiki()).deleteDocument(anyXWikiDocument(), any(Boolean.class), anyXWikiContext());
521  453 doAnswer(new Answer<BaseClass>()
522    {
 
523  621 toggle @Override
524    public BaseClass answer(InvocationOnMock invocation) throws Throwable
525    {
526  621 return getSpyXWiki()
527    .getDocument((DocumentReference) invocation.getArguments()[0], invocation.getArgument(1))
528    .getXClass();
529    }
530    }).when(getSpyXWiki()).getXClass(any(DocumentReference.class), anyXWikiContext());
531  453 doAnswer(new Answer<String>()
532    {
 
533  51 toggle @Override
534    public String answer(InvocationOnMock invocation) throws Throwable
535    {
536  51 return getXWikiContext().getLanguage();
537    }
538    }).when(getSpyXWiki()).getLanguagePreference(anyXWikiContext());
539   
540  453 getXWikiContext().setLocale(Locale.ENGLISH);
541   
542    // XWikiStoreInterface
543   
544  453 when(getMockStore().getTranslationList(anyXWikiDocument(), anyXWikiContext())).then(new Answer<List<String>>()
545    {
 
546  4 toggle @Override
547    public List<String> answer(InvocationOnMock invocation) throws Throwable
548    {
549  4 XWikiDocument document = invocation.getArgument(0);
550   
551  4 List<String> translations = new ArrayList<String>();
552   
553  4 for (XWikiDocument storedDocument : documents.values()) {
554  11 Locale storedLocale = storedDocument.getLocale();
555  11 if (!storedLocale.equals(Locale.ROOT)
556    && storedDocument.getDocumentReference().equals(document.getDocumentReference())) {
557  0 translations.add(storedLocale.toString());
558    }
559    }
560   
561  4 return translations;
562    }
563    });
564  453 when(getMockStore().loadXWikiDoc(anyXWikiDocument(), anyXWikiContext())).then(new Answer<XWikiDocument>()
565    {
 
566  2 toggle @Override
567    public XWikiDocument answer(InvocationOnMock invocation) throws Throwable
568    {
569    // The store is based on the contex for the wiki
570  2 DocumentReference reference = invocation.<XWikiDocument>getArgument(0).getDocumentReference();
571  2 XWikiContext xcontext = invocation.getArgument(1);
572  2 if (!xcontext.getWikiReference().equals(reference.getWikiReference())) {
573  2 reference = reference.setWikiReference(xcontext.getWikiReference());
574    }
575   
576  2 return getSpyXWiki().getDocument(reference, xcontext);
577    }
578    });
579  453 doAnswer(new Answer<Void>()
580    {
 
581  0 toggle @Override
582    public Void answer(InvocationOnMock invocation) throws Throwable
583    {
584    // The store is based on the contex for the wiki
585  0 DocumentReference reference = invocation.<XWikiDocument>getArgument(0).getDocumentReferenceWithLocale();
586  0 XWikiContext xcontext = invocation.getArgument(1);
587  0 if (!xcontext.getWikiReference().equals(reference.getWikiReference())) {
588  0 reference = reference.setWikiReference(xcontext.getWikiReference());
589    }
590   
591  0 documents.remove(reference);
592   
593  0 return null;
594    }
595    }).when(getMockStore()).deleteXWikiDoc(anyXWikiDocument(), anyXWikiContext());
596   
597    // Users
598   
599  453 doAnswer(new Answer<BaseClass>()
600    {
 
601  3 toggle @Override
602    public BaseClass answer(InvocationOnMock invocation) throws Throwable
603    {
604  3 XWikiContext xcontext = invocation.getArgument(0);
605   
606  3 XWikiDocument userDocument = getSpyXWiki()
607    .getDocument(new DocumentReference(USER_CLASS, new WikiReference(xcontext.getWikiId())), xcontext);
608   
609  3 final BaseClass userClass = userDocument.getXClass();
610   
611  3 if (userDocument.isNew()) {
612  2 userClass.addTextField("first_name", "First Name", 30);
613  2 userClass.addTextField("last_name", "Last Name", 30);
614  2 userClass.addEmailField("email", "e-Mail", 30);
615  2 userClass.addPasswordField("password", "Password", 10);
616  2 userClass.addBooleanField("active", "Active", "active");
617  2 userClass.addTextAreaField("comment", "Comment", 40, 5);
618  2 userClass.addTextField("avatar", "Avatar", 30);
619  2 userClass.addTextField("phone", "Phone", 30);
620  2 userClass.addTextAreaField("address", "Address", 40, 3);
621   
622  2 getSpyXWiki().saveDocument(userDocument, xcontext);
623    }
624   
625  3 return userClass;
626    }
627    }).when(getSpyXWiki()).getUserClass(anyXWikiContext());
628  453 doAnswer(new Answer<BaseClass>()
629    {
 
630  6 toggle @Override
631    public BaseClass answer(InvocationOnMock invocation) throws Throwable
632    {
633  6 XWikiContext xcontext = invocation.getArgument(0);
634   
635  6 XWikiDocument groupDocument = getSpyXWiki()
636    .getDocument(new DocumentReference(GROUP_CLASS, new WikiReference(xcontext.getWikiId())), xcontext);
637   
638  6 final BaseClass groupClass = groupDocument.getXClass();
639   
640  6 if (groupDocument.isNew()) {
641  2 groupClass.addTextField("member", "Member", 30);
642   
643  2 getSpyXWiki().saveDocument(groupDocument, xcontext);
644    }
645   
646  6 return groupClass;
647    }
648    }).when(getSpyXWiki()).getGroupClass(anyXWikiContext());
649   
650    // Query Manager
651    // If there's already a Query Manager registered, use it instead.
652    // This allows, for example, using @ComponentList to use the real Query Manager, in integration tests.
653  453 if (!this.componentManager.hasComponent(QueryManager.class)) {
654  378 mockQueryManager();
655    }
656  453 when(getMockStore().getQueryManager()).then(new Answer<QueryManager>()
657    {
658   
 
659  3 toggle @Override
660    public QueryManager answer(InvocationOnMock invocation) throws Throwable
661    {
662  3 return getQueryManager();
663    }
664    });
665   
666    // WikiDescriptorManager
667    // If there's already a WikiDescriptorManager registered, use it instead.
668    // This allows, for example, using @ComponentList to use the real WikiDescriptorManager, in integration tests.
669  453 if (!this.componentManager.hasComponent(WikiDescriptorManager.class)) {
670  403 this.wikiDescriptorManager = getMocker().registerMockComponent(WikiDescriptorManager.class);
671  403 when(this.wikiDescriptorManager.getMainWikiId()).then(new Answer<String>()
672    {
 
673  0 toggle @Override
674    public String answer(InvocationOnMock invocation) throws Throwable
675    {
676  0 return getXWikiContext().getMainXWiki();
677    }
678    });
679  403 when(this.wikiDescriptorManager.getCurrentWikiId()).then(new Answer<String>()
680    {
 
681  14289 toggle @Override
682    public String answer(InvocationOnMock invocation) throws Throwable
683    {
684  14289 return getXWikiContext().getWikiId();
685    }
686    });
687    }
688    }
689   
 
690  0 toggle protected DocumentReference resolveDocument(String documentName) throws ComponentLookupException
691    {
692  0 DocumentReferenceResolver<String> resolver =
693    getMocker().getInstance(DocumentReferenceResolver.TYPE_STRING, "current");
694   
695  0 return resolver.resolve(documentName);
696    }
697   
 
698  453 toggle public void after() throws Exception
699    {
700  453 Utils.setComponentManager(null);
701   
702  453 Execution execution = this.componentManager.getInstance(Execution.class);
703  453 execution.removeContext();
704    }
705   
 
706  18761 toggle public XWikiContext getXWikiContext()
707    {
708  18761 return this.context;
709    }
710   
711    /**
712    * @since 7.3RC1
713    */
 
714  5642 toggle public XWiki getSpyXWiki()
715    {
716  5642 return this.spyXWiki;
717    }
718   
719    /**
720    * @deprecated since 7.3RC1, use {@link #getSpyXWiki()} instead
721    */
 
722  0 toggle @Deprecated
723    public XWiki getMockXWiki()
724    {
725  0 return getSpyXWiki();
726    }
727   
 
728  0 toggle public File getPermanentDirectory()
729    {
730  0 return this.permanentDirectory;
731    }
732   
 
733  8 toggle public File getTemporaryDirectory()
734    {
735  8 return this.temporaryDirectory;
736    }
737   
 
738  100 toggle public XWikiRightService getMockRightService()
739    {
740  100 return this.mockRightService;
741    }
742   
 
743  10 toggle public XWikiGroupService getMockGroupService()
744    {
745  10 return this.mockGroupService;
746    }
747   
 
748  55 toggle public AuthorizationManager getMockAuthorizationManager()
749    {
750  55 return this.mockAuthorizationManager;
751    }
752   
 
753  66 toggle public ContextualAuthorizationManager getMockContextualAuthorizationManager()
754    {
755  66 return this.mockContextualAuthorizationManager;
756    }
757   
 
758  2351 toggle public XWikiStoreInterface getMockStore()
759    {
760  2351 return this.mockHibernateStore;
761    }
762   
763    /**
764    * @since 7.2M2
765    */
 
766  0 toggle public XWikiVersioningStoreInterface getMockVersioningStore()
767    {
768  0 return this.mockVersioningStore;
769    }
770   
771    /**
772    * @since 6.0RC1
773    */
 
774  11 toggle public ExecutionContext getExecutionContext() throws ComponentLookupException
775    {
776  11 return this.componentManager.<Execution>getInstance(Execution.class).getContext();
777    }
778   
779    /**
780    * @since 8.3M1
781    */
 
782  4 toggle public ScriptContext getScriptContext()
783    {
784  4 return this.scriptContext;
785    }
786   
787    /**
788    * @since 6.1M2
789    */
 
790  52 toggle public Map<DocumentReference, XWikiDocument> getDocuments()
791    {
792  52 return this.documents;
793    }
794   
795    /**
796