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

File AbstractBridgedXWikiComponentTestCase.java

 

Coverage histogram

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

Code metrics

0
45
7
1
183
119
7
0.16
6.43
7
1

Classes

Class Line # Actions
AbstractBridgedXWikiComponentTestCase 58 45 0% 7 2
0.9615384396.2%
 

Contributing tests

This file is covered by 112 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    import java.util.Date;
24   
25    import javax.inject.Provider;
26    import javax.servlet.ServletContext;
27   
28    import org.jmock.Mock;
29    import org.jmock.core.Invocation;
30    import org.jmock.core.stub.CustomStub;
31    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.component.util.DefaultParameterizedType;
34    import org.xwiki.context.Execution;
35    import org.xwiki.environment.Environment;
36    import org.xwiki.environment.internal.ServletEnvironment;
37    import org.xwiki.rendering.configuration.ExtendedRenderingConfiguration;
38    import org.xwiki.rendering.syntax.Syntax;
39    import org.xwiki.wiki.descriptor.WikiDescriptor;
40    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
41   
42    import com.xpn.xwiki.CoreConfiguration;
43    import com.xpn.xwiki.XWikiContext;
44    import com.xpn.xwiki.util.XWikiStubContextProvider;
45    import com.xpn.xwiki.web.Utils;
46    import com.xpn.xwiki.web.XWikiResponse;
47   
48    /**
49    * Extension of {@link AbstractXWikiComponentTestCase} that sets up a bridge between the new Execution
50    * Context and the old XWikiContext. This allows code that uses XWikiContext to be tested using this Test Case class.
51    *
52    * @version $Id: 93b2df4aabb8de97915310f3b533acb0adca083d $
53    * @since 1.6M1
54    *
55    * @deprecated use JUnit 4.x and {@link com.xpn.xwiki.test.AbstractBridgedComponentTestCase}
56    */
57    @Deprecated
 
58    public abstract class AbstractBridgedXWikiComponentTestCase extends AbstractXWikiComponentTestCase
59    {
60    private XWikiContext context;
61   
62    protected File permanentDirectory;
63   
64    protected File temporaryDirectory;
65   
66    protected Mock mockWikiDescriptorManager;
67   
 
68  123 toggle @Override
69    protected void setUp() throws Exception
70    {
71  123 super.setUp();
72   
73    // Statically store the component manager in {@link Utils} to be able to access it without
74    // the context.
75  123 Utils.setComponentManager(getComponentManager());
76   
77  123 this.context = new XWikiContext();
78   
79  123 this.context.setWikiId("xwiki");
80  123 this.context.setMainXWiki("xwiki");
81   
82    // Make sure response.encodeURL() calls don't fail
83  123 Mock xwikiResponse = mock(XWikiResponse.class);
84  123 xwikiResponse.stubs().method("setLocale");
85  123 xwikiResponse.stubs().method("encodeURL").will(
86    new CustomStub("Implements XWikiResponse.encodeURL")
87    {
 
88  22 toggle @Override
89    public Object invoke(Invocation invocation) throws Throwable
90    {
91  22 return invocation.parameterValues.get(0);
92    }
93    });
94  123 this.context.setResponse((XWikiResponse) xwikiResponse.proxy());
95   
96    // We need to initialize the Component Manager so that the components can be looked up
97  123 getContext().put(ComponentManager.class.getName(), getComponentManager());
98   
99    // Bridge with old XWiki Context, required for old code.
100  123 Execution execution = getComponentManager().getInstance(Execution.class);
101  123 this.context.declareInExecutionContext(execution.getContext());
102  123 XWikiStubContextProvider stubContextProvider =
103    getComponentManager().getInstance(XWikiStubContextProvider.class);
104  123 stubContextProvider.initialize(this.context);
105   
106    // Bridge with XWiki Context Provider, required by newer code.
107  123 Mock mockContextProvider = mock(Provider.class);
108  123 mockContextProvider.stubs().method("get").will(returnValue(this.context));
109   
110  123 DefaultComponentDescriptor<Provider<XWikiContext>> contextProviderDescriptor =
111    new DefaultComponentDescriptor<Provider<XWikiContext>>();
112  123 contextProviderDescriptor.setRoleType(new DefaultParameterizedType(null, Provider.class, XWikiContext.class));
113  123 contextProviderDescriptor.setRoleHint("default");
114  123 getComponentManager().registerComponent(contextProviderDescriptor,
115    (Provider<XWikiContext>) mockContextProvider.proxy());
116   
117    // Since the oldcore module draws the Servlet Environment in its dependencies we need to ensure it's set up
118    // correctly with a Servlet Context.
119  123 ServletEnvironment environment = getComponentManager().getInstance(Environment.class);
120  123 Mock mockServletContext = mock(ServletContext.class);
121  123 environment.setServletContext((ServletContext) mockServletContext.proxy());
122  123 mockServletContext.stubs().method("getResourceAsStream").will(returnValue(null));
123  123 mockServletContext.stubs().method("getResource").will(returnValue(null));
124  123 mockServletContext.stubs().method("getAttribute").with(eq("javax.servlet.context.tempdir"))
125    .will(returnValue(new File(System.getProperty("java.io.tmpdir"))));
126   
127  123 File testDirectory = new File("target/test-" + new Date().getTime());
128  123 this.temporaryDirectory = new File(testDirectory, "temporary-dir");
129  123 this.permanentDirectory = new File(testDirectory, "permanent-dir");
130  123 environment.setTemporaryDirectory(this.temporaryDirectory);
131  123 environment.setPermanentDirectory(this.permanentDirectory);
132   
133  123 Mock mockCoreConfiguration = registerMockComponent(CoreConfiguration.class);
134  123 mockCoreConfiguration.stubs().method("getDefaultDocumentSyntax").will(returnValue(Syntax.XWIKI_1_0));
135   
136  123 this.mockWikiDescriptorManager = registerMockComponent(WikiDescriptorManager.class);
137  123 this.mockWikiDescriptorManager.stubs().method("getCurrentWikiId")
138    .will(new CustomStub("Implements WikiDescriptorManager.getCurrentWikiId")
139    {
 
140  3819 toggle @Override
141    public String invoke(Invocation invocation) throws Throwable
142    {
143  3819 return getContext().getWikiId();
144    }
145    });
146  123 this.mockWikiDescriptorManager.stubs().method("getMainWikiId")
147    .will(new CustomStub("Implements WikiDescriptorManager.getMainWikiId")
148    {
 
149  0 toggle @Override
150    public String invoke(Invocation invocation) throws Throwable
151    {
152  0 return getContext().getMainXWiki();
153    }
154    });
155  123 this.mockWikiDescriptorManager.stubs().method("getById")
156    .will(new CustomStub("Implements WikiDescriptorManager.getById")
157    {
 
158  22 toggle @Override
159    public String invoke(Invocation invocation) throws Throwable
160    {
161  22 return null;
162    }
163    });
164   
165    // In order not to create a cyclic dependency we have the platform-rendering-xwiki module (which contains
166    // XWikiWikiModel requires for oldcore testing) not depend on platform-rendering-configuration-default. As a
167    // consequence we need to provide a mock ExtendedRenderingConfiguration component as otherwise injecting
168    // WikiModel would fail (since XWikiWikiModel depends on ExtendedRenderingConfiguration).
169  123 registerMockComponent(ExtendedRenderingConfiguration.class);
170    }
171   
 
172  123 toggle @Override
173    protected void tearDown() throws Exception
174    {
175  123 Utils.setComponentManager(null);
176  123 super.tearDown();
177    }
178   
 
179  5690 toggle public XWikiContext getContext()
180    {
181  5690 return this.context;
182    }
183    }