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

File AbstractBridgedXWikiComponentTestCase.java

 

Coverage histogram

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

Code metrics

0
43
6
1
173
109
6
0.14
7.17
6
1

Classes

Class Line # Actions
AbstractBridgedXWikiComponentTestCase 57 43 0% 6 2
0.959183795.9%
 

Contributing tests

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