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

File JMXVelocityEngineTest.java

 

Code metrics

0
28
2
1
103
65
2
0.07
14
2
1

Classes

Class Line # Actions
JMXVelocityEngineTest 52 28 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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.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    * Unit tests for {@link JMXVelocityEngine}.
47    *
48    * @version $Id: 7e9e01e8ce5d873db081409b9efb956b730c1237 $
49    * @since 2.4M2
50    */
51    @ComponentList({DefaultVelocityEngine.class, DefaultVelocityContextFactory.class})
 
52    public class JMXVelocityEngineTest
53    {
54    @Rule
55    public MockitoComponentManagerRule componentManager = new MockitoComponentManagerRule();
56   
 
57  1 toggle @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   
 
67  1 toggle @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    }