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

File DefaultIconSetManagerTest.java

 

Code metrics

0
116
14
1
352
237
17
0.15
8.29
14
1.21

Classes

Class Line # Actions
DefaultIconSetManagerTest 71 116 0% 17 0
1.0100%
 

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.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    * Test class for {@link org.xwiki.icon.internal.DefaultIconSetManager}.
67    *
68    * @since 6.2M1
69    * @version $Id: 617664618100d1458b4197d99005f250a22bf392 $
70    */
 
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   
 
97  13 toggle @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   
 
116  1 toggle @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    // Test
129  1 IconSet result = mocker.getComponentUnderTest().getCurrentIconSet();
130   
131    // Verify
132  1 assertEquals(iconSet, result);
133  1 verify(iconSetCache).put(iconThemeRef, iconSet);
134  1 verify(iconSetCache).put(currentIconTheme, "currentWikiId", iconSet);
135    }
136   
 
137  1 toggle @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    // Test
150  1 IconSet result = mocker.getComponentUnderTest().getCurrentIconSet();
151   
152    // Verify
153  1 assertEquals(iconSet, result);
154  1 verify(iconSetLoader, never()).loadIconSet(any(DocumentReference.class));
155    }
156   
 
157  1 toggle @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    // Test
169  1 IconSet result = mocker.getComponentUnderTest().getCurrentIconSet();
170   
171    // Verify
172  1 assertNull(result);
173  1 verify(iconSetLoader, never()).loadIconSet(any(DocumentReference.class));
174    }
175   
 
176  1 toggle @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    // Test
186  1 IconSet result = mocker.getComponentUnderTest().getDefaultIconSet();
187   
188    // Verify
189  1 assertEquals(iconSet, result);
190  1 verify(iconSetCache).put("default", iconSet);
191    }
192   
 
193  1 toggle @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    // Test
200  1 IconSet result = mocker.getComponentUnderTest().getDefaultIconSet();
201   
202    // Verify
203  1 assertEquals(iconSet, result);
204  1 verify(iconSetLoader, never()).loadIconSet(any(InputStreamReader.class), any());
205    }
206   
 
207  1 toggle @Test
208    public void getDefaultIconWithException() throws Exception
209    {
210    // Mocks
211  1 Exception exception = new MalformedURLException();
212  1 when(xwiki.getResourceAsStream(any())).thenThrow(exception);
213   
214    // Test
215  1 Exception exceptionCaught = null;
216  1 try {
217  1 mocker.getComponentUnderTest().getDefaultIconSet();
218    } catch (IconException e) {
219  1 exceptionCaught = e;
220    }
221   
222    // Verify
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   
 
228  1 toggle @Test
229    public void getIconSetWhenInCache() throws Exception
230    {
231    // Mocks
232  1 IconSet iconSet = new IconSet("silk");
233  1 when(iconSetCache.get("silk", "currentWikiId")).thenReturn(iconSet);
234   
235    // Test
236  1 assertEquals(iconSet, mocker.getComponentUnderTest().getIconSet("silk"));
237   
238    // Verify
239  1 verify(iconSetCache, never()).put(anyString(), any(IconSet.class));
240    }
241   
 
242  1 toggle @Test
243    public void getIconSetWhenNotInCache() throws Exception
244    {
245    // Mocks
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    // Test
258  1 assertEquals(iconSet, mocker.getComponentUnderTest().getIconSet("silk"));
259   
260    // Verify
261  1 verify(query).bindValue("name", "silk");
262  1 verify(iconSetCache).put(documentReference, iconSet);
263  1 verify(iconSetCache).put("silk", "currentWikiId", iconSet);
264    }
265   
 
266  1 toggle @Test
267    public void getIconSetWhenDoesNotExists() throws Exception
268    {
269    // Mocks
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    // Test
277  1 assertNull(mocker.getComponentUnderTest().getIconSet("silk"));
278   
279    // Verify
280  1 verify(query).bindValue("name", "silk");
281    }
282   
 
283  1 toggle @Test
284    public void getIconSetWhenException() throws Exception
285    {
286    // Mocks
287  1 Exception exception = new QueryException("exception in the query", null, null);
288  1 when(queryManager.createQuery(any(), any())).thenThrow(exception);
289   
290    // Test
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   
 
302  1 toggle @Test
303    public void getDefaultIconSet() throws Exception
304    {
305    // Mock
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    // Test
312  1 assertEquals(iconSet, mocker.getComponentUnderTest().getIconSet("default"));
313   
314    // Verify
315  1 verifyZeroInteractions(queryManager);
316    }
317   
 
318  1 toggle @Test
319    public void getIconSetNames() throws Exception
320    {
321    // Mocks
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    // Test
329  1 assertTrue(results == mocker.getComponentUnderTest().getIconSetNames());
330    }
331   
 
332  1 toggle @Test
333    public void getIconSetNamesWhenException() throws Exception
334    {
335    // Mocks
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    // Test
340  1 IconException caughtException = null;
341  1 try {
342  1 mocker.getComponentUnderTest().getIconSetNames();
343    } catch (IconException e) {
344  1 caughtException = e;
345    }
346   
347    // Verify
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    }