Search in sources :

Example 6 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class DefaultIOTargetServiceTest method testGettersWhenTargetIsTypedDocument.

@Test
public void testGettersWhenTargetIsTypedDocument() throws Exception {
    final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            allowing(dabMock).getTranslatedDocumentInstance(new DocumentReference("wiki", "Space", "Page"));
            will(returnValue(dmb));
            oneOf(dmb).getContent();
            will(returnValue("defcontent"));
            oneOf(dmb).getSyntax();
            will(returnValue(new Syntax(SyntaxType.XWIKI, "2.0")));
        }
    });
    String reference = "DOCUMENT://wiki:Space.Page";
    assertEquals("defcontent", ioTargetService.getSource(reference));
    assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 7 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class DefaultIOTargetServiceTest method testGetterWhenTargetIsTypedObjectPropertyInRelativeDocument.

@Test
public void testGetterWhenTargetIsTypedObjectPropertyInRelativeDocument() throws Exception {
    final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            allowing(classResolver).resolve("XWiki.Class", new DocumentReference("xwiki", "Main", "Page"));
            will(returnValue(new DocumentReference("xwiki", "XWiki", "Class")));
            oneOf(dabMock).getProperty(new DocumentReference("xwiki", "Main", "Page"), new DocumentReference("xwiki", "XWiki", "Class"), "property");
            will(returnValue("defcontent"));
            oneOf(dabMock).getTranslatedDocumentInstance(new DocumentReference("xwiki", "Main", "Page"));
            will(returnValue(dmb));
            oneOf(dmb).getSyntax();
            will(returnValue(new Syntax(SyntaxType.XWIKI, "2.0")));
        }
    });
    String reference = "OBJECT_PROPERTY://Page^XWiki.Class.property";
    assertEquals("defcontent", ioTargetService.getSource(reference));
    assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 8 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class DefaultIOTargetServiceTest method testGetterWhenTargetIsNonTypedObjectProperty.

@Test
public void testGetterWhenTargetIsNonTypedObjectProperty() throws Exception {
    final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            // target will be parsed as document, because document is the default
            allowing(dabMock).getTranslatedDocumentInstance(new DocumentReference("wiki", "Space.Page^XWiki.Class", "property"));
            will(returnValue(dmb));
            oneOf(dmb).getContent();
            will(returnValue("defcontent"));
            oneOf(dmb).getSyntax();
            will(returnValue(new Syntax(SyntaxType.XWIKI, "2.0")));
        }
    });
    String reference = "wiki:Space\\.Page^XWiki\\.Class.property";
    assertEquals("defcontent", ioTargetService.getSource(reference));
    assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 9 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class DefaultIOTargetServiceTest method testGettersWhenTargetIsNonTypedRelativeDocument.

@Test
public void testGettersWhenTargetIsNonTypedRelativeDocument() throws Exception {
    final DocumentModelBridge dmb = getMockery().mock(DocumentModelBridge.class);
    getMockery().checking(new Expectations() {

        {
            // default resolver should be used
            allowing(dabMock).getTranslatedDocumentInstance(new DocumentReference("xwiki", "Space", "Page"));
            will(returnValue(dmb));
            oneOf(dmb).getContent();
            will(returnValue("defcontent"));
            oneOf(dmb).getSyntax();
            will(returnValue(new Syntax(SyntaxType.XWIKI, "2.0")));
        }
    });
    String reference = "Space.Page";
    assertEquals("defcontent", ioTargetService.getSource(reference));
    assertEquals("xwiki/2.0", ioTargetService.getSourceSyntax(reference));
}
Also used : Expectations(org.jmock.Expectations) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) Syntax(org.xwiki.rendering.syntax.Syntax) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 10 with DocumentReference

use of org.xwiki.model.reference.DocumentReference in project xwiki-platform by xwiki.

the class AnnotationEventGeneratorEventListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    // Don`t rely on the context from the data parameter.
    XWikiContext context = (XWikiContext) execution.getContext().getProperty("xwikicontext");
    String currentWiki = context.getWikiId();
    try {
        XWikiDocument document = (XWikiDocument) source;
        String wikiOfAffectedDocument = document.getDocumentReference().getWikiReference().getName();
        // Always work on the wiki of the source document. The Annotation Application's configuration looks at the
        // context to provide values for the current wiki. Objects could be modified cross-wiki and the context
        // database might not be right.
        context.setWikiId(wikiOfAffectedDocument);
        // Only work if the Annotations Application is installed on the wiki.
        if (!annotationConfiguration.isInstalled()) {
            return;
        }
        // Extract the BaseObjectReference to be able to inspect the XClassReference.
        BaseObjectReference objectReference = getBaseObjectReference((XObjectEvent) event);
        DocumentReference objectClassReference = objectReference.getXClassReference();
        // Only interested in objects that are of the same class as the currently configured annotation class.
        if (!objectClassReference.equals(annotationConfiguration.getAnnotationClassReference())) {
            return;
        }
        // The object is needed for the final check. See below.
        BaseObject object = document.getXObject(objectReference);
        // Build the new event to launch using the current document reference and object number.
        Event newEvent = null;
        String documentReference = defaultEntityReferenceSerializer.serialize(document.getDocumentReference());
        String number = String.valueOf(objectReference.getObjectNumber());
        if (event instanceof XObjectAddedEvent) {
            newEvent = new AnnotationAddedEvent(documentReference, number);
        } else if (event instanceof XObjectUpdatedEvent) {
            newEvent = new AnnotationUpdatedEvent(documentReference, number);
        } else if (event instanceof XObjectDeletedEvent) {
            // Current document might be deleted. Always use the original document for *Deleted events.
            object = document.getOriginalDocument().getXObject(objectReference);
            newEvent = new AnnotationDeletedEvent(documentReference, number);
        }
        // Handle specially the default annotations class which coincides with the default comments class. We need
        // to avoid mistaking comments for annotations.
        DocumentReference defaultCommentsClassReference = context.getWiki().getCommentsClass(context).getDocumentReference();
        if (defaultCommentsClassReference.equals(object.getXClassReference())) {
            // A comment is considered an annotation when it has a text selection.
            String selection = object.getStringValue(Annotation.SELECTION_FIELD);
            if (selection == null || selection.trim().length() == 0) {
                // This is a simple comment. Skip it.
                return;
            }
        }
        // Launch the new event.
        observationManager.get().notify(newEvent, source, context);
    } catch (Exception e) {
        logger.error("Failed to handle event of type [{}]", event.getClass().getName(), e);
    } finally {
        // Restore the context database.
        context.setWikiId(currentWiki);
    }
}
Also used : AnnotationUpdatedEvent(org.xwiki.annotation.event.AnnotationUpdatedEvent) XObjectAddedEvent(com.xpn.xwiki.internal.event.XObjectAddedEvent) XWikiContext(com.xpn.xwiki.XWikiContext) BaseObject(com.xpn.xwiki.objects.BaseObject) XObjectUpdatedEvent(com.xpn.xwiki.internal.event.XObjectUpdatedEvent) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) BaseObjectReference(com.xpn.xwiki.objects.BaseObjectReference) AnnotationAddedEvent(org.xwiki.annotation.event.AnnotationAddedEvent) AnnotationAddedEvent(org.xwiki.annotation.event.AnnotationAddedEvent) XObjectEvent(com.xpn.xwiki.internal.event.XObjectEvent) AnnotationUpdatedEvent(org.xwiki.annotation.event.AnnotationUpdatedEvent) XObjectUpdatedEvent(com.xpn.xwiki.internal.event.XObjectUpdatedEvent) XObjectAddedEvent(com.xpn.xwiki.internal.event.XObjectAddedEvent) XObjectDeletedEvent(com.xpn.xwiki.internal.event.XObjectDeletedEvent) AnnotationDeletedEvent(org.xwiki.annotation.event.AnnotationDeletedEvent) Event(org.xwiki.observation.event.Event) AnnotationDeletedEvent(org.xwiki.annotation.event.AnnotationDeletedEvent) XObjectDeletedEvent(com.xpn.xwiki.internal.event.XObjectDeletedEvent) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

DocumentReference (org.xwiki.model.reference.DocumentReference)1324 Test (org.junit.Test)711 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)482 BaseObject (com.xpn.xwiki.objects.BaseObject)250 XWikiContext (com.xpn.xwiki.XWikiContext)186 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)157 ArrayList (java.util.ArrayList)128 WikiReference (org.xwiki.model.reference.WikiReference)127 XWikiException (com.xpn.xwiki.XWikiException)121 EntityReference (org.xwiki.model.reference.EntityReference)113 SpaceReference (org.xwiki.model.reference.SpaceReference)96 XWiki (com.xpn.xwiki.XWiki)82 HashMap (java.util.HashMap)54 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)52 Expectations (org.jmock.Expectations)50 Before (org.junit.Before)50 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)46 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)45 AttachmentReference (org.xwiki.model.reference.AttachmentReference)44 Date (java.util.Date)42