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

File AbstractBridgedComponentTestCase.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

0
34
10
1
154
92
10
0.29
3.4
10
1

Classes

Class Line # Actions
AbstractBridgedComponentTestCase 53 34 0% 10 2
0.9545454495.5%
 

Contributing tests

This file is covered by 13 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.test;
21   
22    import java.io.File;
23   
24    import javax.servlet.ServletContext;
25   
26    import org.jmock.Expectations;
27    import org.jmock.api.Imposteriser;
28    import org.jmock.lib.legacy.ClassImposteriser;
29    import org.junit.After;
30    import org.junit.Before;
31    import org.xwiki.component.manager.ComponentManager;
32    import org.xwiki.context.Execution;
33    import org.xwiki.environment.Environment;
34    import org.xwiki.environment.internal.ServletEnvironment;
35    import org.xwiki.rendering.syntax.Syntax;
36    import org.xwiki.security.authorization.AuthorizationManager;
37    import org.xwiki.security.authorization.ContextualAuthorizationManager;
38    import org.xwiki.test.jmock.AbstractComponentTestCase;
39   
40    import com.xpn.xwiki.CoreConfiguration;
41    import com.xpn.xwiki.XWikiContext;
42    import com.xpn.xwiki.util.XWikiStubContextProvider;
43    import com.xpn.xwiki.web.Utils;
44   
45    /**
46    * Same as {@link com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase} but for JUnit 4.x and JMock 2.x.
47    *
48    * @version $Id: df69c6bd7b387bb2df95eedc1b1433d5a9400ae1 $
49    * @since 2.2M2
50    * @deprecated sine 5.2M1 use {@link MockitoOldcoreRule} instead
51    */
52    @Deprecated
 
53    public class AbstractBridgedComponentTestCase extends AbstractComponentTestCase
54    {
55    private XWikiContext context;
56   
57    private ContextualAuthorizationManager contextualAuthorizationManager;
58    private AuthorizationManager authorizationManager;
59   
 
60  17 toggle protected AbstractBridgedComponentTestCase()
61    {
62    // We often need to mock com.xpn.xwiki.XWiki class
63  17 this(ClassImposteriser.INSTANCE);
64    }
65   
 
66  17 toggle protected AbstractBridgedComponentTestCase(Imposteriser imposteriser)
67    {
68  17 getMockery().setImposteriser(imposteriser);
69    }
70   
 
71  17 toggle @Override
72    @Before
73    public void setUp() throws Exception
74    {
75  17 super.setUp();
76   
77    // Statically store the component manager in {@link Utils} to be able to access it without
78    // the context.
79  17 Utils.setComponentManager(getComponentManager());
80   
81  17 this.context = new XWikiContext();
82   
83  17 this.context.setWikiId("xwiki");
84  17 this.context.setMainXWiki("xwiki");
85   
86    // We need to initialize the Component Manager so that the components can be looked up
87  17 getContext().put(ComponentManager.class.getName(), getComponentManager());
88   
89    // Bridge with old XWiki Context, required for old code.
90  17 Execution execution = getComponentManager().getInstance(Execution.class);
91  17 execution.getContext().setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, this.context);
92  17 XWikiStubContextProvider stubContextProvider =
93    getComponentManager().getInstance(XWikiStubContextProvider.class);
94  17 stubContextProvider.initialize(this.context);
95   
96    // Since the oldcore module draws the Servlet Environment in its dependencies we need to ensure it's set up
97    // correctly with a Servlet Context.
98  17 ServletEnvironment environment = (ServletEnvironment) getComponentManager().getInstance(Environment.class);
99  17 final ServletContext mockServletContext = getMockery().mock(ServletContext.class);
100  17 environment.setServletContext(mockServletContext);
 
101  17 toggle getMockery().checking(new Expectations() {{
102  17 allowing(mockServletContext).getResourceAsStream("/WEB-INF/cache/infinispan/config.xml");
103  17 will(returnValue(null));
104  17 allowing(mockServletContext).getResourceAsStream("/WEB-INF/xwiki.cfg");
105  17 will(returnValue(null));
106  17 allowing(mockServletContext).getAttribute("javax.servlet.context.tempdir");
107  17 will(returnValue(new File(System.getProperty("java.io.tmpdir"))));
108    }});
109   
110  17 final CoreConfiguration mockCoreConfiguration = registerMockComponent(CoreConfiguration.class);
 
111  17 toggle getMockery().checking(new Expectations() {{
112  17 allowing(mockCoreConfiguration).getDefaultDocumentSyntax(); will(returnValue(Syntax.XWIKI_1_0));
113    }});
114    }
115   
 
116  17 toggle @Override
117    protected void registerComponents() throws Exception
118    {
119  17 super.registerComponents();
120   
121  17 this.contextualAuthorizationManager = this.registerMockComponent(ContextualAuthorizationManager.class);
122  17 this.authorizationManager = this.registerMockComponent(AuthorizationManager.class);
123    }
124   
 
125  17 toggle @Override
126    @After
127    public void tearDown() throws Exception
128    {
129  17 super.tearDown();
130   
131  17 Utils.setComponentManager(null);
132    }
133   
 
134  154 toggle public XWikiContext getContext()
135    {
136  154 return this.context;
137    }
138   
139    /**
140    * @return a modifiable mock contextual authorization manager.
141    */
 
142  2 toggle public ContextualAuthorizationManager getContextualAuthorizationManager()
143    {
144  2 return contextualAuthorizationManager;
145    }
146   
147    /**
148    * @return a modifiable mock authorization manager.
149    */
 
150  0 toggle public AuthorizationManager getAuthorizationManager()
151    {
152  0 return authorizationManager;
153    }
154    }