1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.render

File DefaultVelocityManagerTest.java

 

Code metrics

0
27
2
1
112
67
2
0.07
13.5
2
1

Classes

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