1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.render

File DefaultVelocityManagerTest.java

 

Code metrics

0
26
2
1
110
65
2
0.08
13
2
1

Classes

Class Line # Actions
DefaultVelocityManagerTest 52 26 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 com.xpn.xwiki.render;
21   
22    import org.apache.velocity.VelocityContext;
23    import org.junit.Before;
24    import org.junit.Rule;
25    import org.junit.Test;
26    import org.xwiki.component.manager.ComponentLookupException;
27    import org.xwiki.localization.ContextualLocalizationManager;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.script.internal.DefaultScriptContextManager;
30    import org.xwiki.test.annotation.ComponentList;
31    import org.xwiki.test.mockito.MockitoComponentMockingRule;
32    import org.xwiki.velocity.VelocityManager;
33    import org.xwiki.velocity.internal.DefaultVelocityConfiguration;
34    import org.xwiki.velocity.internal.VelocityExecutionContextInitializer;
35   
36    import com.xpn.xwiki.api.Document;
37    import com.xpn.xwiki.doc.XWikiDocument;
38    import com.xpn.xwiki.test.MockitoOldcoreRule;
39   
40    import static org.junit.Assert.assertNotNull;
41    import static org.junit.Assert.assertNotSame;
42    import static org.junit.Assert.assertNull;
43    import static org.junit.Assert.assertSame;
44   
45    /**
46    * Validate {@link DefaultVelocityManager}.
47    *
48    * @version $Id: 5af80cbb99dd5a79b802dbc0b2586b6557708f0d $
49    */
50    @ComponentList(value = { DefaultScriptContextManager.class, XWikiScriptContextInitializer.class,
51    DefaultVelocityConfiguration.class })
 
52    public class DefaultVelocityManagerTest
53    {
54    public MockitoComponentMockingRule<VelocityManager> mocker =
55    new MockitoComponentMockingRule<VelocityManager>(DefaultVelocityManager.class);
56   
57    @Rule
58    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(this.mocker);
59   
 
60  1 toggle @Before
61    public void before() throws Exception
62    {
63  1 this.mocker.registerMockComponent(ContextualLocalizationManager.class);
64   
65  1 this.oldcore.getExecutionContext().setProperty(VelocityExecutionContextInitializer.VELOCITY_CONTEXT_ID,
66    new VelocityContext());
67    }
68   
69    // Tests
70   
 
71  1 toggle @Test
72    public void getVelocityContext() throws ComponentLookupException
73    {
74  1 VelocityContext context = this.mocker.getComponentUnderTest().getVelocityContext();
75   
76  1 assertNull(context.get("doc"));
77  1 assertNull(context.get("sdoc"));
78   
79  1 DocumentReference docReference = new DocumentReference("wiki", "space", "doc");
80  1 DocumentReference sdocReference = new DocumentReference("wiki", "space", "sdoc");
81   
82  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(docReference));
83  1 this.oldcore.getXWikiContext().put("sdoc", new XWikiDocument(sdocReference));
84   
85  1 context = this.mocker.getComponentUnderTest().getVelocityContext();
86   
87  1 Document doc = (Document) context.get("doc");
88  1 assertNotNull(doc);
89   
90  1 Document sdoc = (Document) context.get("sdoc");
91  1 assertNotNull(sdoc);
92   
93    // Instances are kept the same when the documents in the context don't change
94  1 context = this.mocker.getComponentUnderTest().getVelocityContext();
95  1 assertSame(doc, context.get("doc"));
96  1 assertSame(sdoc, context.get("sdoc"));
97   
98    // Instances change when the documents in the context change
99  1 docReference = new DocumentReference("wiki", "space", "doc2");
100  1 sdocReference = new DocumentReference("wiki", "space", "sdoc2");
101  1 this.oldcore.getXWikiContext().setDoc(new XWikiDocument(docReference));
102  1 this.oldcore.getXWikiContext().put("sdoc", new XWikiDocument(sdocReference));
103   
104  1 context = this.mocker.getComponentUnderTest().getVelocityContext();
105  1 assertNotNull(context.get("doc"));
106  1 assertNotSame(doc, context.get("doc"));
107  1 assertNotNull(context.get("sdoc"));
108  1 assertNotSame(sdoc, context.get("sdoc"));
109    }
110    }