use of com.xpn.xwiki.objects.BaseObjectReference in project xwiki-platform by xwiki.
the class SolrReferenceResolverTest method setUp.
@Before
public void setUp() throws Exception {
this.defaultSolrReferenceResolver = this.mocker.getInstance(SolrReferenceResolver.class);
Utils.setComponentManager(this.mocker);
// XWiki
this.xwiki = mock(XWiki.class);
// XWikiContext
this.xcontext = new XWikiContext();
this.xcontext.setWikiId("xwiki");
this.xcontext.setWiki(this.xwiki);
// XWikiContext Provider
Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
when(xcontextProvider.get()).thenReturn(this.xcontext);
// XWikiContext trough Execution
Execution execution = this.mocker.getInstance(Execution.class);
execution.setContext(new ExecutionContext());
execution.getContext().setProperty(XWikiContext.EXECUTIONCONTEXT_KEY, this.xcontext);
// References
this.objectReference1221 = new BaseObjectReference(this.classReference111, 0, this.documentReference122);
this.objectReference1222 = new BaseObjectReference(this.classReference111, 1, this.documentReference122);
this.propertyReference12221 = new ObjectPropertyReference("aStringProperty", objectReference1222);
this.propertyReference12223 = new ObjectPropertyReference("anIntegerProperty", objectReference1222);
// XWiki model data
this.xclass111 = mock(BaseClass.class);
this.xdocument111 = mock(XWikiDocument.class, "xwikiDocument111");
this.xdocument112 = mock(XWikiDocument.class, "xwikiDocument112");
this.xdocument113 = mock(XWikiDocument.class, "xwikiDocument113");
this.xdocument121 = mock(XWikiDocument.class, "xwikiDocument121");
this.xattachment1211 = mock(XWikiAttachment.class, "xwikiAttachment1211");
this.xattachment1212 = mock(XWikiAttachment.class, "xwikiAttachment1212");
this.xdocument122 = mock(XWikiDocument.class, "xwikiDocument122");
this.xobject1221 = mock(BaseObject.class, "xwikiObject1221");
this.xobject1222 = mock(BaseObject.class, "xwikiObject1222");
this.xproperty12221 = mock(StringProperty.class, "xwikiProperty12221");
this.xproperty12223 = mock(IntegerProperty.class, "xwikiProperty12223");
this.queryManager = this.mocker.getInstance(QueryManager.class);
final Query spacesWiki1Query = mock(DefaultQuery.class, "getSpacesWiki1");
final Query documentsSpace11Query = mock(DefaultQuery.class, "getSpaceDocsNameSpace11");
final Query documentsSpace12Query = mock(DefaultQuery.class, "getSpaceDocsNameSpace12");
final Query documentsSpace13Query = mock(DefaultQuery.class, "getSpaceDocsNameSpace13");
final Query spacesWiki2Query = mock(DefaultQuery.class, "getSpacesWiki2");
// Data
when(xwiki.exists(any(DocumentReference.class), any(XWikiContext.class))).thenReturn(true);
// Query manager and specific queries mocking.
when(queryManager.getNamedQuery("getSpaces")).thenReturn(spacesWiki1Query);
when(spacesWiki1Query.setWiki(wikiReference1.getName())).thenReturn(spacesWiki1Query);
when(spacesWiki1Query.setWiki(wikiReference2.getName())).thenReturn(spacesWiki2Query);
when(queryManager.getNamedQuery("getSpaceDocsName")).thenReturn(documentsSpace11Query);
when(documentsSpace11Query.setWiki(any(String.class))).thenReturn(documentsSpace11Query);
EntityReferenceSerializer<String> localEntityReferenceSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING, "local");
when(documentsSpace11Query.bindValue("space", localEntityReferenceSerializer.serialize(spaceReference11))).thenReturn(documentsSpace11Query);
when(documentsSpace11Query.bindValue("space", localEntityReferenceSerializer.serialize(spaceReference12))).thenReturn(documentsSpace12Query);
when(documentsSpace11Query.bindValue("space", localEntityReferenceSerializer.serialize(spaceReference13))).thenReturn(documentsSpace13Query);
// Spaces in wikis.
when(spacesWiki1Query.execute()).thenReturn(Arrays.<Object>asList(localEntityReferenceSerializer.serialize(spaceReference11), localEntityReferenceSerializer.serialize(spaceReference12), localEntityReferenceSerializer.serialize(spaceReference13)));
when(spacesWiki2Query.execute()).thenReturn(Collections.emptyList());
// space 11
when(documentsSpace11Query.execute()).thenReturn(Arrays.<Object>asList(classReference111.getName(), documentReference112.getName(), documentReference113.getName()));
// document 111
when(xwiki.getDocument(eq(documentReference111), any(XWikiContext.class))).thenReturn(xdocument111);
when(xdocument111.getXClass()).thenReturn(xclass111);
// document 112
when(xwiki.getDocument(eq(documentReference112), any(XWikiContext.class))).thenReturn(xdocument112);
when(xdocument112.getXObjects()).thenReturn(Collections.<DocumentReference, List<BaseObject>>emptyMap());
when(xdocument112.getTranslationLocales(any(XWikiContext.class))).thenReturn(Collections.<Locale>emptyList());
// document 113
when(xwiki.getDocument(eq(documentReference113), any(XWikiContext.class))).thenReturn(xdocument113);
when(xdocument113.getAttachmentList()).thenReturn(Collections.<XWikiAttachment>emptyList());
when(xdocument113.getXObjects()).thenReturn(Collections.<DocumentReference, List<BaseObject>>emptyMap());
when(xdocument113.getTranslationLocales(any(XWikiContext.class))).thenReturn(Arrays.asList(new Locale("ro")));
// space 12
when(documentsSpace12Query.execute()).thenReturn(Arrays.<Object>asList(documentReference121.getName(), documentReference122.getName()));
// document 121
when(xwiki.getDocument(eq(documentReference121), any(XWikiContext.class))).thenReturn(xdocument121);
when(xdocument121.getAttachmentList()).thenReturn(Arrays.asList(xattachment1211, xattachment1212));
when(xattachment1211.getReference()).thenReturn(attachmentReference1211);
when(xattachment1212.getReference()).thenReturn(attachmentReference1212);
when(xdocument121.getXObjects()).thenReturn(Collections.<DocumentReference, List<BaseObject>>emptyMap());
when(xdocument121.getTranslationLocales(any(XWikiContext.class))).thenReturn(Collections.<Locale>emptyList());
// document 122
when(xwiki.getDocument(eq(documentReference122), any(XWikiContext.class))).thenReturn(xdocument122);
when(xdocument122.getAttachmentList()).thenReturn(Collections.<XWikiAttachment>emptyList());
Map<DocumentReference, List<BaseObject>> xObjects = new HashMap<DocumentReference, List<BaseObject>>();
// Yes, it seems that we can have null objects for some reason.
xObjects.put(classReference111, Arrays.asList(null, xobject1221, xobject1222));
when(xdocument122.getXObjects()).thenReturn(xObjects);
when(xdocument122.getTranslationLocales(any(XWikiContext.class))).thenReturn(Collections.<Locale>emptyList());
// object 1221
when(xdocument122.getXObject(objectReference1221)).thenReturn(xobject1221);
when(xdocument122.getXObject((EntityReference) objectReference1221)).thenReturn(xobject1221);
when(xobject1221.getReference()).thenReturn(objectReference1221);
when(xobject1221.getXClass(any(XWikiContext.class))).thenReturn(xclass111);
when(xobject1221.getFieldList()).thenReturn(Collections.EMPTY_LIST);
// object 1222
when(xdocument122.getXObject(objectReference1222)).thenReturn(xobject1222);
when(xdocument122.getXObject((EntityReference) objectReference1222)).thenReturn(xobject1222);
when(xobject1222.getReference()).thenReturn(objectReference1222);
when(xobject1222.getXClass(any(XWikiContext.class))).thenReturn(xclass111);
when(xobject1222.getFieldList()).thenReturn(Arrays.asList(xproperty12221, xproperty12223));
// object 1222 fields
when(xproperty12221.getReference()).thenReturn(propertyReference12221);
when(xproperty12221.getName()).thenReturn(propertyReference12221.getName());
when(xproperty12223.getReference()).thenReturn(propertyReference12223);
when(xproperty12223.getName()).thenReturn(propertyReference12223.getName());
// class 111 fields
when(xclass111.get(propertyReference12221.getName())).thenReturn(null);
when(xclass111.get(propertyReference12223.getName())).thenReturn(null);
// space 13
when(documentsSpace13Query.execute()).thenReturn(Collections.emptyList());
}
use of com.xpn.xwiki.objects.BaseObjectReference in project xwiki-platform by xwiki.
the class WikiUIExtensionComponentBuilderTest method createExtensionObject.
private BaseObject createExtensionObject(String id, String extensionPointId, String content, String parameters, String scope) {
BaseObject extensionObject = mock(BaseObject.class, id);
when(extensionObject.getStringValue(ID_PROPERTY)).thenReturn(id);
when(extensionObject.getStringValue(EXTENSION_POINT_ID_PROPERTY)).thenReturn(extensionPointId);
when(extensionObject.getStringValue(CONTENT_PROPERTY)).thenReturn(content);
when(extensionObject.getStringValue(PARAMETERS_PROPERTY)).thenReturn(parameters);
when(extensionObject.getStringValue(SCOPE_PROPERTY)).thenReturn(scope);
BaseObjectReference objectReference = new BaseObjectReference(new ObjectReference("XWiki.UIExtensionClass[0]", DOC_REF));
when(extensionObject.getReference()).thenReturn(objectReference);
when(extensionObject.getOwnerDocument()).thenReturn(this.componentDoc);
return extensionObject;
}
use of com.xpn.xwiki.objects.BaseObjectReference in project xwiki-platform by xwiki.
the class WikiObjectComponentManagerEventListenerProxy method registerAllObjectComponents.
/**
* This method goes through every XObject known
* (using {@link WikiObjectComponentManagerEventListenerProxy#collectWikiObjectsList()})
* to be able to create a component and then builds those components.
*/
public void registerAllObjectComponents() {
XWikiContext xWikiContext = this.xWikiContextProvider.get();
String builderHelper;
// For every classes subject to WikiComponents
for (EntityReference xObjectClass : this.collectWikiObjectsList()) {
try {
// Query every document having an XObject of the given class attached
Query query = queryManager.createQuery("select distinct doc.fullName from Document doc, " + "doc.object(" + this.entityReferenceSerializer.serialize(xObjectClass) + ") as document", Query.XWQL);
List<String> results = query.execute();
DocumentReference sourceDocumentReference;
for (String result : results) {
sourceDocumentReference = this.currentDocumentReferenceResolver.resolve(result);
XWikiDocument document = xWikiContext.getWiki().getDocument(sourceDocumentReference, xWikiContext);
for (BaseObject xObject : document.getXObjects(xObjectClass)) {
BaseObjectReference xObjectReference = xObject.getReference();
builderHelper = this.entityReferenceSerializer.serialize(xObjectReference.getXClassReference());
this.registerObjectComponents(xObjectReference, xObject, this.componentManager.getInstance(WikiObjectComponentBuilder.class, builderHelper));
}
}
} catch (Exception e) {
logger.warn(String.format("Unable to register the components for [%s] XObjects: %s", xObjectClass, ExceptionUtils.getRootCauseMessage(e)));
}
}
}
use of com.xpn.xwiki.objects.BaseObjectReference in project xwiki-platform by xwiki.
the class DefaultWikiObjectComponentManagerEventListenerTest method verifyXObjectAddOrUpdate.
private void verifyXObjectAddOrUpdate(AbstractDocumentEvent event) throws Exception {
XWikiDocument fakeSource = mock(XWikiDocument.class);
XWikiDocument fakeOldSource = mock(XWikiDocument.class);
BaseObject xObject = mock(BaseObject.class);
BaseObjectReference xObjectReference = mock(BaseObjectReference.class);
DocumentReference xObjectClassReference = (DocumentReference) this.xClassReferences.get(0);
when(xObject.getReference()).thenReturn(xObjectReference);
Map<DocumentReference, List<BaseObject>> fakeDocumentXObjects = new HashMap<>();
fakeDocumentXObjects.put(xObjectClassReference, Collections.singletonList(xObject));
when(fakeSource.getXObjects()).thenReturn(fakeDocumentXObjects);
when(fakeSource.getOriginalDocument()).thenReturn(fakeOldSource);
WikiObjectComponentBuilder builder = mockAssociatedComponentBuilderMethod(xObject);
this.mocker.getComponentUnderTest().onEvent(event, fakeSource, null);
verify(this.wikiObjectComponentManagerEventListenerProxy, times(1)).registerObjectComponents(xObjectReference, xObject, builder);
}
use of com.xpn.xwiki.objects.BaseObjectReference in project xwiki-platform by xwiki.
the class DefaultWikiObjectComponentManagerEventListenerTest method testOnDocumentDeletedEvent.
@Test
public void testOnDocumentDeletedEvent() throws Exception {
DocumentDeletedEvent event = mock(DocumentDeletedEvent.class);
XWikiDocument source = mock(XWikiDocument.class);
XWikiDocument oldXObjectDocument = mock(XWikiDocument.class);
BaseObject xObject = mock(BaseObject.class);
BaseObjectReference xObjectReference = mock(BaseObjectReference.class);
when(source.getOriginalDocument()).thenReturn(oldXObjectDocument);
when(xObject.getReference()).thenReturn(xObjectReference);
DocumentReference xObjectClassReference = (DocumentReference) this.xClassReferences.get(0);
Map<DocumentReference, List<BaseObject>> fakeDocumentXObjects = new HashMap<>();
fakeDocumentXObjects.put(xObjectClassReference, Collections.singletonList(xObject));
when(oldXObjectDocument.getXObjects()).thenReturn(fakeDocumentXObjects);
mockAssociatedComponentBuilderMethod(xObject);
this.mocker.getComponentUnderTest().onEvent(event, source, null);
verify(this.wikiObjectComponentManagerEventListenerProxy, times(1)).unregisterObjectComponents(xObjectReference);
}
Aggregations