Search in sources :

Example 1 with HibernateSessionFactory

use of com.xpn.xwiki.store.hibernate.HibernateSessionFactory in project xwiki-platform by xwiki.

the class HqlQueryExecutorTest method createNamedNativeHibernateQuery.

@Test
@SuppressWarnings("unchecked")
public void createNamedNativeHibernateQuery() throws Exception {
    DefaultQuery query = new DefaultQuery("queryName", this.executor);
    Session session = mock(Session.class);
    SQLQuery sqlQuery = mock(SQLQuery.class);
    when(session.getNamedQuery(query.getStatement())).thenReturn(sqlQuery);
    when(sqlQuery.getQueryString()).thenReturn("foo");
    // Add a Query Filter to verify it's called and can change the statement
    QueryFilter filter = mock(QueryFilter.class);
    query.addFilter(filter);
    when(filter.filterStatement("foo", "sql")).thenReturn("bar");
    when(filter.filterQuery(any(Query.class))).then(returnsFirstArg());
    SQLQuery finalQuery = mock(SQLQuery.class);
    when(session.createSQLQuery("bar")).thenReturn(finalQuery);
    NamedSQLQueryDefinition definition = mock(NamedSQLQueryDefinition.class);
    when(definition.getResultSetRef()).thenReturn("someResultSet");
    HibernateSessionFactory sessionFactory = this.mocker.getInstance(HibernateSessionFactory.class);
    sessionFactory.getConfiguration().getNamedSQLQueries().put(query.getStatement(), definition);
    assertSame(finalQuery, this.executor.createHibernateQuery(session, query));
    verify(finalQuery).setResultSetMapping(definition.getResultSetRef());
}
Also used : QueryFilter(org.xwiki.query.QueryFilter) NamedSQLQueryDefinition(org.hibernate.engine.NamedSQLQueryDefinition) DefaultQuery(org.xwiki.query.internal.DefaultQuery) Query(org.xwiki.query.Query) SQLQuery(org.hibernate.SQLQuery) DefaultQuery(org.xwiki.query.internal.DefaultQuery) WrappingQuery(org.xwiki.query.WrappingQuery) HibernateSessionFactory(com.xpn.xwiki.store.hibernate.HibernateSessionFactory) SQLQuery(org.hibernate.SQLQuery) Session(org.hibernate.Session) Test(org.junit.Test)

Example 2 with HibernateSessionFactory

use of com.xpn.xwiki.store.hibernate.HibernateSessionFactory in project xwiki-platform by xwiki.

the class HqlQueryExecutorTest method before.

@Before
public void before() throws Exception {
    HibernateSessionFactory sessionFactory = this.mocker.getInstance(HibernateSessionFactory.class);
    when(sessionFactory.getConfiguration()).thenReturn(new Configuration());
    this.executor = this.mocker.getComponentUnderTest();
    this.authorization = this.mocker.getInstance(ContextualAuthorizationManager.class);
    when(this.authorization.hasAccess(Right.PROGRAM)).then(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            return hasProgrammingRight;
        }
    });
    this.hasProgrammingRight = true;
    // Actual Hibernate query
    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.EXECUTIONCONTEXT_KEY)).thenReturn(xwikiContext);
    when(xwikiContext.getWikiId()).thenReturn("currentwikid");
    com.xpn.xwiki.XWiki xwiki = mock(com.xpn.xwiki.XWiki.class);
    when(xwikiContext.getWiki()).thenReturn(xwiki);
    this.store = mock(XWikiHibernateStore.class);
    when(xwiki.getHibernateStore()).thenReturn(store);
}
Also used : ContextualAuthorizationManager(org.xwiki.security.authorization.ContextualAuthorizationManager) Configuration(org.hibernate.cfg.Configuration) XWikiContext(com.xpn.xwiki.XWikiContext) Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) InvocationOnMock(org.mockito.invocation.InvocationOnMock) HibernateSessionFactory(com.xpn.xwiki.store.hibernate.HibernateSessionFactory) Before(org.junit.Before)

Example 3 with HibernateSessionFactory

use of com.xpn.xwiki.store.hibernate.HibernateSessionFactory 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)

Aggregations

HibernateSessionFactory (com.xpn.xwiki.store.hibernate.HibernateSessionFactory)3 XWikiContext (com.xpn.xwiki.XWikiContext)2 Configuration (org.hibernate.cfg.Configuration)2 Before (org.junit.Before)2 Execution (org.xwiki.context.Execution)2 ExecutionContext (org.xwiki.context.ExecutionContext)2 XWiki (com.xpn.xwiki.XWiki)1 HibernateStore (com.xpn.xwiki.internal.store.hibernate.HibernateStore)1 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)1 SQLQuery (org.hibernate.SQLQuery)1 Session (org.hibernate.Session)1 SessionFactory (org.hibernate.SessionFactory)1 NamedSQLQueryDefinition (org.hibernate.engine.NamedSQLQueryDefinition)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 Query (org.xwiki.query.Query)1 QueryFilter (org.xwiki.query.QueryFilter)1 WrappingQuery (org.xwiki.query.WrappingQuery)1 DefaultQuery (org.xwiki.query.internal.DefaultQuery)1 ContextualAuthorizationManager (org.xwiki.security.authorization.ContextualAuthorizationManager)1