1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.velocity.internal; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.Properties; |
24 |
|
|
25 |
|
import org.apache.velocity.VelocityContext; |
26 |
|
import org.apache.velocity.tools.generic.ListTool; |
27 |
|
import org.junit.Assert; |
28 |
|
import org.junit.Before; |
29 |
|
import org.junit.Rule; |
30 |
|
import org.junit.Test; |
31 |
|
import org.xwiki.component.manager.ComponentManager; |
32 |
|
import org.xwiki.test.annotation.AfterComponent; |
33 |
|
import org.xwiki.test.mockito.MockitoComponentMockingRule; |
34 |
|
import org.xwiki.velocity.VelocityConfiguration; |
35 |
|
import org.xwiki.velocity.VelocityContextFactory; |
36 |
|
import org.xwiki.velocity.VelocityContextInitializer; |
37 |
|
|
38 |
|
import static org.mockito.ArgumentMatchers.any; |
39 |
|
import static org.mockito.Mockito.mock; |
40 |
|
import static org.mockito.Mockito.times; |
41 |
|
import static org.mockito.Mockito.verify; |
42 |
|
import static org.mockito.Mockito.when; |
43 |
|
|
44 |
|
|
45 |
|
@link |
46 |
|
|
47 |
|
@version |
48 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 3 |
Complexity Density: 0.17 |
|
49 |
|
public class DefaultVelocityContextFactoryTest |
50 |
|
{ |
51 |
|
@Rule |
52 |
|
public MockitoComponentMockingRule<DefaultVelocityContextFactory> mocker = |
53 |
|
new MockitoComponentMockingRule(DefaultVelocityContextFactory.class); |
54 |
|
|
55 |
|
private VelocityContextFactory factory; |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
57 |
1 |
@Before... |
58 |
|
public void configure() throws Exception |
59 |
|
{ |
60 |
1 |
VelocityConfiguration configuration = this.mocker.getInstance(VelocityConfiguration.class); |
61 |
1 |
Properties properties = new Properties(); |
62 |
1 |
properties.put("listtool", ListTool.class.getName()); |
63 |
1 |
when(configuration.getTools()).thenReturn(properties); |
64 |
|
|
65 |
1 |
this.factory = this.mocker.getInstance(VelocityContextFactory.class); |
66 |
|
} |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
68 |
1 |
@AfterComponent... |
69 |
|
public void overrideComponents() throws Exception |
70 |
|
{ |
71 |
1 |
this.mocker.registerMockComponent(ComponentManager.class); |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
79 |
1 |
@Test... |
80 |
|
public void createDifferentContext() throws Exception |
81 |
|
{ |
82 |
|
|
83 |
1 |
VelocityContextInitializer mockInitializer = mock(VelocityContextInitializer.class); |
84 |
1 |
ComponentManager mockComponentManager = this.mocker.getInstance(ComponentManager.class); |
85 |
1 |
when(mockComponentManager.getInstanceList(VelocityContextInitializer.class)).thenReturn( |
86 |
|
Arrays.<Object>asList(mockInitializer)); |
87 |
|
|
88 |
1 |
VelocityContext context1 = this.factory.createContext(); |
89 |
1 |
context1.put("param", "value"); |
90 |
1 |
VelocityContext context2 = this.factory.createContext(); |
91 |
|
|
92 |
1 |
verify(mockInitializer, times(2)).initialize(any(VelocityContext.class)); |
93 |
1 |
verify(mockComponentManager, times(2)).getInstanceList(VelocityContextInitializer.class); |
94 |
|
|
95 |
1 |
Assert.assertNotSame(context1, context2); |
96 |
1 |
Assert.assertNotNull(context1.get("listtool")); |
97 |
1 |
Assert.assertSame(context2.get("listtool"), context1.get("listtool")); |
98 |
1 |
Assert.assertNull(context2.get("param")); |
99 |
|
} |
100 |
|
} |