Search in sources :

Example 36 with Execution

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

the class AbstractXWikiHibernateStoreTest method setUp.

@Before
public void setUp() throws Exception {
    // For XWikiHibernateBaseStore#initialize()
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(executionContext.getProperty("xwikicontext")).thenReturn(xcontext);
    Execution execution = getMocker().getInstance(Execution.class);
    when(execution.getContext()).thenReturn(executionContext);
    Provider<XWikiContext> xcontextProvider = getMocker().registerMockComponent(XWikiContext.TYPE_PROVIDER);
    when(xcontextProvider.get()).thenReturn(this.xcontext);
    xcontextProvider = getMocker().registerMockComponent(XWikiContext.TYPE_PROVIDER, "readonly");
    when(xcontextProvider.get()).thenReturn(this.xcontext);
    XWiki wiki = mock(XWiki.class);
    when(this.xcontext.getWiki()).thenReturn(wiki);
    // For XWikiHibernateBaseStore#initHibernate()
    HibernateSessionFactory sessionFactory = getMocker().getInstance(HibernateSessionFactory.class);
    when(sessionFactory.getConfiguration()).thenReturn(mock(Configuration.class));
    // For XWikiHibernateBaseStore#beginTransaction()
    SessionFactory wrappedSessionFactory = mock(SessionFactory.class);
    when(sessionFactory.getSessionFactory()).thenReturn(wrappedSessionFactory);
    when(wrappedSessionFactory.openSession()).thenReturn(session);
    when(session.beginTransaction()).thenReturn(transaction);
    // HibernateStore
    this.hibernateStore = getMocker().registerMockComponent(HibernateStore.class);
    // Return null on first get to force the session/transaction creation.
    when(this.hibernateStore.getCurrentSession()).thenReturn(session);
    when(this.hibernateStore.getCurrentTransaction()).thenReturn(transaction);
    // Default is schema mode
    when(this.hibernateStore.isInSchemaMode()).thenReturn(true);
}
Also used : SessionFactory(org.hibernate.SessionFactory) HibernateSessionFactory(com.xpn.xwiki.store.hibernate.HibernateSessionFactory) ExecutionContext(org.xwiki.context.ExecutionContext) Execution(org.xwiki.context.Execution) HibernateStore(com.xpn.xwiki.internal.store.hibernate.HibernateStore) Configuration(org.hibernate.cfg.Configuration) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) HibernateSessionFactory(com.xpn.xwiki.store.hibernate.HibernateSessionFactory) Before(org.junit.Before)

Example 37 with Execution

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

the class ReferenceUserIteratorTest method getMembersWhenNoExecutionContext.

@Test
public void getMembersWhenNoExecutionContext() throws Exception {
    Execution execution = this.componentManager.registerMockComponent(Execution.class);
    DocumentReference userReference = new DocumentReference("userwiki", "XWiki", "userpage");
    try {
        new ReferenceUserIterator(userReference, null, execution).next();
    } catch (RuntimeException expected) {
        assertEquals("Aborting member extraction from passed references [[userwiki:XWiki.userpage]] since no " + "XWiki Context was found", expected.getMessage());
    }
}
Also used : Execution(org.xwiki.context.Execution) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 38 with Execution

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

the class ReferenceUserIteratorTest method getMembersWhenNoXWikiContext.

@Test
public void getMembersWhenNoXWikiContext() throws Exception {
    Execution execution = this.componentManager.registerMockComponent(Execution.class);
    when(execution.getContext()).thenReturn(new ExecutionContext());
    DocumentReference userReference = new DocumentReference("userwiki", "XWiki", "userpage");
    try {
        new ReferenceUserIterator(userReference, null, execution).next();
    } catch (RuntimeException expected) {
        assertEquals("Aborting member extraction from passed references [[userwiki:XWiki.userpage]] since no " + "XWiki Context was found", expected.getMessage());
    }
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 39 with Execution

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

the class CurrentLanguageQueryFilterTest method filterStatement.

@Test
public void filterStatement() throws Exception {
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(execution.getContext()).thenReturn(executionContext);
    XWikiContext xwikiContext = mock(XWikiContext.class);
    when(executionContext.getProperty("xwikicontext")).thenReturn(xwikiContext);
    com.xpn.xwiki.XWiki xwiki = mock(com.xpn.xwiki.XWiki.class);
    when(xwikiContext.getWiki()).thenReturn(xwiki);
    when(xwiki.getDefaultLocale(any(XWikiContext.class))).thenReturn(Locale.FRANCE);
    String result = this.mocker.getComponentUnderTest().filterStatement("select doc.fullName from XWikiDocument doc where 1=1", Query.HQL);
    assertEquals("select doc.fullName from XWikiDocument doc where (doc.language is null or doc.language = '' or " + "doc.language = 'fr_FR') and (1=1)", result);
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) Test(org.junit.Test)

Example 40 with Execution

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

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