Search in sources :

Example 41 with Execution

use of org.xwiki.context.Execution 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 42 with Execution

use of org.xwiki.context.Execution in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    // Script setup.
    ScriptMockSetup scriptMockSetup = new ScriptMockSetup(getMockery(), getComponentManager());
    final DocumentAccessBridge mockDocBridge = scriptMockSetup.bridge;
    this.mockWikiModel = scriptMockSetup.wikiModel;
    this.wikiMacroDocumentReference = new DocumentReference("wiki", "space", "macroPage");
    this.wikiMacroManager = getComponentManager().getInstance(WikiMacroManager.class);
    // Make sure the old XWiki Context is set up in the Execution Context since it's used in
    // DefaultWikiMacro.execute().
    this.xcontext = new HashMap<String, Object>();
    Execution execution = getComponentManager().getInstance(Execution.class);
    execution.getContext().setProperty("xwikicontext", this.xcontext);
    ScriptContextManager scm = getComponentManager().getInstance(ScriptContextManager.class);
    scm.getCurrentScriptContext().setAttribute("xcontext", this.xcontext, ScriptContext.ENGINE_SCOPE);
    getMockery().checking(new Expectations() {

        {
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            will(returnValue("wiki"));
            allowing(mockCurrentDocumentReferenceProvider).get();
            will(returnValue(new DocumentReference("wiki", "space", "document")));
            allowing(mockCurrentSpaceReferenceProvider).get();
            will(returnValue(new SpaceReference("space", new WikiReference("wiki"))));
            allowing(mockDocBridge).getCurrentUser();
            will(returnValue("dummy"));
            allowing(mockDocBridge).setCurrentUser(with(any(String.class)));
            allowing(mockDocBridge).getCurrentUserReference();
            will(returnValue(new DocumentReference("wiki", "XWiki", "dummy")));
            allowing(mockWikiMacroFactory).isAllowed(with(any(DocumentReference.class)), with(any(WikiMacroVisibility.class)));
            will(returnValue(true));
            // This is the document containing the wiki macro that will be put in the context available in the macro
            // Since we're not testing it here, it can be null.
            allowing(mockDocBridge).getDocumentInstance(wikiMacroDocumentReference);
            will(returnValue(null));
        }
    });
}
Also used : Expectations(org.jmock.Expectations) ScriptMockSetup(org.xwiki.rendering.macro.script.ScriptMockSetup) SpaceReference(org.xwiki.model.reference.SpaceReference) DocumentAccessBridge(org.xwiki.bridge.DocumentAccessBridge) ScriptContextManager(org.xwiki.script.ScriptContextManager) WikiMacroManager(org.xwiki.rendering.macro.wikibridge.WikiMacroManager) Execution(org.xwiki.context.Execution) WikiReference(org.xwiki.model.reference.WikiReference) WikiMacroVisibility(org.xwiki.rendering.macro.wikibridge.WikiMacroVisibility) DocumentReference(org.xwiki.model.reference.DocumentReference) Before(org.junit.Before)

Example 43 with Execution

use of org.xwiki.context.Execution in project xwiki-platform by xwiki.

the class DomainWikiReferenceExtractorTest method extractWhenNoAliasAndUnderscoreInDomainName.

@Test
public void extractWhenNoAliasAndUnderscoreInDomainName() throws Exception {
    // Simulate a configured Execution Context
    Execution execution = mocker.getInstance(Execution.class);
    when(execution.getContext()).thenReturn(new ExecutionContext());
    testAndAssert("http://some_domain.server.com/xwiki/bin/view/Main/WebHome", "some_domain");
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) Test(org.junit.Test)

Example 44 with Execution

use of org.xwiki.context.Execution in project xwiki-platform by xwiki.

the class IntegrationTest method setUpComponents.

@BeforeComponent
public void setUpComponents() throws Exception {
    // Isolate from xwiki configuration file
    URLConfiguration urlConfiguration = this.componentManager.registerMockComponent(URLConfiguration.class);
    when(urlConfiguration.getURLFormatId()).thenReturn("standard");
    // Isolate from xwiki configuration file
    StandardURLConfiguration standardURLConfiguration = this.componentManager.registerMockComponent(StandardURLConfiguration.class);
    when(standardURLConfiguration.getEntityPathPrefix()).thenReturn("bin");
    when(standardURLConfiguration.getWikiPathPrefix()).thenReturn("wiki");
    when(standardURLConfiguration.isViewActionHidden()).thenReturn(false);
    // Isolate from xwiki configuration file
    ModelConfiguration modelConfiguration = this.componentManager.registerMockComponent(ModelConfiguration.class);
    when(modelConfiguration.getDefaultReferenceValue(EntityType.WIKI)).thenReturn("xwiki");
    // Isolate from xwiki's model
    WikiDescriptorManager wikiDescriptorManager = this.componentManager.registerMockComponent(WikiDescriptorManager.class);
    when(wikiDescriptorManager.getMainWikiId()).thenReturn("xwiki");
    // Isolate from Environment
    EntityResourceActionLister actionLister = this.componentManager.registerMockComponent(EntityResourceActionLister.class);
    when(actionLister.listActions()).thenReturn(Arrays.asList("view"));
    // Simulate a configured Execution Context
    Execution execution = this.componentManager.registerMockComponent(Execution.class);
    when(execution.getContext()).thenReturn(new ExecutionContext());
    // For test simplicity consider that Context CM == CM
    this.componentManager.registerComponent(ComponentManager.class, "context", this.componentManager);
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) URLConfiguration(org.xwiki.url.URLConfiguration) WikiDescriptorManager(org.xwiki.wiki.descriptor.WikiDescriptorManager) ModelConfiguration(org.xwiki.model.ModelConfiguration) EntityResourceActionLister(org.xwiki.resource.internal.entity.EntityResourceActionLister) BeforeComponent(org.xwiki.test.annotation.BeforeComponent)

Example 45 with Execution

use of org.xwiki.context.Execution in project xwiki-platform by xwiki.

the class PathWikiReferenceExtractorTest method setUpConfiguration.

private void setUpConfiguration(WikiNotFoundBehavior wikiNotFoundBehavior) throws Exception {
    // Simulate a configured Execution Context
    Execution execution = mocker.getInstance(Execution.class);
    when(execution.getContext()).thenReturn(new ExecutionContext());
    StandardURLConfiguration urlConfiguration = mocker.getInstance(StandardURLConfiguration.class);
    when(urlConfiguration.getWikiNotFoundBehavior()).thenReturn(wikiNotFoundBehavior);
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext)

Aggregations

Execution (org.xwiki.context.Execution)82 ExecutionContext (org.xwiki.context.ExecutionContext)58 Before (org.junit.Before)36 XWikiContext (com.xpn.xwiki.XWikiContext)30 Test (org.junit.Test)19 DocumentReference (org.xwiki.model.reference.DocumentReference)17 XWiki (com.xpn.xwiki.XWiki)13 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)13 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)11 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)10 DocumentAccessBridge (org.xwiki.bridge.DocumentAccessBridge)8 ComponentManager (org.xwiki.component.manager.ComponentManager)8 HashMap (java.util.HashMap)7 Expectations (org.jmock.Expectations)7 VelocityContext (org.apache.velocity.VelocityContext)5 ExecutionContextManager (org.xwiki.context.ExecutionContextManager)5 BaseObject (com.xpn.xwiki.objects.BaseObject)4 DefaultComponentDescriptor (org.xwiki.component.descriptor.DefaultComponentDescriptor)4 XDOM (org.xwiki.rendering.block.XDOM)4 AuthorizationManager (org.xwiki.security.authorization.AuthorizationManager)4