Search in sources :

Example 31 with WikiReference

use of org.xwiki.model.reference.WikiReference 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 32 with WikiReference

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

the class XarInstalledExtensionRepository method pagesUpdated.

private void pagesUpdated(XarInstalledExtension installedExtension, String namespace, boolean add) throws UnsupportedNamespaceException {
    if (installedExtension != null) {
        for (XarEntry xarEntry : installedExtension.getXarPackage().getEntries()) {
            if (namespace != null) {
                DocumentReference reference = new DocumentReference(xarEntry, new WikiReference(XarHandlerUtils.getWikiFromNamespace(namespace)));
                synchronized (this.documents) {
                    Collection<XarInstalledExtension> referenceExtensions = this.documents.get(reference);
                    if (referenceExtensions != null || add) {
                        Set<XarInstalledExtension> newSet = referenceExtensions != null ? new LinkedHashSet<>(referenceExtensions) : new LinkedHashSet<>();
                        if (add) {
                            newSet.add(installedExtension);
                        } else {
                            newSet.remove(installedExtension);
                        }
                        this.documents.put(reference, newSet);
                    }
                }
            } else {
                synchronized (this.rootDocuments) {
                    Collection<XarInstalledExtension> referenceExtensions = this.rootDocuments.get(xarEntry);
                    if (referenceExtensions != null || add) {
                        Set<XarInstalledExtension> newSet = referenceExtensions != null ? new LinkedHashSet<>(referenceExtensions) : new LinkedHashSet<>();
                        if (add) {
                            newSet.add(installedExtension);
                        } else {
                            newSet.remove(installedExtension);
                        }
                        this.rootDocuments.put(xarEntry, newSet);
                    }
                }
            }
        }
    }
}
Also used : XarEntry(org.xwiki.xar.XarEntry) WikiReference(org.xwiki.model.reference.WikiReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 33 with WikiReference

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

the class ExtensionHistoryScriptServiceTest method replayWithoutAdmin.

@Test
public void replayWithoutAdmin() throws Exception {
    InstallRequest installRequest = new InstallRequest();
    installRequest.addNamespace("wiki:dev");
    ExtensionJobHistoryRecord install = new ExtensionJobHistoryRecord("install", installRequest, null, null, null);
    List<ExtensionJobHistoryRecord> records = Arrays.asList(install);
    when(this.xcontext.getWikiId()).thenReturn("dev");
    when(this.authorization.hasAccess(Right.ADMIN, new WikiReference("dev"))).thenReturn(false);
    Job replayJob = mock(Job.class);
    ArgumentCaptor<ReplayRequest> requestCaptor = ArgumentCaptor.forClass(ReplayRequest.class);
    when(jobExecutor.execute(eq(ReplayJob.JOB_TYPE), requestCaptor.capture())).thenReturn(replayJob);
    assertSame(replayJob, this.mocker.getComponentUnderTest().replay(records));
    ReplayRequest request = requestCaptor.getValue();
    assertTrue(request.getRecords().isEmpty());
}
Also used : ExtensionJobHistoryRecord(org.xwiki.extension.job.history.ExtensionJobHistoryRecord) ReplayRequest(org.xwiki.extension.job.history.ReplayRequest) InstallRequest(org.xwiki.extension.job.InstallRequest) WikiReference(org.xwiki.model.reference.WikiReference) ReplayJob(org.xwiki.extension.job.history.internal.ReplayJob) Job(org.xwiki.job.Job) Test(org.junit.Test)

Example 34 with WikiReference

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

the class ExtensionHistoryScriptServiceTest method replayWithAdminButNoPR.

@Test
public void replayWithAdminButNoPR() throws Exception {
    InstallRequest installRequest = new InstallRequest();
    installRequest.addNamespace("wiki:drafts");
    installRequest.setProperty("user.reference", new DocumentReference("drafts", "Users", "Alice"));
    ExtensionJobHistoryRecord install = new ExtensionJobHistoryRecord("install", installRequest, null, null, null);
    List<ExtensionJobHistoryRecord> records = Arrays.asList(install);
    when(this.xcontext.getWikiId()).thenReturn("dev");
    when(this.documentAccessBridge.getCurrentUserReference()).thenReturn(new DocumentReference("dev", "Users", "Bob"));
    when(this.authorization.hasAccess(Right.ADMIN, new WikiReference("dev"))).thenReturn(true);
    when(this.authorization.hasAccess(Right.PROGRAM)).thenReturn(false);
    Job replayJob = mock(Job.class);
    ArgumentCaptor<ReplayRequest> requestCaptor = ArgumentCaptor.forClass(ReplayRequest.class);
    when(jobExecutor.execute(eq(ReplayJob.JOB_TYPE), requestCaptor.capture())).thenReturn(replayJob);
    assertSame(replayJob, this.mocker.getComponentUnderTest().replay(records));
    ReplayRequest request = requestCaptor.getValue();
    assertEquals(Arrays.asList(install), request.getRecords());
    assertEquals(Arrays.asList("wiki:dev"), install.getRequest().getNamespaces());
    assertEquals(this.documentAccessBridge.getCurrentUserReference(), install.getRequest().getProperty("user.reference"));
}
Also used : ExtensionJobHistoryRecord(org.xwiki.extension.job.history.ExtensionJobHistoryRecord) ReplayRequest(org.xwiki.extension.job.history.ReplayRequest) InstallRequest(org.xwiki.extension.job.InstallRequest) WikiReference(org.xwiki.model.reference.WikiReference) ReplayJob(org.xwiki.extension.job.history.internal.ReplayJob) Job(org.xwiki.job.Job) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 35 with WikiReference

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

the class DefaultStringSpaceReferenceResolverTest method resolveWithExplicitSpaceReference.

@Test
public void resolveWithExplicitSpaceReference() {
    SpaceReference reference = this.resolver.resolve("", new SpaceReference("space", new WikiReference("wiki")));
    Assert.assertEquals("space", reference.getName());
    Assert.assertEquals("wiki", reference.getWikiReference().getName());
}
Also used : SpaceReference(org.xwiki.model.reference.SpaceReference) WikiReference(org.xwiki.model.reference.WikiReference) Test(org.junit.Test)

Aggregations

WikiReference (org.xwiki.model.reference.WikiReference)220 DocumentReference (org.xwiki.model.reference.DocumentReference)127 Test (org.junit.Test)106 SpaceReference (org.xwiki.model.reference.SpaceReference)58 XWikiContext (com.xpn.xwiki.XWikiContext)33 XWikiException (com.xpn.xwiki.XWikiException)24 EntityReference (org.xwiki.model.reference.EntityReference)24 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)23 ArrayList (java.util.ArrayList)19 AccessDeniedException (org.xwiki.security.authorization.AccessDeniedException)18 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)15 WikiDescriptor (org.xwiki.wiki.descriptor.WikiDescriptor)11 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)10 WikiManagerException (org.xwiki.wiki.manager.WikiManagerException)10 XWiki (com.xpn.xwiki.XWiki)9 BaseObject (com.xpn.xwiki.objects.BaseObject)9 ComponentManager (org.xwiki.component.manager.ComponentManager)9 Expectations (org.jmock.Expectations)8 Before (org.junit.Before)8 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)8