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

File DefaultIconRendererTest.java

 

Code metrics

0
81
11
1
242
156
12
0.15
7.36
11
1.09

Classes

Class Line # Actions
DefaultIconRendererTest 48 81 0% 12 0
1.0100%
 

Contributing tests

This file is covered by 10 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.util.HashMap;
23    import java.util.Map;
24   
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.xwiki.icon.Icon;
29    import org.xwiki.icon.IconException;
30    import org.xwiki.icon.IconSet;
31    import org.xwiki.skinx.SkinExtension;
32    import org.xwiki.test.mockito.MockitoComponentMockingRule;
33   
34    import static org.junit.Assert.assertEquals;
35    import static org.junit.Assert.assertNotNull;
36    import static org.mockito.ArgumentMatchers.any;
37    import static org.mockito.ArgumentMatchers.eq;
38    import static org.mockito.Mockito.never;
39    import static org.mockito.Mockito.verify;
40    import static org.mockito.Mockito.when;
41   
42    /**
43    * Test class for {@link org.xwiki.icon.internal.DefaultIconRenderer}.
44    *
45    * @since 6.2M1
46    * @version $Id: c13011601d58a05cf43612950265ab33c70982ea $
47    */
 
48    public class DefaultIconRendererTest
49    {
50    @Rule
51    public MockitoComponentMockingRule<DefaultIconRenderer> mocker =
52    new MockitoComponentMockingRule<>(DefaultIconRenderer.class);
53   
54    private SkinExtension skinExtension;
55   
56    private SkinExtension linkExtension;
57   
58    private SkinExtension jsExtension;
59   
60    private VelocityRenderer velocityRenderer;
61   
 
62  10 toggle @Before
63    public void setUp() throws Exception
64    {
65  10 skinExtension = mocker.getInstance(SkinExtension.class, "ssx");
66  10 linkExtension = mocker.getInstance(SkinExtension.class, "linkx");
67  10 jsExtension = mocker.getInstance(SkinExtension.class, "jsx");
68  10 velocityRenderer = mocker.getInstance(VelocityRenderer.class);
69    }
70   
 
71  1 toggle @Test
72    public void render() throws Exception
73    {
74  1 IconSet iconSet = new IconSet("default");
75  1 iconSet.setRenderWiki("image:$icon.png");
76  1 iconSet.addIcon("test", new Icon("blabla"));
77  1 when(velocityRenderer.render("#set($icon = \"blabla\")\nimage:$icon.png")).thenReturn("image:blabla.png");
78   
79    // Test
80  1 String result = mocker.getComponentUnderTest().render("test", iconSet);
81   
82    // Verify
83  1 assertEquals("image:blabla.png", result);
84  1 verify(linkExtension, never()).use(any());
85  1 verify(skinExtension, never()).use(any());
86  1 verify(jsExtension, never()).use(any());
87    }
88   
 
89  1 toggle @Test
90    public void renderWithCSS() throws Exception
91    {
92  1 IconSet iconSet = new IconSet("default");
93  1 iconSet.setCss("css");
94  1 iconSet.addIcon("test", new Icon("blabla"));
95  1 when(velocityRenderer.render("css")).thenReturn("velocityParsedCSS");
96   
97    // Test
98  1 mocker.getComponentUnderTest().render("test", iconSet);
99   
100    // Verify
101  1 Map<String, Object> parameters = new HashMap();
102  1 parameters.put("rel", "stylesheet");
103  1 verify(linkExtension).use(eq("velocityParsedCSS"), eq(parameters));
104  1 verify(skinExtension, never()).use(any());
105  1 verify(jsExtension, never()).use(any());
106    }
107   
 
108  1 toggle @Test
109    public void renderWithSSX() throws Exception
110    {
111  1 IconSet iconSet = new IconSet("default");
112  1 iconSet.setSsx("ssx");
113  1 iconSet.addIcon("test", new Icon("blabla"));
114   
115    // Test
116  1 mocker.getComponentUnderTest().render("test", iconSet);
117   
118    // Verify
119  1 verify(skinExtension).use("ssx");
120  1 verify(linkExtension, never()).use(any());
121  1 verify(jsExtension, never()).use(any());
122    }
123   
 
124  1 toggle @Test
125    public void renderWithJSX() throws Exception
126    {
127  1 IconSet iconSet = new IconSet("default");
128  1 iconSet.setJsx("jsx");
129  1 iconSet.addIcon("test", new Icon("blabla"));
130   
131    // Test
132  1 mocker.getComponentUnderTest().render("test", iconSet);
133   
134    // Verify
135  1 verify(jsExtension).use("jsx");
136  1 verify(linkExtension, never()).use(any());
137  1 verify(skinExtension, never()).use(any());
138    }
139   
 
140  1 toggle @Test
141    public void renderHTML() throws Exception
142    {
143  1 IconSet iconSet = new IconSet("default");
144  1 iconSet.setRenderHTML("<img src=\"$icon.png\" />");
145  1 iconSet.addIcon("test", new Icon("blabla"));
146   
147  1 when(velocityRenderer.render("#set($icon = \"blabla\")\n<img src=\"$icon.png\" />"))
148    .thenReturn("<img src=\"blabla.png\" />");
149   
150    // Test
151  1 String result = mocker.getComponentUnderTest().renderHTML("test", iconSet);
152   
153    // Verify
154  1 assertEquals("<img src=\"blabla.png\" />", result);
155    }
156   
 
157  1 toggle @Test
158    public void renderHTMLWithCSS() throws Exception
159    {
160  1 IconSet iconSet = new IconSet("default");
161  1 iconSet.setCss("css");
162  1 iconSet.addIcon("test", new Icon("blabla"));
163  1 when(velocityRenderer.render("css")).thenReturn("velocityParsedCSS");
164   
165    // Test
166  1 mocker.getComponentUnderTest().renderHTML("test", iconSet);
167   
168    // Verify
169  1 Map<String, Object> parameters = new HashMap();
170  1 parameters.put("rel", "stylesheet");
171  1 verify(linkExtension).use(eq("velocityParsedCSS"), eq(parameters));
172  1 verify(skinExtension, never()).use(any());
173  1 verify(jsExtension, never()).use(any());
174    }
175   
 
176  1 toggle @Test
177    public void renderHTMLWithSSX() throws Exception
178    {
179  1 IconSet iconSet = new IconSet("default");
180  1 iconSet.setSsx("ssx");
181  1 iconSet.addIcon("test", new Icon("blabla"));
182   
183    // Test
184  1 mocker.getComponentUnderTest().renderHTML("test", iconSet);
185   
186    // Verify
187  1 verify(skinExtension).use("ssx");
188  1 verify(linkExtension, never()).use(any());
189  1 verify(jsExtension, never()).use(any());
190    }
191   
 
192  1 toggle @Test
193    public void renderHTMLWithJSX() throws Exception
194    {
195  1 IconSet iconSet = new IconSet("default");
196  1 iconSet.setJsx("jsx");
197  1 iconSet.addIcon("test", new Icon("blabla"));
198   
199    // Test
200  1 mocker.getComponentUnderTest().renderHTML("test", iconSet);
201   
202    // Verify
203  1 verify(jsExtension).use("jsx");
204  1 verify(linkExtension, never()).use(any());
205  1 verify(skinExtension, never()).use(any());
206    }
207   
 
208  1 toggle @Test
209    public void renderNonExistentIcon() throws Exception
210    {
211  1 IconSet iconSet = new IconSet("default");
212   
213    // Test
214  1 String result = mocker.getComponentUnderTest().render("non-existent-icon", iconSet);
215   
216    // Verify
217  1 assertEquals("", result);
218    }
219   
 
220  1 toggle @Test
221    public void renderWithException() throws Exception
222    {
223  1 IconSet iconSet = new IconSet("default");
224  1 iconSet.setRenderWiki("image:$icon.png");
225  1 iconSet.addIcon("test", new Icon("blabla"));
226  1 IconException exception = new IconException("exception", null);
227  1 when(velocityRenderer.render(any())).thenThrow(exception);
228   
229    // Test
230  1 IconException caughtException = null;
231  1 try {
232  1 mocker.getComponentUnderTest().render("test", iconSet);
233    } catch (IconException e) {
234  1 caughtException = e;
235    }
236   
237    // Verify
238  1 assertNotNull(caughtException);
239  1 assertEquals(exception, caughtException);
240    }
241   
242    }