use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.
the class DefaultResourceReferenceSerializer method serialize.
@Override
public ExtendedURL serialize(ResourceReference resource) throws SerializeResourceReferenceException, UnsupportedResourceReferenceException {
ResourceReferenceSerializer<ResourceReference, ExtendedURL> serializer;
ParameterizedType type = new DefaultParameterizedType(null, ResourceReferenceSerializer.class, ResourceReference.class, ExtendedURL.class);
try {
serializer = this.componentManager.getInstance(type, this.urlContextManager.getURLFormatId());
} catch (ComponentLookupException e) {
throw new UnsupportedResourceReferenceException(String.format("Invalid URL format id [%s]. Cannot serialize Resource Reference [%s].", this.urlContextManager.getURLFormatId(), resource), e);
}
return serializer.serialize(resource);
}
use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.
the class AbstractDocumentMojo method createXWikiContext.
protected XWikiContext createXWikiContext() throws MojoExecutionException {
EmbeddableComponentManager ecm = new EmbeddableComponentManager();
ecm.initialize(this.getClass().getClassLoader());
Utils.setComponentManager(ecm);
// Context component manager is totally useless here
ecm.unregisterComponent(ComponentManager.class, "context");
// We need to initialize the Component Manager so that the components can be looked up
XWikiContext xcontext = new XWikiContext();
xcontext.put(ComponentManager.class.getName(), ecm);
// Initialize the Container fields (request, response, session).
try {
ExecutionContextManager ecim = ecm.getInstance(ExecutionContextManager.class);
ExecutionContext econtext = new ExecutionContext();
// Bridge with old XWiki Context, required for old code.
xcontext.declareInExecutionContext(econtext);
ecim.initialize(econtext);
} catch (ExecutionContextException | ComponentLookupException e) {
throw new MojoExecutionException("Failed to initialize Execution Context.", e);
}
return xcontext;
}
use of org.xwiki.component.manager.ComponentLookupException 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.
}
}
use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.
the class ComponentScriptServiceTest method getComponentInstanceWithHintWhenComponentDoesntExist.
@Test
public void getComponentInstanceWithHintWhenComponentDoesntExist() throws Exception {
when(this.dab.hasProgrammingRights()).thenReturn(true);
when(this.contextComponentManager.getInstance(SomeRole.class, "hint")).thenThrow(new ComponentLookupException("error"));
ExecutionContext context = new ExecutionContext();
when(this.execution.getContext()).thenReturn(context);
assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class, "hint"));
assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage());
}
use of org.xwiki.component.manager.ComponentLookupException in project xwiki-platform by xwiki.
the class ComponentScriptServiceTest method getComponentInstanceWhenComponentDoesntExist.
@Test
public void getComponentInstanceWhenComponentDoesntExist() throws Exception {
when(this.dab.hasProgrammingRights()).thenReturn(true);
when(this.contextComponentManager.getInstance(SomeRole.class)).thenThrow(new ComponentLookupException("error"));
ExecutionContext context = new ExecutionContext();
when(this.execution.getContext()).thenReturn(context);
assertNull(this.mocker.getComponentUnderTest().getInstance(SomeRole.class));
assertEquals("error", this.mocker.getComponentUnderTest().getLastError().getMessage());
}
Aggregations