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

File DefaultIconSetManagerTest.java

 

Code metrics

0
127
15
1
377
255
18
0.14
8.47
15
1.2

Classes

Class Line # Actions
DefaultIconSetManagerTest 73 127 0% 18 0
1.0100%
 

Contributing tests

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