1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.store.hibernate.query

File HqlQueryExecutorTest.java

 

Code metrics

6
110
26
1
383
286
34
0.31
4.23
26
1.31

Classes

Class Line # Actions
HqlQueryExecutorTest 67 110 0% 34 6
0.957746595.8%
 

Contributing tests

This file is covered by 22 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.store.hibernate.query;
21   
22    import java.util.Arrays;
23    import java.util.Collections;
24    import java.util.Date;
25    import java.util.HashMap;
26    import java.util.List;
27    import java.util.Map;
28   
29    import org.hibernate.SQLQuery;
30    import org.hibernate.Session;
31    import org.hibernate.cfg.Configuration;
32    import org.hibernate.engine.NamedSQLQueryDefinition;
33    import org.junit.Before;
34    import org.junit.Rule;
35    import org.junit.Test;
36    import org.mockito.invocation.InvocationOnMock;
37    import org.mockito.stubbing.Answer;
38    import org.xwiki.context.Execution;
39    import org.xwiki.context.ExecutionContext;
40    import org.xwiki.query.Query;
41    import org.xwiki.query.QueryException;
42    import org.xwiki.query.QueryFilter;
43    import org.xwiki.query.internal.DefaultQuery;
44    import org.xwiki.security.authorization.ContextualAuthorizationManager;
45    import org.xwiki.security.authorization.Right;
46    import org.xwiki.test.mockito.MockitoComponentMockingRule;
47   
48    import com.xpn.xwiki.XWikiContext;
49    import com.xpn.xwiki.XWikiException;
50    import com.xpn.xwiki.store.XWikiHibernateBaseStore;
51    import com.xpn.xwiki.store.XWikiHibernateStore;
52    import com.xpn.xwiki.store.hibernate.HibernateSessionFactory;
53   
54    import static org.junit.Assert.assertEquals;
55    import static org.junit.Assert.assertSame;
56    import static org.junit.Assert.fail;
57    import static org.mockito.ArgumentMatchers.any;
58    import static org.mockito.Mockito.mock;
59    import static org.mockito.Mockito.verify;
60    import static org.mockito.Mockito.when;
61   
62    /**
63    * Unit tests for {@link HqlQueryExecutor}
64    *
65    * @version $Id: 9621ee5f02e50f5284acda15e9cc80fe8725ed35 $
66    */
 
67    public class HqlQueryExecutorTest
68    {
69    @Rule
70    public MockitoComponentMockingRule<HqlQueryExecutor> mocker = new MockitoComponentMockingRule<>(
71    HqlQueryExecutor.class);
72   
73    private ContextualAuthorizationManager authorization;
74   
75    private boolean hasProgrammingRight;
76   
77    /**
78    * The component under test.
79    */
80    private HqlQueryExecutor executor;
81   
82    private XWikiHibernateStore store;
83   
 
84  22 toggle @Before
85    public void before() throws Exception
86    {
87  22 HibernateSessionFactory sessionFactory = this.mocker.getInstance(HibernateSessionFactory.class);
88  22 when(sessionFactory.getConfiguration()).thenReturn(new Configuration());
89   
90  22 this.executor = this.mocker.getComponentUnderTest();
91  22 this.authorization = this.mocker.getInstance(ContextualAuthorizationManager.class);
92   
93  22 when(this.authorization.hasAccess(Right.PROGRAM)).then(new Answer<Boolean>()
94    {
 
95  10 toggle @Override
96    public Boolean answer(InvocationOnMock invocation) throws Throwable
97    {
98  10 return hasProgrammingRight;
99    }
100    });
101   
102  22 this.hasProgrammingRight = true;
103   
104    // Actual Hibernate query
105   
106  22 Execution execution = this.mocker.getInstance(Execution.class);
107  22 ExecutionContext executionContext = mock(ExecutionContext.class);
108  22 when(execution.getContext()).thenReturn(executionContext);
109  22 XWikiContext xwikiContext = mock(XWikiContext.class);
110  22 when(executionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)).thenReturn(xwikiContext);
111  22 when(xwikiContext.getWikiId()).thenReturn("currentwikid");
112   
113  22 com.xpn.xwiki.XWiki xwiki = mock(com.xpn.xwiki.XWiki.class);
114  22 when(xwikiContext.getWiki()).thenReturn(xwiki);
115  22 this.store = mock(XWikiHibernateStore.class);
116  22 when(xwiki.getHibernateStore()).thenReturn(store);
117    }
118   
 
119  9 toggle private void execute(String statement, Boolean withProgrammingRights) throws QueryException
120    {
121  9 this.hasProgrammingRight = withProgrammingRights != null ? withProgrammingRights : true;
122   
123  9 DefaultQuery query = new DefaultQuery(statement, Query.HQL, this.executor);
124  9 if (withProgrammingRights != null) {
125  8 query.checkCurrentAuthor(true);
126    }
127   
128  9 this.executor.execute(query);
129    }
130   
 
131  2 toggle private void executeNamed(String name, Boolean withProgrammingRights) throws QueryException
132    {
133  2 this.hasProgrammingRight = withProgrammingRights;
134   
135  2 DefaultQuery query = new DefaultQuery(name, this.executor);
136  2 if (withProgrammingRights != null) {
137  2 query.checkCurrentAuthor(true);
138    }
139   
140  2 this.executor.execute(query);
141    }
142   
143    // Tests
144   
 
145  1 toggle @Test
146    public void completeShortStatementWhenEmpty()
147    {
148  1 assertEquals("select doc.fullName from XWikiDocument doc ", this.executor.completeShortFormStatement(""));
149    }
150   
 
151  1 toggle @Test
152    public void completeShortStatementStartingWithWhere()
153    {
154  1 assertEquals("select doc.fullName from XWikiDocument doc where doc.author='XWiki.Admin'",
155    this.executor.completeShortFormStatement("where doc.author='XWiki.Admin'"));
156    }
157   
 
158  1 toggle @Test
159    public void completeShortStatementStartingWithFrom()
160    {
161  1 assertEquals("select doc.fullName from XWikiDocument doc , BaseObject obj where doc.fullName=obj.name "
162    + "and obj.className='XWiki.MyClass'", this.executor.completeShortFormStatement(", BaseObject obj where "
163    + "doc.fullName=obj.name and obj.className='XWiki.MyClass'"));
164    }
165   
 
166  1 toggle @Test
167    public void completeShortStatementStartingWithOrderBy()
168    {
169  1 assertEquals("select doc.fullName from XWikiDocument doc order by doc.date desc",
170    this.executor.completeShortFormStatement("order by doc.date desc"));
171    }
172   
 
173  1 toggle @Test
174    public void completeShortStatementPassingAnAlreadyCompleteQuery()
175    {
176  1 assertEquals("select doc.fullName from XWikiDocument doc order by doc.date desc",
177    this.executor
178    .completeShortFormStatement("select doc.fullName from XWikiDocument doc order by doc.date desc"));
179    }
180   
 
181  1 toggle @Test
182    public void completeShortStatementPassingAQueryOnSomethingElseThanADocument()
183    {
184  1 assertEquals("select lock.docId from XWikiLock as lock ",
185    this.executor.completeShortFormStatement("select lock.docId from XWikiLock as lock "));
186    }
187   
 
188  1 toggle @Test
189    public void setNamedParameter()
190    {
191  1 org.hibernate.Query query = mock(org.hibernate.Query.class);
192  1 String name = "abc";
193  1 Date value = new Date();
194  1 this.executor.setNamedParameter(query, name, value);
195   
196  1 verify(query).setParameter(name, value);
197    }
198   
 
199  1 toggle @Test
200    public void setNamedParameterList()
201    {
202  1 org.hibernate.Query query = mock(org.hibernate.Query.class);
203  1 String name = "foo";
204  1 List<String> value = Arrays.asList("one", "two", "three");
205  1 this.executor.setNamedParameter(query, name, value);
206   
207  1 verify(query).setParameterList(name, value);
208    }
209   
 
210  1 toggle @Test
211    public void setNamedParameterArray()
212    {
213  1 org.hibernate.Query query = mock(org.hibernate.Query.class);
214  1 String name = "bar";
215  1 Integer[] value = new Integer[] { 1, 2, 3 };
216  1 this.executor.setNamedParameter(query, name, value);
217   
218  1 verify(query).setParameterList(name, value);
219    }
220   
 
221  1 toggle @Test
222    public void populateParameters()
223    {
224  1 org.hibernate.Query hquery = mock(org.hibernate.Query.class);
225  1 Query query = mock(Query.class);
226   
227  1 int offset = 13;
228  1 when(query.getOffset()).thenReturn(offset);
229   
230  1 int limit = 7;
231  1 when(query.getLimit()).thenReturn(limit);
232   
233  1 Map<String, Object> namedParameters = new HashMap<String, Object>();
234  1 namedParameters.put("alice", 10);
235  1 List<String> listValue = Collections.singletonList("yellow");
236  1 namedParameters.put("bob", listValue);
237  1 when(query.getNamedParameters()).thenReturn(namedParameters);
238   
239  1 this.executor.populateParameters(hquery, query);
240   
241  1 verify(hquery).setFirstResult(offset);
242  1 verify(hquery).setMaxResults(limit);
243  1 verify(hquery).setParameter("alice", 10);
244  1 verify(hquery).setParameterList("bob", listValue);
245    }
246   
 
247  1 toggle @Test
248    public void executeWhenStoreException() throws Exception
249    {
250  1 XWikiException exception = mock(XWikiException.class);
251  1 when(exception.getMessage()).thenReturn("nestedmessage");
252   
253  1 when(this.store.executeRead(any(XWikiContext.class), any(XWikiHibernateBaseStore.HibernateCallback.class)))
254    .thenThrow(exception);
255   
256  1 try {
257  1 execute("statement", null);
258  0 fail("Should have thrown an exception here");
259    } catch (QueryException expected) {
260  1 assertEquals("Exception while executing query. Query statement = [statement]", expected.getMessage());
261    // Verify nested exception!
262  1 assertEquals("nestedmessage", expected.getCause().getMessage());
263    }
264    }
265   
 
266  1 toggle @Test
267    @SuppressWarnings("unchecked")
268    public void createNamedNativeHibernateQuery() throws Exception
269    {
270  1 DefaultQuery query = new DefaultQuery("queryName", this.executor);
271  1 QueryFilter filter = mock(QueryFilter.class);
272  1 query.addFilter(filter);
273   
274  1 Session session = mock(Session.class);
275  1 SQLQuery sqlQuery = mock(SQLQuery.class);
276  1 when(session.getNamedQuery(query.getStatement())).thenReturn(sqlQuery);
277   
278  1 when(sqlQuery.getQueryString()).thenReturn("foo");
279  1 when(filter.filterStatement("foo", "sql")).thenReturn("bar");
280   
281  1 SQLQuery finalQuery = mock(SQLQuery.class);
282  1 when(session.createSQLQuery("bar")).thenReturn(finalQuery);
283   
284  1 NamedSQLQueryDefinition definition = mock(NamedSQLQueryDefinition.class);
285  1 when(definition.getResultSetRef()).thenReturn("someResultSet");
286   
287  1 HibernateSessionFactory sessionFactory = this.mocker.getInstance(HibernateSessionFactory.class);
288  1 sessionFactory.getConfiguration().getNamedSQLQueries().put(query.getStatement(), definition);
289   
290  1 assertSame(finalQuery, this.executor.createHibernateQuery(session, query));
291   
292  1 verify(finalQuery).setResultSetMapping(definition.getResultSetRef());
293    }
294   
295    // Allowed
296   
 
297  1 toggle @Test
298    public void executeShortWhereHQLQueryWithProgrammingRights() throws QueryException
299    {
300  1 execute("where doc.space='Main'", true);
301    }
302   
 
303  1 toggle @Test
304    public void executeShortFromHQLQueryWithProgrammingRights() throws QueryException
305    {
306  1 execute(", BaseObject as obj", true);
307    }
308   
 
309  1 toggle @Test
310    public void executeCompleteHQLQueryWithProgrammingRights() throws QueryException
311    {
312  1 execute("select u from XWikiDocument as doc", true);
313   
314    }
315   
 
316  1 toggle @Test
317    public void executeNamedQueryWithProgrammingRights() throws QueryException
318    {
319  1 executeNamed("somename", true);
320    }
321   
 
322  1 toggle @Test
323    public void executeShortWhereHQLQueryWithoutProgrammingRights() throws QueryException
324    {
325  1 execute("where doc.space='Main'", false);
326    }
327   
 
328  1 toggle @Test
329    public void executeShortFromHQLQueryWithoutProgrammingRights() throws QueryException
330    {
331  1 execute(", BaseObject as obj", false);
332    }
333   
334    // Not allowed
335   
 
336  1 toggle @Test
337    public void executeWhenNotAllowedSelect() throws Exception
338    {
339  1 try {
340  1 execute("select notallowed.name from NotAllowedTable notallowed", false);
341  0 fail("Should have thrown an exception here");
342    } catch (QueryException expected) {
343  1 assertEquals("The query requires programming right."
344    + " Query statement = [select notallowed.name from NotAllowedTable notallowed]", expected.getMessage());
345    }
346    }
347   
 
348  1 toggle @Test
349    public void executeDeleteWithoutProgrammingRight() throws Exception
350    {
351  1 try {
352  1 execute("delete from XWikiDocument as doc", false);
353  0 fail("Should have thrown an exception here");
354    } catch (QueryException expected) {
355  1 assertEquals("The query requires programming right. Query statement = [delete from XWikiDocument as doc]",
356    expected.getMessage());
357    }
358    }
359   
 
360  1 toggle @Test
361    public void executeNamedQueryWithoutProgrammingRight() throws Exception
362    {
363  1 try {
364  1 executeNamed("somename", false);
365  0 fail("Should have thrown an exception here");
366    } catch (QueryException expected) {
367  1 assertEquals("Named queries requires programming right. Named query = [somename]", expected.getMessage());
368    }
369    }
370   
 
371  1 toggle @Test
372    public void executeUpdateWithoutProgrammingRight() throws Exception
373    {
374  1 try {
375  1 execute("update XWikiDocument set name='name'", false);
376  0 fail("Should have thrown an exception here");
377    } catch (QueryException expected) {
378  1 assertEquals(
379    "The query requires programming right. Query statement = [update XWikiDocument set name='name']",
380    expected.getMessage());
381    }
382    }
383    }