1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.component.wiki

File DefaultWikiComponentManagerTest.java

 

Code metrics

0
13
1
1
87
36
1
0.08
13
1
1

Classes

Class Line # Actions
DefaultWikiComponentManagerTest 39 13 0% 1 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.wiki;
21   
22    import org.junit.Rule;
23    import org.junit.Test;
24    import org.xwiki.component.descriptor.ComponentDescriptor;
25    import org.xwiki.component.manager.ComponentManager;
26    import org.xwiki.component.wiki.internal.DefaultWikiComponentManager;
27    import org.xwiki.component.wiki.internal.WikiComponentManagerContext;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.test.mockito.MockitoComponentMockingRule;
30   
31    import static org.mockito.ArgumentMatchers.*;
32    import static org.mockito.Mockito.*;
33   
34    /**
35    * Unit tests for {@link DefaultWikiComponentManager}.
36    *
37    * @version $Id: 395a56d27d86bbce315b289af5fdb38656f6b653 $
38    */
 
39    public class DefaultWikiComponentManagerTest
40    {
41    private static final DocumentReference DOC_REFERENCE = new DocumentReference("xwiki", "XWiki", "MyComponent");
42   
43    private static final DocumentReference AUTHOR_REFERENCE = new DocumentReference("xwiki", "XWiki", "Admin");
44   
45    @Rule
46    public MockitoComponentMockingRule<DefaultWikiComponentManager> mocker = new MockitoComponentMockingRule<>(
47    DefaultWikiComponentManager.class);
48   
 
49  1 toggle @Test
50    public void registerAndUnregisterWikiComponent() throws Exception
51    {
52  1 WikiComponentManagerContext wcmc = this.mocker.getInstance(WikiComponentManagerContext.class);
53  1 when(wcmc.getCurrentEntityReference()).thenReturn(DOC_REFERENCE);
54  1 when(wcmc.getCurrentUserReference()).thenReturn(AUTHOR_REFERENCE);
55   
56  1 WikiComponent component = new TestImplementation(DOC_REFERENCE, AUTHOR_REFERENCE, WikiComponentScope.WIKI);
57   
58  1 ComponentManager wikiComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "wiki");
59   
60    // Register the wiki component
61  1 this.mocker.getComponentUnderTest().registerWikiComponent(component);
62   
63    // Test 1: we verify that the component has been registered against the CM
64  1 verify(wikiComponentManager, times(1)).registerComponent(any(ComponentDescriptor.class), eq(component));
65   
66    // Try to register the wiki component again
67  1 this.mocker.getComponentUnderTest().registerWikiComponent(component);
68   
69    // Test 2: we verify that the component has been registered again against the CM
70  1 verify(wikiComponentManager, times(2)).registerComponent(any(ComponentDescriptor.class), eq(component));
71   
72    // Unregister the wiki component
73  1 this.mocker.getComponentUnderTest().unregisterWikiComponents(DOC_REFERENCE);
74   
75    // Test 3: we verify that the component has been unregistered from the CM
76    // Note that indirectly this tests that the wiki component has been added to the wiki component cache during
77    // the call to registerWikiComponent()
78  1 verify(wikiComponentManager, times(1)).unregisterComponent(TestRole.class, "roleHint");
79   
80    // Try to unregister the wiki component again
81  1 this.mocker.getComponentUnderTest().unregisterWikiComponents(DOC_REFERENCE);
82   
83    // Test 4: We verify that nothing happens on the CM since the wiki component is not in the wiki cache.
84    // Note: the times(1) comes from the previous call in test 2 above.
85  1 verify(wikiComponentManager, times(1)).unregisterComponent(TestRole.class, "roleHint");
86    }
87    }