1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.uiextension

File WikiUIExtensionComponentBuilderTest.java

 

Code metrics

0
57
6
1
194
144
9
0.16
9.5
6
1.5

Classes

Class Line # Actions
WikiUIExtensionComponentBuilderTest 61 57 0% 9 3
0.9523809695.2%
 

Contributing tests

This file is covered by 4 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.uiextension;
21   
22    import java.io.StringWriter;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.List;
26    import java.util.Locale;
27    import java.util.Map;
28   
29    import org.apache.velocity.VelocityContext;
30    import org.junit.Before;
31    import org.junit.Rule;
32    import org.junit.Test;
33    import org.xwiki.component.internal.multi.DelegateComponentManager;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.component.wiki.WikiComponent;
36    import org.xwiki.component.wiki.WikiComponentBuilder;
37    import org.xwiki.component.wiki.WikiComponentException;
38    import org.xwiki.component.wiki.internal.bridge.ContentParser;
39    import org.xwiki.model.ModelContext;
40    import org.xwiki.model.reference.DocumentReference;
41    import org.xwiki.model.reference.DocumentReferenceResolver;
42    import org.xwiki.model.reference.ObjectReference;
43    import org.xwiki.rendering.syntax.Syntax;
44    import org.xwiki.rendering.transformation.RenderingContext;
45    import org.xwiki.rendering.transformation.Transformation;
46    import org.xwiki.test.mockito.MockitoComponentMockingRule;
47    import org.xwiki.uiextension.internal.WikiUIExtensionComponentBuilder;
48    import org.xwiki.uiextension.internal.WikiUIExtensionConstants;
49    import org.xwiki.velocity.VelocityEngine;
50    import org.xwiki.velocity.VelocityManager;
51   
52    import com.xpn.xwiki.doc.XWikiDocument;
53    import com.xpn.xwiki.objects.BaseObject;
54    import com.xpn.xwiki.objects.BaseObjectReference;
55    import com.xpn.xwiki.test.MockitoOldcoreRule;
56   
57    import static org.junit.Assert.*;
58    import static org.mockito.ArgumentMatchers.*;
59    import static org.mockito.Mockito.*;
60   
 
61    public class WikiUIExtensionComponentBuilderTest implements WikiUIExtensionConstants
62    {
63    public MockitoComponentMockingRule<WikiComponentBuilder> mocker =
64    new MockitoComponentMockingRule<WikiComponentBuilder>(WikiUIExtensionComponentBuilder.class,
65    WikiComponentBuilder.class, "uiextension");
66   
67    @Rule
68    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule(mocker);
69   
70    private static final DocumentReference DOC_REF = new DocumentReference("xwiki", "XWiki", "MyUIExtension",
71    Locale.ROOT);
72   
73    private static final DocumentReference AUTHOR_REFERENCE = new DocumentReference("xwiki", "XWiki", "Admin");
74   
75    private XWikiDocument componentDoc;
76   
 
77  4 toggle @Before
78    public void configure() throws Exception
79    {
80    // Required by BaseObjectReference
81  4 DocumentReferenceResolver<String> resolver =
82    this.mocker.registerMockComponent(DocumentReferenceResolver.TYPE_STRING);
83  4 when(resolver.resolve("XWiki.UIExtension")).thenReturn(
84    new DocumentReference("xwiki", "XWiki", "UIExtensionClass"));
85   
86  4 DelegateComponentManager wikiComponentManager = new DelegateComponentManager();
87  4 wikiComponentManager.setComponentManager(this.mocker);
88  4 this.mocker.registerComponent(ComponentManager.class, "wiki", wikiComponentManager);
89   
90    // Components accessed through dynamic lookup.
91  4 VelocityManager velocityManager = this.mocker.registerMockComponent(VelocityManager.class);
92  4 when(velocityManager.getVelocityEngine()).thenReturn(mock(VelocityEngine.class));
93  4 when(velocityManager.getVelocityContext()).thenReturn(mock(VelocityContext.class));
94   
95  4 ModelContext modelContext = this.mocker.registerMockComponent(ModelContext.class);
96  4 when(modelContext.getCurrentEntityReference()).thenReturn(DOC_REF);
97   
98  4 this.mocker.registerMockComponent(RenderingContext.class);
99  4 this.mocker.registerMockComponent(Transformation.class, "macro");
100  4 this.mocker.registerMockComponent(ContentParser.class);
101   
102    // The document holding the UI extension object.
103  4 this.componentDoc = mock(XWikiDocument.class, "xwiki:XWiki.MyUIExtension");
104  4 when(this.componentDoc.getDocumentReference()).thenReturn(DOC_REF);
105  4 when(this.componentDoc.getAuthorReference()).thenReturn(AUTHOR_REFERENCE);
106  4 when(this.componentDoc.getContentAuthorReference()).thenReturn(AUTHOR_REFERENCE);
107  4 when(this.componentDoc.getSyntax()).thenReturn(Syntax.XWIKI_2_1);
108  4 this.oldcore.getDocuments().put(DOC_REF, componentDoc);
109    }
110   
 
111  1 toggle @Test
112    public void buildComponentsWithoutExtensionObject() throws Exception
113    {
114  1 when(this.componentDoc.getXObjects(UI_EXTENSION_CLASS)).thenReturn(Collections.<BaseObject>emptyList());
115   
116  1 try {
117  1 this.mocker.getComponentUnderTest().buildComponents(DOC_REF);
118  0 fail("Should have thrown an exception");
119    } catch (WikiComponentException expected) {
120  1 assertEquals("No UI extension object could be found in document [xwiki:XWiki.MyUIExtension()]",
121    expected.getMessage());
122    }
123    }
124   
 
125  1 toggle @Test
126    public void buildGlobalComponentsWithoutPR() throws Exception
127    {
128  1 BaseObject extensionObject = createExtensionObject("id", "extensionPointId", "content", "parameters", "global");
129  1 when(this.componentDoc.getXObjects(UI_EXTENSION_CLASS)).thenReturn(Arrays.asList(null, extensionObject));
130   
131  1 try {
132  1 this.mocker.getComponentUnderTest().buildComponents(DOC_REF);
133  0 fail("You shouldn't be able to register global UI extensions without PR.");
134    } catch (WikiComponentException expected) {
135  1 assertEquals("Registering global UI extensions requires programming rights", expected.getMessage());
136    }
137    }
138   
 
139  1 toggle @Test
140    public void buildWikiLevelComponentsWithoutAdminRights() throws Exception
141    {
142  1 BaseObject extensionObject = createExtensionObject("id", "extensionPointId", "content", "parameters", "wiki");
143  1 when(this.componentDoc.getXObjects(UI_EXTENSION_CLASS)).thenReturn(Arrays.asList(null, extensionObject));
144   
145  1 try {
146  1 this.mocker.getComponentUnderTest().buildComponents(DOC_REF);
147  0 fail("You shouldn't be able to register UI extensions at wiki level without wiki admin rights.");
148    } catch (WikiComponentException expected) {
149  1 assertEquals("Registering UI extensions at wiki level requires wiki administration rights",
150    expected.getMessage());
151    }
152    }
153   
 
154  1 toggle @Test
155    public void buildComponents() throws Exception
156    {
157  1 BaseObject extensionObject =
158    createExtensionObject("name", "extensionPointId", "content",
159    "key=value=foo\nkey2=value2\ninvalid=\n\n=invalid", "user");
160  1 when(this.componentDoc.getXObjects(UI_EXTENSION_CLASS)).thenReturn(Arrays.asList(null, extensionObject));
161   
162  1 ContentParser contentParser = this.mocker.getInstance(ContentParser.class);
163  1 VelocityManager velocityManager = this.mocker.getInstance(VelocityManager.class);
164   
165  1 List<WikiComponent> components = this.mocker.getComponentUnderTest().buildComponents(DOC_REF);
166   
167  1 assertEquals(1, components.size());
168  1 verify(contentParser).parse("content", Syntax.XWIKI_2_1, DOC_REF);
169   
170  1 UIExtension uiExtension = (UIExtension) components.get(0);
171  1 Map<String, String> parameters = uiExtension.getParameters();
172   
173  1 assertEquals(2, parameters.size());
174  1 verify(velocityManager.getVelocityEngine()).evaluate(any(VelocityContext.class), any(StringWriter.class),
175    eq("name:key"), eq("value=foo"));
176  1 verify(velocityManager.getVelocityEngine()).evaluate(any(VelocityContext.class), any(StringWriter.class),
177    eq("name:key2"), eq("value2"));
178    }
179   
 
180  3 toggle private BaseObject createExtensionObject(String id, String extensionPointId, String content, String parameters,
181    String scope)
182    {
183  3 BaseObject extensionObject = mock(BaseObject.class, id);
184  3 when(extensionObject.getStringValue(ID_PROPERTY)).thenReturn(id);
185  3 when(extensionObject.getStringValue(EXTENSION_POINT_ID_PROPERTY)).thenReturn(extensionPointId);
186  3 when(extensionObject.getStringValue(CONTENT_PROPERTY)).thenReturn(content);
187  3 when(extensionObject.getStringValue(PARAMETERS_PROPERTY)).thenReturn(parameters);
188  3 when(extensionObject.getStringValue(SCOPE_PROPERTY)).thenReturn(scope);
189  3 BaseObjectReference objectReference =
190    new BaseObjectReference(new ObjectReference("XWiki.UIExtensionClass[0]", DOC_REF));
191  3 when(extensionObject.getReference()).thenReturn(objectReference);
192  3 return extensionObject;
193    }
194    }