| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
|
| |
|
| 95.2% |
Uncovered Elements: 3 (63) |
Complexity: 9 |
Complexity Density: 0.16 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
|
| 77 |
4 |
@Before... |
| 78 |
|
public void configure() throws Exception |
| 79 |
|
{ |
| 80 |
|
|
| 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 |
|
|
| 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 |
|
|
| 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 |
|
|
| |
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
| 111 |
1 |
@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 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
1PASS
|
|
| 125 |
1 |
@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 |
|
|
| |
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
1PASS
|
|
| 139 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
1PASS
|
|
| 154 |
1 |
@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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
|
| 180 |
3 |
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 |
|
} |