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

File VelocityRendererTest.java

 

Code metrics

0
30
5
1
137
85
7
0.23
6
5
1.4

Classes

Class Line # Actions
VelocityRendererTest 50 30 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 3 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.Writer;
23   
24    import org.apache.velocity.VelocityContext;
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.junit.Test;
28    import org.mockito.invocation.InvocationOnMock;
29    import org.mockito.stubbing.Answer;
30    import org.xwiki.icon.IconException;
31    import org.xwiki.test.mockito.MockitoComponentMockingRule;
32    import org.xwiki.velocity.VelocityEngine;
33    import org.xwiki.velocity.VelocityManager;
34    import org.xwiki.velocity.XWikiVelocityException;
35   
36    import static org.junit.Assert.assertEquals;
37    import static org.junit.Assert.assertNotNull;
38    import static org.mockito.ArgumentMatchers.any;
39    import static org.mockito.ArgumentMatchers.eq;
40    import static org.mockito.Mockito.mock;
41    import static org.mockito.Mockito.verify;
42    import static org.mockito.Mockito.when;
43   
44    /**
45    * Test class for {@link org.xwiki.icon.internal.VelocityRenderer}.
46    *
47    * @since 6.4M1
48    * @version $Id: 2d2d81739cd811f696f372df423a66ac226d5b09 $
49    */
 
50    public class VelocityRendererTest
51    {
52    @Rule
53    public MockitoComponentMockingRule<VelocityRenderer> mocker =
54    new MockitoComponentMockingRule<>(VelocityRenderer.class);
55   
56    private VelocityManager velocityManager;
57   
 
58  3 toggle @Before
59    public void setUp() throws Exception
60    {
61  3 velocityManager = mocker.getInstance(VelocityManager.class);
62    }
63   
 
64  1 toggle @Test
65    public void renderTest() throws Exception
66    {
67    // Mocks
68  1 VelocityEngine engine = mock(VelocityEngine.class);
69  1 when(velocityManager.getVelocityEngine()).thenReturn(engine);
70  1 when(engine.evaluate(any(VelocityContext.class), any(Writer.class), any(), eq("myCode"))).thenAnswer(
71    new Answer<Object>()
72    {
 
73  1 toggle @Override
74    public Object answer(InvocationOnMock invocation) throws Throwable
75    {
76    // Get the writer
77  1 Writer writer = (Writer) invocation.getArguments()[1];
78  1 writer.write("Rendered code");
79  1 return true;
80    }
81    });
82   
83    // Test
84  1 assertEquals("Rendered code", mocker.getComponentUnderTest().render("myCode"));
85   
86    // Verify
87  1 verify(engine).startedUsingMacroNamespace("IconVelocityRenderer_" + Thread.currentThread().getId());
88  1 verify(engine).stoppedUsingMacroNamespace("IconVelocityRenderer_" + Thread.currentThread().getId());
89    }
90   
 
91  1 toggle @Test
92    public void renderWithException() throws Exception
93    {
94    // Mocks
95  1 Exception exception = new XWikiVelocityException("exception");
96  1 when(velocityManager.getVelocityEngine()).thenThrow(exception);
97   
98    // Test
99  1 IconException caughtException = null;
100  1 try {
101  1 mocker.getComponentUnderTest().render("myCode");
102    } catch(IconException e) {
103  1 caughtException = e;
104    }
105   
106    // Verify
107  1 assertNotNull(caughtException);
108  1 assertEquals("Failed to render the icon.", caughtException.getMessage());
109  1 assertEquals(exception, caughtException.getCause());
110    }
111   
 
112  1 toggle @Test
113    public void renderWhenEvaluateReturnsFalse() throws Exception
114    {
115    // Mocks
116  1 VelocityEngine engine = mock(VelocityEngine.class);
117  1 when(velocityManager.getVelocityEngine()).thenReturn(engine);
118  1 when(engine.evaluate(any(VelocityContext.class), any(Writer.class), any(),
119    eq("myCode"))).thenReturn(false);
120   
121    // Test
122  1 IconException caughtException = null;
123  1 try {
124  1 mocker.getComponentUnderTest().render("myCode");
125    } catch(IconException e) {
126  1 caughtException = e;
127    }
128   
129    // Verify
130  1 assertNotNull(caughtException);
131  1 assertEquals("Failed to render the icon. See the Velocity runtime log.", caughtException.getMessage());
132   
133  1 verify(engine).startedUsingMacroNamespace("IconVelocityRenderer_" + Thread.currentThread().getId());
134  1 verify(engine).stoppedUsingMacroNamespace("IconVelocityRenderer_" + Thread.currentThread().getId());
135    }
136   
137    }