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

File DefaultLocalExtensionRepositoryTest.java

 

Code metrics

0
92
14
1
278
197
17
0.18
6.57
14
1.21

Classes

Class Line # Actions
DefaultLocalExtensionRepositoryTest 48 92 0% 17 3
0.971698197.2%
 

Contributing tests

This file is covered by 13 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.repository.local;
21   
22    import org.junit.Assert;
23    import org.junit.Before;
24    import org.junit.Rule;
25    import org.junit.Test;
26    import org.xwiki.component.manager.ComponentLookupException;
27    import org.xwiki.extension.DefaultExtensionDependency;
28    import org.xwiki.extension.Extension;
29    import org.xwiki.extension.ExtensionId;
30    import org.xwiki.extension.LocalExtension;
31    import org.xwiki.extension.ResolveException;
32    import org.xwiki.extension.TestResources;
33    import org.xwiki.extension.repository.LocalExtensionRepository;
34    import org.xwiki.extension.repository.result.CollectionIterableResult;
35    import org.xwiki.extension.repository.result.IterableResult;
36    import org.xwiki.extension.repository.search.ExtensionQuery;
37    import org.xwiki.extension.repository.search.ExtensionQuery.COMPARISON;
38    import org.xwiki.extension.repository.search.SearchException;
39    import org.xwiki.extension.test.MockitoRepositoryUtilsRule;
40    import org.xwiki.extension.version.Version;
41    import org.xwiki.extension.version.internal.DefaultVersionConstraint;
42    import org.xwiki.test.annotation.AllComponents;
43    import org.xwiki.test.mockito.MockitoComponentManagerRule;
44   
45    import static org.junit.Assert.assertEquals;
46   
47    @AllComponents
 
48    public class DefaultLocalExtensionRepositoryTest
49    {
50    private LocalExtensionRepository localExtensionRepository;
51   
52    private MockitoComponentManagerRule mocker = new MockitoComponentManagerRule();
53   
54    @Rule
55    public MockitoRepositoryUtilsRule repositoryUtil = new MockitoRepositoryUtilsRule(this.mocker);
56   
 
57  13 toggle @Before
58    public void before() throws ComponentLookupException
59    {
60  13 this.localExtensionRepository = this.mocker.getInstance(LocalExtensionRepository.class);
61    }
62   
 
63  1 toggle @Test
64    public void testInit()
65    {
66  1 Assert.assertTrue(this.localExtensionRepository.countExtensions() > 0);
67    }
68   
 
69  1 toggle @Test
70    public void testResolve() throws ResolveException
71    {
72  1 try {
73  1 this.localExtensionRepository.resolve(new ExtensionId("unexistingextension", "version"));
74   
75  0 Assert.fail("Resolve should have failed");
76    } catch (ResolveException expected) {
77    // expected
78    }
79   
80  1 try {
81  1 this.localExtensionRepository.resolve(new ExtensionId(TestResources.INSTALLED_ID.getId(), "wrongversion"));
82   
83  0 Assert.fail("Resolve should have failed");
84    } catch (ResolveException expected) {
85    // expected
86    }
87   
88  1 Extension extension = this.localExtensionRepository.resolve(TestResources.INSTALLED_ID);
89   
90  1 Assert.assertNotNull(extension);
91  1 Assert.assertEquals(TestResources.INSTALLED_ID, extension.getId());
92    }
93   
 
94  1 toggle @Test
95    public void testResolveExistingDependency() throws ResolveException
96    {
97  1 Extension extension =
98    this.localExtensionRepository.resolve(new DefaultExtensionDependency(TestResources.INSTALLED_ID.getId(),
99    new DefaultVersionConstraint(TestResources.INSTALLED_ID.getVersion().getValue())));
100   
101  1 Assert.assertNotNull(extension);
102  1 Assert.assertEquals(TestResources.INSTALLED_ID, extension.getId());
103    }
104   
 
105  1 toggle @Test(expected = ResolveException.class)
106    public void testResolveUnexistingDependencyId() throws ResolveException
107    {
108  1 this.localExtensionRepository
109    .resolve(new DefaultExtensionDependency("unexistingextension", new DefaultVersionConstraint("version")));
110    }
111   
 
112  1 toggle @Test(expected = ResolveException.class)
113    public void testResolveIncompatibleDependencyVersion() throws ResolveException
114    {
115  1 this.localExtensionRepository.resolve(new DefaultExtensionDependency(TestResources.INSTALLED_ID.getId(),
116    new DefaultVersionConstraint("wrongversion")));
117    }
118   
 
119  1 toggle @Test(expected = ResolveException.class)
120    public void testResolveUnexistingButFeatureCompatibleDependency() throws ResolveException
121    {
122  1 this.localExtensionRepository.resolve(new DefaultExtensionDependency("installedextension-feature",
123    new DefaultVersionConstraint(TestResources.INSTALLED_ID.getVersion().getValue())));
124    }
125   
 
126  1 toggle @Test(expected = ResolveException.class)
127    public void testResolveUnexistingButSmalerVersionDependency() throws ResolveException
128    {
129  1 this.localExtensionRepository.resolve(
130    new DefaultExtensionDependency(TestResources.INSTALLED_ID.getId(), new DefaultVersionConstraint("0.9")));
131    }
132   
 
133  1 toggle @Test
134    public void testSearch() throws SearchException
135    {
136  1 CollectionIterableResult<Extension> result =
137    (CollectionIterableResult<Extension>) this.localExtensionRepository.search(null, 0, -1);
138   
139  1 Assert.assertEquals(15, result.getTotalHits());
140  1 Assert.assertEquals(15, result.getSize());
141  1 Assert.assertEquals(0, result.getOffset());
142   
143  1 result = (CollectionIterableResult<Extension>) this.localExtensionRepository.search("", 0, -1);
144   
145  1 Assert.assertEquals(15, result.getTotalHits());
146  1 Assert.assertEquals(15, result.getSize());
147  1 Assert.assertEquals(0, result.getOffset());
148   
149  1 result = (CollectionIterableResult<Extension>) this.localExtensionRepository.search("extension", 0, -1);
150   
151  1 Assert.assertEquals(5, result.getTotalHits());
152  1 Assert.assertEquals(5, result.getSize());
153  1 Assert.assertEquals(0, result.getOffset());
154   
155  1 result = (CollectionIterableResult<Extension>) this.localExtensionRepository.search("dependency", 0, -1);
156   
157  1 Assert.assertEquals(8, result.getTotalHits());
158  1 Assert.assertEquals(8, result.getSize());
159  1 Assert.assertEquals(0, result.getOffset());
160   
161  1 result = (CollectionIterableResult<Extension>) this.localExtensionRepository.search(null, 0, 0);
162   
163  1 Assert.assertEquals(15, result.getTotalHits());
164  1 Assert.assertEquals(0, result.getSize());
165  1 Assert.assertEquals(0, result.getOffset());
166   
167  1 result = (CollectionIterableResult<Extension>) this.localExtensionRepository.search(null, 0, 2);
168   
169  1 Assert.assertEquals(15, result.getTotalHits());
170  1 Assert.assertEquals(2, result.getSize());
171  1 Assert.assertEquals(0, result.getOffset());
172   
173  1 result = (CollectionIterableResult<Extension>) this.localExtensionRepository.search(null, 0, 1);
174   
175  1 Assert.assertEquals(15, result.getTotalHits());
176  1 Assert.assertEquals(1, result.getSize());
177  1 Assert.assertEquals(0, result.getOffset());
178   
179  1 result = (CollectionIterableResult<Extension>) this.localExtensionRepository.search(null, 1, 2);
180   
181  1 Assert.assertEquals(15, result.getTotalHits());
182  1 Assert.assertEquals(2, result.getSize());
183  1 Assert.assertEquals(1, result.getOffset());
184   
185  1 result = (CollectionIterableResult<Extension>) this.localExtensionRepository.search(null, 2, 2);
186   
187  1 Assert.assertEquals(15, result.getTotalHits());
188  1 Assert.assertEquals(2, result.getSize());
189  1 Assert.assertEquals(2, result.getOffset());
190   
191  1 result = (CollectionIterableResult<Extension>) this.localExtensionRepository.search(null, -1, 2);
192   
193  1 Assert.assertEquals(15, result.getTotalHits());
194  1 Assert.assertEquals(2, result.getSize());
195  1 Assert.assertEquals(-1, result.getOffset());
196   
197  1 result = (CollectionIterableResult<Extension>) this.localExtensionRepository.search(null, -1, 1);
198   
199  1 Assert.assertEquals(15, result.getTotalHits());
200  1 Assert.assertEquals(1, result.getSize());
201  1 Assert.assertEquals(-1, result.getOffset());
202    }
203   
 
204  1 toggle @Test
205    public void testSearchWithQueryEQUAL() throws SearchException
206    {
207  1 ExtensionQuery query = new ExtensionQuery();
208   
209  1 query.addFilter(Extension.FIELD_ID, TestResources.INSTALLED_ID.getId(), COMPARISON.EQUAL);
210   
211  1 IterableResult<Extension> result = this.localExtensionRepository.search(query);
212   
213  1 assertEquals(1, result.getTotalHits());
214  1 assertEquals(1, result.getSize());
215  1 assertEquals(0, result.getOffset());
216  1 assertEquals(TestResources.INSTALLED_ID, result.iterator().next().getId());
217    }
218   
 
219  1 toggle @Test
220    public void testSearchWithQueryMATCH() throws SearchException
221    {
222  1 ExtensionQuery query = new ExtensionQuery();
223   
224  1 query.addFilter(Extension.FIELD_ID, TestResources.INSTALLED_ID.getId(), COMPARISON.MATCH);
225   
226  1 IterableResult<Extension> result = this.localExtensionRepository.search(query);
227   
228  1 assertEquals(3, result.getTotalHits());
229  1 assertEquals(3, result.getSize());
230  1 assertEquals(0, result.getOffset());
231    }
232   
 
233  1 toggle @Test
234    public void testSearchWithQueryWithNullValue() throws SearchException
235    {
236  1 ExtensionQuery query = new ExtensionQuery();
237   
238  1 query.addFilter(Extension.FIELD_CATEGORY, "category", COMPARISON.EQUAL);
239   
240  1 IterableResult<Extension> result = this.localExtensionRepository.search(query);
241   
242  1 assertEquals(0, result.getTotalHits());
243  1 assertEquals(0, result.getSize());
244  1 assertEquals(0, result.getOffset());
245    }
246   
 
247  1 toggle @Test
248    public void testRemove() throws ResolveException
249    {
250  1 LocalExtension localExtension = this.localExtensionRepository.resolve(TestResources.INSTALLED_ID);
251   
252  1 this.localExtensionRepository.removeExtension(localExtension);
253   
254  1 try {
255  1 this.localExtensionRepository.resolve(TestResources.INSTALLED_ID);
256  0 Assert.fail(
257    "Extension [" + TestResources.INSTALLED_ID + "] should not exist anymore in the local repository");
258    } catch (ResolveException e) {
259    // expected
260    }
261   
262  1 Assert.assertFalse(this.localExtensionRepository.getLocalExtensionVersions(TestResources.INSTALLED_ID.getId())
263    .contains(localExtension));
264  1 Assert.assertFalse(this.localExtensionRepository
265    .getLocalExtensionVersions(TestResources.INSTALLED_ID.getId() + "-feature").contains(localExtension));
266    }
267   
 
268  1 toggle @Test
269    public void resolveVersions() throws ResolveException
270    {
271  1 IterableResult<Version> versions =
272    this.localExtensionRepository.resolveVersions(TestResources.INSTALLED_ONNAMESPACE_ID.getId(), 0, -1);
273   
274  1 assertEquals(1, versions.getTotalHits());
275  1 assertEquals(1, versions.getSize());
276  1 assertEquals(TestResources.INSTALLED_ONNAMESPACE_ID.getVersion(), versions.iterator().next());
277    }
278    }