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

File ExtensionManagerScriptServiceTest.java

 

Code metrics

12
53
14
1
250
163
20
0.38
3.79
14
1.43

Classes

Class Line # Actions
ExtensionManagerScriptServiceTest 58 53 0% 20 8
0.8987341589.9%
 

Contributing tests

This file is covered by 10 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.extension.script;
21   
22    import java.util.HashMap;
23    import java.util.List;
24    import java.util.Map;
25   
26    import org.junit.Assert;
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.junit.Test;
30    import org.xwiki.extension.InstallException;
31    import org.xwiki.extension.UninstallException;
32    import org.xwiki.extension.job.InstallRequest;
33    import org.xwiki.extension.repository.InstalledExtensionRepository;
34    import org.xwiki.extension.repository.internal.core.CoreExtensionScanner;
35    import org.xwiki.extension.test.MockitoRepositoryUtilsRule;
36    import org.xwiki.job.Job;
37    import org.xwiki.logging.LogLevel;
38    import org.xwiki.logging.event.LogEvent;
39    import org.xwiki.model.reference.DocumentReference;
40    import org.xwiki.script.service.ScriptService;
41    import org.xwiki.security.authorization.AccessDeniedException;
42    import org.xwiki.security.authorization.Right;
43    import org.xwiki.test.annotation.AfterComponent;
44    import org.xwiki.test.annotation.AllComponents;
45    import org.xwiki.test.mockito.MockitoComponentManagerRule;
46   
47    import com.xpn.xwiki.XWiki;
48    import com.xpn.xwiki.objects.classes.BaseClass;
49    import com.xpn.xwiki.test.MockitoOldcoreRule;
50    import com.xpn.xwiki.util.XWikiStubContextProvider;
51   
52    import static org.junit.Assert.assertEquals;
53    import static org.junit.Assert.assertNotNull;
54    import static org.mockito.Mockito.doThrow;
55    import static org.mockito.Mockito.mock;
56   
57    @AllComponents
 
58    public class ExtensionManagerScriptServiceTest
59    {
60    private MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
61   
62    private MockitoOldcoreRule xwikiBridge = new MockitoOldcoreRule(this.mocker);
63   
64    @Rule
65    public MockitoRepositoryUtilsRule repositoryUtil = new MockitoRepositoryUtilsRule(this.mocker, this.xwikiBridge);
66   
67    private XWiki mockXWiki;
68   
69    private Map<String, BaseClass> classes = new HashMap<String, BaseClass>();
70   
71    private DocumentReference contextUser;
72   
73    private ExtensionManagerScriptService scriptService;
74   
 
75  10 toggle @AfterComponent
76    public void afterComponent() throws Exception
77    {
78    // Skip core extension scanner
79  10 this.mocker.registerMockComponent(CoreExtensionScanner.class);
80    }
81   
 
82  10 toggle @Before
83    public void before() throws Exception
84    {
85    // mock
86   
87  10 this.mockXWiki = mock(XWiki.class);
88   
89  10 this.xwikiBridge.getXWikiContext().setWiki(this.mockXWiki);
90  10 this.xwikiBridge.getXWikiContext().setWikiId("xwiki");
91  10 this.contextUser =
92    new DocumentReference(this.xwikiBridge.getXWikiContext().getWikiId(), "XWiki", "ExtensionUser");
93   
94    // classes
95   
96  10 BaseClass styleSheetClass = new BaseClass();
97  10 this.classes.put("StyleSheetExtension", styleSheetClass);
98   
99    // checking
100   
101  10 this.xwikiBridge.getXWikiContext().setUserReference(this.contextUser);
102   
103  10 ((XWikiStubContextProvider) this.mocker.getInstance(XWikiStubContextProvider.class))
104    .initialize(this.xwikiBridge.getXWikiContext());
105   
106    // lookup
107   
108  10 this.scriptService = this.mocker.getInstance(ScriptService.class, "extension");
109    }
110   
111    // tools
112   
 
113  4 toggle private Job install(String id, String version, String namespace) throws Throwable
114    {
115  4 Job job = this.scriptService.install(id, version, namespace);
116  4 if (job == null) {
117  0 throw this.scriptService.getLastError();
118    }
119   
120  4 job.join();
121   
122  4 List<LogEvent> errors = job.getStatus().getLog().getLogsFrom(LogLevel.WARN);
123  4 if (!errors.isEmpty()) {
124  2 throw errors.get(0).getThrowable();
125    }
126   
127  2 return job;
128    }
129   
 
130  4 toggle private Job uninstall(String id, String namespace) throws Throwable
131    {
132  4 Job job = this.scriptService.uninstall(id, namespace);
133  4 if (job == null) {
134  0 throw this.scriptService.getLastError();
135    }
136   
137  4 job.join();
138   
139  4 List<LogEvent> errors = job.getStatus().getLog().getLogsFrom(LogLevel.WARN);
140  4 if (!errors.isEmpty()) {
141  2 throw errors.get(0).getThrowable();
142    }
143   
144  2 return job;
145    }
146   
147    // Tests
148   
149    // install
150   
 
151  1 toggle @Test
152    public void testInstallOnRoot() throws Throwable
153    {
154  1 install("extension", "version", null);
155    }
156   
 
157  1 toggle @Test
158    public void testInstallOnNamespace() throws Throwable
159    {
160  1 install("extension", "version", "namespace");
161    }
162   
 
163  1 toggle @Test
164    public void testOverwriteAllowedNamespaces() throws Throwable
165    {
166  1 InstallRequest installRequest = this.scriptService.createInstallRequest("extension", "version", "namespace");
167   
168    // Indicate all extensions of type "test" should be installed on root
169  1 ((ScriptExtensionRewriter) installRequest.getRewriter()).installExtensionTypeOnRootNamespace("test");
170   
171    // Allow redirect on root
172  1 installRequest.setRootModificationsAllowed(true);
173   
174  1 Job job = this.scriptService.install(installRequest);
175  1 if (job == null) {
176  0 throw this.scriptService.getLastError();
177    }
178   
179  1 job.join();
180   
181  1 List<LogEvent> errors = job.getStatus().getLog().getLogsFrom(LogLevel.WARN);
182  1 if (!errors.isEmpty()) {
183  0 throw errors.get(0).getThrowable();
184    }
185   
186    // Validate
187   
188  1 InstalledExtensionRepository repository = mocker.getInstance(InstalledExtensionRepository.class);
189   
190  1 assertNotNull(repository.getInstalledExtension("extension", null));
191    }
192   
 
193  1 toggle @Test(expected = InstallException.class)
194    public void testInstallOnRootWithoutProgrammingRigths() throws Throwable
195    {
196  1 doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager())
197    .checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null);
198   
199  1 install("extension", "version", null);
200    }
201   
 
202  1 toggle @Test(expected = InstallException.class)
203    public void testInstallOnNamespaceWithoutProgrammingRigths() throws Throwable
204    {
205  1 doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager())
206    .checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null);
207   
208  1 install("extension", "version", "namespace");
209    }
210   
211    // uninstall
212   
 
213  1 toggle @Test
214    public void testUninstallFromRoot() throws Throwable
215    {
216  1 uninstall("installedonroot", null);
217    }
218   
 
219  1 toggle @Test
220    public void testUninstallOnNamespace() throws Throwable
221    {
222  1 uninstall("installedonnamespace", "namespace");
223    }
224   
 
225  1 toggle @Test(expected = UninstallException.class)
226    public void testUninstallOnRootWithoutProgrammingRigths() throws Throwable
227    {
228  1 doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager())
229    .checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null);
230   
231  1 uninstall("installedonroot", null);
232    }
233   
 
234  1 toggle @Test(expected = UninstallException.class)
235    public void testUninstallOnNamespaceWithoutProgrammingRigths() throws Throwable
236    {
237  1 doThrow(AccessDeniedException.class).when(this.xwikiBridge.getMockAuthorizationManager())
238    .checkAccess(Right.PROGRAM, new DocumentReference("xwiki", "XWiki", "ExtensionUser"), null);
239   
240  1 uninstall("installedonnamespace", "namespace");
241    }
242   
 
243  1 toggle @Test
244    public void testGet()
245    {
246  1 Assert.assertNotNull(this.scriptService.get(CoreExtensionScriptService.ID));
247  1 Assert.assertNotNull(this.scriptService.get(LocalExtensionScriptService.ID));
248  1 Assert.assertNotNull(this.scriptService.get(InstalledExtensionScriptService.ID));
249    }
250    }