Search in sources :

Example 66 with ComponentManager

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

the class ContextComponentManagerTest method testRegisterComponentInRootComponentManager.

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

        {
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            when(state.isNot("otherwiki"));
            will(returnValue("wiki"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            when(state.isNot("otherwiki"));
            will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            when(state.isNot("otherwiki"));
            will(returnValue(new DocumentReference("wiki", "space", "document")));
            allowing(mockDocumentAccessBridge).getCurrentUserReference();
            when(state.isNot("otherwiki"));
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
        }
    });
    // Register in the current wiki.
    DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
    cd.setRoleType(Role.class);
    cd.setImplementation(RoleImpl.class);
    getComponentManager().registerComponent(cd);
    // Verify we can lookup the component from the context CM.
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    Assert.assertNotNull(contextCM.getInstance(Role.class));
}
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) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 67 with ComponentManager

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

the class ContextComponentManagerTest method testDeleteDocument.

@Test
public void testDeleteDocument() throws Exception {
    getMockery().checking(new Expectations() {

        {
            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")));
            allowing(mockDocumentAccessBridge).getCurrentUserReference();
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
        }
    });
    ComponentManager documentCM = getComponentManager().getInstance(ComponentManager.class, "document");
    DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
    cd.setRoleType(Role.class);
    cd.setImplementation(RoleImpl.class);
    // Register component for the current user
    documentCM.registerComponent(cd);
    // Verify we can lookup the component from the Context CM
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    Assert.assertNotNull(contextCM.getComponentDescriptor(Role.class, "default"));
    ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
    observationManager.notify(new DocumentDeletedEvent(new DocumentReference("wiki", "space", "document")), null, null);
    Assert.assertNull(contextCM.getComponentDescriptor(Role.class, "default"));
}
Also used : Expectations(org.jmock.Expectations) DocumentDeletedEvent(org.xwiki.bridge.event.DocumentDeletedEvent) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) SpaceReference(org.xwiki.model.reference.SpaceReference) ComponentManager(org.xwiki.component.manager.ComponentManager) NamespacedComponentManager(org.xwiki.component.manager.NamespacedComponentManager) ObservationManager(org.xwiki.observation.ObservationManager) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 68 with ComponentManager

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

the class ContextComponentManagerTest method testRegisterComponentInSpaceComponentManager.

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

        {
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            when(state.isNot("otherspace"));
            will(returnValue("wiki1"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            when(state.isNot("otherspace"));
            will(returnValue(new SpaceReference("space1", new WikiReference("wiki1"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            when(state.isNot("otherspace"));
            will(returnValue(new DocumentReference("wiki1", "space1", "document1")));
            allowing(mockDocumentAccessBridge).getCurrentUserReference();
            when(state.isNot("otherspace"));
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
        }
    });
    ComponentManager userCM = getComponentManager().getInstance(ComponentManager.class, "space");
    DefaultComponentDescriptor<Role> cd = new DefaultComponentDescriptor<Role>();
    cd.setRoleType(Role.class);
    cd.setImplementation(RoleImpl.class);
    // Register component for the current user
    userCM.registerComponent(cd);
    // Verify we can lookup the component from the Context CM
    ComponentManager contextCM = getComponentManager().getInstance(ComponentManager.class, "context");
    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("otherspace");
    getMockery().checking(new Expectations() {

        {
            exactly(1).of(mockDocumentAccessBridge).getCurrentUserReference();
            will(returnValue(new DocumentReference("wiki", "XWiki", "user")));
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            will(returnValue("wiki2"));
            allowing(mockCurrentSpaceReferenceProvider).get();
            will(returnValue(new SpaceReference("space2", new WikiReference("wiki2"))));
            allowing(mockCurrentDocumentReferenceProvider).get();
            will(returnValue(new DocumentReference("wiki2", "space2", "document2")));
        }
    });
    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 69 with ComponentManager

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

the class ComponentScriptServiceTest method configure.

@Before
public void configure() throws Exception {
    this.dab = this.mocker.getInstance(DocumentAccessBridge.class);
    this.contextComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "context");
    Provider<ComponentManager> contextComponentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context");
    when(contextComponentManagerProvider.get()).thenReturn(this.contextComponentManager);
    this.contextrootComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "context/root");
    Provider<ComponentManager> contextrootComponentManagerProvider = this.mocker.registerMockComponent(new DefaultParameterizedType(null, Provider.class, ComponentManager.class), "context/root");
    when(contextrootComponentManagerProvider.get()).thenReturn(this.contextrootComponentManager);
    this.rootComponentManager = this.mocker.registerMockComponent(ComponentManager.class, "root");
    this.componentManagerManager = this.mocker.getInstance(ComponentManagerManager.class);
    when(this.componentManagerManager.getComponentManager(null, false)).thenReturn(this.rootComponentManager);
    this.execution = this.mocker.getInstance(Execution.class);
}
Also used : ComponentManagerManager(org.xwiki.component.internal.multi.ComponentManagerManager) Execution(org.xwiki.context.Execution) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ComponentManager(org.xwiki.component.manager.ComponentManager) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) Provider(javax.inject.Provider) ContextComponentManagerProvider(org.xwiki.component.internal.ContextComponentManagerProvider) Before(org.junit.Before)

Example 70 with ComponentManager

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

the class DocumentDeletedListener method onEvent.

@Override
public void onEvent(Event event, Object o, Object context) {
    String document = ((DocumentDeletedEvent) event).getEventFilter().getFilter();
    ComponentManager componentManager = this.componentManagerManager.getComponentManager(new DocumentNamespace(document).serialize(), false);
    if (componentManager instanceof Disposable) {
        try {
            ((Disposable) componentManager).dispose();
        } catch (ComponentLifecycleException e) {
            this.logger.error(String.format("Failed to dispose component manager for document [%s]", document), e);
        }
    }
}
Also used : Disposable(org.xwiki.component.phase.Disposable) DocumentNamespace(org.xwiki.model.namespace.DocumentNamespace) ComponentManager(org.xwiki.component.manager.ComponentManager) ComponentLifecycleException(org.xwiki.component.manager.ComponentLifecycleException)

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