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

File AbstractExtensionHandlerTest.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

18
67
36
1
313
237
45
0.67
1.86
36
1.25

Classes

Class Line # Actions
AbstractExtensionHandlerTest 49 67 0% 45 15
0.8760330787.6%
 

Contributing tests

This file is covered by 77 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.test;
21   
22    import java.util.Collection;
23    import java.util.List;
24   
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.xwiki.cache.CacheManager;
28    import org.xwiki.extension.ExtensionId;
29    import org.xwiki.extension.InstalledExtension;
30    import org.xwiki.extension.LocalExtension;
31    import org.xwiki.extension.job.InstallRequest;
32    import org.xwiki.extension.job.UninstallRequest;
33    import org.xwiki.extension.job.plan.ExtensionPlan;
34    import org.xwiki.extension.job.plan.ExtensionPlanNode;
35    import org.xwiki.extension.job.plan.internal.DefaultExtensionPlan;
36    import org.xwiki.extension.repository.InstalledExtensionRepository;
37    import org.xwiki.extension.repository.LocalExtensionRepository;
38    import org.xwiki.extension.repository.internal.core.CoreExtensionScanner;
39    import org.xwiki.job.Job;
40    import org.xwiki.job.JobExecutor;
41    import org.xwiki.job.Request;
42    import org.xwiki.logging.LogLevel;
43    import org.xwiki.logging.event.LogEvent;
44    import org.xwiki.test.annotation.AfterComponent;
45    import org.xwiki.test.annotation.AllComponents;
46    import org.xwiki.test.mockito.MockitoComponentManagerRule;
47   
48    @AllComponents
 
49    public abstract class AbstractExtensionHandlerTest
50    {
51    protected MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
52   
53    @Rule
54    public MockitoRepositoryUtilsRule repositoryUtil = new MockitoRepositoryUtilsRule(this.mocker);
55   
56    protected LocalExtensionRepository localExtensionRepository;
57   
58    protected InstalledExtensionRepository installedExtensionRepository;
59   
60    protected JobExecutor jobExecutor;
61   
 
62  77 toggle @AfterComponent
63    public void afterComponent() throws Exception
64    {
65    // Skip core extension scanner
66  77 this.mocker.registerMockComponent(CoreExtensionScanner.class);
67    }
68   
 
69  77 toggle @Before
70    public void setUp() throws Exception
71    {
72  77 this.jobExecutor = this.mocker.getInstance(JobExecutor.class);
73  77 this.localExtensionRepository = this.mocker.getInstance(LocalExtensionRepository.class);
74  77 this.installedExtensionRepository = this.mocker.getInstance(InstalledExtensionRepository.class);
75   
76  77 this.mocker.registerMockComponent(CacheManager.class);
77    }
78   
 
79  4 toggle protected ExtensionPlanNode getNode(ExtensionId id, Collection<ExtensionPlanNode> nodes)
80    {
81  4 for (ExtensionPlanNode node : nodes) {
82  6 if (node.getAction().getExtension().getId().equals(id)) {
83  4 return node;
84    }
85    }
86   
87  0 return null;
88    }
89   
 
90  158 toggle protected Job executeJob(String jobId, Request request, LogLevel failFrom) throws Throwable
91    {
92  158 Job installJob = this.jobExecutor.execute(jobId, request);
93   
94  158 installJob.join();
95   
96  158 List<LogEvent> errors = installJob.getStatus().getLog().getLogsFrom(failFrom);
97  158 if (!errors.isEmpty()) {
98  13 throw errors.get(0).getThrowable() != null ? errors.get(0).getThrowable()
99    : new Exception(errors.get(0).getFormattedMessage());
100    }
101   
102  145 return installJob;
103    }
104   
 
105  41 toggle protected InstalledExtension install(ExtensionId extensionId) throws Throwable
106    {
107  41 return install(extensionId, true);
108    }
109   
 
110  41 toggle protected InstalledExtension install(ExtensionId extensionId, boolean rootModifications) throws Throwable
111    {
112  41 return install(extensionId, (String[]) null, rootModifications);
113    }
114   
 
115  1 toggle protected InstalledExtension install(ExtensionId extensionId, String[] namespaces) throws Throwable
116    {
117  1 return install(extensionId, namespaces, true);
118    }
119   
 
120  42 toggle protected InstalledExtension install(ExtensionId extensionId, String[] namespaces, boolean rootModifications)
121    throws Throwable
122    {
123  42 return install(extensionId, namespaces, rootModifications, LogLevel.WARN);
124    }
125   
 
126  38 toggle protected InstalledExtension install(ExtensionId extensionId, String namespace) throws Throwable
127    {
128  38 return install(extensionId, namespace, true);
129    }
130   
 
131  39 toggle protected InstalledExtension install(ExtensionId extensionId, String namespace, boolean rootModifications)
132    throws Throwable
133    {
134  39 return install(extensionId, namespace, rootModifications, LogLevel.WARN);
135    }
136   
 
137  4 toggle protected InstalledExtension install(ExtensionId extensionId, String namespace, LogLevel failFrom) throws Throwable
138    {
139  4 return install(extensionId, namespace, true, failFrom);
140    }
141   
 
142  43 toggle protected InstalledExtension install(ExtensionId extensionId, String namespace, boolean rootModifications,
143    LogLevel failFrom) throws Throwable
144    {
145  43 return install(extensionId, namespace != null ? new String[] { namespace } : (String[]) null, rootModifications,
146    failFrom);
147    }
148   
 
149  0 toggle protected InstalledExtension install(ExtensionId extensionId, String[] namespaces, LogLevel failFrom)
150    throws Throwable
151    {
152  0 return install(extensionId, namespaces, true, failFrom);
153    }
154   
 
155  85 toggle protected InstalledExtension install(ExtensionId extensionId, String[] namespaces, boolean rootModifications,
156    LogLevel failFrom) throws Throwable
157    {
158  85 install("install", extensionId, namespaces, rootModifications, failFrom);
159   
160  82 return this.installedExtensionRepository.resolve(extensionId);
161    }
162   
 
163  19 toggle protected ExtensionPlan installPlan(ExtensionId extensionId) throws Throwable
164    {
165  19 return installPlan(extensionId, true);
166    }
167   
 
168  20 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, boolean rootModifications) throws Throwable
169    {
170  20 return installPlan(extensionId, (String[]) null, rootModifications);
171    }
172   
 
173  0 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, String[] namespaces) throws Throwable
174    {
175  0 return installPlan(extensionId, namespaces, true);
176    }
177   
 
178  20 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, String[] namespaces, boolean rootModifications)
179    throws Throwable
180    {
181  20 return installPlan(extensionId, namespaces, rootModifications, LogLevel.WARN);
182    }
183   
 
184  6 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, String namespace) throws Throwable
185    {
186  6 return installPlan(extensionId, namespace, true);
187    }
188   
 
189  8 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, String namespace, boolean rootModifications)
190    throws Throwable
191    {
192  8 return installPlan(extensionId, namespace != null ? new String[] { namespace } : null, rootModifications,
193    LogLevel.WARN);
194    }
195   
 
196  0 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, String[] namespaces, LogLevel failFrom)
197    throws Throwable
198    {
199  0 return installPlan(extensionId, namespaces, true, failFrom);
200    }
201   
 
202  28 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, String[] namespaces, boolean rootModifications,
203    LogLevel failFrom) throws Throwable
204    {
205  28 Job installJob = install("installplan", extensionId, namespaces, rootModifications, failFrom);
206   
207  19 return (ExtensionPlan) installJob.getStatus();
208    }
209   
 
210  3 toggle protected Job install(String jobId, ExtensionId extensionId, String[] namespaces, LogLevel failFrom)
211    throws Throwable
212    {
213  3 return install(jobId, extensionId, namespaces, true, failFrom);
214    }
215   
 
216  116 toggle protected Job install(String jobId, ExtensionId extensionId, String[] namespaces, boolean rootModifications,
217    LogLevel failFrom) throws Throwable
218    {
219  116 InstallRequest installRequest = createInstallRequest(extensionId, namespaces, rootModifications);
220   
221  116 return executeJob(jobId, installRequest, failFrom);
222    }
223   
 
224  116 toggle protected InstallRequest createInstallRequest(ExtensionId extensionId, String[] namespaces,
225    boolean rootModifications)
226    {
227  116 InstallRequest installRequest = new InstallRequest();
228  116 installRequest.addExtension(extensionId);
229  116 if (namespaces != null) {
230  53 for (String namespace : namespaces) {
231  54 installRequest.addNamespace(namespace);
232    }
233    }
234  116 installRequest.setRootModificationsAllowed(rootModifications);
235   
236  116 return installRequest;
237    }
238   
 
239  31 toggle protected LocalExtension uninstall(ExtensionId extensionId, String namespace) throws Throwable
240    {
241  31 return uninstall(extensionId, namespace, LogLevel.WARN);
242    }
243   
 
244  34 toggle protected LocalExtension uninstall(ExtensionId extensionId, String namespace, LogLevel failFrom) throws Throwable
245    {
246  34 uninstall("uninstall", extensionId, namespace, failFrom);
247   
248  33 return this.localExtensionRepository.resolve(extensionId);
249    }
250   
 
251  0 toggle protected DefaultExtensionPlan<UninstallRequest> uninstallPlan(ExtensionId extensionId, String namespace,
252    LogLevel failFrom) throws Throwable
253    {
254  0 Job uninstallJob = uninstall("installplan", extensionId, namespace, failFrom);
255   
256  0 return (DefaultExtensionPlan<UninstallRequest>) uninstallJob.getStatus();
257    }
258   
 
259  34 toggle protected Job uninstall(String jobId, ExtensionId extensionId, String namespace, LogLevel failFrom) throws Throwable
260    {
261  34 UninstallRequest uninstallRequest = new UninstallRequest();
262  34 uninstallRequest.addExtension(extensionId);
263  34 if (namespace != null) {
264  11 uninstallRequest.addNamespace(namespace);
265    }
266   
267  34 return executeJob(jobId, uninstallRequest, failFrom);
268    }
269   
 
270  2 toggle protected ExtensionPlan upgradePlan() throws Throwable
271    {
272  2 return upgradePlan((String) null);
273    }
274   
 
275  4 toggle protected ExtensionPlan upgradePlan(String namespace) throws Throwable
276    {
277  4 return upgradePlan(namespace, (Collection<ExtensionId>) null);
278    }
279   
 
280  7 toggle protected ExtensionPlan upgradePlan(String namespace, Collection<ExtensionId> excludedExtensions) throws Throwable
281    {
282  7 return upgradePlan(namespace, excludedExtensions, LogLevel.WARN);
283    }
284   
 
285  0 toggle protected ExtensionPlan upgradePlan(String namespace, LogLevel failFrom) throws Throwable
286    {
287  0 return upgradePlan(namespace, null, failFrom);
288    }
289   
 
290  7 toggle protected ExtensionPlan upgradePlan(String namespace, Collection<ExtensionId> excludedExtensions, LogLevel failFrom)
291    throws Throwable
292    {
293  7 InstallRequest installRequest = new InstallRequest();
294  7 if (namespace != null) {
295  2 installRequest.addNamespace(namespace);
296    }
297  7 if (excludedExtensions != null) {
298  3 installRequest.getExcludedExtensions().addAll(excludedExtensions);
299    }
300   
301  7 return (ExtensionPlan) executeJob("upgradeplan", installRequest, failFrom).getStatus();
302    }
303   
 
304  1 toggle protected ExtensionPlan upgradePlan(InstallRequest installRequest) throws Throwable
305    {
306  1 return upgradePlan(installRequest, LogLevel.WARN);
307    }
308   
 
309  1 toggle protected ExtensionPlan upgradePlan(InstallRequest installRequest, LogLevel failFrom) throws Throwable
310    {
311  1 return (ExtensionPlan) executeJob("upgradeplan", installRequest, failFrom).getStatus();
312    }
313    }