use of org.xwiki.component.descriptor.DefaultComponentDescriptor in project xwiki-platform by xwiki.
the class DefaultWikiMacroTest method testDefaultParameterNames.
/**
* Makes sure getParameterNames return parameters with source case.
*/
@Test
public void testDefaultParameterNames() throws Exception {
// Velocity Manager mock.
final VelocityManager mockVelocityManager = getMockery().mock(VelocityManager.class);
DefaultComponentDescriptor<VelocityManager> descriptorVM = new DefaultComponentDescriptor<VelocityManager>();
descriptorVM.setRoleType(VelocityManager.class);
getComponentManager().registerComponent(descriptorVM, mockVelocityManager);
// Initialize velocity engine.
final VelocityEngine vEngine = getComponentManager().getInstance(VelocityEngine.class);
Properties properties = new Properties();
properties.setProperty("resource.loader", "file");
vEngine.initialize(properties);
// Hack into velocity context.
Execution execution = getComponentManager().getInstance(Execution.class);
Map<?, ?> xwikiContext = (Map<?, ?>) execution.getContext().getProperty("xwikicontext");
final VelocityContext vContext = new VelocityContext();
vContext.put("xcontext", xwikiContext);
getMockery().checking(new Expectations() {
{
oneOf(mockVelocityManager).getCurrentVelocityContext();
will(returnValue(vContext));
oneOf(mockVelocityManager).evaluate(with(any(Writer.class)), with(any(String.class)), with(any(Reader.class)));
will(new Action() {
@Override
public Object invoke(Invocation invocation) throws Throwable {
return vEngine.evaluate(vContext, (Writer) invocation.getParameter(0), (String) invocation.getParameter(1), (Reader) invocation.getParameter(2));
}
@Override
public void describeTo(Description description) {
}
});
}
});
registerWikiMacro("wikimacro1", "{{velocity}}$xcontext.macro.params.parameterNames{{/velocity}}", Syntax.XWIKI_2_0);
Converter converter = getComponentManager().getInstance(Converter.class);
DefaultWikiPrinter printer = new DefaultWikiPrinter();
converter.convert(new StringReader("{{wikimacro1 paRam1=\"value1\" paraM2=\"value2\"/}}"), Syntax.XWIKI_2_0, Syntax.PLAIN_1_0, printer);
Assert.assertEquals("[paRam1, paraM2]", printer.toString());
}
use of org.xwiki.component.descriptor.DefaultComponentDescriptor in project xwiki-platform by xwiki.
the class DefaultWikiMacroTest method testDefaultParameterValues.
/**
* Test default parameter value injection.
*/
@Test
public void testDefaultParameterValues() throws Exception {
// Velocity Manager mock.
final VelocityManager mockVelocityManager = getMockery().mock(VelocityManager.class);
DefaultComponentDescriptor<VelocityManager> descriptorVM = new DefaultComponentDescriptor<VelocityManager>();
descriptorVM.setRoleType(VelocityManager.class);
getComponentManager().registerComponent(descriptorVM, mockVelocityManager);
// Initialize velocity engine.
final VelocityEngine vEngine = getComponentManager().getInstance(VelocityEngine.class);
Properties properties = new Properties();
properties.setProperty("resource.loader", "file");
vEngine.initialize(properties);
// Hack into velocity context.
Execution execution = getComponentManager().getInstance(Execution.class);
Map<?, ?> xwikiContext = (Map<?, ?>) execution.getContext().getProperty("xwikicontext");
final VelocityContext vContext = new VelocityContext();
vContext.put("xcontext", xwikiContext);
getMockery().checking(new Expectations() {
{
oneOf(mockVelocityManager).getCurrentVelocityContext();
will(returnValue(vContext));
oneOf(mockVelocityManager).evaluate(with(any(Writer.class)), with(any(String.class)), with(any(Reader.class)));
will(new Action() {
@Override
public Object invoke(Invocation invocation) throws Throwable {
return vEngine.evaluate(vContext, (Writer) invocation.getParameter(0), (String) invocation.getParameter(1), (Reader) invocation.getParameter(2));
}
@Override
public void describeTo(Description description) {
}
});
}
});
List<WikiMacroParameterDescriptor> parameterDescriptors = Arrays.asList(new WikiMacroParameterDescriptor("param1", "This is param1", false, "default_value"));
registerWikiMacro("wikimacro1", "{{velocity}}$xcontext.macro.params.param1 $xcontext.macro.params.paraM1{{/velocity}}", Syntax.XWIKI_2_0, parameterDescriptors);
Converter converter = getComponentManager().getInstance(Converter.class);
DefaultWikiPrinter printer = new DefaultWikiPrinter();
converter.convert(new StringReader("{{wikimacro1/}}"), Syntax.XWIKI_2_0, Syntax.PLAIN_1_0, printer);
Assert.assertEquals("default_value default_value", printer.toString());
}
use of org.xwiki.component.descriptor.DefaultComponentDescriptor in project xwiki-platform by xwiki.
the class DefaultWikiMacroManager method registerWikiMacro.
@Override
public void registerWikiMacro(DocumentReference documentReference, WikiMacro wikiMacro) throws InsufficientPrivilegesException, WikiMacroException {
WikiMacroDescriptor macroDescriptor = (WikiMacroDescriptor) wikiMacro.getDescriptor();
// Verify that the user has the right to register this wiki macro the chosen visibility
if (this.wikiMacroFactory.isAllowed(documentReference, macroDescriptor.getVisibility())) {
DefaultComponentDescriptor<Macro> componentDescriptor = new DefaultComponentDescriptor<>();
componentDescriptor.setRoleType(Macro.class);
componentDescriptor.setRoleHint(wikiMacro.getDescriptor().getId().getId());
// Save current context informations
String currentUser = this.bridge.getCurrentUser();
EntityReference currentEntityReference = this.modelContext.getCurrentEntityReference();
try {
// Put the proper context information to let components manager use the proper keys to find
// components to unregister
this.bridge.setCurrentUser(this.serializer.serialize(wikiMacro.getAuthorReference() != null ? wikiMacro.getAuthorReference() : this.bridge.getCurrentUserReference()));
this.modelContext.setCurrentEntityReference(documentReference);
// Register the macro against the right Component Manager, depending on the defined macro visibility.
findComponentManager(macroDescriptor.getVisibility()).registerComponent(componentDescriptor, wikiMacro);
this.wikiMacroMap.put(documentReference, new WikiMacroData(componentDescriptor.getRoleHint(), wikiMacro));
} catch (Exception e) {
throw new WikiMacroException(String.format("Failed to register macro [%s] in [%s] for visibility [%s]", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility()), e);
} finally {
// Restore previous context informations
this.bridge.setCurrentUser(currentUser);
this.modelContext.setCurrentEntityReference(currentEntityReference);
}
} else {
throw new InsufficientPrivilegesException(String.format("Unable to register macro [%s] in [%s] for visibility [%s] due to insufficient privileges", wikiMacro.getDescriptor().getId().getId(), documentReference, macroDescriptor.getVisibility()));
}
}
use of org.xwiki.component.descriptor.DefaultComponentDescriptor in project xwiki-platform by xwiki.
the class DefaultIOTargetServiceTest method registerComponents.
@Override
protected void registerComponents() throws Exception {
super.registerComponents();
// register the dab
this.dabMock = registerMockComponent(DocumentAccessBridge.class);
getMockery().checking(new Expectations() {
{
allowing(dabMock).getCurrentUserReference();
}
});
this.classResolver = registerMockComponent(DocumentReferenceResolver.TYPE_STRING);
// We don't care about multi CM
DefaultComponentDescriptor<ComponentManager> componentDescriptor = new DefaultComponentDescriptor<>();
componentDescriptor.setRoleType(ComponentManager.class);
componentDescriptor.setRoleHint("context");
getComponentManager().registerComponent(componentDescriptor, getComponentManager());
}
use of org.xwiki.component.descriptor.DefaultComponentDescriptor in project xwiki-platform by xwiki.
the class AbstractRefactoringTestCase method registerComponents.
@Override
protected void registerComponents() throws Exception {
// Document Access Bridge Mock
final DocumentAccessBridge mockDocumentAccessBridge = this.mockery.mock(DocumentAccessBridge.class);
DefaultComponentDescriptor<DocumentAccessBridge> descriptorDAB = new DefaultComponentDescriptor<DocumentAccessBridge>();
descriptorDAB.setRoleType(DocumentAccessBridge.class);
getComponentManager().registerComponent(descriptorDAB, mockDocumentAccessBridge);
mockery.checking(new Expectations() {
{
allowing(mockDocumentAccessBridge).exists(with(any(String.class)));
will(returnValue(false));
}
});
this.docBridge = getComponentManager().getInstance(DocumentAccessBridge.class, "default");
this.xwikiParser = getComponentManager().getInstance(Parser.class, "xwiki/2.0");
}
Aggregations