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

File PageTest.java

 
verifySubHeadingVelocityVariableCorrectlyEvaluatedWhenUsedInSection: Failed to lookup default document displayer.
webRssDisplay: Failed to lookup default document displayer.
webRssFiltersHiddenDocuments: Failed to lookup default document displayer.
 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

0
40
8
1
253
120
8
0.2
5
8
1

Classes

Class Line # Actions
PageTest 71 40 0% 8 17
0.645833364.6%
 

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 org.xwiki.test.page;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.apache.commons.lang.StringUtils;
26    import org.junit.After;
27    import org.junit.Before;
28    import org.junit.Rule;
29    import org.xwiki.cache.Cache;
30    import org.xwiki.cache.CacheManager;
31    import org.xwiki.cache.config.CacheConfiguration;
32    import org.xwiki.context.Execution;
33    import org.xwiki.context.ExecutionContext;
34    import org.xwiki.context.ExecutionContextManager;
35    import org.xwiki.environment.Environment;
36    import org.xwiki.job.event.status.JobProgressManager;
37    import org.xwiki.management.JMXBeanRegistration;
38    import org.xwiki.model.reference.DocumentReference;
39    import org.xwiki.model.reference.SpaceReference;
40    import org.xwiki.query.Query;
41    import org.xwiki.rendering.internal.transformation.MutableRenderingContext;
42    import org.xwiki.rendering.syntax.Syntax;
43    import org.xwiki.rendering.transformation.RenderingContext;
44    import org.xwiki.resource.internal.entity.EntityResourceActionLister;
45    import org.xwiki.test.annotation.AfterComponent;
46    import org.xwiki.test.annotation.BeforeComponent;
47    import org.xwiki.test.mockito.MockitoComponentManagerRule;
48    import org.xwiki.velocity.VelocityManager;
49   
50    import com.xpn.xwiki.XWiki;
51    import com.xpn.xwiki.XWikiContext;
52    import com.xpn.xwiki.doc.XWikiDocument;
53    import com.xpn.xwiki.internal.cache.rendering.RenderingCache;
54    import com.xpn.xwiki.test.MockitoOldcoreRule;
55    import com.xpn.xwiki.web.XWikiServletRequestStub;
56    import com.xpn.xwiki.web.XWikiServletResponseStub;
57   
58    import static org.mockito.ArgumentMatchers.any;
59    import static org.mockito.ArgumentMatchers.eq;
60    import static org.mockito.Mockito.mock;
61    import static org.mockito.Mockito.when;
62   
63    /**
64    * Tests that wishes to unit test wiki page should extend this class and call {@link #renderPage(DocumentReference)}
65    * to load and render a page located in the classpath.
66    *
67    * @version $Id: d200e0ce772a58b28b00bbaa4ee3e34418d047ea $
68    * @since 7.3M1
69    */
70    @PageComponentList
 
71    public class PageTest
72    {
73    /**
74    * Page tests use the Oldcore rule to configure some base mocks (such as XWiki).
75    */
76    @Rule
77    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
78   
79    /**
80    * The stubbed request used to simulate a real Servlet Request.
81    */
82    protected XWikiServletRequestStub request;
83   
84    /**
85    * The stubbed response used to simulate a real Servlet Response.
86    */
87    protected XWikiServletResponseStub response;
88   
89    /**
90    * The mocked XWiki instance, provided for ease of use (can also be retrieved through {@link #oldcore}).
91    */
92    protected XWiki xwiki;
93   
94    /**
95    * The configured XWiki Context, provided for ease of use (can also be retrieved through {@link #oldcore}).
96    */
97    protected XWikiContext context;
98   
99    /**
100    * The Component Manager to use for getting Component instances or registering Mock Components in the test,
101    * provided for ease of use (can also be retrieved through {@link #oldcore}).
102    */
103    protected MockitoComponentManagerRule mocker = oldcore.getMocker();
104   
105    /**
106    * Set up components before Components declared in {@link org.xwiki.test.annotation.ComponentList} are handled.
107    *
108    * @throws Exception in case of errors
109    */
 
110  7 toggle @BeforeComponent
111    public void setUpComponentsForPageTest() throws Exception
112    {
113  7 mocker.registerMockComponent(JMXBeanRegistration.class);
114  7 mocker.registerMockComponent(Environment.class);
115  7 mocker.registerMockComponent(JobProgressManager.class);
116  7 mocker.registerMockComponent(RenderingCache.class);
117  7 mocker.registerMockComponent(EntityResourceActionLister.class);
118   
119  7 CacheManager cacheManager = mocker.registerMockComponent(CacheManager.class);
120  7 when(cacheManager.createNewCache(any(CacheConfiguration.class))).thenReturn(mock(Cache.class));
121    }
122   
123    /**
124    * Set up of Components after the Components declared in {@link org.xwiki.test.annotation.ComponentList} have been
125    * handled but before {@link MockitoOldcoreRule#before(Class)} has been called (i.e. before it has created Mocks
126    * and configured Components).
127    *
128    * @throws Exception in case of errors
129    */
 
130  7 toggle @AfterComponent
131    public void configureComponentsBeforeOldcoreRuleForPageTest() throws Exception
132    {
133    // Configure the Execution Context
134  7 ExecutionContext ec = new ExecutionContext();
135  7 mocker.<Execution>getInstance(Execution.class).setContext(ec);
136    }
137   
138    /**
139    * @param documentReference the reference of the Document to load from the ClassLoader
140    * @return the loaded document
141    * @throws Exception in case of errors
142    */
 
143  0 toggle protected XWikiDocument loadPage(DocumentReference documentReference) throws Exception
144    {
145  0 List<String> path = new ArrayList<>();
146  0 for (SpaceReference spaceReference : documentReference.getSpaceReferences()) {
147  0 path.add(spaceReference.getName());
148    }
149  0 path.add(documentReference.getName() + ".xml");
150  0 XWikiDocument document = new XWikiDocument(documentReference);
151  0 document.fromXML(getClass().getClassLoader().getResourceAsStream(StringUtils.join(path, '/')));
152  0 this.xwiki.saveDocument(document, "registering document", true, this.context);
153  0 return document;
154    }
155   
156    /**
157    * @param reference the reference of the Document to load and render (and thus load from the Classloader)
158    * @return the result of rendering the Document corresponding to the passed reference
159    * @throws Exception in case of errors
160    */
 
161  0 toggle protected String renderPage(DocumentReference reference) throws Exception
162    {
163  0 XWikiDocument doc = loadPage(reference);
164   
165    // Set up the current doc in the context so that $doc is bound in scripts
166  0 context.setDoc(doc);
167   
168  0 Test failure here return doc.getRenderedContent(this.context);
169    }
170   
171    /**
172    * Sets the Syntax with which the Document to test will be rendered into. If not called, the Document will be
173    * rendered as XHTML.
174    *
175    * @param syntax the Syntax to render the Document into
176    * @throws Exception in case of errors
177    */
 
178  0 toggle protected void setOutputSyntax(Syntax syntax) throws Exception
179    {
180  0 MutableRenderingContext renderingContext = mocker.getInstance(RenderingContext.class);
181  0 renderingContext.push(renderingContext.getTransformation(), renderingContext.getXDOM(),
182    renderingContext.getDefaultSyntax(), "test", renderingContext.isRestricted(), syntax);
183    }
184   
185    /**
186    * Configures the various Components and their mocks with default values for page tests.
187    *
188    * @throws Exception in case of errors
189    */
 
190  7 toggle @Before
191    public void setUpForPageTest() throws Exception
192    {
193    // Configure mocks from OldcoreRule
194  7 context = oldcore.getXWikiContext();
195  7 xwiki = oldcore.getSpyXWiki();
196   
197    // We need this one because some component in its init creates a query...
198  7 when(oldcore.getQueryManager().createQuery(any(String.class), any(String.class))).thenReturn(mock(Query.class));
199   
200    // Set up a fake Request
201    // Configure request so that $!request.outputSyntax" == 'plain
202    // Need to be executed before ecm.initialize() so that XWikiScriptContextInitializer will initialize the
203    // script context properly
204  7 request = new XWikiServletRequestStub();
205  7 request.setScheme("http");
206  7 context.setRequest(request);
207   
208  7 response = new XWikiServletResponseStub();
209  7 context.setResponse(response);
210   
211  7 ExecutionContextManager ecm = mocker.getInstance(ExecutionContextManager.class);
212  7 ecm.initialize(oldcore.getExecutionContext());
213   
214    // Let the user have view access to all pages
215  7 when(oldcore.getMockRightService().hasAccessLevel(eq("view"), eq("XWiki.XWikiGuest"), any(),
216    eq(context))).thenReturn(true);
217   
218    // Set up URL Factory
219  7 URLFactorySetup.setUp(xwiki, context);
220   
221    // Set up Localization
222  7 LocalizationSetup.setUp(mocker);
223   
224    // Set up Skin Extensions
225  7 SkinExtensionSetup.setUp(xwiki, context);
226    }
227   
228    /**
229    * Clean up after the test.
230    *
231    * @throws Exception in case of errors
232    */
 
233  7 toggle @After
234    public void tearDown() throws Exception
235    {
236  7 MutableRenderingContext renderingContext = mocker.getInstance(RenderingContext.class);
237  7 renderingContext.pop();
238    }
239   
240    /**
241    * Adds a tool to the Velocity context.
242    *
243    * @param name the name of the tool
244    * @param tool the tool to register; can be a mock
245    * @throws Exception in case of errors
246    * @since 7.4M1
247    */
 
248  4 toggle protected void registerVelocityTool(String name, Object tool) throws Exception
249    {
250  4 VelocityManager velocityManager = this.oldcore.getMocker().getInstance(VelocityManager.class);
251  0 velocityManager.getVelocityContext().put(name, tool);
252    }
253    }