Search in sources :

Example 21 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class MessageStreamTest method testPostGroupMessageWithNonExistingTarget.

@Test(expected = IllegalArgumentException.class)
public void testPostGroupMessageWithNonExistingTarget() throws Exception {
    final DocumentAccessBridge mockBridge = getComponentManager().getInstance(DocumentAccessBridge.class);
    final DocumentReference targetGroup = new DocumentReference("xwiki", "XWiki", "Nobodies");
    getMockery().checking(new Expectations() {

        {
            exactly(1).of(mockBridge).exists(targetGroup);
            will(returnValue(false));
        }
    });
    this.stream.postMessageToGroup("Hello Nobodies!", targetGroup);
}
Also used : Expectations(org.jmock.Expectations) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 22 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class ContextMacroTest method executeWithReferencedDocumentHavingProgrammingRightsButNotTheCallingDocument.

@Test
public void executeWithReferencedDocumentHavingProgrammingRightsButNotTheCallingDocument() throws Exception {
    MacroTransformationContext macroContext = new MacroTransformationContext();
    MacroBlock macroBlock = new MacroBlock("context", Collections.emptyMap(), false);
    macroContext.setCurrentMacroBlock(macroBlock);
    DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
    when(resolver.resolve("wiki:space.page", macroBlock)).thenReturn(new DocumentReference("wiki", "space", "page"));
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    when(dab.hasProgrammingRights()).thenReturn(false).thenReturn(true);
    ContextMacroParameters parameters = new ContextMacroParameters();
    parameters.setDocument("wiki:space.page");
    try {
        this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
        fail("Should have thrown an exception");
    } catch (MacroExecutionException expected) {
        assertEquals("Current document must have programming rights since the context document provided [" + "wiki:space.page] has programming rights.", expected.getMessage());
    }
}
Also used : MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) MacroExecutionException(org.xwiki.rendering.macro.MacroExecutionException) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 23 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class ContextMacroTest method executeWithRelativeDocumentReferenceParameter.

@Test
public void executeWithRelativeDocumentReferenceParameter() throws Exception {
    MacroBlock macroBlock = new MacroBlock("context", Collections.<String, String>emptyMap(), false);
    MacroTransformationContext macroContext = new MacroTransformationContext();
    macroContext.setSyntax(Syntax.XWIKI_2_0);
    macroContext.setCurrentMacroBlock(macroBlock);
    DocumentReferenceResolver<String> resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING, "macro");
    DocumentReference referencedDocumentReference = new DocumentReference("basewiki", "basespace", "page");
    when(resolver.resolve("page", macroBlock)).thenReturn(referencedDocumentReference);
    DocumentAccessBridge dab = this.mocker.getInstance(DocumentAccessBridge.class);
    DocumentModelBridge dmb = mock(DocumentModelBridge.class);
    when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
    MacroContentParser parser = this.mocker.getInstance(MacroContentParser.class);
    when(parser.parse(eq(""), same(macroContext), eq(false), any(MetaData.class), eq(false))).thenReturn(new XDOM(Collections.emptyList()));
    ContextMacroParameters parameters = new ContextMacroParameters();
    parameters.setDocument("page");
    this.mocker.getComponentUnderTest().execute(parameters, "", macroContext);
}
Also used : XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) MacroContentParser(org.xwiki.rendering.macro.MacroContentParser) MetaData(org.xwiki.rendering.listener.MetaData) MacroTransformationContext(org.xwiki.rendering.transformation.MacroTransformationContext) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ContextMacroParameters(org.xwiki.rendering.macro.context.ContextMacroParameters) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Test(org.junit.Test)

Example 24 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class IntegrationTests method initialize.

@RenderingTestSuite.Initialized
public void initialize(MockitoComponentManager componentManager) throws Exception {
    // For performance reasons we mock some components to avoid having to draw all oldcore components
    // Macro Reference Resolver
    DocumentReferenceResolver<String> macroResolver = componentManager.registerMockComponent(new DefaultParameterizedType(null, DocumentReferenceResolver.class, String.class), "macro");
    DocumentReference referencedDocumentReference = new DocumentReference("Wiki", "Space", "Page");
    when(macroResolver.resolve(eq("Space.Page"), any(MacroBlock.class))).thenReturn(referencedDocumentReference);
    // Document Access Bridge mock
    // Simulate the XDOM of the referenced document
    DocumentAccessBridge dab = componentManager.registerMockComponent(DocumentAccessBridge.class);
    DocumentModelBridge dmb = mock(DocumentModelBridge.class);
    when(dab.getTranslatedDocumentInstance(referencedDocumentReference)).thenReturn(dmb);
    Parser parser = componentManager.getInstance(Parser.class, "xwiki/2.1");
    XDOM xdom = parser.parse(new StringReader("= heading1 =\n==heading2=="));
    when(dmb.getXDOM()).thenReturn(xdom);
}
Also used : DocumentReferenceResolver(org.xwiki.model.reference.DocumentReferenceResolver) XDOM(org.xwiki.rendering.block.XDOM) DocumentModelBridge(org.xwiki.bridge.DocumentModelBridge) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) StringReader(java.io.StringReader) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) DocumentReference(org.xwiki.model.reference.DocumentReference) MacroBlock(org.xwiki.rendering.block.MacroBlock) Parser(org.xwiki.rendering.parser.Parser)

Example 25 with DocumentAccessBridge

use of org.xwiki.bridge.DocumentAccessBridge in project xwiki-platform by xwiki.

the class IntegrationTests method initialize.

@RenderingTestSuite.Initialized
public void initialize(ComponentManager componentManager) throws Exception {
    Mockery mockery = new JUnit4Mockery();
    // Skin Access Bridge Mock
    final SkinAccessBridge mockSkinAccessBridge = registerMockComponent(componentManager, mockery, SkinAccessBridge.class);
    mockery.checking(new Expectations() {

        {
            allowing(mockSkinAccessBridge).getSkinFile("icons/xwiki/noavatar.png");
            will(returnValue("/xwiki/resources/icons/xwiki/noavatar.png"));
        }
    });
    // Document Access Bridge Mock
    final DocumentReference adminUserReference = new DocumentReference("wiki", "XWiki", "Admin");
    final DocumentReference userWithoutAvatarReference = new DocumentReference("wiki", "XWiki", "ExistingUserWithoutAvatar");
    final DocumentReference userNotExistingReference = new DocumentReference("wiki", "XWiki", "UserNotExisting");
    final DocumentReference userWithNonExistingAvatarFileReference = new DocumentReference("wiki", "XWiki", "UserWithNonExistingAvatarFile");
    final DocumentReference userWithExceptionRetrievingAvatarFileReference = new DocumentReference("wiki", "XWiki", "UserWithExceptionRetrievingAvatarFile");
    final DocumentReference userClassReference = new DocumentReference("wiki", "XWiki", "XWikiUsers");
    final DocumentAccessBridge mockDocumentAccessBridge = registerMockComponent(componentManager, mockery, DocumentAccessBridge.class);
    mockery.checking(new Expectations() {

        {
            allowing(mockDocumentAccessBridge).exists(adminUserReference);
            will(returnValue(true));
            allowing(mockDocumentAccessBridge).exists(userWithoutAvatarReference);
            will(returnValue(true));
            allowing(mockDocumentAccessBridge).exists(with(any(String.class)));
            will(returnValue(false));
            allowing(mockDocumentAccessBridge).exists(userNotExistingReference);
            will(returnValue(false));
            allowing(mockDocumentAccessBridge).exists(userWithNonExistingAvatarFileReference);
            will(returnValue(true));
            allowing(mockDocumentAccessBridge).exists(userWithExceptionRetrievingAvatarFileReference);
            will(returnValue(true));
            allowing(mockDocumentAccessBridge).getProperty(adminUserReference, userClassReference, "avatar");
            will(returnValue("mockAvatar.png"));
            allowing(mockDocumentAccessBridge).getProperty(userWithoutAvatarReference, userClassReference, "avatar");
            will(returnValue(null));
            allowing(mockDocumentAccessBridge).getProperty(userWithNonExistingAvatarFileReference, userClassReference, "avatar");
            will(returnValue("mockAvatar.png"));
            allowing(mockDocumentAccessBridge).getProperty(userWithExceptionRetrievingAvatarFileReference, userClassReference, "avatar");
            will(returnValue("mockAvatar.png"));
            allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png", adminUserReference));
            will(returnValue("1.1"));
            allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png", userWithNonExistingAvatarFileReference));
            will(returnValue(null));
            allowing(mockDocumentAccessBridge).getAttachmentVersion(new AttachmentReference("mockAvatar.png", userWithExceptionRetrievingAvatarFileReference));
            will(throwException(new Exception("Sum Ting Wong")));
        }
    });
    // Document Resolver Mock
    final DocumentReferenceResolver<String> mockDocumentReferenceResolver = registerMockComponent(componentManager, mockery, DocumentReferenceResolver.TYPE_STRING, "current");
    mockery.checking(new Expectations() {

        {
            allowing(mockDocumentReferenceResolver).resolve("XWiki.Admin", new EntityReference("XWiki", EntityType.SPACE));
            will(returnValue(adminUserReference));
            allowing(mockDocumentReferenceResolver).resolve("XWiki.ExistingUserWithoutAvatar", new EntityReference("XWiki", EntityType.SPACE));
            will(returnValue(userWithoutAvatarReference));
            allowing(mockDocumentReferenceResolver).resolve("XWiki.UserNotExisting", new EntityReference("XWiki", EntityType.SPACE));
            will(returnValue(userNotExistingReference));
            allowing(mockDocumentReferenceResolver).resolve("XWiki.UserWithNonExistingAvatarFile", new EntityReference("XWiki", EntityType.SPACE));
            will(returnValue(userWithNonExistingAvatarFileReference));
            allowing(mockDocumentReferenceResolver).resolve("XWiki.UserWithExceptionRetrievingAvatarFile", new EntityReference("XWiki", EntityType.SPACE));
            will(returnValue(userWithExceptionRetrievingAvatarFileReference));
        }
    });
    // Entity Reference Serializer Mock
    final EntityReferenceSerializer<String> mockEntityReferenceSerializer = registerMockComponent(componentManager, mockery, EntityReferenceSerializer.TYPE_STRING, "compactwiki");
    mockery.checking(new Expectations() {

        {
            allowing(mockEntityReferenceSerializer).serialize(new AttachmentReference("mockAvatar.png", adminUserReference));
            will(returnValue("XWiki.Admin@mockAvatar.png"));
            allowing(mockEntityReferenceSerializer).serialize(userNotExistingReference);
            will(returnValue("XWiki.UserNotExisting"));
            allowing(mockEntityReferenceSerializer).serialize(new AttachmentReference("mockAvatar.png", userWithNonExistingAvatarFileReference));
            will(returnValue("XWiki.UserWithNonExistingAvatarFile@mockAvatar.png"));
            allowing(mockEntityReferenceSerializer).serialize(new AttachmentReference("mockAvatar.png", userWithExceptionRetrievingAvatarFileReference));
            will(returnValue("XWiki.UserWithExceptionRetrievingAvatarFile@mockAvatar.png"));
            allowing(mockEntityReferenceSerializer).serialize(userWithExceptionRetrievingAvatarFileReference);
            will(returnValue("XWiki.UserWithExceptionRetrievingAvatarFile"));
        }
    });
    // Entity Reference Serializer Mock
    final EntityReferenceValueProvider mockEntityReferenceValueProvider = registerMockComponent(componentManager, mockery, EntityReferenceValueProvider.class, "current");
    mockery.checking(new Expectations() {

        {
            allowing(mockEntityReferenceValueProvider).getDefaultValue(EntityType.WIKI);
            will(returnValue("wiki"));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) AttachmentReference(org.xwiki.model.reference.AttachmentReference) EntityReferenceValueProvider(org.xwiki.model.reference.EntityReferenceValueProvider) SkinAccessBridge(org.xwiki.bridge.SkinAccessBridge) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Mockery(org.jmock.Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) JUnit4Mockery(org.jmock.integration.junit4.JUnit4Mockery) EntityReference(org.xwiki.model.reference.EntityReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Aggregations

DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)56 DocumentReference (org.xwiki.model.reference.DocumentReference)39 Test (org.junit.Test)33 Expectations (org.jmock.Expectations)17 DocumentModelBridge (org.xwiki.bridge.DocumentModelBridge)15 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)11 Before (org.junit.Before)10 XDOM (org.xwiki.rendering.block.XDOM)10 Execution (org.xwiki.context.Execution)7 SpaceReference (org.xwiki.model.reference.SpaceReference)7 ResourceReference (org.xwiki.rendering.listener.reference.ResourceReference)7 EntityReferenceResolver (org.xwiki.model.reference.EntityReferenceResolver)6 MacroBlock (org.xwiki.rendering.block.MacroBlock)6 DocumentResourceReference (org.xwiki.rendering.listener.reference.DocumentResourceReference)6 HashMap (java.util.HashMap)5 VelocityContext (org.apache.velocity.VelocityContext)5 Event (org.xwiki.eventstream.Event)5 DefaultEvent (org.xwiki.eventstream.internal.DefaultEvent)5 VelocityEngine (org.xwiki.velocity.VelocityEngine)5 VelocityManager (org.xwiki.velocity.VelocityManager)5