1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
55 |
|
|
56 |
|
@version |
57 |
|
|
|
|
| 88.5% |
Uncovered Elements: 7 (61) |
Complexity: 12 |
Complexity Density: 0.25 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
80 |
2 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
103 |
5 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
114 |
2 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
121 |
6 |
private IterableResult<Extension> toIterableExtensions(Extension... extensions)... |
122 |
|
{ |
123 |
6 |
return new CollectionIterableResult<Extension>(extensions.length, 0, Arrays.asList(extensions)); |
124 |
|
} |
125 |
|
|
|
|
| 36.4% |
Uncovered Elements: 7 (11) |
Complexity: 4 |
Complexity Density: 0.57 |
|
126 |
9 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
140 |
3 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
147 |
9 |
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 |
|
|
154 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
155 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 1 |
Complexity Density: 0.07 |
1PASS
|
|
166 |
1 |
@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 |
|
} |