1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.icon.internal; |
21 |
|
|
22 |
|
import java.io.InputStream; |
23 |
|
import java.io.InputStreamReader; |
24 |
|
import java.io.Reader; |
25 |
|
import java.net.MalformedURLException; |
26 |
|
import java.util.ArrayList; |
27 |
|
import java.util.List; |
28 |
|
|
29 |
|
import javax.inject.Provider; |
30 |
|
|
31 |
|
import org.junit.Before; |
32 |
|
import org.junit.Rule; |
33 |
|
import org.junit.Test; |
34 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
35 |
|
import org.xwiki.component.util.DefaultParameterizedType; |
36 |
|
import org.xwiki.configuration.ConfigurationSource; |
37 |
|
import org.xwiki.icon.IconException; |
38 |
|
import org.xwiki.icon.IconSet; |
39 |
|
import org.xwiki.icon.IconSetCache; |
40 |
|
import org.xwiki.icon.IconSetLoader; |
41 |
|
import org.xwiki.model.reference.DocumentReference; |
42 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
43 |
|
import org.xwiki.query.Query; |
44 |
|
import org.xwiki.query.QueryException; |
45 |
|
import org.xwiki.query.QueryManager; |
46 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
47 |
|
import org.xwiki.wiki.descriptor.WikiDescriptorManager; |
48 |
|
|
49 |
|
import com.xpn.xwiki.XWiki; |
50 |
|
import com.xpn.xwiki.XWikiContext; |
51 |
|
|
52 |
|
import static org.junit.Assert.assertEquals; |
53 |
|
import static org.junit.Assert.assertNotNull; |
54 |
|
import static org.junit.Assert.assertNull; |
55 |
|
import static org.junit.Assert.assertTrue; |
56 |
|
import static org.mockito.ArgumentMatchers.any; |
57 |
|
import static org.mockito.ArgumentMatchers.anyString; |
58 |
|
import static org.mockito.ArgumentMatchers.eq; |
59 |
|
import static org.mockito.Mockito.mock; |
60 |
|
import static org.mockito.Mockito.never; |
61 |
|
import static org.mockito.Mockito.verify; |
62 |
|
import static org.mockito.Mockito.verifyZeroInteractions; |
63 |
|
import static org.mockito.Mockito.when; |
64 |
|
|
65 |
|
|
66 |
|
@link |
67 |
|
|
68 |
|
@since |
69 |
|
@version |
70 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (130) |
Complexity: 17 |
Complexity Density: 0.15 |
|
71 |
|
public class DefaultIconSetManagerTest |
72 |
|
{ |
73 |
|
@Rule |
74 |
|
public MockitoComponentMockingRule<DefaultIconSetManager> mocker = |
75 |
|
new MockitoComponentMockingRule<>(DefaultIconSetManager.class); |
76 |
|
|
77 |
|
private Provider<XWikiContext> xcontextProvider; |
78 |
|
|
79 |
|
private DocumentReferenceResolver<String> documentReferenceResolver; |
80 |
|
|
81 |
|
private DocumentAccessBridge documentAccessBridge; |
82 |
|
|
83 |
|
private IconSetCache iconSetCache; |
84 |
|
|
85 |
|
private IconSetLoader iconSetLoader; |
86 |
|
|
87 |
|
private QueryManager queryManager; |
88 |
|
|
89 |
|
private WikiDescriptorManager wikiDescriptorManager; |
90 |
|
|
91 |
|
private ConfigurationSource configurationSource; |
92 |
|
|
93 |
|
private XWikiContext xcontext; |
94 |
|
|
95 |
|
private XWiki xwiki; |
96 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
97 |
13 |
@Before... |
98 |
|
public void setUp() throws Exception |
99 |
|
{ |
100 |
13 |
xcontextProvider = mocker.registerMockComponent(XWikiContext.TYPE_PROVIDER); |
101 |
13 |
xcontext = mock(XWikiContext.class); |
102 |
13 |
when(xcontextProvider.get()).thenReturn(xcontext); |
103 |
13 |
xwiki = mock(XWiki.class); |
104 |
13 |
when(xcontext.getWiki()).thenReturn(xwiki); |
105 |
13 |
documentReferenceResolver = mocker.getInstance(new DefaultParameterizedType(null, |
106 |
|
DocumentReferenceResolver.class, String.class), "current"); |
107 |
13 |
documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class); |
108 |
13 |
iconSetCache = mocker.getInstance(IconSetCache.class); |
109 |
13 |
iconSetLoader = mocker.getInstance(IconSetLoader.class); |
110 |
13 |
queryManager = mocker.getInstance(QueryManager.class); |
111 |
13 |
wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class); |
112 |
13 |
when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("currentWikiId"); |
113 |
13 |
configurationSource = mocker.getInstance(ConfigurationSource.class, "all"); |
114 |
|
} |
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1PASS
|
|
116 |
1 |
@Test... |
117 |
|
public void getCurrentIconSet() throws Exception |
118 |
|
{ |
119 |
1 |
String currentIconTheme = "IconThemes.SilkTheme"; |
120 |
1 |
when(configurationSource.getProperty("iconTheme")).thenReturn(currentIconTheme); |
121 |
1 |
DocumentReference iconThemeRef = new DocumentReference("xwiki", "IconThemes", "SilkTheme"); |
122 |
1 |
when(documentReferenceResolver.resolve(currentIconTheme)).thenReturn(iconThemeRef); |
123 |
1 |
when(documentAccessBridge.exists(iconThemeRef)).thenReturn(true); |
124 |
|
|
125 |
1 |
IconSet iconSet = new IconSet(currentIconTheme); |
126 |
1 |
when(iconSetLoader.loadIconSet(iconThemeRef)).thenReturn(iconSet); |
127 |
|
|
128 |
|
|
129 |
1 |
IconSet result = mocker.getComponentUnderTest().getCurrentIconSet(); |
130 |
|
|
131 |
|
|
132 |
1 |
assertEquals(iconSet, result); |
133 |
1 |
verify(iconSetCache).put(iconThemeRef, iconSet); |
134 |
1 |
verify(iconSetCache).put(currentIconTheme, "currentWikiId", iconSet); |
135 |
|
} |
136 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
137 |
1 |
@Test... |
138 |
|
public void getCurrentIconSetWhenInCache() throws Exception |
139 |
|
{ |
140 |
1 |
String currentIconTheme = "IconThemes.SilkTheme"; |
141 |
1 |
when(configurationSource.getProperty("iconTheme")).thenReturn(currentIconTheme); |
142 |
1 |
DocumentReference iconThemeRef = new DocumentReference("xwiki", "IconThemes", "SilkTheme"); |
143 |
1 |
when(documentReferenceResolver.resolve(currentIconTheme)).thenReturn(iconThemeRef); |
144 |
1 |
when(documentAccessBridge.exists(iconThemeRef)).thenReturn(true); |
145 |
|
|
146 |
1 |
IconSet iconSet = new IconSet(currentIconTheme); |
147 |
1 |
when(iconSetCache.get(iconThemeRef)).thenReturn(iconSet); |
148 |
|
|
149 |
|
|
150 |
1 |
IconSet result = mocker.getComponentUnderTest().getCurrentIconSet(); |
151 |
|
|
152 |
|
|
153 |
1 |
assertEquals(iconSet, result); |
154 |
1 |
verify(iconSetLoader, never()).loadIconSet(any(DocumentReference.class)); |
155 |
|
} |
156 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
157 |
1 |
@Test... |
158 |
|
public void getCurrentIconSetWhenItDoesNotExist() throws Exception |
159 |
|
{ |
160 |
1 |
String currentIconTheme = "xwiki:IconThemes.SilkTheme"; |
161 |
1 |
when(configurationSource.getProperty("iconTheme")).thenReturn(currentIconTheme); |
162 |
1 |
DocumentReference iconThemeRef = new DocumentReference("xwiki", "IconThemes", "SilkTheme"); |
163 |
1 |
when(documentReferenceResolver.resolve(currentIconTheme)).thenReturn(iconThemeRef); |
164 |
1 |
when(documentAccessBridge.exists(iconThemeRef)).thenReturn(false); |
165 |
|
|
166 |
1 |
when(iconSetCache.get(iconThemeRef)).thenReturn(null); |
167 |
|
|
168 |
|
|
169 |
1 |
IconSet result = mocker.getComponentUnderTest().getCurrentIconSet(); |
170 |
|
|
171 |
|
|
172 |
1 |
assertNull(result); |
173 |
1 |
verify(iconSetLoader, never()).loadIconSet(any(DocumentReference.class)); |
174 |
|
} |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1PASS
|
|
176 |
1 |
@Test... |
177 |
|
public void getDefaultIcon() throws Exception |
178 |
|
{ |
179 |
1 |
InputStream is = getClass().getResourceAsStream("/test.iconset"); |
180 |
1 |
when(xwiki.getResourceAsStream("/resources/icons/default.iconset")).thenReturn(is); |
181 |
|
|
182 |
1 |
IconSet iconSet = new IconSet("default"); |
183 |
1 |
when(iconSetLoader.loadIconSet(any(InputStreamReader.class), eq("default"))).thenReturn(iconSet); |
184 |
|
|
185 |
|
|
186 |
1 |
IconSet result = mocker.getComponentUnderTest().getDefaultIconSet(); |
187 |
|
|
188 |
|
|
189 |
1 |
assertEquals(iconSet, result); |
190 |
1 |
verify(iconSetCache).put("default", iconSet); |
191 |
|
} |
192 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
193 |
1 |
@Test... |
194 |
|
public void getDefaultIconWhenInCache() throws Exception |
195 |
|
{ |
196 |
1 |
IconSet iconSet = new IconSet("default"); |
197 |
1 |
when(iconSetCache.get("default")).thenReturn(iconSet); |
198 |
|
|
199 |
|
|
200 |
1 |
IconSet result = mocker.getComponentUnderTest().getDefaultIconSet(); |
201 |
|
|
202 |
|
|
203 |
1 |
assertEquals(iconSet, result); |
204 |
1 |
verify(iconSetLoader, never()).loadIconSet(any(InputStreamReader.class), any()); |
205 |
|
} |
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1PASS
|
|
207 |
1 |
@Test... |
208 |
|
public void getDefaultIconWithException() throws Exception |
209 |
|
{ |
210 |
|
|
211 |
1 |
Exception exception = new MalformedURLException(); |
212 |
1 |
when(xwiki.getResourceAsStream(any())).thenThrow(exception); |
213 |
|
|
214 |
|
|
215 |
1 |
Exception exceptionCaught = null; |
216 |
1 |
try { |
217 |
1 |
mocker.getComponentUnderTest().getDefaultIconSet(); |
218 |
|
} catch (IconException e) { |
219 |
1 |
exceptionCaught = e; |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
1 |
assertNotNull(exceptionCaught); |
224 |
1 |
assertEquals(exception, exceptionCaught.getCause()); |
225 |
1 |
assertEquals("Failed to get the current default icon set.", exceptionCaught.getMessage()); |
226 |
|
} |
227 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
228 |
1 |
@Test... |
229 |
|
public void getIconSetWhenInCache() throws Exception |
230 |
|
{ |
231 |
|
|
232 |
1 |
IconSet iconSet = new IconSet("silk"); |
233 |
1 |
when(iconSetCache.get("silk", "currentWikiId")).thenReturn(iconSet); |
234 |
|
|
235 |
|
|
236 |
1 |
assertEquals(iconSet, mocker.getComponentUnderTest().getIconSet("silk")); |
237 |
|
|
238 |
|
|
239 |
1 |
verify(iconSetCache, never()).put(anyString(), any(IconSet.class)); |
240 |
|
} |
241 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
242 |
1 |
@Test... |
243 |
|
public void getIconSetWhenNotInCache() throws Exception |
244 |
|
{ |
245 |
|
|
246 |
1 |
IconSet iconSet = new IconSet("silk"); |
247 |
1 |
Query query = mock(Query.class); |
248 |
1 |
when(queryManager.createQuery("FROM doc.object(IconThemesCode.IconThemeClass) obj WHERE obj.name = :name", |
249 |
|
Query.XWQL)).thenReturn(query); |
250 |
1 |
List<String> results = new ArrayList<>(); |
251 |
1 |
results.add("IconThemes.Silk"); |
252 |
1 |
when(query.<String>execute()).thenReturn(results); |
253 |
1 |
DocumentReference documentReference = new DocumentReference("wiki", "IconThemes", "Silk"); |
254 |
1 |
when(documentReferenceResolver.resolve("IconThemes.Silk")).thenReturn(documentReference); |
255 |
1 |
when(iconSetLoader.loadIconSet(documentReference)).thenReturn(iconSet); |
256 |
|
|
257 |
|
|
258 |
1 |
assertEquals(iconSet, mocker.getComponentUnderTest().getIconSet("silk")); |
259 |
|
|
260 |
|
|
261 |
1 |
verify(query).bindValue("name", "silk"); |
262 |
1 |
verify(iconSetCache).put(documentReference, iconSet); |
263 |
1 |
verify(iconSetCache).put("silk", "currentWikiId", iconSet); |
264 |
|
} |
265 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
266 |
1 |
@Test... |
267 |
|
public void getIconSetWhenDoesNotExists() throws Exception |
268 |
|
{ |
269 |
|
|
270 |
1 |
Query query = mock(Query.class); |
271 |
1 |
when(queryManager.createQuery("FROM doc.object(IconThemesCode.IconThemeClass) obj WHERE obj.name = :name", |
272 |
|
Query.XWQL)).thenReturn(query); |
273 |
1 |
List<String> results = new ArrayList<>(); |
274 |
1 |
when(query.<String>execute()).thenReturn(results); |
275 |
|
|
276 |
|
|
277 |
1 |
assertNull(mocker.getComponentUnderTest().getIconSet("silk")); |
278 |
|
|
279 |
|
|
280 |
1 |
verify(query).bindValue("name", "silk"); |
281 |
|
} |
282 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1PASS
|
|
283 |
1 |
@Test... |
284 |
|
public void getIconSetWhenException() throws Exception |
285 |
|
{ |
286 |
|
|
287 |
1 |
Exception exception = new QueryException("exception in the query", null, null); |
288 |
1 |
when(queryManager.createQuery(any(), any())).thenThrow(exception); |
289 |
|
|
290 |
|
|
291 |
1 |
Exception caughtException = null; |
292 |
1 |
try { |
293 |
1 |
mocker.getComponentUnderTest().getIconSet("silk"); |
294 |
|
} catch (IconException e) { |
295 |
1 |
caughtException = e; |
296 |
|
} |
297 |
1 |
assertNotNull(caughtException); |
298 |
1 |
assertEquals(exception, caughtException.getCause()); |
299 |
1 |
assertEquals("Failed to load the icon set [silk].", caughtException.getMessage()); |
300 |
|
} |
301 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
302 |
1 |
@Test... |
303 |
|
public void getDefaultIconSet() throws Exception |
304 |
|
{ |
305 |
|
|
306 |
1 |
IconSet iconSet = new IconSet("default"); |
307 |
1 |
when(iconSetLoader.loadIconSet(any(Reader.class), eq("default"))).thenReturn(iconSet); |
308 |
1 |
InputStream is = getClass().getResourceAsStream("/test.iconset"); |
309 |
1 |
when(xwiki.getResourceAsStream("/resources/icons/default.iconset")).thenReturn(is); |
310 |
|
|
311 |
|
|
312 |
1 |
assertEquals(iconSet, mocker.getComponentUnderTest().getIconSet("default")); |
313 |
|
|
314 |
|
|
315 |
1 |
verifyZeroInteractions(queryManager); |
316 |
|
} |
317 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
318 |
1 |
@Test... |
319 |
|
public void getIconSetNames() throws Exception |
320 |
|
{ |
321 |
|
|
322 |
1 |
Query query = mock(Query.class); |
323 |
1 |
when(queryManager.createQuery("SELECT obj.name FROM Document doc, doc.object(IconThemesCode.IconThemeClass) obj" |
324 |
|
+ " ORDER BY obj.name", Query.XWQL)).thenReturn(query); |
325 |
1 |
List<String> results = new ArrayList<>(); |
326 |
1 |
when(query.<String>execute()).thenReturn(results); |
327 |
|
|
328 |
|
|
329 |
1 |
assertTrue(results == mocker.getComponentUnderTest().getIconSetNames()); |
330 |
|
} |
331 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1PASS
|
|
332 |
1 |
@Test... |
333 |
|
public void getIconSetNamesWhenException() throws Exception |
334 |
|
{ |
335 |
|
|
336 |
1 |
QueryException exception = new QueryException("exception in the query", null, null); |
337 |
1 |
when(queryManager.createQuery(any(), eq(Query.XWQL))).thenThrow(exception); |
338 |
|
|
339 |
|
|
340 |
1 |
IconException caughtException = null; |
341 |
1 |
try { |
342 |
1 |
mocker.getComponentUnderTest().getIconSetNames(); |
343 |
|
} catch (IconException e) { |
344 |
1 |
caughtException = e; |
345 |
|
} |
346 |
|
|
347 |
|
|
348 |
1 |
assertNotNull(caughtException); |
349 |
1 |
assertEquals("Failed to get the name of all icon sets.", caughtException.getMessage()); |
350 |
1 |
assertEquals(exception, caughtException.getCause()); |
351 |
|
} |
352 |
|
} |