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

File ComponentScriptServiceTest.java

 

Code metrics

0
57
15
2
244
158
15
0.26
3.8
7.5
1

Classes

Class Line # Actions
ComponentScriptServiceTest 55 57 0% 15 0
1.0100%
ComponentScriptServiceTest.SomeRole 60 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 14 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.script;
21   
22    import javax.inject.Provider;
23   
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.bridge.DocumentAccessBridge;
28    import org.xwiki.component.internal.ContextComponentManagerProvider;
29    import org.xwiki.component.internal.multi.ComponentManagerManager;
30    import org.xwiki.component.manager.ComponentLookupException;
31    import org.xwiki.component.manager.ComponentManager;
32    import org.xwiki.component.util.DefaultParameterizedType;
33    import org.xwiki.context.Execution;
34    import org.xwiki.context.ExecutionContext;
35    import org.xwiki.test.annotation.ComponentList;
36    import org.xwiki.test.mockito.MockitoComponentMockingRule;
37   
38    import static org.junit.Assert.assertEquals;
39    import static org.junit.Assert.assertNull;
40    import static org.junit.Assert.assertSame;
41    import static org.mockito.ArgumentMatchers.any;
42    import static org.mockito.ArgumentMatchers.anyBoolean;
43    import static org.mockito.Mockito.mock;
44    import static org.mockito.Mockito.never;
45    import static org.mockito.Mockito.verify;
46    import static org.mockito.Mockito.when;
47   
48    /**
49    * Unit tests for {@link org.xwiki.component.script.ComponentScriptService}.
50    *
51    * @version $Id: 63fd76a29a90327eff5e5c9001a3c2bd9284c01e $
52    * @since 4.1M2
53    */
54    @ComponentList(ContextComponentManagerProvider.class)
 
55    public class ComponentScriptServiceTest
56    {
57    /**
58    * Used to test component lookup.
59    */
 
60    private interface SomeRole
61    {
62    }
63   
64    @Rule
65    public MockitoComponentMockingRule<ComponentScriptService> mocker = new MockitoComponentMockingRule<>(
66    ComponentScriptService.class);
67   
68    /**
69    * Used to check programming rights.
70    */
71    private DocumentAccessBridge dab;
72   
73    /**
74    * The mock component manager used by the script service under test.
75    */
76    private ComponentManager contextComponentManager;
77   
78    private ComponentManager contextrootComponentManager;
79   
80    private ComponentManager rootComponentManager;
81   
82    private ComponentManagerManager componentManagerManager;
83   
84    private Execution execution;
85   
 
86  14 toggle @Before
87    public void configure() throws Exception
88    {
89  14 this.dab = this.mocker.getInstance(DocumentAccessBridge.class);
90   
91  14 this.contextComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "context");
92  14 Provider<ComponentManager> contextComponentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
93  14 when(contextComponentManagerProvider.get()).thenReturn(this.contextComponentManager);
94   
95  14 this.contextrootComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "context/root");
96  14 Provider<ComponentManager> contextrootComponentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context/root");
97  14 when(contextrootComponentManagerProvider.get()).thenReturn(this.contextrootComponentManager);
98   
99  14 this.rootComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "root");
100   
101  14 this.componentManagerManager = this.mocker.getInstance(ComponentManagerManager.class);
102   
103  14 when(this.componentManagerManager.getComponentManager(null, false)).thenReturn(this.rootComponentManager);
104   
105  14 this.execution = this.mocker.getInstance(Execution.class);
106    }
107   
 
108  1 toggle @Test
109    public void getComponentManagerWhenNoProgrammingRights() throws Exception
110    {
111  1 when(this.dab.hasProgrammingRights()).thenReturn(false);
112   
113  1 assertNull(this.mocker.getComponentUnderTest().getComponentManager());
114    }
115   
 
116  1 toggle @Test
117    public void getRootComponentManagerWhenNoProgrammingRights() throws Exception
118    {
119  1 when(this.dab.hasProgrammingRights()).thenReturn(false);
120   
121  1 assertNull(this.mocker.getComponentUnderTest().getRootComponentManager());
122    }
123   
 
124  1 toggle @Test
125    public void getContextComponentManagerWhenNoProgrammingRights() throws Exception
126    {
127  1 when(this.dab.hasProgrammingRights()).thenReturn(false);
128   
129  1 assertNull(this.mocker.getComponentUnderTest().getContextComponentManager());
130    }
131   
 
132  1 toggle @Test
133    public void getComponentManagerWhenProgrammingRights() throws Exception
134    {
135  1 when(this.dab.hasProgrammingRights()).thenReturn(true);
136   
137  1 assertSame(this.contextrootComponentManager, this.mocker.getComponentUnderTest().getComponentManager());
138    }
139   
 
140  1 toggle @Test
141    public void getRootComponentManagerWhenProgrammingRights() throws Exception
142    {
143  1 when(this.dab.hasProgrammingRights()).thenReturn(true);
144   
145  1 assertSame(this.rootComponentManager, this.mocker.getComponentUnderTest().getRootComponentManager());
146    }
147   
 
148  1 toggle @Test
149    public void getContextComponentManagerWhenProgrammingRights() throws Exception
150    {
151  1 when(this.dab.hasProgrammingRights()).thenReturn(true);
152   
153  1 assertSame(this.contextComponentManager, this.mocker.getComponentUnderTest().getContextComponentManager());
154    }
155   
 
156  1 toggle @Test
157    public void getComponentManagerForNamespaceWhenNoProgrammingRights() throws Exception
158    {
159  1 when(this.dab.hasProgrammingRights()).thenReturn(false);
160   
161  1 assertNull(this.mocker.getComponentUnderTest().getComponentManager("wiki:xwiki"));
162   
163  1 ComponentManagerManager componentManagerManager = this.mocker.getInstance(ComponentManagerManager.class);
164  1 verify(componentManagerManager, never()).getComponentManager(any(), anyBoolean());
165    }
166   
 
167  1 toggle @Test
168    public void getComponentManagerForNamespaceWhenProgrammingRights() throws Exception
169    {
170  1 when(this.dab.hasProgrammingRights()).thenReturn(true);
171   
172  1 ComponentManager wikiComponentManager = mock(ComponentManager.class);
173  1 when(this.componentManagerManager.getComponentManager("wiki:chess", false)).thenReturn(wikiComponentManager);
174   
175  1 assertSame(wikiComponentManager, this.mocker.getComponentUnderTest().getComponentManager("wiki:chess"));
176    }
177   
 
178  1 toggle @Test
179    public void getComponentInstanceWithNoHintWhenNoProgrammingRights() throws Exception
180    {
181  1 when(this.dab.hasProgrammingRights()).thenReturn(false);
182   
183  1 assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class));
184   
185  1 verify(this.contextComponentManager, never()).getInstance(SomeRole.class);
186    }
187   
 
188  1 toggle @Test
189    public void getComponentInstanceWithNoHintWhenProgrammingRights() throws Exception
190    {
191  1 when(this.dab.hasProgrammingRights()).thenReturn(true);
192   
193  1 SomeRole instance = mock(SomeRole.class);
194  1 when(this.contextComponentManager.getInstance(SomeRole.class)).thenReturn(instance);
195   
196  1 assertSame(instance, this.mocker.getComponentUnderTest().getInstance(SomeRole.class));
197    }
198   
 
199  1 toggle @Test
200    public void getComponentInstanceWithHintWhenNoProgrammingRights() throws Exception
201    {
202  1 when(this.dab.hasProgrammingRights()).thenReturn(false);
203   
204  1 assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class, "hint"));
205   
206  1 verify(this.contextComponentManager, never()).getInstance(SomeRole.class, "hint");
207    }
208   
 
209  1 toggle @Test
210    public void getComponentInstanceWithHintWhenProgrammingRights() throws Exception
211    {
212  1 when(this.dab.hasProgrammingRights()).thenReturn(true);
213   
214  1 SomeRole instance = mock(SomeRole.class);
215  1 when(this.contextComponentManager.getInstance(SomeRole.class, "hint")).thenReturn(instance);
216   
217  1 assertSame(instance, this.mocker.getComponentUnderTest().getInstance(SomeRole.class, "hint"));
218    }
219   
 
220  1 toggle @Test
221    public void getComponentInstanceWhenComponentDoesntExist() throws Exception
222    {
223  1 when(this.dab.hasProgrammingRights()).thenReturn(true);
224  1 when(this.contextComponentManager.getInstance(SomeRole.class)).thenThrow(new ComponentLookupException("error"));
225  1 ExecutionContext context = new ExecutionContext();
226  1 when(this.execution.getContext()).thenReturn(context);
227   
228  1 assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class));
229  1 assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage());
230    }
231   
 
232  1 toggle @Test
233    public void getComponentInstanceWithHintWhenComponentDoesntExist() throws Exception
234    {
235  1 when(this.dab.hasProgrammingRights()).thenReturn(true);
236  1 when(this.contextComponentManager.getInstance(SomeRole.class, "hint")).thenThrow(
237    new ComponentLookupException("error"));
238  1 ExecutionContext context = new ExecutionContext();
239  1 when(this.execution.getContext()).thenReturn(context);
240   
241  1 assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class, "hint"));
242  1 assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage());
243    }
244    }