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

File WikiUIExtensionComponentBuilderTest.java

 

Code metrics

0
48
5
1
195
142
5
0.1
9.6
5
1

Classes

Class Line # Actions
WikiUIExtensionComponentBuilderTest 69 48 0% 5 0
1.0100%
 

Contributing tests

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