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

File PageTest.java

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

Coverage histogram

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

Code metrics

0
41
8
1
261
129
8
0.2
5.12
8
1

Classes

Class Line # Actions
PageTest 77 41 0% 8 17
0.653061265.3%
 

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