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

File DefaultWikiComponentBridgeTest.java

 

Code metrics

0
115
24
1
350
283
26
0.23
4.79
24
1.08

Classes

Class Line # Actions
DefaultWikiComponentBridgeTest 81 115 0% 26 2
0.985611598.6%
 

Contributing tests

This file is covered by 11 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.component.wiki;
21   
22    import java.util.ArrayList;
23    import java.util.Vector;
24   
25    import javax.inject.Provider;
26   
27    import org.jmock.Expectations;
28    import org.jmock.lib.legacy.ClassImposteriser;
29    import org.junit.Assert;
30    import org.junit.Before;
31    import org.junit.Test;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.component.wiki.internal.WikiComponentConstants;
34    import org.xwiki.component.wiki.internal.bridge.ContentParser;
35    import org.xwiki.component.wiki.internal.bridge.DefaultWikiComponentBridge;
36    import org.xwiki.component.wiki.internal.bridge.WikiComponentBridge;
37    import org.xwiki.context.Execution;
38    import org.xwiki.context.ExecutionContext;
39    import org.xwiki.model.internal.DefaultModelConfiguration;
40    import org.xwiki.model.internal.DefaultModelContext;
41    import org.xwiki.model.internal.reference.DefaultEntityReferenceProvider;
42    import org.xwiki.model.internal.reference.LocalStringEntityReferenceSerializer;
43    import org.xwiki.model.internal.reference.RelativeStringEntityReferenceResolver;
44    import org.xwiki.model.reference.DocumentReference;
45    import org.xwiki.model.reference.EntityReferenceSerializer;
46    import org.xwiki.rendering.block.Block;
47    import org.xwiki.rendering.block.XDOM;
48    import org.xwiki.rendering.parser.Parser;
49    import org.xwiki.rendering.syntax.Syntax;
50    import org.xwiki.test.annotation.ComponentList;
51    import org.xwiki.test.jmock.AbstractMockingComponentTestCase;
52    import org.xwiki.test.jmock.annotation.MockingRequirement;
53   
54    import com.xpn.xwiki.XWiki;
55    import com.xpn.xwiki.XWikiContext;
56    import com.xpn.xwiki.doc.XWikiDocument;
57    import com.xpn.xwiki.internal.model.reference.CompactWikiStringEntityReferenceSerializer;
58    import com.xpn.xwiki.internal.model.reference.CurrentEntityReferenceProvider;
59    import com.xpn.xwiki.internal.model.reference.CurrentMixedEntityReferenceProvider;
60    import com.xpn.xwiki.internal.model.reference.CurrentMixedStringDocumentReferenceResolver;
61    import com.xpn.xwiki.internal.model.reference.CurrentReferenceDocumentReferenceResolver;
62    import com.xpn.xwiki.internal.model.reference.CurrentReferenceEntityReferenceResolver;
63    import com.xpn.xwiki.objects.BaseObject;
64    import com.xpn.xwiki.web.Utils;
65   
66    @ComponentList({
67    DefaultModelContext.class,
68    DefaultModelConfiguration.class,
69    LocalStringEntityReferenceSerializer.class,
70    RelativeStringEntityReferenceResolver.class,
71    CurrentReferenceDocumentReferenceResolver.class,
72    CurrentReferenceEntityReferenceResolver.class,
73    CurrentEntityReferenceProvider.class,
74    CurrentMixedStringDocumentReferenceResolver.class,
75    CurrentMixedEntityReferenceProvider.class,
76    DefaultEntityReferenceProvider.class,
77    CompactWikiStringEntityReferenceSerializer.class
78    })
79    @MockingRequirement(value = DefaultWikiComponentBridge.class,
80    exceptions = {EntityReferenceSerializer.class, Parser.class})
 
81    public class DefaultWikiComponentBridgeTest extends AbstractMockingComponentTestCase implements WikiComponentConstants
82    {
83    private static final DocumentReference DOC_REFERENCE = new DocumentReference("xwiki", "XWiki", "MyComponent");
84   
85    private static final DocumentReference AUTHOR_REFERENCE = new DocumentReference("xwiki", "XWiki", "Admin");
86   
87    private XWiki xwiki;
88   
89    private XWikiContext xwikiContext;
90   
91    private XWikiDocument componentDoc;
92   
93    private BaseObject componentObject;
94   
95    private WikiComponentBridge bridge;
96   
 
97  11 toggle @Before
98    public void configure() throws Exception
99    {
100  11 getMockery().setImposteriser(ClassImposteriser.INSTANCE);
101   
102  11 Utils.setComponentManager(getComponentManager());
103   
104  11 final Execution execution = registerMockComponent(Execution.class);
105  11 final ExecutionContext context = new ExecutionContext();
106   
107  11 final Provider<XWikiContext> xcontextProvider = registerMockComponent(XWikiContext.TYPE_PROVIDER);
108   
109  11 this.xwiki = getMockery().mock(XWiki.class);
110   
111  11 this.xwikiContext = new XWikiContext();
112  11 this.xwikiContext.setWikiId("xwiki");
113  11 this.xwikiContext.setWiki(this.xwiki);
114   
115  11 context.setProperty("xwikicontext", this.xwikiContext);
116   
117  11 this.componentDoc = getMockery().mock(XWikiDocument.class);
118  11 this.componentObject = getMockery().mock(BaseObject.class, "component");
119   
120  11 getMockery().checking(new Expectations()
121    {
 
122  11 toggle {
123  11 allowing(xcontextProvider).get();
124  11 will(returnValue(xwikiContext));
125  11 allowing(execution).getContext();
126  11 will(returnValue(context));
127  11 allowing(xwiki).getDocument(DOC_REFERENCE, xwikiContext);
128  11 will(returnValue(componentDoc));
129    }
130    });
131   
132  11 this.bridge = getComponentManager().getInstance(WikiComponentBridge.class);
133    }
134   
 
135  1 toggle @Test
136    public void getAuthorReference() throws Exception
137    {
138  1 getMockery().checking(new Expectations()
139    {
 
140  1 toggle {
141  1 allowing(componentDoc).getAuthorReference();
142  1 will(returnValue(AUTHOR_REFERENCE));
143    }
144    });
145   
146  1 Assert.assertEquals(AUTHOR_REFERENCE, bridge.getAuthorReference(DOC_REFERENCE));
147    }
148   
 
149  1 toggle @Test
150    public void getDeclaredInterfaces() throws Exception
151    {
152  1 final BaseObject interfaceObject = getMockery().mock(BaseObject.class, "interface");
153  1 final Vector<BaseObject> interfaceObjects = new Vector<BaseObject>();
154  1 interfaceObjects.add(interfaceObject);
155   
156  1 getMockery().checking(new Expectations()
157    {
 
158  1 toggle {
159  1 oneOf(componentDoc).getObjectNumbers(INTERFACE_CLASS);
160  1 will(returnValue(1));
161  1 oneOf(componentDoc).getObjects(INTERFACE_CLASS);
162  1 will(returnValue(interfaceObjects));
163  1 allowing(interfaceObject).getStringValue(INTERFACE_NAME_FIELD);
164  1 will(returnValue("org.xwiki.component.phase.Initializable"));
165    }
166    });
167   
168  1 Assert.assertEquals(1, bridge.getDeclaredInterfaces(DOC_REFERENCE).size());
169    }
170   
 
171  1 toggle @Test
172    public void getDependencies() throws Exception
173    {
174  1 final BaseObject dependencyObject = getMockery().mock(BaseObject.class, "dependency");
175  1 final Vector<BaseObject> dependencyObjects = new Vector<BaseObject>();
176  1 dependencyObjects.add(dependencyObject);
177   
178  1 getMockery().checking(new Expectations()
179    {
 
180  1 toggle {
181  1 oneOf(componentDoc).getObjectNumbers(DEPENDENCY_CLASS);
182  1 will(returnValue(1));
183  1 oneOf(componentDoc).getObjects(DEPENDENCY_CLASS);
184  1 will(returnValue(dependencyObjects));
185  1 allowing(dependencyObject).getStringValue(COMPONENT_ROLE_TYPE_FIELD);
186  1 will(returnValue("org.xwiki.component.wiki.TestRole"));
187  1 allowing(dependencyObject).getStringValue(COMPONENT_ROLE_HINT_FIELD);
188  1 will(returnValue("default"));
189  1 allowing(dependencyObject).getStringValue(DEPENDENCY_BINDING_NAME_FIELD);
190  1 will(returnValue("test"));
191    }
192    });
193   
194  1 Assert.assertEquals(1, bridge.getDependencies(DOC_REFERENCE).size());
195    }
196   
 
197  1 toggle @Test
198    public void getHandledMethods() throws Exception
199    {
200  1 final ComponentManager componentManager = getComponentManager().getInstance(ComponentManager.class);
201  1 final ContentParser contentParser = getComponentManager().getInstance(ContentParser.class);
202  1 final Parser parser = getMockery().mock(Parser.class);
203  1 final XDOM xdom = new XDOM(new ArrayList<Block>());
204  1 final BaseObject methodObject = getMockery().mock(BaseObject.class, "method");
205  1 final Vector<BaseObject> methodObjects = new Vector<BaseObject>();
206  1 methodObjects.add(methodObject);
207   
208  1 getMockery().checking(new Expectations()
209    {
 
210  1 toggle {
211   
212  1 oneOf(componentDoc).getObjectNumbers(METHOD_CLASS);
213  1 will(returnValue(1));
214  1 oneOf(componentDoc).getObjects(METHOD_CLASS);
215  1 will(returnValue(methodObjects));
216  1 allowing(methodObject).getStringValue(METHOD_NAME_FIELD);
217  1 will(returnValue("test"));
218  1 allowing(methodObject).getStringValue(METHOD_CODE_FIELD);
219  1 will(returnValue("test"));
220  1 oneOf(componentDoc).getSyntax();
221  1 will(returnValue(Syntax.XWIKI_2_1));
222  1 oneOf(contentParser).parse("test", Syntax.XWIKI_2_1, DOC_REFERENCE);
223  1 will(returnValue(xdom));
224    }
225    });
226   
227  1 Assert.assertEquals(1, bridge.getHandledMethods(DOC_REFERENCE).size());
228    }
229   
 
230  1 toggle @Test
231    public void getRoleHint() throws Exception
232    {
233  1 getMockery().checking(new Expectations()
234    {
 
235  1 toggle {
236  1 oneOf(componentDoc).getObject(COMPONENT_CLASS);
237  1 will(returnValue(componentObject));
238  1 oneOf(componentObject).getStringValue(COMPONENT_ROLE_HINT_FIELD);
239  1 will(returnValue("roleHint"));
240    }
241    });
242   
243  1 Assert.assertEquals("roleHint", bridge.getRoleHint(DOC_REFERENCE));
244    }
245   
 
246  1 toggle @Test
247    public void getRoleType() throws Exception
248    {
249  1 getMockery().checking(new Expectations()
250    {
 
251  1 toggle {
252  1 oneOf(componentDoc).getObject(COMPONENT_CLASS);
253  1 will(returnValue(componentObject));
254  1 oneOf(componentObject).getStringValue(COMPONENT_ROLE_TYPE_FIELD);
255  1 will(returnValue("org.xwiki.component.wiki.TestRole"));
256    }
257    });
258   
259  1 Assert.assertEquals(TestRole.class, bridge.getRoleType(DOC_REFERENCE));
260    }
261   
 
262  1 toggle @Test
263    public void getRoleTypeWithoutComponentObject() throws Exception
264    {
265  1 getMockery().checking(new Expectations()
266    {
 
267  1 toggle {
268  1 oneOf(componentDoc).getObject(COMPONENT_CLASS);
269  1 will(returnValue(null));
270    }
271    });
272   
273  1 try {
274  1 bridge.getRoleType(DOC_REFERENCE);
275  0 Assert.fail("Should have thrown an exception");
276    } catch (WikiComponentException expected) {
277  1 Assert.assertEquals("No component object could be found in document [xwiki:XWiki.MyComponent]",
278    expected.getMessage());
279    }
280    }
281   
 
282  1 toggle @Test
283    public void getRoleTypeWithWrongRole() throws Exception
284    {
285  1 getMockery().checking(new Expectations()
286    {
 
287  1 toggle {
288  1 oneOf(componentDoc).getObject(COMPONENT_CLASS);
289  1 will(returnValue(componentObject));
290  1 oneOf(componentObject).getStringValue(COMPONENT_ROLE_TYPE_FIELD);
291  1 will(returnValue("org.xwiki.component.wiki.DoesNotExist"));
292    }
293    });
294   
295  1 try {
296  1 this.bridge.getRoleType(DOC_REFERENCE);
297  0 Assert.fail("Should have thrown an exception");
298    } catch (WikiComponentException expected) {
299  1 Assert.assertEquals("The role type [org.xwiki.component.wiki.DoesNotExist] does not exist",
300    expected.getMessage());
301    }
302    }
303   
 
304  1 toggle @Test
305    public void getScope() throws Exception
306    {
307  1 getMockery().checking(new Expectations()
308    {
 
309  1 toggle {
310  1 oneOf(componentDoc).getObject(COMPONENT_CLASS);
311  1 will(returnValue(componentObject));
312  1 oneOf(componentObject).getStringValue(COMPONENT_SCOPE_FIELD);
313  1 will(returnValue("user"));
314    }
315    });
316   
317  1 Assert.assertEquals(WikiComponentScope.USER, bridge.getScope(DOC_REFERENCE));
318    }
319   
 
320  1 toggle @Test
321    public void getScopeWithWrongScope() throws Exception
322    {
323  1 getMockery().checking(new Expectations()
324    {
 
325  1 toggle {
326  1 oneOf(componentDoc).getObject(COMPONENT_CLASS);
327  1 will(returnValue(componentObject));
328  1 oneOf(componentObject).getStringValue(COMPONENT_SCOPE_FIELD);
329  1 will(returnValue("doesnotexist"));
330    }
331    });
332   
333    // Wiki is the default value
334  1 Assert.assertEquals(WikiComponentScope.WIKI, this.bridge.getScope(DOC_REFERENCE));
335    }
336   
 
337  1 toggle @Test
338    public void getSyntax() throws Exception
339    {
340  1 getMockery().checking(new Expectations()
341    {
 
342  1 toggle {
343  1 oneOf(componentDoc).getSyntax();
344  1 will(returnValue(Syntax.XWIKI_2_1));
345    }
346    });
347   
348  1 Assert.assertEquals(Syntax.XWIKI_2_1, bridge.getSyntax(DOC_REFERENCE));
349    }
350    }