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

File ContextComponentManagerTest.java

 

Code metrics

0
224
26
3
508
379
32
0.14
8.62
8.67
1.23

Classes

Class Line # Actions
ContextComponentManagerTest 54 224 0% 32 6
0.97697.6%
ContextComponentManagerTest.Role 67 0 - 0 0
-1.0 -
ContextComponentManagerTest.RoleImpl 71 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 13 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.component.internal;
21   
22    import javax.inject.Provider;
23   
24    import org.jmock.Expectations;
25    import org.jmock.States;
26    import org.junit.Assert;
27    import org.junit.Test;
28    import org.xwiki.bridge.DocumentAccessBridge;
29    import org.xwiki.bridge.event.DocumentDeletedEvent;
30    import org.xwiki.bridge.event.WikiDeletedEvent;
31    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
32    import org.xwiki.component.internal.multi.ComponentManagerManager;
33    import org.xwiki.component.manager.ComponentLookupException;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.component.manager.NamespacedComponentManager;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.model.reference.SpaceReference;
38    import org.xwiki.model.reference.WikiReference;
39    import org.xwiki.observation.ObservationManager;
40    import org.xwiki.test.jmock.AbstractComponentTestCase;
41    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
42   
43    import static org.junit.Assert.assertEquals;
44    import static org.junit.Assert.assertNotNull;
45   
46    /**
47    * Unit tests for {@link ContextComponentManager} which indirectly test
48    * {@link org.xwiki.component.internal.WikiComponentManager} and
49    * {@link org.xwiki.component.internal.UserComponentManager} (and their ancillary classes).
50    *
51    * @version $Id: f112a76ebb7b54a485363a81333c51d505e1414c $
52    * @since 2.1RC1
53    */
 
54    public class ContextComponentManagerTest extends AbstractComponentTestCase
55    {
56    /**
57    * Mock document access bridge.
58    */
59    private DocumentAccessBridge mockDocumentAccessBridge;
60   
61    private WikiDescriptorManager mockWikiDescriptorManager;
62   
63    private Provider<DocumentReference> mockCurrentDocumentReferenceProvider;
64   
65    private Provider<SpaceReference> mockCurrentSpaceReferenceProvider;
66   
 
67    public static interface Role
68    {
69    }
70   
 
71    public static class RoleImpl implements Role
72    {
73    }
74   
 
75  13 toggle @Override
76    protected void registerComponents() throws Exception
77    {
78  13 super.registerComponents();
79   
80    // Document Access Bridge Mock
81  13 this.mockDocumentAccessBridge = registerMockComponent(DocumentAccessBridge.class);
82  13 this.mockWikiDescriptorManager = registerMockComponent(WikiDescriptorManager.class);
83  13 this.mockCurrentDocumentReferenceProvider = registerMockComponent(DocumentReference.TYPE_PROVIDER, "current");
84  13 this.mockCurrentSpaceReferenceProvider = registerMockComponent(SpaceReference.TYPE_PROVIDER, "current");
85    }
86   
 
87  13 toggle @Override
88    public void setUp() throws Exception
89    {
90  13 super.setUp();
91   
92    // Enabled component registration events
93  13 StackingComponentEventManager eventManager = new StackingComponentEventManager();
94  13 eventManager
95    .setObservationManager(getComponentManager().<ObservationManager>getInstance(ObservationManager.class));
96  13 eventManager.shouldStack(false);
97  13 getComponentManager().setComponentEventManager(eventManager);
98    }
99   
 
100  1 toggle @Test
101    public void testRegisterComponentInUserComponentManager() throws Exception
102    {
103  1 final States state = getMockery().states("test");
104   
105  1 getMockery().checking(new Expectations()
106    {
 
107  1 toggle {
108  1 allowing(mockDocumentAccessBridge).getCurrentUserReference();
109  1 when(state.isNot("otheruser"));
110  1 will(returnValue(new DocumentReference("wiki", "XWiki", "user1")));
111  1 allowing(mockWikiDescriptorManager).getCurrentWikiId();
112  1 will(returnValue("wiki"));
113  1 allowing(mockCurrentSpaceReferenceProvider).get();
114  1 will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
115  1 allowing(mockCurrentDocumentReferenceProvider).get();
116  1 will(returnValue(new DocumentReference("wiki", "space", "document")));
117    }
118    });
119   
120  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
121   
122    // Lookup not yet registered component (and feel the caches with null CMs)
123   
124  1 try {
125  1 contextCM.getInstance(Role.class);
126  0 Assert.fail("Should have raised an exception");
127    } catch (ComponentLookupException expected) {
128    // No need to assert the message, we just want to ensure an exception is raised.
129    }
130   
131    // Register component for the current user
132  1 ComponentManager userCM = getComponentManager().getInstance(ComponentManager.class, "user");
133  1 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
134  1 cd.setRole(Role.class);
135  1 cd.setImplementation(RoleImpl.class);
136  1 userCM.registerComponent(cd);
137   
138    // Verify we can lookup the component from the Context CM
139  1 Assert.assertNotNull(contextCM.getInstance(Role.class));
140   
141    // Now verify that we cannot look it up anymore if there's another user in the context
142  1 state.become("otheruser");
143  1 getMockery().checking(new Expectations()
144    {
 
145  1 toggle {
146  1 allowing(mockDocumentAccessBridge).getCurrentUserReference();
147  1 will(returnValue(new DocumentReference("wiki", "XWiki", "user2")));
148  1 allowing(mockWikiDescriptorManager).getCurrentWikiId();
149  1 will(returnValue("wiki"));
150  1 allowing(mockCurrentSpaceReferenceProvider).get();
151  1 will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
152  1 allowing(mockCurrentDocumentReferenceProvider).get();
153  1 will(returnValue(new DocumentReference("wiki", "space", "document")));
154    }
155    });
156   
157  1 try {
158  1 contextCM.getInstance(Role.class);
159  0 Assert.fail("Should have raised an exception");
160    } catch (ComponentLookupException expected) {
161    // No need to assert the message, we just want to ensure an exception is raised.
162    }
163    }
164   
 
165  1 toggle @Test
166    public void testRegisterComponentInDocumentComponentManager() throws Exception
167    {
168  1 final States state = getMockery().states("test");
169   
170  1 getMockery().checking(new Expectations()
171    {
 
172  1 toggle {
173  1 allowing(mockWikiDescriptorManager).getCurrentWikiId();
174  1 when(state.isNot("otherdocument"));
175  1 will(returnValue("wiki1"));
176  1 allowing(mockCurrentSpaceReferenceProvider).get();
177  1 when(state.isNot("otherdocument"));
178  1 will(returnValue(new SpaceReference("space1", new WikiReference("wiki"))));
179  1 allowing(mockCurrentDocumentReferenceProvider).get();
180  1 when(state.isNot("otherdocument"));
181  1 will(returnValue(new DocumentReference("wiki1", "space1", "document1")));
182  1 allowing(mockDocumentAccessBridge).getCurrentUserReference();
183  1 when(state.isNot("otherdocument"));
184  1 will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
185    }
186    });
187   
188  1 ComponentManager documentCM = getComponentManager().getInstance(ComponentManager.class, "document");
189  1 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
190  1 cd.setRole(Role.class);
191  1 cd.setImplementation(RoleImpl.class);
192   
193    // Register component for the current user
194  1 documentCM.registerComponent(cd);
195   
196    // Verify we can lookup the component from the Context CM
197  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
198  1 Assert.assertNotNull(contextCM.getInstance(Role.class));
199   
200    // Now verify that we cannot look it up anymore if there's another user in the context
201  1 state.become("otherdocument");
202  1 getMockery().checking(new Expectations()
203    {
 
204  1 toggle {
205  1 exactly(1).of(mockDocumentAccessBridge).getCurrentUserReference();
206  1 will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
207  1 allowing(mockWikiDescriptorManager).getCurrentWikiId();
208  1 will(returnValue("wiki2"));
209  1 allowing(mockCurrentSpaceReferenceProvider).get();
210  1 will(returnValue(new SpaceReference("space2", new WikiReference("wiki2"))));
211  1 allowing(mockCurrentDocumentReferenceProvider).get();
212  1 will(returnValue(new DocumentReference("wiki2", "space2", "document2")));
213    }
214    });
215   
216  1 try {
217  1 contextCM.getInstance(Role.class);
218  0 Assert.fail("Should have raised an exception");
219    } catch (ComponentLookupException expected) {
220    // No need to assert the message, we just want to ensure an exception is raised.
221    }
222    }
223   
 
224  1 toggle @Test
225    public void testDeleteDocument() throws Exception
226    {
227  1 getMockery().checking(new Expectations()
228    {
 
229  1 toggle {
230  1 allowing(mockWikiDescriptorManager).getCurrentWikiId();
231  1 will(returnValue("wiki"));
232  1 allowing(mockCurrentSpaceReferenceProvider).get();
233  1 will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
234  1 allowing(mockCurrentDocumentReferenceProvider).get();
235  1 will(returnValue(new DocumentReference("wiki", "space", "document")));
236  1 allowing(mockDocumentAccessBridge).getCurrentUserReference();
237  1 will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
238    }
239    });
240   
241  1 ComponentManager documentCM = getComponentManager().getInstance(ComponentManager.class, "document");
242  1 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
243  1 cd.setRoleType(Role.class);
244  1 cd.setImplementation(RoleImpl.class);
245   
246    // Register component for the current user
247  1 documentCM.registerComponent(cd);
248   
249    // Verify we can lookup the component from the Context CM
250  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
251  1 Assert.assertNotNull(contextCM.getComponentDescriptor(Role.class, "default"));
252   
253  1 ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
254   
255  1 observationManager.notify(new DocumentDeletedEvent(new DocumentReference("wiki", "space", "document")), null,
256    null);
257   
258  1 Assert.assertNull(contextCM.getComponentDescriptor(Role.class, "default"));
259    }
260   
 
261  1 toggle @Test
262    public void testRegisterComponentInSpaceComponentManager() throws Exception
263    {
264  1 final States state = getMockery().states("test");
265   
266  1 getMockery().checking(new Expectations()
267    {
 
268  1 toggle {
269  1 allowing(mockWikiDescriptorManager).getCurrentWikiId();
270  1 when(state.isNot("otherspace"));
271  1 will(returnValue("wiki1"));
272  1 allowing(mockCurrentSpaceReferenceProvider).get();
273  1 when(state.isNot("otherspace"));
274  1 will(returnValue(new SpaceReference("space1", new WikiReference("wiki1"))));
275  1 allowing(mockCurrentDocumentReferenceProvider).get();
276  1 when(state.isNot("otherspace"));
277  1 will(returnValue(new DocumentReference("wiki1", "space1", "document1")));
278  1 allowing(mockDocumentAccessBridge).getCurrentUserReference();
279  1 when(state.isNot("otherspace"));
280  1 will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
281    }
282    });
283   
284  1 ComponentManager userCM = getComponentManager().getInstance(ComponentManager.class, "space");
285  1 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
286  1 cd.setRole(Role.class);
287  1 cd.setImplementation(RoleImpl.class);
288   
289    // Register component for the current user
290  1 userCM.registerComponent(cd);
291   
292    // Verify we can lookup the component from the Context CM
293  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
294  1 Assert.assertNotNull(contextCM.getInstance(Role.class));
295   
296    // Now verify that we cannot look it up anymore if there's another user in the context
297  1 state.become("otherspace");
298  1 getMockery().checking(new Expectations()
299    {
 
300  1 toggle {
301  1 exactly(1).of(mockDocumentAccessBridge).getCurrentUserReference();
302  1 will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
303  1 allowing(mockWikiDescriptorManager).getCurrentWikiId();
304  1 will(returnValue("wiki2"));
305  1 allowing(mockCurrentSpaceReferenceProvider).get();
306  1 will(returnValue(new SpaceReference("space2", new WikiReference("wiki2"))));
307  1 allowing(mockCurrentDocumentReferenceProvider).get();
308  1 will(returnValue(new DocumentReference("wiki2", "space2", "document2")));
309    }
310    });
311   
312  1 try {
313  1 contextCM.getInstance(Role.class);
314  0 Assert.fail("Should have raised an exception");
315    } catch (ComponentLookupException expected) {
316    // No need to assert the message, we just want to ensure an exception is raised.
317    }
318    }
319   
 
320  1 toggle @Test
321    public void testRegisterComponentInWikiComponentManager() throws Exception
322    {
323  1 final States state = getMockery().states("test");
324   
325  1 getMockery().checking(new Expectations()
326    {
 
327  1 toggle {
328  1 allowing(mockWikiDescriptorManager).getCurrentWikiId();
329  1 when(state.isNot("otherwiki"));
330  1 will(returnValue("wiki1"));
331  1 allowing(mockCurrentSpaceReferenceProvider).get();
332  1 when(state.isNot("otherwiki"));
333  1 will(returnValue(new SpaceReference("space1", new WikiReference("wiki1"))));
334  1 allowing(mockCurrentDocumentReferenceProvider).get();
335  1 when(state.isNot("otherwiki"));
336  1 will(returnValue(new DocumentReference("wiki1", "space1", "document1")));
337  1 allowing(mockDocumentAccessBridge).getCurrentUserReference();
338  1 when(state.isNot("otherwiki"));
339  1 will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
340    }
341    });
342   
343    // Register in the current wiki.
344  1 ComponentManager wikiCM = getComponentManager().getInstance(ComponentManager.class, "wiki");
345  1 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
346  1 cd.setRole(Role.class);
347  1 cd.setImplementation(RoleImpl.class);
348  1 wikiCM.registerComponent(cd);
349   
350    // Verify we can lookup the component from the context CM.
351  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
352  1 Assert.assertNotNull(contextCM.getComponentDescriptor(Role.class, "default"));
353   
354    // Now verify that we cannot look it up anymore if there's another wiki in the context
355  1 state.become("otherwiki");
356   
357  1 getMockery().checking(new Expectations()
358    {
 
359  1 toggle {
360  1 exactly(1).of(mockDocumentAccessBridge).getCurrentUserReference();
361  1 will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
362  1 allowing(mockWikiDescriptorManager).getCurrentWikiId();
363  1 will(returnValue("wiki2"));
364  1 allowing(mockCurrentSpaceReferenceProvider).get();
365  1 will(returnValue(new SpaceReference("space2", new WikiReference("wiki2"))));
366  1 allowing(mockCurrentDocumentReferenceProvider).get();
367  1 will(returnValue(new DocumentReference("wiki2", "space2", "document2")));
368    }
369    });
370   
371  1 try {
372  1 contextCM.getInstance(Role.class);
373  0 Assert.fail("Should have raised an exception");
374    } catch (ComponentLookupException expected) {
375    // No need to assert the message, we just want to ensure an exception is raised.
376    }
377    }
378   
 
379  1 toggle @Test
380    public void testDeleteWiki() throws ComponentLookupException, Exception
381    {
382  1 getMockery().checking(new Expectations()
383    {
 
384  1 toggle {
385  1 allowing(mockWikiDescriptorManager).getCurrentWikiId();
386  1 will(returnValue("wiki"));
387  1 allowing(mockCurrentSpaceReferenceProvider).get();
388  1 will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
389  1 allowing(mockCurrentDocumentReferenceProvider).get();
390  1 will(returnValue(new DocumentReference("wiki", "space", "document")));
391  1 allowing(mockDocumentAccessBridge).getCurrentUserReference();
392  1 will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
393    }
394    });
395   
396    // Register in the current wiki.
397  1 ComponentManager wikiCM = getComponentManager().getInstance(ComponentManager.class, "wiki");
398  1 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
399  1 cd.setRoleType(Role.class);
400  1 cd.setImplementation(RoleImpl.class);
401  1 wikiCM.registerComponent(cd);
402   
403  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
404  1 Assert.assertNotNull(contextCM.getComponentDescriptor(Role.class, "default"));
405   
406  1 ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
407   
408  1 observationManager.notify(new WikiDeletedEvent("wiki"), null, null);
409   
410  1 Assert.assertNull(contextCM.getComponentDescriptor(Role.class, "default"));
411    }
412   
 
413  1 toggle @Test
414    public void testRegisterComponentInRootComponentManager() throws Exception
415    {
416  1 final States state = getMockery().states("test");
417   
418  1 getMockery().checking(new Expectations()
419    {
 
420  1 toggle {
421  1 allowing(mockWikiDescriptorManager).getCurrentWikiId();
422  1 when(state.isNot("otherwiki"));
423  1 will(returnValue("wiki"));
424  1 allowing(mockCurrentSpaceReferenceProvider).get();
425  1 when(state.isNot("otherwiki"));
426  1 will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
427  1 allowing(mockCurrentDocumentReferenceProvider).get();
428  1 when(state.isNot("otherwiki"));
429  1 will(returnValue(new DocumentReference("wiki", "space", "document")));
430  1 allowing(mockDocumentAccessBridge).getCurrentUserReference();
431  1 when(state.isNot("otherwiki"));
432  1 will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
433    }
434    });
435   
436    // Register in the current wiki.
437  1 DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
438  1 cd.setRole(Role.class);
439  1 cd.setImplementation(RoleImpl.class);
440  1 getComponentManager().registerComponent(cd);
441   
442    // Verify we can lookup the component from the context CM.
443  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
444  1 Assert.assertNotNull(contextCM.getInstance(Role.class));
445    }
446   
 
447  1 toggle @Test
448    public void testCreateDocumentComponentManager() throws Exception
449    {
450  1 ComponentManagerManager manager = getComponentManager().getInstance(ComponentManagerManager.class);
451   
452  1 NamespacedComponentManager componentManager =
453    (NamespacedComponentManager) manager.getComponentManager("document:wiki1:space1.space2.document1", true);
454   
455  1 assertNotNull(componentManager);
456  1 assertEquals("document:wiki1:space1.space2.document1", componentManager.getNamespace());
457  1 assertEquals("space:wiki1:space1.space2", ((NamespacedComponentManager)componentManager.getParent()).getNamespace());
458  1 assertEquals("space:wiki1:space1", ((NamespacedComponentManager)componentManager.getParent().getParent()).getNamespace());
459  1 assertEquals("wiki:wiki1", ((NamespacedComponentManager)componentManager.getParent().getParent().getParent()).getNamespace());
460    }
461   
462    // Failures
463   
 
464  1 toggle @Test
465    public void testRegisterComponentInContextComponentManagerThrowsException() throws Exception
466    {
467  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
468  1 try {
469  1 contextCM.registerComponent(new DefaultComponentDescriptor<Role>());
470  0 Assert.fail("Should have thrown an exception error");
471    } catch (RuntimeException expected) {
472  1 Assert.assertEquals("The Context Component Manager should only be used for read access. Write operations "
473    + "should be done against specific Component Managers.", expected.getMessage());
474    }
475    }
476   
 
477  1 toggle @Test(expected = RuntimeException.class)
478    public void testRegisterComponentInstance() throws ComponentLookupException, Exception
479    {
480  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
481   
482  1 contextCM.registerComponent(null, null);
483    }
484   
 
485  1 toggle @Test(expected = RuntimeException.class)
486    public void testRegisterComponentDesciptor() throws ComponentLookupException, Exception
487    {
488  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
489   
490  1 contextCM.registerComponent(null);
491    }
492   
 
493  1 toggle @Test(expected = RuntimeException.class)
494    public void testSetComponentEventManager() throws ComponentLookupException, Exception
495    {
496  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
497   
498  1 contextCM.setComponentEventManager(null);
499    }
500   
 
501  1 toggle @Test(expected = RuntimeException.class)
502    public void testSetParent() throws ComponentLookupException, Exception
503    {
504  1 ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
505   
506  1 contextCM.setParent(null);
507    }
508    }