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

File DefaultExtensionRepositoryManagerTest.java

 

Code metrics

4
48
9
1
194
134
12
0.25
5.33
9
1.33

Classes

Class Line # Actions
DefaultExtensionRepositoryManagerTest 58 48 0% 12 7
0.885245988.5%
 

Contributing tests

This file is covered by 2 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.internal;
21   
22    import java.net.URI;
23    import java.util.ArrayList;
24    import java.util.Arrays;
25    import java.util.Iterator;
26    import java.util.List;
27   
28    import org.junit.Assert;
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.xwiki.component.manager.ComponentLookupException;
33    import org.xwiki.extension.Extension;
34    import org.xwiki.extension.ResolveException;
35    import org.xwiki.extension.repository.DefaultExtensionRepositoryDescriptor;
36    import org.xwiki.extension.repository.ExtensionRepository;
37    import org.xwiki.extension.repository.ExtensionRepositoryManager;
38    import org.xwiki.extension.repository.result.CollectionIterableResult;
39    import org.xwiki.extension.repository.result.IterableResult;
40    import org.xwiki.extension.repository.search.SearchException;
41    import org.xwiki.extension.repository.search.Searchable;
42    import org.xwiki.extension.test.ExtensionUtils;
43    import org.xwiki.extension.version.Version;
44    import org.xwiki.extension.version.internal.DefaultVersion;
45    import org.xwiki.test.mockito.MockitoComponentMockingRule;
46   
47    import com.google.common.collect.Lists;
48   
49    import static org.mockito.Mockito.mock;
50    import static org.mockito.Mockito.when;
51    import static org.mockito.Mockito.withSettings;
52   
53    /**
54    * Test {@link DefaultExtensionRepositoryManager}.
55    *
56    * @version $Id: 3eadc40a1e15eea1c7c15701a5c3f3d6ea067830 $
57    */
 
58    public class DefaultExtensionRepositoryManagerTest
59    {
60    @Rule
61    public MockitoComponentMockingRule<ExtensionRepositoryManager> mock =
62    new MockitoComponentMockingRule<ExtensionRepositoryManager>(DefaultExtensionRepositoryManager.class);
63   
64    private ExtensionRepository mockRepository1;
65   
66    private Searchable mockSearchableRepository1;
67   
68    private ExtensionRepository mockRepository2;
69   
70    private Searchable mockSearchableRepository2;
71   
72    private Extension extension1;
73   
74    private Extension extension2;
75   
76    private Extension extension3;
77   
78    private Extension extension4;
79   
 
80  2 toggle @Before
81    public void configure() throws Exception
82    {
83  2 this.mockRepository1 =
84    mock(ExtensionRepository.class, withSettings().extraInterfaces(Searchable.class).name("repository1"));
85  2 this.mockSearchableRepository1 = (Searchable) this.mockRepository1;
86  2 when(this.mockRepository1.getDescriptor())
87    .thenReturn(new DefaultExtensionRepositoryDescriptor("repository1", "type", new URI("uri:uri")));
88  2 this.mock.getComponentUnderTest().addRepository(this.mockRepository1);
89   
90  2 this.mockRepository2 =
91    mock(ExtensionRepository.class, withSettings().extraInterfaces(Searchable.class).name("repository2"));
92  2 this.mockSearchableRepository2 = (Searchable) this.mockRepository2;
93  2 when(this.mockRepository2.getDescriptor())
94    .thenReturn(new DefaultExtensionRepositoryDescriptor("repository2", "type", new URI("uri:uri")));
95  2 this.mock.getComponentUnderTest().addRepository(this.mockRepository2);
96   
97  2 this.extension1 = ExtensionUtils.mockExtension("id1", "version1");
98  2 this.extension2 = ExtensionUtils.mockExtension("id2", "version2");
99  2 this.extension3 = ExtensionUtils.mockExtension("id3", "version3");
100  2 this.extension4 = ExtensionUtils.mockExtension("id4", "version4");
101    }
102   
 
103  5 toggle private List<Version> toVersionList(String... versions)
104    {
105  5 List<Version> versionList = new ArrayList<Version>(versions.length);
106   
107  5 for (String version : versions) {
108  13 versionList.add(new DefaultVersion(version));
109    }
110   
111  5 return versionList;
112    }
113   
 
114  2 toggle private IterableResult<Version> toIterableVersions(String... versions)
115    {
116  2 List<Version> versionList = toVersionList(versions);
117   
118  2 return new CollectionIterableResult<Version>(versionList.size(), 0, versionList);
119    }
120   
 
121  6 toggle private IterableResult<Extension> toIterableExtensions(Extension... extensions)
122    {
123  6 return new CollectionIterableResult<Extension>(extensions.length, 0, Arrays.asList(extensions));
124    }
125   
 
126  9 toggle private void assertSameElements(Iterable<?> it1, Iterable<?> it2)
127    {
128  9 Iterator<?> i1 = it1.iterator();
129  9 Iterator<?> i2 = it2.iterator();
130   
131  9 while (i1.hasNext() && i2.hasNext()) {
132  0 Object element1 = i1.next();
133  0 Object element2 = i2.next();
134  0 if (element1 != element2) {
135  0 Assert.fail("[" + element1 + "] is not the same as [" + element2 + "]");
136    }
137    }
138    }
139   
 
140  3 toggle private void assertResolveVersions(String id, int offset, int nb, String... versions)
141    throws ResolveException, ComponentLookupException
142    {
143  3 Assert.assertEquals(toVersionList(versions),
144    Lists.newArrayList(this.mock.getComponentUnderTest().resolveVersions("id", offset, nb)));
145    }
146   
 
147  9 toggle private void assertSearch(String id, int offset, int nb, Extension... extensions)
148    throws ComponentLookupException, SearchException
149    {
150  9 assertSameElements(Arrays.asList(extensions), this.mock.getComponentUnderTest().search("id", offset, nb));
151    }
152   
153    // Tests
154   
 
155  1 toggle @Test
156    public void resolveVersions() throws ResolveException, ComponentLookupException
157    {
158  1 when(this.mockRepository1.resolveVersions("id", 0, -1)).thenReturn(toIterableVersions("1.0", "2.0"));
159  1 when(this.mockRepository2.resolveVersions("id", 0, -1)).thenReturn(toIterableVersions("3.0", "2.0", "4.0"));
160   
161  1 assertResolveVersions("id", 0, -1, "1.0", "2.0", "3.0", "4.0");
162  1 assertResolveVersions("id", 0, 1, "1.0");
163  1 assertResolveVersions("id", 1, -1, "2.0", "3.0", "4.0");
164    }
165   
 
166  1 toggle @Test
167    public void search() throws SearchException, ComponentLookupException
168    {
169  1 when(this.mockSearchableRepository1.search("pattern", 0, -1))
170    .thenReturn(toIterableExtensions(this.extension1, this.extension2));
171  1 when(this.mockSearchableRepository2.search("pattern", 0, -1))
172    .thenReturn(toIterableExtensions(this.extension3, this.extension4));
173   
174  1 assertSearch("pattern", 0, -1, this.extension1, this.extension2, this.extension3, this.extension4);
175  1 assertSearch("pattern", 0, 1, this.extension1);
176  1 assertSearch("pattern", 1, -1, this.extension2, this.extension3, this.extension4);
177   
178  1 when(this.mockSearchableRepository1.search("pattern", 0, -1)).thenReturn(toIterableExtensions());
179  1 when(this.mockSearchableRepository2.search("pattern", 0, -1))
180    .thenReturn(toIterableExtensions(this.extension3, this.extension4));
181   
182  1 assertSearch("pattern", 0, -1, this.extension3, this.extension4);
183  1 assertSearch("pattern", 0, 1, this.extension3);
184  1 assertSearch("pattern", 1, -1, this.extension4);
185   
186  1 when(this.mockSearchableRepository1.search("pattern", 0, -1))
187    .thenReturn(toIterableExtensions(this.extension1, this.extension2));
188  1 when(this.mockSearchableRepository2.search("pattern", 0, -1)).thenReturn(toIterableExtensions());
189   
190  1 assertSearch("pattern", 0, -1, this.extension1, this.extension2, this.extension3, this.extension4);
191  1 assertSearch("pattern", 0, 1, this.extension1);
192  1 assertSearch("pattern", 1, -1, this.extension2, this.extension3, this.extension4);
193    }
194    }