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

File AbstractExtensionHandlerTest.java

 

Coverage histogram

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

Code metrics

20
74
38
1
331
250
48
0.65
1.95
38
1.26

Classes

Class Line # Actions
AbstractExtensionHandlerTest 48 74 0% 48 18
0.863636486.4%
 

Contributing tests

This file is covered by 83 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.configuration.internal.MemoryConfigurationSource;
29    import org.xwiki.extension.ExtensionId;
30    import org.xwiki.extension.InstalledExtension;
31    import org.xwiki.extension.LocalExtension;
32    import org.xwiki.extension.job.InstallRequest;
33    import org.xwiki.extension.job.UninstallRequest;
34    import org.xwiki.extension.job.plan.ExtensionPlan;
35    import org.xwiki.extension.job.plan.ExtensionPlanNode;
36    import org.xwiki.extension.job.plan.internal.DefaultExtensionPlan;
37    import org.xwiki.extension.repository.InstalledExtensionRepository;
38    import org.xwiki.extension.repository.LocalExtensionRepository;
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.AllComponents;
45    import org.xwiki.test.mockito.MockitoComponentManagerRule;
46   
47    @AllComponents
 
48    public abstract class AbstractExtensionHandlerTest
49    {
50    protected MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
51   
52    @Rule
53    public MockitoRepositoryUtilsRule repositoryUtil = new MockitoRepositoryUtilsRule(this.mocker);
54   
55    protected LocalExtensionRepository localExtensionRepository;
56   
57    protected InstalledExtensionRepository installedExtensionRepository;
58   
59    protected JobExecutor jobExecutor;
60   
61    protected MemoryConfigurationSource memoryConfigurationSource;
62   
 
63  83 toggle @Before
64    public void setUp() throws Exception
65    {
66  83 this.jobExecutor = this.mocker.getInstance(JobExecutor.class);
67  83 this.localExtensionRepository = this.mocker.getInstance(LocalExtensionRepository.class);
68  83 this.installedExtensionRepository = this.mocker.getInstance(InstalledExtensionRepository.class);
69   
70  83 this.mocker.registerMockComponent(CacheManager.class);
71   
72  83 this.memoryConfigurationSource = this.mocker.registerMemoryConfigurationSource();
73    }
74   
 
75  4 toggle protected ExtensionPlanNode getNode(ExtensionId id, Collection<ExtensionPlanNode> nodes)
76    {
77  4 for (ExtensionPlanNode node : nodes) {
78  6 if (node.getAction().getExtension().getId().equals(id)) {
79  4 return node;
80    }
81    }
82   
83  0 return null;
84    }
85   
 
86  164 toggle protected Job executeJob(String jobId, Request request, LogLevel failFrom) throws Throwable
87    {
88  164 Job installJob = this.jobExecutor.execute(jobId, request);
89   
90  164 installJob.join();
91   
92  164 if (installJob.getStatus().getError() != null) {
93  14 throw installJob.getStatus().getError();
94    }
95   
96  150 List<LogEvent> errors = installJob.getStatus().getLog().getLogsFrom(failFrom);
97  150 if (!errors.isEmpty()) {
98  0 throw errors.get(0).getThrowable() != null ? errors.get(0).getThrowable()
99    : new Exception(errors.get(0).getFormattedMessage());
100    }
101   
102  150 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  20 toggle protected ExtensionPlan installPlan(ExtensionId extensionId) throws Throwable
164    {
165  20 return installPlan(extensionId, true);
166    }
167   
 
168  21 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, boolean rootModifications) throws Throwable
169    {
170  21 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  21 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, String[] namespaces, boolean rootModifications)
179    throws Throwable
180    {
181  21 return installPlan(extensionId, namespaces, rootModifications, LogLevel.WARN);
182    }
183   
 
184  10 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, String namespace) throws Throwable
185    {
186  10 return installPlan(extensionId, namespace, true);
187    }
188   
 
189  12 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, String namespace, boolean rootModifications)
190    throws Throwable
191    {
192  12 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  33 toggle protected ExtensionPlan installPlan(ExtensionId extensionId, String[] namespaces, boolean rootModifications,
203    LogLevel failFrom) throws Throwable
204    {
205  33 InstallRequest installRequest = createInstallRequest(extensionId, namespaces, rootModifications);
206   
207  33 return installPlan(installRequest, failFrom);
208    }
209   
 
210  1 toggle protected ExtensionPlan installPlan(InstallRequest installRequest) throws Throwable
211    {
212  1 return installPlan(installRequest, LogLevel.WARN);
213    }
214   
 
215  34 toggle protected ExtensionPlan installPlan(InstallRequest installRequest, LogLevel failFrom) throws Throwable
216    {
217  34 Job installJob = executeJob("installplan", installRequest, failFrom);
218   
219  24 return (ExtensionPlan) installJob.getStatus();
220    }
221   
 
222  3 toggle protected Job install(String jobId, ExtensionId extensionId, String[] namespaces, LogLevel failFrom)
223    throws Throwable
224    {
225  3 return install(jobId, extensionId, namespaces, true, failFrom);
226    }
227   
 
228  88 toggle protected Job install(String jobId, ExtensionId extensionId, String[] namespaces, boolean rootModifications,
229    LogLevel failFrom) throws Throwable
230    {
231  88 InstallRequest installRequest = createInstallRequest(extensionId, namespaces, rootModifications);
232   
233  88 return executeJob(jobId, installRequest, failFrom);
234    }
235   
 
236  1 toggle protected InstallRequest createInstallRequest(ExtensionId extensionId)
237    {
238  1 return createInstallRequest(extensionId, null, true);
239    }
240   
 
241  122 toggle protected InstallRequest createInstallRequest(ExtensionId extensionId, String[] namespaces,
242    boolean rootModifications)
243    {
244  122 InstallRequest installRequest = new InstallRequest();
245  122 installRequest.addExtension(extensionId);
246  122 if (namespaces != null) {
247  57 for (String namespace : namespaces) {
248  58 installRequest.addNamespace(namespace);
249    }
250    }
251  122 installRequest.setRootModificationsAllowed(rootModifications);
252  122 installRequest.setVerbose(false);
253   
254  122 return installRequest;
255    }
256   
 
257  31 toggle protected LocalExtension uninstall(ExtensionId extensionId, String namespace) throws Throwable
258    {
259  31 return uninstall(extensionId, namespace, LogLevel.WARN);
260    }
261   
 
262  34 toggle protected LocalExtension uninstall(ExtensionId extensionId, String namespace, LogLevel failFrom) throws Throwable
263    {
264  34 uninstall("uninstall", extensionId, namespace, failFrom);
265   
266  33 return this.localExtensionRepository.resolve(extensionId);
267    }
268   
 
269  0 toggle protected DefaultExtensionPlan<UninstallRequest> uninstallPlan(ExtensionId extensionId, String namespace,
270    LogLevel failFrom) throws Throwable
271    {
272  0 Job uninstallJob = uninstall("installplan", extensionId, namespace, failFrom);
273   
274  0 return (DefaultExtensionPlan<UninstallRequest>) uninstallJob.getStatus();
275    }
276   
 
277  34 toggle protected Job uninstall(String jobId, ExtensionId extensionId, String namespace, LogLevel failFrom) throws Throwable
278    {
279  34 UninstallRequest uninstallRequest = new UninstallRequest();
280  34 uninstallRequest.addExtension(extensionId);
281  34 if (namespace != null) {
282  11 uninstallRequest.addNamespace(namespace);
283    }
284   
285  34 return executeJob(jobId, uninstallRequest, failFrom);
286    }
287   
 
288  2 toggle protected ExtensionPlan upgradePlan() throws Throwable
289    {
290  2 return upgradePlan((String) null);
291    }
292   
 
293  4 toggle protected ExtensionPlan upgradePlan(String namespace) throws Throwable
294    {
295  4 return upgradePlan(namespace, (Collection<ExtensionId>) null);
296    }
297   
 
298  7 toggle protected ExtensionPlan upgradePlan(String namespace, Collection<ExtensionId> excludedExtensions) throws Throwable
299    {
300  7 return upgradePlan(namespace, excludedExtensions, LogLevel.WARN);
301    }
302   
 
303  0 toggle protected ExtensionPlan upgradePlan(String namespace, LogLevel failFrom) throws Throwable
304    {
305  0 return upgradePlan(namespace, null, failFrom);
306    }
307   
 
308  7 toggle protected ExtensionPlan upgradePlan(String namespace, Collection<ExtensionId> excludedExtensions, LogLevel failFrom)
309    throws Throwable
310    {
311  7 InstallRequest installRequest = new InstallRequest();
312  7 if (namespace != null) {
313  2 installRequest.addNamespace(namespace);
314    }
315  7 if (excludedExtensions != null) {
316  3 installRequest.getExcludedExtensions().addAll(excludedExtensions);
317    }
318   
319  7 return (ExtensionPlan) executeJob("upgradeplan", installRequest, failFrom).getStatus();
320    }
321   
 
322  1 toggle protected ExtensionPlan upgradePlan(InstallRequest installRequest) throws Throwable
323    {
324  1 return upgradePlan(installRequest, LogLevel.WARN);
325    }
326   
 
327  1 toggle protected ExtensionPlan upgradePlan(InstallRequest installRequest, LogLevel failFrom) throws Throwable
328    {
329  1 return (ExtensionPlan) executeJob("upgradeplan", installRequest, failFrom).getStatus();
330    }
331    }