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

File AbstractXWikiComponentTestCase.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

0
24
13
1
164
82
13
0.54
1.85
13
1

Classes

Class Line # Actions
AbstractXWikiComponentTestCase 40 24 0% 13 10
0.729729773%
 

Contributing tests

This file is covered by 15 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 org.jmock.Mock;
23    import org.jmock.cglib.MockObjectTestCase;
24    import org.xwiki.component.descriptor.ComponentDescriptor;
25    import org.xwiki.component.descriptor.DefaultComponentDescriptor;
26    import org.xwiki.security.authorization.AuthorizationManager;
27    import org.xwiki.security.authorization.ContextualAuthorizationManager;
28    import org.xwiki.test.internal.MockConfigurationSource;
29    import org.xwiki.test.jmock.MockingComponentManager;
30    import org.xwiki.test.jmock.XWikiComponentInitializer;
31   
32    /**
33    * Tests which needs to have XWiki Components set up should extend this class which makes the Component Manager
34    * available. Use this class for JUnit 3.x tests. For JUnit 4.x tests use {@link org.xwiki.test.ComponentManagerRule}
35    * instead.
36    *
37    * @deprecated use JUnit 4.x and {@link org.xwiki.test.ComponentManagerRule}
38    */
39    @Deprecated
 
40    public abstract class AbstractXWikiComponentTestCase extends MockObjectTestCase
41    {
42    private XWikiComponentInitializer initializer = new XWikiComponentInitializer();
43   
44    private Mock contextualAuthorizationManager;
45    private Mock authorizationManager;
46   
 
47  133 toggle public AbstractXWikiComponentTestCase()
48    {
49  133 super();
50    }
51   
 
52  0 toggle public AbstractXWikiComponentTestCase(String testName)
53    {
54  0 super(testName);
55    }
56   
57    /**
58    * Tests that require fine-grained initializations can override this method and not call super.
59    *
60    * @see junit.framework.TestCase#setUp()
61    */
 
62  133 toggle @Override
63    protected void setUp() throws Exception
64    {
65  133 this.initializer.initializeConfigurationSource();
66   
67    // Put before execution context initialization because it could be needed for some executing context
68    // initializer.
69  133 registerComponents();
70   
71  133 this.initializer.initializeExecution();
72    }
73   
74    /**
75    * Register custom/mock components
76    */
 
77  133 toggle protected void registerComponents() throws Exception
78    {
79  133 this.contextualAuthorizationManager = this.registerMockComponent(ContextualAuthorizationManager.class);
80  133 this.authorizationManager = this.registerMockComponent(AuthorizationManager.class);
81   
82  133 this.contextualAuthorizationManager.stubs().method("hasAccess").will(returnValue(true));
83  133 this.authorizationManager.stubs().method("hasAccess").will(returnValue(true));
84   
85    // Extending classes can override to provide custom component registration.
86    }
87   
 
88  133 toggle @Override
89    protected void tearDown() throws Exception
90    {
91  133 this.initializer.shutdown();
92    }
93   
94    /**
95    * @return a configured Component Manager (which uses the plexus.xml file in the test resources directory) which can
96    * then be put in the XWiki Context for testing.
97    */
 
98  1614 toggle public MockingComponentManager getComponentManager() throws Exception
99    {
100  1614 return this.initializer.getComponentManager();
101    }
102   
103    /**
104    * @return a modifiable mock configuration source
105    */
 
106  70 toggle public MockConfigurationSource getConfigurationSource()
107    {
108  70 return this.initializer.getConfigurationSource();
109    }
110   
111    /**
112    * @return a modifiable mock contextual authorization manager.
113    */
 
114  0 toggle public Mock getContextualAuthorizationManager()
115    {
116  0 return contextualAuthorizationManager;
117    }
118   
119    /**
120    * @return a modifiable mock authorization manager.
121    */
 
122  0 toggle public Mock getAuthorizationManager()
123    {
124  0 return authorizationManager;
125    }
126   
127    /**
128    * @since 2.4M2
129    */
 
130  0 toggle public <T> Mock registerMockComponent(Class<T> role, String hint) throws Exception
131    {
132  0 DefaultComponentDescriptor<T> descriptor = createComponentDescriptor(role);
133  0 descriptor.setRoleHint(hint);
134  0 return registerMockComponent(descriptor);
135    }
136   
137    /**
138    * @since 2.4M2
139    */
 
140  756 toggle public <T> Mock registerMockComponent(Class<T> role) throws Exception
141    {
142  756 return registerMockComponent(createComponentDescriptor(role));
143    }
144   
145    /**
146    * @since 2.4M2
147    */
 
148  756 toggle private <T> Mock registerMockComponent(ComponentDescriptor<T> descriptor) throws Exception
149    {
150  756 Mock mock = mock(descriptor.getRole());
151  756 getComponentManager().registerComponent(descriptor, (T) mock.proxy());
152  756 return mock;
153    }
154   
155    /**
156    * @since 2.4M2
157    */
 
158  756 toggle private <T> DefaultComponentDescriptor<T> createComponentDescriptor(Class<T> role)
159    {
160  756 DefaultComponentDescriptor<T> descriptor = new DefaultComponentDescriptor<T>();
161  756 descriptor.setRole(role);
162  756 return descriptor;
163    }
164    }