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

File DefaultVelocityContextFactoryTest.java

 

Code metrics

0
18
3
1
100
58
3
0.17
6
3
1

Classes

Class Line # Actions
DefaultVelocityContextFactoryTest 49 18 0% 3 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;
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    * Unit tests for {@link DefaultVelocityContextFactory}.
46    *
47    * @version $Id: 9ea3e6fe8e54a675ce81d229a84b55a9654c2020 $
48    */
 
49    public class DefaultVelocityContextFactoryTest
50    {
51    @Rule
52    public MockitoComponentMockingRule<DefaultVelocityContextFactory> mocker =
53    new MockitoComponentMockingRule(DefaultVelocityContextFactory.class);
54   
55    private VelocityContextFactory factory;
56   
 
57  1 toggle @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   
 
68  1 toggle @AfterComponent
69    public void overrideComponents() throws Exception
70    {
71  1 this.mocker.registerMockComponent(ComponentManager.class);
72    }
73   
74    /**
75    * Verify that we get different contexts when we call the createContext method but that they contain the same
76    * references to the Velocity tools. Also tests that objects we put in one context are not shared with other
77    * contexts. Also verifies that Velocity Context Initializers are called.
78    */
 
79  1 toggle @Test
80    public void createDifferentContext() throws Exception
81    {
82    // We also verify that the VelocityContextInitializers are called.
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    }