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());
}
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);
}
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);
}
Aggregations