1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.velocity.internal

File DefaultVelocityContextFactoryTest.java

 

Code metrics

0
14
2
1
96
55
2
0.14
7
2
1

Classes

Class Line # Actions
DefaultVelocityContextFactoryTest 52 14 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;
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.jupiter.api.Test;
28    import org.xwiki.component.manager.ComponentManager;
29    import org.xwiki.test.annotation.BeforeComponent;
30    import org.xwiki.test.junit5.mockito.ComponentTest;
31    import org.xwiki.test.junit5.mockito.InjectMockComponents;
32    import org.xwiki.test.junit5.mockito.MockComponent;
33    import org.xwiki.velocity.VelocityConfiguration;
34    import org.xwiki.velocity.VelocityContextInitializer;
35   
36    import static org.junit.jupiter.api.Assertions.assertNotNull;
37    import static org.junit.jupiter.api.Assertions.assertNotSame;
38    import static org.junit.jupiter.api.Assertions.assertNull;
39    import static org.junit.jupiter.api.Assertions.assertSame;
40    import static org.mockito.ArgumentMatchers.any;
41    import static org.mockito.Mockito.mock;
42    import static org.mockito.Mockito.times;
43    import static org.mockito.Mockito.verify;
44    import static org.mockito.Mockito.when;
45   
46    /**
47    * Unit tests for {@link DefaultVelocityContextFactory}.
48    *
49    * @version $Id: febcf5600147d413b77430b12aacdc3625499e98 $
50    */
51    @ComponentTest
 
52    public class DefaultVelocityContextFactoryTest
53    {
54    @MockComponent
55    private VelocityConfiguration configuration;
56   
57    @MockComponent
58    private ComponentManager componentManager;
59   
60    @InjectMockComponents
61    private DefaultVelocityContextFactory factory;
62   
 
63  1 toggle @BeforeComponent
64    public void configure() throws Exception
65    {
66  1 Properties properties = new Properties();
67  1 properties.put("listtool", ListTool.class.getName());
68  1 when(this.configuration.getTools()).thenReturn(properties);
69    }
70   
71    /**
72    * Verify that we get different contexts when we call the createContext method but that they contain the same
73    * references to the Velocity tools. Also tests that objects we put in one context are not shared with other
74    * contexts. Also verifies that Velocity Context Initializers are called.
75    */
 
76  1 toggle @Test
77    public void createDifferentContext() throws Exception
78    {
79    // We also verify that the VelocityContextInitializers are called.
80  1 VelocityContextInitializer mockInitializer = mock(VelocityContextInitializer.class);
81  1 when(this.componentManager.getInstanceList(VelocityContextInitializer.class)).thenReturn(
82    Arrays.asList(mockInitializer));
83   
84  1 VelocityContext context1 = this.factory.createContext();
85  1 context1.put("param", "value");
86  1 VelocityContext context2 = this.factory.createContext();
87   
88  1 verify(mockInitializer, times(2)).initialize(any(VelocityContext.class));
89  1 verify(this.componentManager, times(2)).getInstanceList(VelocityContextInitializer.class);
90   
91  1 assertNotSame(context1, context2);
92  1 assertNotNull(context1.get("listtool"));
93  1 assertSame(context2.get("listtool"), context1.get("listtool"));
94  1 assertNull(context2.get("param"));
95    }
96    }