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

File TemplateManagerTest.java

 

Code metrics

0
21
8
1
141
94
8
0.38
2.62
8
1

Classes

Class Line # Actions
TemplateManagerTest 60 21 0% 8 0
1.0100%
 

Contributing tests

This file is covered by 3 tests. .

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.internal.template;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.UnsupportedEncodingException;
24    import java.io.Writer;
25    import java.net.MalformedURLException;
26    import java.net.URL;
27   
28    import org.apache.velocity.VelocityContext;
29    import org.junit.Before;
30    import org.junit.Rule;
31    import org.junit.Test;
32    import org.mockito.invocation.InvocationOnMock;
33    import org.mockito.stubbing.Answer;
34    import org.xwiki.component.manager.ComponentLookupException;
35    import org.xwiki.configuration.ConfigurationSource;
36    import org.xwiki.configuration.internal.MemoryConfigurationSource;
37    import org.xwiki.environment.Environment;
38    import org.xwiki.rendering.transformation.TransformationManager;
39    import org.xwiki.security.authorization.ContextualAuthorizationManager;
40    import org.xwiki.template.TemplateManager;
41    import org.xwiki.test.annotation.AfterComponent;
42    import org.xwiki.test.annotation.AllComponents;
43    import org.xwiki.test.internal.MockConfigurationSource;
44    import org.xwiki.test.mockito.MockitoComponentMockingRule;
45    import org.xwiki.test.mockito.StringReaderMatcher;
46    import org.xwiki.velocity.VelocityManager;
47    import org.xwiki.velocity.XWikiVelocityException;
48   
49    import static org.junit.Assert.assertEquals;
50    import static org.mockito.ArgumentMatchers.any;
51    import static org.mockito.ArgumentMatchers.argThat;
52    import static org.mockito.Mockito.when;
53   
54    /**
55    * Validate {@link DefaultTemplateManager}.
56    *
57    * @version $Id: 1978d15cf364c9d80de64408ea3ee95227b0e4a5 $
58    */
59    @AllComponents
 
60    public class TemplateManagerTest
61    {
62    @Rule
63    public final MockitoComponentMockingRule<TemplateManager> mocker =
64    new MockitoComponentMockingRule<TemplateManager>(DefaultTemplateManager.class);
65   
66    private Environment environmentMock;
67   
68    private VelocityManager velocityManagerMock;
69   
 
70  3 toggle @Before
71    public void before() throws Exception
72    {
73  3 when(this.velocityManagerMock.getVelocityContext()).thenReturn(new VelocityContext());
74   
75  3 MemoryConfigurationSource configuration = this.mocker.registerMemoryConfigurationSource();
76  3 this.mocker.registerComponent(MockConfigurationSource.getDescriptor("all"), configuration);
77   
78  3 this.mocker.registerMockComponent(ContextualAuthorizationManager.class);
79    }
80   
 
81  3 toggle @AfterComponent
82    public void afterComponent() throws Exception
83    {
84  3 this.environmentMock = this.mocker.registerMockComponent(Environment.class);
85  3 this.velocityManagerMock = this.mocker.registerMockComponent(VelocityManager.class);
86  3 this.mocker.registerMockComponent(ConfigurationSource.class);
87  3 this.mocker.registerMockComponent(TransformationManager.class);
88    }
89   
 
90  2 toggle private void setTemplateContent(String content) throws UnsupportedEncodingException, MalformedURLException
91    {
92  2 when(this.environmentMock.getResourceAsStream("/templates/template"))
93    .thenReturn(new ByteArrayInputStream(content.getBytes("UTF8")));
94  2 when(this.environmentMock.getResource("/templates/template")).thenReturn(new URL("http://url"));
95    }
96   
 
97  2 toggle private void mockVelocity(String source, String result) throws XWikiVelocityException
98    {
99  2 when(this.velocityManagerMock.evaluate(any(Writer.class), any(),
100    argThat(new StringReaderMatcher(source)))).then(new Answer<Boolean>()
101    {
 
102  2 toggle @Override
103    public Boolean answer(InvocationOnMock invocation) throws Throwable
104    {
105  2 Writer writer = (Writer) invocation.getArguments()[0];
106   
107  2 writer.write(result);
108   
109  2 return Boolean.TRUE;
110    }
111    });
112    }
113   
114    // Tests
115   
 
116  1 toggle @Test
117    public void testRenderVelocity() throws Exception
118    {
119  1 mockVelocity("source", "OK");
120   
121  1 setTemplateContent("source");
122   
123  1 assertEquals("OK", mocker.getComponentUnderTest().render("template"));
124    }
125   
 
126  1 toggle @Test
127    public void testRenderWiki() throws Exception
128    {
129  1 setTemplateContent("##!source.syntax=xwiki/2.1\nfirst line\\\\second line");
130   
131  1 assertEquals("<p>first line<br/>second line</p>", mocker.getComponentUnderTest().render("template"));
132    }
133   
 
134  1 toggle @Test
135    public void testRenderClassloaderTemplate() throws ComponentLookupException, Exception
136    {
137  1 mockVelocity("classloader template content", "OK");
138   
139  1 assertEquals("OK", this.mocker.getComponentUnderTest().render("classloader_template.vm"));
140    }
141    }