Search in sources :

Example 11 with DefaultComponentDescriptor

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());
}
Also used : Expectations(org.jmock.Expectations) VelocityEngine(org.xwiki.velocity.VelocityEngine) Action(org.jmock.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) VelocityContext(org.apache.velocity.VelocityContext) Reader(java.io.Reader) StringReader(java.io.StringReader) Properties(java.util.Properties) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) Execution(org.xwiki.context.Execution) VelocityManager(org.xwiki.velocity.VelocityManager) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) Test(org.junit.Test)

Example 12 with DefaultComponentDescriptor

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());
}
Also used : Expectations(org.jmock.Expectations) VelocityEngine(org.xwiki.velocity.VelocityEngine) Action(org.jmock.api.Action) Description(org.hamcrest.Description) Invocation(org.jmock.api.Invocation) DefaultWikiPrinter(org.xwiki.rendering.renderer.printer.DefaultWikiPrinter) VelocityContext(org.apache.velocity.VelocityContext) Reader(java.io.Reader) StringReader(java.io.StringReader) Properties(java.util.Properties) WikiMacroParameterDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroParameterDescriptor) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) Execution(org.xwiki.context.Execution) VelocityManager(org.xwiki.velocity.VelocityManager) StringReader(java.io.StringReader) Converter(org.xwiki.rendering.converter.Converter) HashMap(java.util.HashMap) Map(java.util.Map) Writer(java.io.Writer) Test(org.junit.Test)

Example 13 with DefaultComponentDescriptor

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()));
    }
}
Also used : InsufficientPrivilegesException(org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) WikiMacro(org.xwiki.rendering.macro.wikibridge.WikiMacro) Macro(org.xwiki.rendering.macro.Macro) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException) EntityReference(org.xwiki.model.reference.EntityReference) WikiMacroDescriptor(org.xwiki.rendering.macro.wikibridge.WikiMacroDescriptor) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) InsufficientPrivilegesException(org.xwiki.rendering.macro.wikibridge.InsufficientPrivilegesException) WikiMacroException(org.xwiki.rendering.macro.wikibridge.WikiMacroException)

Example 14 with DefaultComponentDescriptor

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());
}
Also used : Expectations(org.jmock.Expectations) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ComponentManager(org.xwiki.component.manager.ComponentManager)

Example 15 with DefaultComponentDescriptor

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");
}
Also used : Expectations(org.jmock.Expectations) DefaultComponentDescriptor(org.xwiki.component.descriptor.DefaultComponentDescriptor) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) Parser(org.xwiki.rendering.parser.Parser)

Aggregations

DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)19 Expectations (org.jmock.Expectations)13 ComponentManager (org.xwiki.component.manager.ComponentManager)10 Test (org.junit.Test)9 DocumentReference (org.xwiki.model.reference.DocumentReference)9 WikiReference (org.xwiki.model.reference.WikiReference)8 NamespacedComponentManager (org.xwiki.component.manager.NamespacedComponentManager)7 SpaceReference (org.xwiki.model.reference.SpaceReference)7 States (org.jmock.States)5 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)5 Execution (org.xwiki.context.Execution)4 HashMap (java.util.HashMap)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 XWikiStubContextProvider (com.xpn.xwiki.util.XWikiStubContextProvider)2 File (java.io.File)2 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 Writer (java.io.Writer)2 Date (java.util.Date)2