Search in sources :

Example 11 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class ContextComponentManagerTest method testRegisterComponentInContextComponentManagerThrowsException.

// Failures
@Test
public void testRegisterComponentInContextComponentManagerThrowsException() throws Exception {
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    try {
        contextCM.registerComponent(new DefaultComponentDescriptor<Role>());
        Assert.fail("Should have thrown an exception error");
    } catch (RuntimeException expected) {
        Assert.assertEquals("The Context Component Manager should only be used for read access. Write operations " + "should be done against specific Component Managers.", expected.getMessage());
    }
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) NamespacedComponentManager(org.xwiki.component.manager.NamespacedComponentManager) Test(org.junit.Test)

Example 12 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class ContextComponentManagerTest method testRegisterComponentInUserComponentManager.

@Test
public void testRegisterComponentInUserComponentManager() throws Exception {
    final States state = getMockery().states("test");
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentAccessBridge).getCurrentUserReference();
            when(state.isNot("otheruser"));
            will(returnValue(new DocumentReference("wiki", "XWiki", "user1")));
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            will(returnValue("wiki"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            will(returnValue(new DocumentReference("wiki", "space", "document")));
        }
    });
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    try {
        contextCM.getInstance(Role.class);
        Assert.fail("Should have raised an exception");
    } catch (ComponentLookupException expected) {
    // No need to assert the message, we just want to ensure an exception is raised.
    }
    // Register component for the current user
    ComponentManager userCM = getComponentManager().getInstance(ComponentManager.class, "user");
    DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
    cd.setRoleType(Role.class);
    cd.setImplementation(RoleImpl.class);
    userCM.registerComponent(cd);
    // Verify we can lookup the component from the Context CM
    Assert.assertNotNull(contextCM.getInstance(Role.class));
    // Now verify that we cannot look it up anymore if there's another user in the context
    state.become("otheruser");
    getMockery().checking(new Expectations() {

        {
            allowing(mockDocumentAccessBridge).getCurrentUserReference();
            will(returnValue(new DocumentReference("wiki", "XWiki", "user2")));
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            will(returnValue("wiki"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            will(returnValue(new DocumentReference("wiki", "space", "document")));
        }
    });
    try {
        contextCM.getInstance(Role.class);
        Assert.fail("Should have raised an exception");
    } catch (ComponentLookupException expected) {
    // No need to assert the message, we just want to ensure an exception is raised.
    }
}
Also used : States(org.jmock.States) Expectations(org.jmock.Expectations) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) SpaceReference(org.xwiki.model.reference.SpaceReference) ComponentManager(org.xwiki.component.manager.ComponentManager) NamespacedComponentManager(org.xwiki.component.manager.NamespacedComponentManager) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 13 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class ContextComponentManagerTest method testRegisterComponentDesciptor.

@Test(expected = RuntimeException.class)
public void testRegisterComponentDesciptor() throws ComponentLookupException, Exception {
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    contextCM.registerComponent(null);
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) NamespacedComponentManager(org.xwiki.component.manager.NamespacedComponentManager) Test(org.junit.Test)

Example 14 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class ContextComponentManagerTest method testSetParent.

@Test(expected = RuntimeException.class)
public void testSetParent() throws ComponentLookupException, Exception {
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    contextCM.setParent(null);
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) NamespacedComponentManager(org.xwiki.component.manager.NamespacedComponentManager) Test(org.junit.Test)

Example 15 with ComponentManager

use of org.xwiki.component.manager.ComponentManager in project xwiki-platform by xwiki.

the class ContextComponentManagerTest method testSetComponentEventManager.

@Test(expected = RuntimeException.class)
public void testSetComponentEventManager() throws ComponentLookupException, Exception {
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    contextCM.setComponentEventManager(null);
}
Also used : ComponentManager(org.xwiki.component.manager.ComponentManager) NamespacedComponentManager(org.xwiki.component.manager.NamespacedComponentManager) Test(org.junit.Test)

Aggregations

ComponentManager (org.xwiki.component.manager.ComponentManager)76 Test (org.junit.Test)34 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)23 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)18 DocumentReference (org.xwiki.model.reference.DocumentReference)17 Provider (javax.inject.Provider)14 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)12 Before (org.junit.Before)11 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)11 XWikiContext (com.xpn.xwiki.XWikiContext)9 Expectations (org.jmock.Expectations)9 WikiReference (org.xwiki.model.reference.WikiReference)9 HashMap (java.util.HashMap)7 Execution (org.xwiki.context.Execution)7 SpaceReference (org.xwiki.model.reference.SpaceReference)7 XWiki (com.xpn.xwiki.XWiki)6 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)6 ArrayList (java.util.ArrayList)6 ExecutionContext (org.xwiki.context.ExecutionContext)6 MimeMessage (javax.mail.internet.MimeMessage)5