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.jmx; |
21 |
|
|
22 |
|
import java.io.StringWriter; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.HashMap; |
25 |
|
import java.util.Map; |
26 |
|
import java.util.Properties; |
27 |
|
|
28 |
|
import javax.management.openmbean.CompositeData; |
29 |
|
import javax.management.openmbean.TabularData; |
30 |
|
|
31 |
|
import org.apache.velocity.VelocityContext; |
32 |
|
import org.junit.Assert; |
33 |
|
import org.junit.Before; |
34 |
|
import org.junit.Rule; |
35 |
|
import org.junit.Test; |
36 |
|
import org.xwiki.test.annotation.ComponentList; |
37 |
|
import org.xwiki.test.mockito.MockitoComponentManagerRule; |
38 |
|
import org.xwiki.velocity.VelocityConfiguration; |
39 |
|
import org.xwiki.velocity.VelocityEngine; |
40 |
|
import org.xwiki.velocity.internal.DefaultVelocityContextFactory; |
41 |
|
import org.xwiki.velocity.internal.DefaultVelocityEngine; |
42 |
|
|
43 |
|
import static org.mockito.Mockito.when; |
44 |
|
|
45 |
|
|
46 |
|
@link |
47 |
|
|
48 |
|
@version |
49 |
|
@since |
50 |
|
|
51 |
|
@ComponentList({DefaultVelocityEngine.class, DefaultVelocityContextFactory.class}) |
|
|
| 100% |
Uncovered Elements: 0 (30) |
Complexity: 2 |
Complexity Density: 0.07 |
|
52 |
|
public class JMXVelocityEngineTest |
53 |
|
{ |
54 |
|
@Rule |
55 |
|
public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule(); |
56 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
57 |
1 |
@Before... |
58 |
|
public void setUp() throws Exception |
59 |
|
{ |
60 |
1 |
VelocityConfiguration vc = this.componentManager.registerMockComponent(VelocityConfiguration.class); |
61 |
1 |
Properties props = new Properties(); |
62 |
1 |
props.setProperty("velocimacro.permissions.allow.inline.local.scope", Boolean.TRUE.toString()); |
63 |
1 |
when(vc.getProperties()).thenReturn(props); |
64 |
1 |
when(vc.getTools()).thenReturn(new Properties()); |
65 |
|
} |
66 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (23) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
67 |
1 |
@Test... |
68 |
|
public void testGetTemplates() throws Exception |
69 |
|
{ |
70 |
1 |
VelocityEngine engine = this.componentManager.getInstance(VelocityEngine.class); |
71 |
1 |
engine.initialize(new Properties()); |
72 |
1 |
JMXVelocityEngine jmxBean = new JMXVelocityEngine(engine); |
73 |
|
|
74 |
1 |
TabularData data = jmxBean.getTemplates(); |
75 |
|
|
76 |
1 |
Assert.assertEquals(1, data.values().size()); |
77 |
1 |
CompositeData cd = ((CompositeData) data.values().iterator().next()); |
78 |
1 |
Assert.assertEquals("<global>", cd.get("templateName")); |
79 |
1 |
Assert.assertEquals(0, ((String[]) cd.get("macroNames")).length); |
80 |
|
|
81 |
1 |
engine.startedUsingMacroNamespace("testmacronamespace"); |
82 |
|
|
83 |
1 |
try { |
84 |
1 |
StringWriter out = new StringWriter(); |
85 |
1 |
engine.evaluate(new VelocityContext(), out, "testmacronamespace", "#macro(testmacro)#end"); |
86 |
1 |
data = jmxBean.getTemplates(); |
87 |
|
|
88 |
1 |
Assert.assertEquals(2, data.values().size()); |
89 |
1 |
Map<String, String[]> retrievedData = new HashMap<String, String[]>(); |
90 |
1 |
for (CompositeData cdata : (Collection<CompositeData>) data.values()) { |
91 |
2 |
retrievedData.put((String) cdata.get("templateName"), (String[]) cdata.get("macroNames")); |
92 |
|
} |
93 |
1 |
Assert.assertEquals(0, retrievedData.get("<global>").length); |
94 |
|
|
95 |
1 |
String[] namespace = retrievedData.get(Thread.currentThread().getId() + ":testmacronamespace"); |
96 |
1 |
Assert.assertNotNull(namespace); |
97 |
1 |
Assert.assertEquals(1, namespace.length); |
98 |
1 |
Assert.assertEquals("testmacro", namespace[0]); |
99 |
|
} finally { |
100 |
1 |
engine.stoppedUsingMacroNamespace("testmacronamespace"); |
101 |
|
} |
102 |
|
} |
103 |
|
} |