1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.security.authorization.testwikibuilding

File LegacyTestWiki.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

100
407
121
7
1,349
1,037
171
0.42
3.36
17.29
1.41

Classes

Class Line # Actions
LegacyTestWiki 72 146 0% 58 65
0.705882470.6%
LegacyTestWiki.TestWiki 464 90 0% 40 53
0.629370662.9%
LegacyTestWiki.TestSpace 750 24 0% 16 12
0.7272727572.7%
LegacyTestWiki.UserTestDocument 846 13 0% 6 6
0.6842105468.4%
LegacyTestWiki.GroupTestDocument 898 32 0% 11 8
0.8260869482.6%
LegacyTestWiki.TestDocument 991 59 0% 24 27
0.696629269.7%
LegacyTestWiki.TestAcl 1169 43 0% 16 7
0.893939489.4%
 

Contributing tests

This file is covered by 18 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.security.authorization.testwikibuilding;
21   
22    import java.util.AbstractCollection;
23    import java.util.ArrayList;
24    import java.util.Arrays;
25    import java.util.Collection;
26    import java.util.Collections;
27    import java.util.Formatter;
28    import java.util.HashMap;
29    import java.util.HashSet;
30    import java.util.Iterator;
31    import java.util.LinkedHashMap;
32    import java.util.List;
33    import java.util.Map;
34    import java.util.Set;
35    import java.util.Vector;
36   
37    import org.jmock.Expectations;
38    import org.jmock.Mockery;
39    import org.jmock.api.Invocation;
40    import org.jmock.lib.action.CustomAction;
41    import org.jmock.lib.legacy.ClassImposteriser;
42    import org.xwiki.bridge.event.DocumentCreatedEvent;
43    import org.xwiki.bridge.event.DocumentDeletedEvent;
44    import org.xwiki.bridge.event.DocumentUpdatedEvent;
45    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
46    import org.xwiki.component.manager.ComponentManager;
47    import org.xwiki.model.EntityType;
48    import org.xwiki.model.reference.DocumentReference;
49    import org.xwiki.model.reference.DocumentReferenceResolver;
50    import org.xwiki.model.reference.EntityReference;
51    import org.xwiki.model.reference.EntityReferenceSerializer;
52    import org.xwiki.model.reference.LocalDocumentReference;
53    import org.xwiki.model.reference.SpaceReference;
54    import org.xwiki.model.reference.WikiReference;
55    import org.xwiki.observation.ObservationManager;
56    import org.xwiki.security.authorization.Right;
57    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
58   
59    import com.xpn.xwiki.XWiki;
60    import com.xpn.xwiki.XWikiContext;
61    import com.xpn.xwiki.doc.XWikiDocument;
62    import com.xpn.xwiki.objects.BaseObject;
63    import com.xpn.xwiki.user.api.XWikiGroupService;
64   
65    /**
66    * Utility class for mocking the objects as needed to test both the old and the caching right service implementations.
67    *
68    * @since 4.2
69    * @version $Id: 8f54fc42a7592d57a5180517005c2adbcb01d054 $
70    */
71    // TODO: replace with MockitoOldcoreRule
 
72    public class LegacyTestWiki extends AbstractTestWiki
73    {
74    private final boolean legacymock;
75   
76    private final XWikiContext context;
77   
78    private final XWiki xwiki;
79   
80    private final Mockery mockery;
81   
82    private final DocumentReferenceResolver<String> documentReferenceResolver;
83   
84    private final EntityReferenceSerializer<String> entityReferenceSerializer;
85   
86    private final ObservationManager observationManager;
87   
88    protected WikiDescriptorManager wikiDescriptorManager;
89   
90    private final Map<String, TestWiki> wikis = new HashMap<String, TestWiki>();
91   
92    /**
93    * @param mockery The mockery.
94    * @param componentManager The component manager.
95    * @param testWikiFilename The file name of the test wiki configuration.
96    */
 
97  18 toggle public LegacyTestWiki(Mockery mockery, ComponentManager componentManager, String testWikiFilename,
98    boolean legacymock) throws Exception
99    {
100  18 this.legacymock = legacymock;
101  18 this.mockery = mockery;
102   
103  18 this.documentReferenceResolver = componentManager.getInstance(DocumentReferenceResolver.TYPE_STRING);
104  18 this.entityReferenceSerializer = componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING);
105  18 this.observationManager = componentManager.getInstance(ObservationManager.class);
106   
107  18 mockery.setImposteriser(ClassImposteriser.INSTANCE);
108   
109  18 context = new XWikiContext();
110   
111  18 xwiki = mockery.mock(XWiki.class);
112  18 this.context.setWiki(xwiki);
113   
114  18 if (!componentManager.hasComponent(WikiDescriptorManager.class)) {
115  18 DefaultComponentDescriptor<WikiDescriptorManager> descriptor = new DefaultComponentDescriptor<>();
116  18 descriptor.setRoleType(WikiDescriptorManager.class);
117  18 this.wikiDescriptorManager = mockery.mock(WikiDescriptorManager.class);
118  18 componentManager.registerComponent(descriptor, this.wikiDescriptorManager);
119    }
120   
121  18 final XWikiGroupService groupService = mockery.mock(XWikiGroupService.class);
122   
123  18 loadTestWiki(testWikiFilename);
124   
125  18 if (this.context.getMainXWiki() == null) {
126  0 throw new RuntimeException("None of the declared wikis had attribute mainWiki=\"true\"!");
127    }
128   
129  18 this.context.setWikiId(this.context.getMainXWiki());
130   
131  18 mockery.checking(new Expectations()
132    {
 
133  18 toggle {
134    // Expectations for XWiki
135   
136  18 allowing(xwiki).getDatabase();
137  18 will(returnValue(context.getMainXWiki()));
138  18 allowing(xwiki).isReadOnly();
139  18 will(new CustomAction("indicate wether the wiki is read only")
140    {
 
141  71 toggle @Override
142    public Object invoke(Invocation invocation)
143    {
144  71 return isReadOnly(context.getWikiId());
145    }
146    });
147  18 allowing(xwiki).getMaxRecursiveSpaceChecks(with(any(XWikiContext.class)));
148  18 will(returnValue(100));
149  18 allowing(xwiki).getDocument(with(any(DocumentReference.class)), with(any(XWikiContext.class)));
150  18 will(new CustomAction("return a mocked document")
151    {
 
152  99 toggle @Override
153    public Object invoke(Invocation invocation)
154    {
155  99 DocumentReference documentReference = (DocumentReference) invocation.getParameter(0);
156  99 return getDocument(documentReference);
157    }
158    });
159  18 allowing(xwiki).getDocument(with(any(EntityReference.class)), with(any(XWikiContext.class)));
160  18 will(new CustomAction("return a mocked document")
161    {
 
162  68 toggle @Override
163    public Object invoke(Invocation invocation)
164    {
165  68 EntityReference documentReference = (EntityReference) invocation.getParameter(0);
166  68 return getDocument(documentReference, (XWikiContext) invocation.getParameter(1));
167    }
168    });
169  18 allowing(xwiki).getWikiOwner(with(aNonNull(String.class)), with(any(XWikiContext.class)));
170  18 will(new CustomAction("return the wiki owner")
171    {
 
172  88 toggle @Override
173    public Object invoke(Invocation invocation)
174    {
175  88 String wikiName = (String) invocation.getParameter(0);
176  88 TestWiki wiki = wikis.get(wikiName);
177  88 return wiki.getOwner();
178    }
179    });
180  18 allowing(xwiki).getGroupService(with(any(XWikiContext.class)));
181  18 will(returnValue(groupService));
182   
183    // Expectations for XWikiGroupService
184   
185  18 allowing(groupService).getAllGroupsReferencesForMember(with(any(DocumentReference.class)),
186    with(any(Integer.class)), with(any(Integer.class)), with(any(XWikiContext.class)));
187  18 will(new CustomAction("return a collection of member profile document references")
188    {
 
189  64 toggle @Override
190    public Object invoke(Invocation invocation)
191    {
192  64 return getAllGroupReferences((DocumentReference) invocation.getParameter(0));
193    }
194    });
195   
196    // Expectations for WikiDescriptorManager
197  18 allowing(wikiDescriptorManager).getCurrentWikiId();
198  18 will(new CustomAction("get the current wiki id")
199    {
 
200  136 toggle @Override
201    public Object invoke(Invocation invocation)
202    {
203  136 return context.getWikiId();
204    }
205    });
206   
207    // Expectations needed for old implementation
208   
209  18 allowing(xwiki).getXWikiPreference(with(any(String.class)), with(equal("")),
210    with(any(XWikiContext.class)));
211  18 will(returnValue("false"));
212  18 allowing(xwiki).getXWikiPreferenceAsInt(with(any(String.class)), with(0),
213    with(any(XWikiContext.class)));
214  18 will(returnValue(0));
215  18 allowing(xwiki).getSpacePreference(with(any(String.class)), with(equal("")),
216    with(any(XWikiContext.class)));
217  18 will(returnValue("false"));
218  18 allowing(xwiki).getSpacePreferenceAsInt(with(any(String.class)), with(0),
219    with(any(XWikiContext.class)));
220  18 will(returnValue(0));
221   
222    }
223    });
224   
225  18 if (legacymock) {
226  10 mockery.checking(new Expectations()
227    {
 
228  10 toggle {
229  10 allowing(xwiki).getDocument(with(any(String.class)), with(any(XWikiContext.class)));
230  10 will(new CustomAction("return a mocked document")
231    {
 
232  32 toggle @Override
233    public Object invoke(Invocation invocation)
234    {
235  32 String documentName = (String) invocation.getParameter(0);
236  32 return getDocument(documentName);
237    }
238    });
239  10 allowing(xwiki).getDocument(with(any(String.class)), with(any(String.class)),
240    with(any(XWikiContext.class)));
241  10 will(new CustomAction("return a mocked document")
242    {
 
243  58 toggle @Override
244    public Object invoke(Invocation invocation)
245    {
246  58 String spaceName = (String) invocation.getParameter(0);
247  58 String documentName = (String) invocation.getParameter(1);
248  58 return getDocument(spaceName, documentName);
249    }
250    });
251    }
252    });
253    }
254   
255    }
256   
 
257  0 toggle public String getMainWikiName()
258    {
259  0 return context.getMainXWiki();
260    }
261   
 
262  26 toggle @Override
263    public HasWikiContents addWiki(String name, String owner, boolean isMainWiki, boolean isReadOnly, String alt)
264    {
265   
266  26 if (isMainWiki) {
267  18 if (context.getMainXWiki() != null) {
268  0 throw new RuntimeException("Only one wiki can be the main wiki!");
269    }
270   
271  18 context.setMainXWiki(name);
272    }
273   
274  26 return mockWiki(name, owner, isReadOnly, isMainWiki, alt);
275    }
276   
 
277  4 toggle public void setUser(String username)
278    {
279  4 context.setUser(username);
280    }
281   
 
282  34 toggle public WikiReference getCurrentWikiReference()
283    {
284  34 String currentWikiId = this.context.getWikiId();
285   
286  34 return currentWikiId != null ? new WikiReference(currentWikiId) : null;
287    }
288   
 
289  1 toggle public void setSdoc(String sdocFullname)
290    {
291  1 XWikiDocument sdoc;
292  1 if (sdocFullname != null) {
293  0 sdoc = new XWikiDocument(this.documentReferenceResolver.resolve(sdocFullname, getCurrentWikiReference()));
294    } else {
295  1 sdoc = null;
296    }
297   
298  1 setSdoc(sdoc);
299    }
300   
 
301  2 toggle public void setSdoc(XWikiDocument sdoc)
302    {
303  2 this.context.put("sdoc", sdoc);
304    }
305   
 
306  2 toggle public void setDoc(String docFullname)
307    {
308  2 XWikiDocument doc;
309  2 if (docFullname != null) {
310  2 doc = new XWikiDocument(this.documentReferenceResolver.resolve(docFullname, getCurrentWikiReference()));
311    } else {
312  0 doc = null;
313    }
314   
315  2 this.context.setDoc(doc);
316    }
317   
 
318  26 toggle private TestWiki mockWiki(String name, String owner, boolean isReadOnly, boolean isMainWiki, String alt)
319    {
320  26 TestWiki wiki = wikis.get(name);
321   
322  26 if (wiki == null) {
323  26 wiki = new TestWiki(name, owner, isReadOnly, isMainWiki, alt);
324  26 wikis.put(name, wiki);
325    }
326   
327  26 return wiki;
328    }
329   
 
330  64 toggle private Collection<DocumentReference> getAllGroupReferences(final DocumentReference userReference)
331    {
332  64 final TestWiki wiki = wikis.get(context.getWikiId());
333   
334  64 if (wiki == null) {
335  0 return Collections.<DocumentReference>emptySet();
336    }
337   
338  64 final Collection<String> groupNames = wiki.getGroupsForUser(userReference);
339   
340  64 final SpaceReference userSpaceReference = new SpaceReference("XWiki", new WikiReference(wiki.getName()));
341   
342  64 return new AbstractCollection<DocumentReference>()
343    {
344   
 
345  23 toggle @Override
346    public int size()
347    {
348  23 return groupNames.size();
349    }
350   
 
351  64 toggle @Override
352    public Iterator<DocumentReference> iterator()
353    {
354  64 return new Iterator<DocumentReference>()
355    {
356   
357    private final Iterator<String> groupNamesIterator = groupNames.iterator();
358   
 
359  80 toggle @Override
360    public boolean hasNext()
361    {
362  80 return groupNamesIterator.hasNext();
363    }
364   
 
365  16 toggle @Override
366    public DocumentReference next()
367    {
368  16 String groupName = groupNamesIterator.next();
369   
370  16 return documentReferenceResolver.resolve(groupName, userSpaceReference);
371    }
372   
 
373  0 toggle @Override
374    public void remove()
375    {
376  0 throw new UnsupportedOperationException();
377    }
378    };
379    }
380    };
381    }
382   
 
383  28 toggle @Override
384    public XWikiContext getXWikiContext()
385    {
386  28 return context;
387    }
388   
 
389  71 toggle public boolean isReadOnly(String wikiName)
390    {
391  71 return wikis.get(wikiName).isReadOnly();
392    }
393   
 
394  0 toggle public String getWikiPrettyName(WikiReference wikiReference)
395    {
396  0 TestWiki wiki = wikis.get(wikiReference.getName());
397   
398  0 return (wiki != null) ? wiki.getPrettyName() : wikiReference.getName();
399    }
400   
 
401  0 toggle public String getSpacePrettyName(SpaceReference spaceReference)
402    {
403  0 TestWiki wiki = wikis.get(spaceReference.getParent().getName());
404   
405  0 if (wiki == null) {
406  0 return null;
407    }
408   
409  0 TestSpace space = wiki.getTestSpace(spaceReference.getName());
410   
411  0 return (space != null) ? space.getPrettyName() : spaceReference.getName();
412    }
413   
 
414  0 toggle public String getDocumentPrettyName(DocumentReference documentReference)
415    {
416  0 TestWiki wiki = wikis.get(documentReference.getWikiReference().getName());
417   
418  0 if (wiki == null) {
419  0 return null;
420    }
421   
422  0 TestDocument doc = wiki.getTestDocument(documentReference.getParent().getName(), documentReference.getName());
423   
424  0 return (doc != null) ? doc.getPrettyName() : documentReference.getName();
425    }
426   
 
427  167 toggle private XWikiDocument getDocument(DocumentReference documentReference)
428    {
429  167 TestWiki wiki = wikis.get(documentReference.getWikiReference().getName());
430  167 return wiki.getDocument(documentReference);
431    }
432   
 
433  68 toggle private XWikiDocument getDocument(EntityReference entityReference, XWikiContext context)
434    {
435  68 return getDocument(new DocumentReference(entityReference.appendParent(new WikiReference(context.getWikiId()))));
436    }
437   
 
438  32 toggle private XWikiDocument getDocument(String name)
439    {
440  32 DocumentReference documentReference = documentReferenceResolver.resolve(name, getCurrentWikiReference());
441   
442  32 TestWiki wiki = wikis.get(documentReference.getWikiReference().getName());
443   
444  32 if (wiki == null) {
445  0 return null;
446    }
447   
448  32 return wiki.getDocument(documentReference);
449    }
450   
 
451  58 toggle private XWikiDocument getDocument(String spaceName, String documentName)
452    {
453  58 DocumentReference documentReference = new DocumentReference(this.context.getWikiId(), spaceName, documentName);
454   
455  58 TestWiki wiki = wikis.get(documentReference.getWikiReference().getName());
456   
457  58 if (wiki == null) {
458  0 return null;
459    }
460   
461  58 return wiki.getDocument(documentReference);
462    }
463   
 
464    private class TestWiki extends TestAcl implements HasWikiContents
465    {
466    private final SpaceReference userSpaceReference;
467   
468    private final String name;
469   
470    private final String owner;
471   
472    private final String alt;
473   
474    private final Map<String, TestSpace> spaces = new HashMap<String, TestSpace>();
475   
476    private final Set<String> users = new HashSet<String>();
477   
478    private final Map<String, Set<String>> groups = new HashMap<String, Set<String>>();
479   
480    private final Map<String, Set<String>> groupsForUser = new HashMap<String, Set<String>>();
481   
482    private final boolean isReadOnly;
483   
484    private final boolean isMainWiki;
485   
 
486  0 toggle TestWiki(String name, String owner, boolean isReadOnly)
487    {
488  0 this(name, owner, isReadOnly, false);
489    }
490   
 
491  0 toggle TestWiki(String name, String owner, boolean isReadOnly, boolean isMainWiki)
492    {
493  0 this(name, owner, isReadOnly, isMainWiki, null);
494    }
495   
 
496  26 toggle TestWiki(String name, String owner, boolean isReadOnly, boolean isMainWiki, String alt)
497    {
498  26 this.name = name;
499  26 this.owner = owner;
500  26 this.isReadOnly = isReadOnly;
501  26 this.isMainWiki = isMainWiki;
502  26 this.alt = alt;
503  26 this.userSpaceReference = new SpaceReference("XWiki", new WikiReference(name));
504   
505    // The XWikiPreferences document always exist (unless explicitly deleted!) since it will be created on
506    // application startup or at wiki creation.
507  26 mockDocument("XWiki", "XWikiPreferences", "XWiki.Admin", false);
508    }
509   
 
510  22 toggle @Override
511    public HasDocuments addSpace(String name, String alt)
512    {
513  22 return mockSpace(name, alt);
514    }
515   
 
516  26 toggle TestDocument mockDocument(String spaceName, String name, String creator, boolean isNew)
517    {
518  26 TestSpace space = mockSpace(spaceName);
519   
520  26 return space.mockDocument(name, creator, isNew, alt);
521    }
522   
 
523  306 toggle TestSpace mockSpace(String name)
524    {
525  306 return mockSpace(name, null);
526    }
527   
 
528  328 toggle TestSpace mockSpace(String name, String alt)
529    {
530  328 TestSpace space = spaces.get(name);
531   
532  328 if (space == null) {
533  42 space = new TestSpace(name, this, alt);
534  42 spaces.put(name, space);
535    }
536   
537  328 return space;
538    }
539   
 
540  0 toggle XWikiDocument removeDocument(DocumentReference documentReference)
541    {
542  0 return removeDocument(documentReference.getParent().getName(), documentReference.getName());
543    }
544   
 
545  1 toggle XWikiDocument removeDocument(String spaceName, String documentName)
546    {
547  1 TestSpace space = mockSpace(spaceName);
548   
549  1 return space.removeDocument(documentName);
550    }
551   
 
552  257 toggle XWikiDocument getDocument(DocumentReference documentReference)
553    {
554  257 TestSpace space = mockSpace(documentReference.getParent().getName());
555   
556  257 return space.getDocument(documentReference);
557    }
558   
 
559  0 toggle TestSpace getTestSpace(String spaceName)
560    {
561  0 if (spaces.containsKey(spaceName)) {
562  0 TestSpace space = mockSpace(spaceName);
563   
564  0 return space;
565    }
566   
567  0 return null;
568    }
569   
 
570  1 toggle TestDocument getTestDocument(String spaceName, String documentName)
571    {
572  1 if (spaces.containsKey(spaceName)) {
573  1 TestSpace space = mockSpace(spaceName);
574   
575  1 return space.getTestDocument(documentName);
576    }
577   
578  0 return null;
579    }
580   
 
581  0 toggle @Override
582    EntityType getType()
583    {
584  0 if (isMainWiki) {
585  0 return null; // FARM
586    } else {
587  0 return EntityType.WIKI;
588    }
589    }
590   
 
591  669 toggle @Override
592    String getName()
593    {
594  669 return name;
595    }
596   
 
597  0 toggle @Override
598    String getPrettyName()
599    {
600  0 return (this.alt != null) ? this.alt : this.name;
601    }
602   
 
603  88 toggle String getOwner()
604    {
605  88 return owner;
606    }
607   
 
608  71 toggle public boolean isReadOnly()
609    {
610  71 return isReadOnly;
611    }
612   
 
613  0 toggle Collection<String> getGroupsForUser(String userName)
614    {
615  0 DocumentReference userDoc = documentReferenceResolver.resolve(userName, userSpaceReference);
616  0 return getGroupsForUser(userDoc);
617    }
618   
 
619  64 toggle Collection<String> getGroupsForUser(DocumentReference userDoc)
620    {
621  64 Set<String> groups;
622  64 if (userDoc.getWikiReference().getName().equals(getName())) {
623  43 groups = groupsForUser.get(userDoc.getName());
624    } else {
625  21 groups = groupsForUser.get(entityReferenceSerializer.serialize(userDoc));
626    }
627   
628  64 return groups == null ? Collections.<String>emptySet() : groups;
629    }
630   
 
631  21 toggle void notifyCreatedDocument(XWikiDocument document)
632    {
633    // Send event
634    // Do not notify during parsing !
635  21 if (context.getMainXWiki() == null) {
636  0 observationManager.notify(new DocumentCreatedEvent(document.getDocumentReference()), document, context);
637    }
638    }
639   
 
640  1 toggle void notifyDeleteDocument(XWikiDocument document)
641    {
642  1 XWikiDocument newDocument = new XWikiDocument(document.getDocumentReference());
643  1 newDocument.setOriginalDocument(document);
644   
645    // Send event
646  1 observationManager.notify(new DocumentDeletedEvent(newDocument.getDocumentReference()), newDocument,
647    context);
648    }
649   
 
650  16 toggle @Override
651    public void addUser(String userName)
652    {
653  16 DocumentReference userDoc = documentReferenceResolver.resolve(userName, userSpaceReference);
654  16 users.add(userDoc.getName());
655  16 if (groupsForUser.get(userDoc.getName()) == null) {
656  16 groupsForUser.put(userDoc.getName(), new HashSet<String>());
657    }
658   
659  16 TestSpace testSpace = mockSpace(userDoc.getParent().getName());
660   
661  16 UserTestDocument userTestDocument = new UserTestDocument(userDoc.getName(), testSpace, null, false);
662   
663  16 testSpace.documents.put(userDoc.getName(), userTestDocument);
664   
665    // Send event
666  16 notifyCreatedDocument(userTestDocument.getDocument());
667    }
668   
 
669  1 toggle void deleteUser(String userName)
670    {
671  1 DocumentReference userDoc = documentReferenceResolver.resolve(userName, userSpaceReference);
672  1 if (users.contains(userDoc.getName())) {
673  1 users.remove(userDoc.getName());
674  1 groupsForUser.remove(userDoc.getName());
675  1 for (Map.Entry<String, Set<String>> entry : groups.entrySet()) {
676  1 entry.getValue().remove(userDoc.getName());
677  1 ((GroupTestDocument) getTestDocument(userDoc.getParent().getName(), entry.getKey()))
678    .removeUser(userDoc.getName());
679    }
680   
681    // Make sure user document is removed
682  1 XWikiDocument userDocument = removeDocument(userDoc.getParent().getName(), userDoc.getName());
683   
684    // Send event
685  1 notifyDeleteDocument(userDocument);
686    }
687    }
688   
 
689  5 toggle @Override
690    public GroupTestDocument addGroup(final String groupName)
691    {
692  5 DocumentReference groupDoc = documentReferenceResolver.resolve(groupName, userSpaceReference);
693  5 Set<String> groupMembers = groups.get(groupDoc.getName());
694   
695  5 if (groupMembers == null) {
696  5 groupMembers = new HashSet<String>();
697  5 groups.put(groupDoc.getName(), groupMembers);
698    }
699   
700  5 TestSpace testSpace = mockSpace(groupDoc.getParent().getName());
701   
702  5 GroupTestDocument groupTestDocument = new GroupTestDocument(groupDoc.getName(), testSpace, null, false);
703   
704  5 testSpace.documents.put(groupDoc.getName(), groupTestDocument);
705   
706    // Send event
707  5 notifyCreatedDocument(groupTestDocument.getDocument());
708   
709  5 return groupTestDocument;
710    }
711   
 
712  0 toggle void deleteGroup(String groupName)
713    {
714  0 DocumentReference groupDoc = documentReferenceResolver.resolve(groupName, userSpaceReference);
715  0 if (groups.containsKey(groupDoc.getName())) {
716  0 groups.remove(groupDoc.getName());
717  0 for (Set<String> groups : groupsForUser.values()) {
718  0 groups.remove(groupDoc.getName());
719    }
720   
721    // Make sure group document is removed
722  0 XWikiDocument groupDocument = removeDocument(groupDoc.getParent().getName(), groupDoc.getName());
723   
724    // Send event
725  0 notifyDeleteDocument(groupDocument);
726    }
727    }
728   
 
729  0 toggle TestDocument addDocument(DocumentReference documentReference, String creator, boolean isNew)
730    {
731  0 TestDocument document = mockDocument(documentReference.getParent().getName(), creator, creator, isNew);
732   
733    // Send event
734  0 notifyCreatedDocument(document.getDocument());
735   
736  0 return document;
737    }
738   
 
739  0 toggle void deleteDocument(DocumentReference documentReference)
740    {
741  0 XWikiDocument document = removeDocument(documentReference);
742   
743  0 if (document != null) {
744    // Send event
745  0 notifyDeleteDocument(document);
746    }
747    }
748    }
749   
 
750    private class TestSpace extends TestAcl implements HasDocuments
751    {
752    private final String name;
753   
754    private final String alt;
755   
756    private final TestWiki wiki;
757   
758    private final Map<String, TestDocument> documents = new HashMap<String, TestDocument>();
759   
 
760  0 toggle TestSpace(String name, TestWiki wiki)
761    {
762  0 this(name, wiki, null);
763    }
764   
 
765  42 toggle TestSpace(String name, TestWiki wiki, String alt)
766    {
767  42 this.name = name;
768  42 this.alt = alt;
769  42 this.wiki = wiki;
770    }
771   
 
772  24 toggle @Override
773    public HasAcl addDocument(String name, String creator, String alt)
774    {
775  24 return mockDocument(name, creator, false, alt);
776    }
777   
 
778  1 toggle @Override
779    public XWikiDocument removeDocument(String name)
780    {
781  1 TestDocument document = documents.get(name);
782   
783  1 if (document == null) {
784  0 return null;
785    }
786   
787  1 return document.getDocument();
788    }
789   
 
790  257 toggle TestDocument mockDocument(String name, String creator, boolean isNew)
791    {
792  257 return mockDocument(name, creator, isNew, null);
793    }
794   
 
795  307 toggle TestDocument mockDocument(String name, String creator, boolean isNew, String alt)
796    {
797  307 TestDocument document = getTestDocument(name);
798   
799  307 if (document == null) {
800  76 if (creator == null) {
801  0 creator = "XWiki.Admin";
802    }
803  76 document = new TestDocument(name, this, creator, isNew, alt);
804  76 documents.put(name, document);
805    }
806   
807  307 return document;
808    }
809   
 
810  257 toggle XWikiDocument getDocument(DocumentReference documentReference)
811    {
812  257 TestDocument document = mockDocument(documentReference.getName(), context.getUser(), true);
813   
814  257 return document.getDocument();
815    }
816   
 
817  308 toggle public TestDocument getTestDocument(String name)
818    {
819  308 return documents.get(name);
820    }
821   
 
822  0 toggle @Override
823    EntityType getType()
824    {
825  0 return EntityType.SPACE;
826    }
827   
 
828  254 toggle @Override
829    String getName()
830    {
831  254 return name;
832    }
833   
 
834  0 toggle @Override
835    String getPrettyName()
836    {
837  0 return (this.alt != null) ? this.alt : this.name;
838    }
839   
 
840  496 toggle TestWiki getWiki()
841    {
842  496 return wiki;
843    }
844    }
845   
 
846    private class UserTestDocument extends TestDocument
847    {
848    private BaseObject userObject;
849   
 
850  16 toggle UserTestDocument(String name, TestSpace space, String creator, Boolean isNew)
851    {
852  16 this(name, space, creator, isNew, null);
853    }
854   
 
855  16 toggle UserTestDocument(String name, TestSpace space, String creator, Boolean isNew, String alt)
856    {
857  16 super(name, space, creator, isNew, alt);
858   
859  16 this.userObject = mockUserBaseObject();
860   
861  16 mockery.checking(new Expectations()
862    {
 
863  16 toggle {
864  16 allowing(mockedDocument).getXObject(with(equal(new LocalDocumentReference("XWiki", "XWikiUsers"))));
865  16 will(new CustomAction("return the user object")
866    {
 
867  0 toggle @Override
868    public Object invoke(Invocation invocation)
869    {
870  0 return userObject;
871    }
872    });
873    }
874    });
875    }
876   
 
877  16 toggle private BaseObject mockUserBaseObject()
878    {
879  16 ++objectNumber;
880   
881  16 final BaseObject userBaseObject = mockery.mock(BaseObject.class, getName() + objectNumber);
882   
883  16 return userBaseObject;
884    }
885   
 
886  0 toggle @Override
887    public Map<DocumentReference, List<BaseObject>> getLegacyXObjects()
888    {
889  0 Map<DocumentReference, List<BaseObject>> xobjects = super.getLegacyXObjects();
890   
891  0 xobjects.put(new DocumentReference(this.space.wiki.getName(), "XWiki", "XWikiUsers"),
892    Arrays.asList(this.userObject));
893   
894  0 return xobjects;
895    }
896    }
897   
 
898    private class GroupTestDocument extends TestDocument implements HasUsers
899    {
900    private Map<String, BaseObject> memberObjects = new LinkedHashMap<String, BaseObject>();
901   
 
902  5 toggle GroupTestDocument(String name, TestSpace space, String creator, Boolean isNew)
903    {
904  5 this(name, space, creator, isNew, null);
905    }
906   
 
907  5 toggle GroupTestDocument(String name, TestSpace space, String creator, Boolean isNew, String alt)
908    {
909  5 super(name, space, creator, isNew, alt);
910   
911  5 mockery.checking(new Expectations()
912    {
 
913  5 toggle {
914  5 allowing(mockedDocument)
915    .getXObjects(with(equal(new LocalDocumentReference("XWiki", "XWikiGroups"))));
916  5 will(new CustomAction("return a vector of group members")
917    {
 
918  0 toggle @Override
919    public Object invoke(Invocation invocation)
920    {
921  0 return new Vector<BaseObject>(memberObjects.values());
922    }
923    });
924    }
925    });
926    }
927   
 
928  6 toggle private BaseObject mockGroupBaseObject(String user)
929    {
930  6 ++objectNumber;
931   
932  6 final BaseObject userBaseObject = mockery.mock(BaseObject.class, getName() + objectNumber);
933   
934  6 return userBaseObject;
935    }
936   
 
937  0 toggle @Override
938    public Map<DocumentReference, List<BaseObject>> getLegacyXObjects()
939    {
940  0 Map<DocumentReference, List<BaseObject>> xobjects = super.getLegacyXObjects();
941   
942  0 xobjects.put(new DocumentReference(this.space.wiki.getName(), "XWiki", "XWikiUsers"),
943    new ArrayList<BaseObject>(this.memberObjects.values()));
944   
945  0 return xobjects;
946    }
947   
 
948  6 toggle @Override
949    public void addUser(String userName)
950    {
951  6 TestWiki testWiki = space.getWiki();
952  6 DocumentReference userDoc = documentReferenceResolver.resolve(userName,
953    new SpaceReference("XWiki", new WikiReference(testWiki.getName())));
954   
955  6 String uname;
956  6 if (userDoc.getWikiReference().getName().equals(testWiki.getName())) {
957  4 uname = userDoc.getName();
958    } else {
959  2 uname = entityReferenceSerializer.serialize(userDoc);
960    }
961   
962  6 Set<String> groups = testWiki.groupsForUser.get(uname);
963  6 if (groups == null) {
964  2 groups = new HashSet<String>();
965  2 testWiki.groupsForUser.put(uname, groups);
966    }
967  6 groups.add(getName());
968   
969  6 testWiki.groups.get(getName()).add(uname);
970   
971  6 this.memberObjects.put(uname, mockGroupBaseObject(uname));
972    }
973   
 
974  1 toggle public void removeUser(String userName)
975    {
976  1 TestWiki testWiki = space.getWiki();
977  1 DocumentReference userDoc = documentReferenceResolver.resolve(userName,
978    new SpaceReference("XWiki", new WikiReference(testWiki.getName())));
979   
980  1 String uname;
981  1 if (userDoc.getWikiReference().getName().equals(testWiki.getName())) {
982  1 uname = userDoc.getName();
983    } else {
984  0 uname = entityReferenceSerializer.serialize(userDoc);
985    }
986   
987  1 this.memberObjects.remove(uname);
988    }
989    }
990   
 
991    private class TestDocument extends TestAcl
992    {
993    protected final XWikiDocument mockedDocument;
994   
995    protected final TestSpace space;
996   
997    protected final String name;
998   
999    protected final String alt;
1000   
1001    protected final String creator;
1002   
 
1003  0 toggle TestDocument(final String name, final TestSpace space, final String creator, final Boolean isNew)
1004    {
1005  0 this(name, space, creator, isNew, null);
1006    }
1007   
 
1008  97 toggle TestDocument(final String name, final TestSpace space, final String creator, final Boolean isNew, String alt)
1009    {
1010  97 this.space = space;
1011  97 this.name = name;
1012  97 this.creator = creator;
1013  97 this.alt = alt;
1014   
1015  97 mockedDocument = mockery.mock(XWikiDocument.class, new Formatter()
1016    .format("%s:%s.%s", getSpace().getWiki().getName(), getSpace().getName(), getName()).toString());
1017   
1018  97 final DocumentReference documentReference =
1019    new DocumentReference(getSpace().getWiki().getName(), getSpace().getName(), getName());
1020   
1021  97 mockery.checking(new Expectations()
1022    {
 
1023  97 toggle {
1024  97 allowing(mockedDocument)
1025    .getXObjects(with(equal(new LocalDocumentReference("XWiki", "XWikiRights"))));
1026  97 will(new CustomAction("return a vector of rights")
1027    {
 
1028  66 toggle @Override
1029    public Object invoke(Invocation invocation)
1030    {
1031  66 return getLegacyDocumentRights();
1032    }
1033    });
1034  97 allowing(mockedDocument)
1035    .getXObjects(with(equal(new LocalDocumentReference("XWiki", "XWikiGlobalRights"))));
1036  97 will(new CustomAction("return a vector of rights")
1037    {
 
1038  153 toggle @Override
1039    public Object invoke(Invocation invocation)
1040    {
1041  153 return getLegacyGlobalRights();
1042    }
1043    });
1044  97 allowing(mockedDocument)
1045    .getXObjects(new DocumentReference(space.wiki.getName(), "XWiki", "XWikiRights"));
1046  97 will(new CustomAction("return a vector of rights")
1047    {
 
1048  36 toggle @Override
1049    public Object invoke(Invocation invocation)
1050    {
1051  36 return getLegacyDocumentRights();
1052    }
1053    });
1054  97 allowing(mockedDocument)
1055    .getXObjects(new DocumentReference(space.wiki.getName(), "XWiki", "XWikiGlobalRights"));
1056  97 will(new CustomAction("return a vector of rights")
1057    {
 
1058  28 toggle @Override
1059    public Object invoke(Invocation invocation)
1060    {
1061  28 return getLegacyGlobalRights();
1062    }
1063    });
1064  97 allowing(mockedDocument).getCreatorReference();
1065  97 will(returnValue(documentReferenceResolver.resolve(TestDocument.this.creator, documentReference)));
1066  97 allowing(mockedDocument).getDocumentReference();
1067  97 will(returnValue(documentReference));
1068  97 allowing(mockedDocument).isNew();
1069  97 will(returnValue(isNew));
1070    }
1071    });
1072   
1073  97 if (legacymock) {
1074  58 mockery.checking(new Expectations()
1075    {
 
1076  58 toggle {
1077  58 allowing(mockedDocument).getObjects("XWiki.XWikiGlobalRights");
1078  58 will(new CustomAction("return a vector of global rights")
1079    {
 
1080  0 toggle @Override
1081    public Object invoke(Invocation invocation)
1082    {
1083  0 return getLegacyGlobalRights();
1084    }
1085    });
1086  58 allowing(mockedDocument).getObjects("XWiki.XWikiRights");
1087  58 will(new CustomAction("return a vector of document rights")
1088    {
 
1089  0 toggle @Override
1090    public Object invoke(Invocation invocation)
1091    {
1092  0 return getLegacyDocumentRights();
1093    }
1094    });
1095  58 allowing(mockedDocument).getWikiName();
1096  58 will(returnValue(getSpace().getWiki().getName()));
1097  58 allowing(mockedDocument).getDatabase();
1098  58 will(returnValue(getSpace().getWiki().getName()));
1099  58 allowing(mockedDocument).getSpace();
1100  58 will(returnValue(getSpace().getName()));
1101    }
1102    });
1103    }
1104    }
1105   
 
1106  279 toggle public XWikiDocument getDocument()
1107    {
1108  279 return mockedDocument;
1109    }
1110   
 
1111  181 toggle public Vector<BaseObject> getLegacyGlobalRights()
1112    {
1113  181 if ("XWikiPreferences".equals(getName())) {
1114  179 return getSpace().getWiki().getLegacyRightObjects();
1115  2 } else if ("WebPreferences".equals(getName())) {
1116  2 return getSpace().getLegacyRightObjects();
1117    }
1118  0 return null;
1119    }
1120   
 
1121  102 toggle public Vector<BaseObject> getLegacyDocumentRights()
1122    {
1123  102 return getLegacyRightObjects();
1124    }
1125   
 
1126  0 toggle public Map<DocumentReference, List<BaseObject>> getLegacyXObjects()
1127    {
1128  0 Map<DocumentReference, List<BaseObject>> objects = new HashMap<DocumentReference, List<BaseObject>>();
1129   
1130  0 Vector<BaseObject> globalRights = getLegacyGlobalRights();
1131  0 if (globalRights != null) {
1132  0 objects.put(new DocumentReference(this.space.wiki.getName(), "XWiki", "XWikiGlobalRights"),
1133    globalRights);
1134    }
1135  0 Vector<BaseObject> rights = getLegacyDocumentRights();
1136  0 if (globalRights != null) {
1137  0 objects.put(new DocumentReference(this.space.wiki.getName(), "XWiki", "XWikiRights"), rights);
1138    }
1139   
1140  0 return objects;
1141    }
1142   
 
1143  0 toggle @Override
1144    EntityType getType()
1145    {
1146  0 return EntityType.DOCUMENT;
1147    }
1148   
 
1149  415 toggle @Override
1150    public String getName()
1151    {
1152  415 return this.name;
1153    }
1154   
 
1155  0 toggle @Override
1156    public String getPrettyName()
1157    {
1158  0 return (this.alt != null) ? this.alt : this.name;
1159    }
1160   
 
1161  743 toggle TestSpace getSpace()
1162    {
1163  743 return this.space;
1164    }
1165    }
1166   
1167    public static int objectNumber = 0;
1168   
 
1169    private abstract class TestAcl implements HasAcl
1170    {
1171    private final Map<String, Set<String>> allowUser = new HashMap<String, Set<String>>();
1172   
1173    private final Map<String, Set<String>> denyUser = new HashMap<String, Set<String>>();
1174   
1175    private final Map<String, Set<String>> allowGroup = new HashMap<String, Set<String>>();
1176   
1177    private final Map<String, Set<String>> denyGroup = new HashMap<String, Set<String>>();
1178   
1179    private List<BaseObject> mockedObjects = null;
1180   
 
1181  30 toggle private void addType(Map<String, Set<String>> map, String key, String type)
1182    {
1183  30 Set<String> types = map.get(key);
1184   
1185  30 if (types == null) {
1186  23 types = new HashSet<String>();
1187  23 map.put(key, types);
1188    }
1189   
1190  30 types.add(type);
1191    }
1192   
 
1193  30 toggle private void addRightType(Map<String, Set<String>> map, String key, String type)
1194    {
1195  30 if (type == null) {
1196  0 for (Right right : Right.getEnabledRights(getType())) {
1197  0 addType(map, key, right.toString());
1198    }
1199    } else {
1200  30 addType(map, key, type);
1201    }
1202    }
1203   
 
1204  18 toggle @Override
1205    public void addAllowUser(String name, String type)
1206    {
1207  18 addRightType(this.allowUser, name, type);
1208    }
1209   
 
1210  2 toggle @Override
1211    public void addDenyUser(String name, String type)
1212    {
1213  2 addRightType(this.denyUser, name, type);
1214    }
1215   
 
1216  10 toggle @Override
1217    public void addAllowGroup(String name, String type)
1218    {
1219  10 addRightType(this.allowGroup, name, type);
1220    }
1221   
 
1222  0 toggle @Override
1223    public void addDenyGroup(String name, String type)
1224    {
1225  0 addRightType(this.denyGroup, name, type);
1226    }
1227   
1228    abstract EntityType getType();
1229   
1230    abstract String getName();
1231   
1232    abstract String getPrettyName();
1233   
 
1234  283 toggle public Vector<BaseObject> getLegacyRightObjects()
1235    {
1236  283 if (this.mockedObjects == null) {
1237  64 this.mockedObjects = new ArrayList<BaseObject>();
1238  64 for (Map.Entry<String, Set<String>> entry : this.allowUser.entrySet()) {
1239  11 for (String type : entry.getValue()) {
1240  17 this.mockedObjects.add(mockRightBaseObject(entry.getKey(), type, true, true));
1241    }
1242    }
1243  64 for (Map.Entry<String, Set<String>> entry : this.denyUser.entrySet()) {
1244  1 for (String type : entry.getValue()) {
1245  2 this.mockedObjects.add(mockRightBaseObject(entry.getKey(), type, true, false));
1246    }
1247    }
1248   
1249  64 for (Map.Entry<String, Set<String>> entry : this.allowGroup.entrySet()) {
1250  10 for (String type : entry.getValue()) {
1251  10 this.mockedObjects.add(mockRightBaseObject(entry.getKey(), type, false, true));
1252    }
1253    }
1254   
1255  64 for (Map.Entry<String, Set<String>> entry : this.denyGroup.entrySet()) {
1256  0 for (String type : entry.getValue()) {
1257  0 this.mockedObjects.add(mockRightBaseObject(entry.getKey(), type, false, false));
1258    }
1259    }
1260    }
1261   
1262  283 return this.mockedObjects.size() == 0 ? null : new Vector<BaseObject>(this.mockedObjects);
1263    }
1264   
 
1265  29 toggle private BaseObject mockRightBaseObject(final String name, final String type, final boolean isUser,
1266    final boolean allow)
1267    {
1268  29 objectNumber++;
1269   
1270  29 final BaseObject rightBaseObjects =
1271    mockery.mock(BaseObject.class, getName() + objectNumber + ' ' + name + ' ' + type + ' ' + allow);
1272   
1273  29 final String usersString = isUser ? name : "";
1274  29 final String groupsString = isUser ? "" : name;
1275  29 final String levelsString = type;
1276   
1277  29 mockery.checking(new Expectations()
1278    {
 
1279  29 toggle {
1280  29 allowing(rightBaseObjects).getIntValue("allow");
1281  29 will(returnValue(allow ? 1 : 0));
1282   
1283  29 allowing(rightBaseObjects).getStringValue("users");
1284  29 will(returnValue(usersString));
1285  29 allowing(rightBaseObjects).getStringValue("groups");
1286  29 will(returnValue(groupsString));
1287  29 allowing(rightBaseObjects).getStringValue("levels");
1288  29 will(returnValue(levelsString));
1289    }
1290    });
1291   
1292  29 return rightBaseObjects;
1293    }
1294    }
1295   
1296    // Modifications
1297   
 
1298  0 toggle public void addUser(String userName, String wikiName)
1299    {
1300  0 TestWiki wiki = this.wikis.get(wikiName);
1301   
1302  0 wiki.addUser(userName);
1303    }
1304   
 
1305  1 toggle public void deleteUser(String userName, String wikiName)
1306    {
1307  1 TestWiki wiki = this.wikis.get(wikiName);
1308   
1309  1 wiki.deleteUser(userName);
1310    }
1311   
 
1312  0 toggle public void addGroup(String groupName, String wikiName)
1313    {
1314  0 TestWiki wiki = wikis.get(wikiName);
1315   
1316  0 wiki.addGroup(groupName);
1317    }
1318   
 
1319  0 toggle public void deleteGroup(String groupName, String wikiName)
1320    {
1321  0 TestWiki wiki = wikis.get(wikiName);
1322   
1323  0 wiki.deleteGroup(groupName);
1324    }
1325   
 
1326  0 toggle public TestDocument addDocument(DocumentReference documentReference, String creator, boolean isNew)
1327    {
1328  0 TestWiki wiki = wikis.get(documentReference.getWikiReference().getName());
1329   
1330  0 return wiki.addDocument(documentReference, creator, isNew);
1331    }
1332   
 
1333  0 toggle public void deleteDocument(DocumentReference documentReference)
1334    {
1335  0 TestWiki wiki = wikis.get(documentReference.getWikiReference().getName());
1336   
1337  0 wiki.deleteDocument(documentReference);
1338    }
1339   
 
1340  0 toggle public void notifyDocumentModified(DocumentReference documentReference)
1341    {
1342  0 TestWiki wiki = wikis.get(documentReference.getWikiReference().getName());
1343   
1344  0 XWikiDocument document = wiki.getDocument(documentReference);
1345   
1346    // Send event
1347  0 observationManager.notify(new DocumentUpdatedEvent(documentReference), document, context);
1348    }
1349    }