1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
46 |
|
|
47 |
|
@since |
48 |
|
@version |
49 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (35) |
Complexity: 7 |
Complexity Density: 0.23 |
|
50 |
|
public class VelocityRendererTest |
51 |
|
{ |
52 |
|
@Rule |
53 |
|
public MockitoComponentMockingRule<VelocityRenderer> mocker = |
54 |
|
new MockitoComponentMockingRule<>(VelocityRenderer.class); |
55 |
|
|
56 |
|
private VelocityManager velocityManager; |
57 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
58 |
3 |
@Before... |
59 |
|
public void setUp() throws Exception |
60 |
|
{ |
61 |
3 |
velocityManager = mocker.getInstance(VelocityManager.class); |
62 |
|
} |
63 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
64 |
1 |
@Test... |
65 |
|
public void renderTest() throws Exception |
66 |
|
{ |
67 |
|
|
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 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
73 |
1 |
@Override... |
74 |
|
public Object answer(InvocationOnMock invocation) throws Throwable |
75 |
|
{ |
76 |
|
|
77 |
1 |
Writer writer = (Writer) invocation.getArguments()[1]; |
78 |
1 |
writer.write("Rendered code"); |
79 |
1 |
return true; |
80 |
|
} |
81 |
|
}); |
82 |
|
|
83 |
|
|
84 |
1 |
assertEquals("Rendered code", mocker.getComponentUnderTest().render("myCode")); |
85 |
|
|
86 |
|
|
87 |
1 |
verify(engine).startedUsingMacroNamespace("IconVelocityRenderer_" + Thread.currentThread().getId()); |
88 |
1 |
verify(engine).stoppedUsingMacroNamespace("IconVelocityRenderer_" + Thread.currentThread().getId()); |
89 |
|
} |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1PASS
|
|
91 |
1 |
@Test... |
92 |
|
public void renderWithException() throws Exception |
93 |
|
{ |
94 |
|
|
95 |
1 |
Exception exception = new XWikiVelocityException("exception"); |
96 |
1 |
when(velocityManager.getVelocityEngine()).thenThrow(exception); |
97 |
|
|
98 |
|
|
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 |
|
|
107 |
1 |
assertNotNull(caughtException); |
108 |
1 |
assertEquals("Failed to render the icon.", caughtException.getMessage()); |
109 |
1 |
assertEquals(exception, caughtException.getCause()); |
110 |
|
} |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
1PASS
|
|
112 |
1 |
@Test... |
113 |
|
public void renderWhenEvaluateReturnsFalse() throws Exception |
114 |
|
{ |
115 |
|
|
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 |
|
|
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 |
|
|
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 |
|
} |