| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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}) |
| |
|
| 98.6% |
Uncovered Elements: 2 (139) |
Complexity: 26 |
Complexity Density: 0.23 |
|
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 1 |
Complexity Density: 0.07 |
|
| 97 |
11 |
@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 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 122 |
11 |
{... |
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 135 |
1 |
@Test... |
| 136 |
|
public void getAuthorReference() throws Exception |
| 137 |
|
{ |
| 138 |
1 |
getMockery().checking(new Expectations() |
| 139 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 140 |
1 |
{... |
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 149 |
1 |
@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 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 158 |
1 |
{... |
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
| 171 |
1 |
@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 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
|
| 180 |
1 |
{... |
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
| 197 |
1 |
@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 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
| 210 |
1 |
{... |
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 230 |
1 |
@Test... |
| 231 |
|
public void getRoleHint() throws Exception |
| 232 |
|
{ |
| 233 |
1 |
getMockery().checking(new Expectations() |
| 234 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 235 |
1 |
{... |
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 246 |
1 |
@Test... |
| 247 |
|
public void getRoleType() throws Exception |
| 248 |
|
{ |
| 249 |
1 |
getMockery().checking(new Expectations() |
| 250 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 251 |
1 |
{... |
| 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 |
|
|
| |
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
| 262 |
1 |
@Test... |
| 263 |
|
public void getRoleTypeWithoutComponentObject() throws Exception |
| 264 |
|
{ |
| 265 |
1 |
getMockery().checking(new Expectations() |
| 266 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 267 |
1 |
{... |
| 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 |
|
|
| |
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.4 |
1PASS
|
|
| 282 |
1 |
@Test... |
| 283 |
|
public void getRoleTypeWithWrongRole() throws Exception |
| 284 |
|
{ |
| 285 |
1 |
getMockery().checking(new Expectations() |
| 286 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 287 |
1 |
{... |
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 304 |
1 |
@Test... |
| 305 |
|
public void getScope() throws Exception |
| 306 |
|
{ |
| 307 |
1 |
getMockery().checking(new Expectations() |
| 308 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 309 |
1 |
{... |
| 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 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 320 |
1 |
@Test... |
| 321 |
|
public void getScopeWithWrongScope() throws Exception |
| 322 |
|
{ |
| 323 |
1 |
getMockery().checking(new Expectations() |
| 324 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
| 325 |
1 |
{... |
| 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 |
|
|
| 334 |
1 |
Assert.assertEquals(WikiComponentScope.WIKI, this.bridge.getScope(DOC_REFERENCE)); |
| 335 |
|
} |
| 336 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
| 337 |
1 |
@Test... |
| 338 |
|
public void getSyntax() throws Exception |
| 339 |
|
{ |
| 340 |
1 |
getMockery().checking(new Expectations() |
| 341 |
|
{ |
| |
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 342 |
1 |
{... |
| 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 |
|
} |