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

File DefaultIconSetLoaderTest.java

 

Code metrics

4
46
6
1
158
106
10
0.22
7.67
6
1.67

Classes

Class Line # Actions
DefaultIconSetLoaderTest 53 46 0% 10 2
0.9642857396.4%
 

Contributing tests

This file is covered by 4 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.IOException;
23    import java.io.InputStreamReader;
24    import java.io.Reader;
25    import java.io.StringWriter;
26   
27    import org.apache.commons.io.IOUtils;
28    import org.junit.Before;
29    import org.junit.Rule;
30    import org.junit.Test;
31    import org.xwiki.bridge.DocumentAccessBridge;
32    import org.xwiki.bridge.DocumentModelBridge;
33    import org.xwiki.icon.IconException;
34    import org.xwiki.icon.IconSet;
35    import org.xwiki.icon.IconType;
36    import org.xwiki.model.reference.DocumentReference;
37    import org.xwiki.test.mockito.MockitoComponentMockingRule;
38    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
39   
40    import static org.junit.Assert.assertEquals;
41    import static org.junit.Assert.assertNotNull;
42    import static org.mockito.ArgumentMatchers.any;
43    import static org.mockito.ArgumentMatchers.eq;
44    import static org.mockito.Mockito.mock;
45    import static org.mockito.Mockito.when;
46   
47    /**
48    * Test class for {@link org.xwiki.icon.internal.DefaultIconSetLoader}.
49    *
50    * @since 6.2M1
51    * @version $Id: be480c6f767801dec50d5493572fda0581940e9b $
52    */
 
53    public class DefaultIconSetLoaderTest
54    {
55    @Rule
56    public MockitoComponentMockingRule<DefaultIconSetLoader> mocker =
57    new MockitoComponentMockingRule<>(DefaultIconSetLoader.class);
58   
59    private DocumentAccessBridge documentAccessBridge;
60   
61    private WikiDescriptorManager wikiDescriptorManager;
62   
 
63  4 toggle @Before
64    public void setUp() throws Exception
65    {
66  4 documentAccessBridge = mocker.getInstance(DocumentAccessBridge.class);
67  4 wikiDescriptorManager = mocker.getInstance(WikiDescriptorManager.class);
68  4 when(wikiDescriptorManager.getCurrentWikiId()).thenReturn("wikiId");
69    }
70   
 
71  2 toggle private void verifies(IconSet result) throws Exception
72    {
73  2 assertNotNull(result);
74  2 assertEquals("http://url_to_css", result.getCss());
75  2 assertEquals("IconThemes.Default", result.getSsx());
76  2 assertEquals("IconThemes.JS", result.getJsx());
77  2 assertEquals("{{html clean=\"false\"}}<span class=\"fa fa-$icon\"></span>{{/html}}", result.getRenderWiki());
78  2 assertEquals("<span class=\"fa fa-$icon\"></span>", result.getRenderHTML());
79  2 assertEquals("anchor", result.getIcon("transmit").getValue());
80  2 assertEquals("globe", result.getIcon("earth").getValue());
81  2 assertEquals(IconType.FONT, result.getType());
82    }
83   
 
84  1 toggle @Test
85    public void loadIconSet() throws Exception
86    {
87  1 Reader content = new InputStreamReader(getClass().getResourceAsStream("/test.iconset"));
88   
89    // Test
90  1 IconSet result = mocker.getComponentUnderTest().loadIconSet(content, "FontAwesome");
91   
92    // Verify
93  1 verifies(result);
94  1 assertEquals("FontAwesome", result.getName());
95    }
96   
 
97  1 toggle @Test
98    public void loadIconSetFromWikiDocument() throws Exception
99    {
100  1 DocumentReference iconSetRef = new DocumentReference("xwiki", "IconThemes", "Default");
101  1 DocumentReference iconClassRef = new DocumentReference("wikiId", "IconThemesCode", "IconThemeClass");
102  1 when(documentAccessBridge.getProperty(eq(iconSetRef), eq(iconClassRef), eq("name"))).thenReturn("MyIconTheme");
103  1 DocumentModelBridge doc = mock(DocumentModelBridge.class);
104  1 when(documentAccessBridge.getDocument(iconSetRef)).thenReturn(doc);
105   
106  1 StringWriter content = new StringWriter();
107  1 IOUtils.copyLarge(new InputStreamReader(getClass().getResourceAsStream("/test.iconset")), content);
108  1 when(doc.getContent()).thenReturn(content.toString());
109   
110    // Test
111  1 IconSet result = mocker.getComponentUnderTest().loadIconSet(iconSetRef);
112   
113    // Verify
114  1 verifies(result);
115  1 assertEquals("MyIconTheme", result.getName());
116    }
117   
 
118  1 toggle @Test
119    public void loadIconSetWithException() throws Exception
120    {
121  1 Reader content = mock(Reader.class);
122  1 IOException exception = new IOException("test");
123  1 when(content.read(any(char[].class))).thenThrow(exception);
124   
125    // Test
126  1 Exception caughException = null;
127  1 try {
128  1 mocker.getComponentUnderTest().loadIconSet(content, "FontAwesome");
129    } catch (IconException e) {
130  1 caughException = e;
131    }
132   
133  1 assertNotNull(caughException);
134  1 assert(caughException instanceof IconException);
135  1 assertEquals(exception, caughException.getCause());
136  1 assertEquals("Failed to load the IconSet [FontAwesome].", caughException.getMessage());
137    }
138   
 
139  1 toggle @Test
140    public void loadIconSetFromWikiDocumentWithException() throws Exception
141    {
142  1 Exception exception = new Exception("test");
143  1 when(documentAccessBridge.getDocument(any(DocumentReference.class))).thenThrow(exception);
144   
145    // Test
146  1 Exception caughException = null;
147  1 try {
148  1 mocker.getComponentUnderTest().loadIconSet(new DocumentReference("a", "b", "c"));
149    } catch (IconException e) {
150  1 caughException = e;
151    }
152   
153  1 assertNotNull(caughException);
154  1 assert(caughException instanceof IconException);
155  1 assertEquals(exception, caughException.getCause());
156  1 assertEquals("Failed to load the IconSet [a:b.c].", caughException.getMessage());
157    }
158    }