1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.query.xwql.internal

File XWQLtoHQLTranslatorTest.java

 

Code metrics

8
54
16
1
276
223
20
0.37
3.38
16
1.25

Classes

Class Line # Actions
XWQLtoHQLTranslatorTest 33 54 0% 20 4
0.9487179594.9%
 

Contributing tests

This file is covered by 12 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 org.xwiki.query.xwql.internal;
21   
22    import org.apache.commons.lang3.StringUtils;
23    import org.jmock.Expectations;
24    import org.junit.Before;
25    import org.junit.Rule;
26    import org.junit.Test;
27    import org.xwiki.bridge.DocumentAccessBridge;
28    import org.xwiki.query.xwql.internal.hql.XWQLtoHQLTranslator;
29    import org.xwiki.test.jmock.JMockRule;
30   
31    import static org.junit.Assert.fail;
32   
 
33    public class XWQLtoHQLTranslatorTest
34    {
35    @Rule
36    public final JMockRule mockery = new JMockRule();
37   
38    private DocumentAccessBridge dab = this.mockery.mock(DocumentAccessBridge.class);
39   
40    private XWQLtoHQLTranslator translator = new XWQLtoHQLTranslator()
41    {
 
42  32 toggle @Override
43    public DocumentAccessBridge getDocumentAccessBridge()
44    {
45  32 return dab;
46    }
47    };
48   
 
49  12 toggle @Before
50    public void setUp() throws Exception
51    {
52  12 this.mockery.checking(new Expectations()
 
53  12 toggle {{
54  12 allowing(dab).getPropertyType(with(any(String.class)), with(equal("number")));
55  12 will(returnValue(null));
56   
57  12 allowing(dab).getPropertyType(with(any(String.class)), with(equal("category")));
58  12 will(returnValue("DBStringListProperty"));
59   
60  12 allowing(dab).getPropertyType(with(any(String.class)), with(equal("stringlist")));
61  12 will(returnValue("StringListProperty"));
62   
63  12 allowing(dab).getPropertyType(with(any(String.class)), with(any(String.class)));
64  12 will(returnValue("StringProperty"));
65   
66  12 allowing(dab).isPropertyCustomMapped("Custom.Mapping", "cmprop");
67  12 will(returnValue(true));
68   
69  12 allowing(dab).isPropertyCustomMapped(with(any(String.class)), with(any(String.class)));
70  12 will(returnValue(false));
71    }});
72    }
73   
 
74  32 toggle void assertTranslate(String input, String expectedOutput) throws Exception
75    {
76  32 String output = translator.translate(input);
77  32 String exp[] = StringUtils.split(expectedOutput, " ");
78  32 String actual[] = StringUtils.split(output, " ");
79  764 for (int i = 0; i < Math.max(exp.length, actual.length); i++) {
80  732 String e = i < exp.length ? exp[i] : null;
81  732 String a = i < actual.length ? actual[i] : null;
82  732 if (!StringUtils.equalsIgnoreCase(e, a)) {
83  0 fail(String.format(
84    "translate assertion. input = [%s]\n expected output = [%s]\n actual output = [%s]\n first mismatch: [%s]!=[%s]",
85    input, expectedOutput, output, e, a));
86    }
87    }
88    }
89   
 
90  1 toggle @Test
91    public void testDocument() throws Exception
92    {
93  1 assertTranslate("select doc from Document as doc", "select doc from XWikiDocument as doc");
94  1 assertTranslate("select doc from Document as doc where doc.title like '%test'",
95    "select doc from XWikiDocument as doc where doc.title like '%test'");
96    }
97   
 
98  1 toggle @Test
99    public void testObject() throws Exception
100    {
101  1 assertTranslate("select doc from Document as doc, doc.object('XWiki.XWikiUs\u00E9rs') as user",
102    "select doc from XWikiDocument as doc , BaseObject as user " +
103    "where 1=1 and doc.fullName=user.name and user.className='XWiki.XWikiUs\u00E9rs'");
104    }
105   
 
106  1 toggle @Test
107    public void testProperty() throws Exception
108    {
109  1 assertTranslate(
110    "select doc from Document as doc, doc.object(XWiki.XWikiUsers) as user where user.email = 'some'",
111    "select doc from XWikiDocument as doc , BaseObject as user , StringProperty as user_email1 " +
112    "where ( user_email1.value = 'some' ) and doc.fullName=user.name and user.className='XWiki.XWikiUsers' and user_email1.id.id=user.id and user_email1.id.name='email'");
113    }
114   
 
115  1 toggle @Test
116    public void testShort() throws Exception
117    {
118  1 assertTranslate("", "select doc.fullName from XWikiDocument as doc");
119  1 assertTranslate("where doc.title like '%test'",
120    "select doc.fullName from XWikiDocument as doc where doc.title like '%test'");
121  1 assertTranslate("from doc.object(XWiki.XWikiUsers) as user",
122    "select doc.fullName from XWikiDocument as doc , BaseObject as user " +
123    "where 1=1 and doc.fullName=user.name and user.className='XWiki.XWikiUsers'");
124  1 assertTranslate("from doc.object('XWiki.XWikiUs\u00E9rs') as user where user.email = 'some'",
125    "select doc.fullName from XWikiDocument as doc , BaseObject as user , StringProperty as user_email1 " +
126    "where ( user_email1.value = 'some' ) and doc.fullName=user.name and user.className='XWiki.XWikiUs\u00E9rs' and user_email1.id.id=user.id and user_email1.id.name='email'");
127    }
128   
 
129  1 toggle @Test
130    public void testObjDeclInWhere() throws Exception
131    {
132  1 assertTranslate("where doc.object('XWiki.XWikiUs\u00E9rs').email = 'some'",
133    "select doc.fullName from XWikiDocument as doc , BaseObject as _o1, StringProperty as _o1_email2 " +
134    "where ( _o1_email2.value = 'some' ) and doc.fullName=_o1.name and _o1.className='XWiki.XWikiUs\u00E9rs' and _o1_email2.id.id=_o1.id and _o1_email2.id.name='email'");
135    }
136   
 
137  1 toggle @Test
138    public void testObjDeclInWhereWithTwoInstances() throws Exception
139    {
140  1 assertTranslate(
141    "where doc.object(XWiki.XWikiUsers).email = 'some' and doc.object(XWiki.XWikiUsers).first_name = 'Name'",
142    "select doc.fullName from XWikiDocument as doc , BaseObject as _o1, " +
143    "StringProperty as _o1_email2, StringProperty as _o1_first_name3 " +
144    "where ( _o1_email2.value = 'some' and _o1_first_name3.value = 'Name' ) " +
145    "and doc.fullName=_o1.name and _o1.className='XWiki.XWikiUsers' " +
146    "and _o1_email2.id.id=_o1.id and _o1_email2.id.name='email' " +
147    "and _o1_first_name3.id.id=_o1.id and _o1_first_name3.id.name='first_name'");
148    }
149   
 
150  1 toggle @Test
151    public void testOrderBy() throws Exception
152    {
153  1 assertTranslate("order by doc.fullName",
154    "select doc.fullName from XWikiDocument as doc order by doc.fullName");
155  1 assertTranslate("from doc.object(XWiki.XWikiUsers) as user order by user.firstname",
156    "select doc.fullName from XWikiDocument as doc , BaseObject as user , StringProperty as user_firstname1 " +
157    "where 1=1 and doc.fullName=user.name and user.className='XWiki.XWikiUsers' and user_firstname1.id.id=user.id and user_firstname1.id.name='firstname' order by user_firstname1.value");
158  1 assertTranslate("order by lower(doc.fullName)",
159    "select doc.fullName from XWikiDocument as doc order by lower ( doc.fullName )");
160  1 assertTranslate("order by upper(doc.fullName)",
161    "select doc.fullName from XWikiDocument as doc order by upper ( doc.fullName )");
162  1 assertTranslate("order by trim(doc.fullName)",
163    "select doc.fullName from XWikiDocument as doc order by trim ( doc.fullName )");
164  1 assertTranslate("order by abs(doc.elements)",
165    "select doc.fullName from XWikiDocument as doc order by abs ( doc.elements )");
166    }
167   
 
168  1 toggle @Test
169    public void testGroupBy() throws Exception
170    {
171  1 assertTranslate("where 1=1 group by doc.space",
172    "select doc.fullName from XWikiDocument as doc where 1 = 1 group by doc.space");
173  1 assertTranslate("where 1=1 group by upper(doc.space)",
174    "select doc.fullName from XWikiDocument as doc where 1 = 1 group by upper ( doc.space )");
175  1 assertTranslate("where 1=1 group by lower(doc.space)",
176    "select doc.fullName from XWikiDocument as doc where 1 = 1 group by lower ( doc.space )");
177  1 assertTranslate("where 1=1 group by trim(doc.space)",
178    "select doc.fullName from XWikiDocument as doc where 1 = 1 group by trim ( doc.space )");
179  1 assertTranslate("where 1=1 group by abs(doc.elements)",
180    "select doc.fullName from XWikiDocument as doc where 1 = 1 group by abs ( doc.elements )");
181    }
182   
 
183  1 toggle @Test
184    public void testGroupByAndOrderBy() throws Exception
185    {
186  1 assertTranslate("select obj.property1, count(obj.property2) from Document doc, "
187    + "doc.object(Some.Class) as obj group by obj.property1 order by count(obj.property2)",
188    " select obj_property11.value , count ( obj_property22.value ) "
189    + "from XWikiDocument as doc , BaseObject as obj , StringProperty as obj_property11, StringProperty as obj_property22 "
190    + "WHERE 1=1 and doc.fullName=obj.name and obj.className='Some.Class' "
191    + "and obj_property11.id.id=obj.id and obj_property11.id.name='property1' "
192    + "and obj_property22.id.id=obj.id and obj_property22.id.name='property2' "
193    + "group by obj_property11.value "
194    + "order by count ( obj_property22.value )");
195   
196  1 assertTranslate("select obj.property1, sum(obj.property2) from Document doc, "
197    + "doc.object(Some.Class) as obj group by obj.property1 order by sum(obj.property2)",
198    " select obj_property11.value , sum ( obj_property22.value ) "
199    + "from XWikiDocument as doc , BaseObject as obj , StringProperty as obj_property11, StringProperty as obj_property22 "
200    + "WHERE 1=1 and doc.fullName=obj.name and obj.className='Some.Class' "
201    + "and obj_property11.id.id=obj.id and obj_property11.id.name='property1' "
202    + "and obj_property22.id.id=obj.id and obj_property22.id.name='property2' "
203    + "group by obj_property11.value "
204    + "order by sum ( obj_property22.value )");
205   
206  1 assertTranslate("select obj.property1, avg(obj.property2) from Document doc, "
207    + "doc.object(Some.Class) as obj group by obj.property1 order by avg(obj.property2)",
208    " select obj_property11.value , avg ( obj_property22.value ) "
209    + "from XWikiDocument as doc , BaseObject as obj , StringProperty as obj_property11, StringProperty as obj_property22 "
210    + "WHERE 1=1 and doc.fullName=obj.name and obj.className='Some.Class' "
211    + "and obj_property11.id.id=obj.id and obj_property11.id.name='property1' "
212    + "and obj_property22.id.id=obj.id and obj_property22.id.name='property2' "
213    + "group by obj_property11.value "
214    + "order by avg ( obj_property22.value )");
215   
216  1 assertTranslate("select obj.property1, max(obj.property2) from Document doc, "
217    + "doc.object(Some.Class) as obj group by obj.property1 order by max(obj.property2)",
218    " select obj_property11.value , max ( obj_property22.value ) "
219    + "from XWikiDocument as doc , BaseObject as obj , StringProperty as obj_property11, StringProperty as obj_property22 "
220    + "WHERE 1=1 and doc.fullName=obj.name and obj.className='Some.Class' "
221    + "and obj_property11.id.id=obj.id and obj_property11.id.name='property1' "
222    + "and obj_property22.id.id=obj.id and obj_property22.id.name='property2' "
223    + "group by obj_property11.value "
224    + "order by max ( obj_property22.value )");
225   
226  1 assertTranslate("select obj.property1, min(obj.property2) from Document doc, "
227    + "doc.object(Some.Class) as obj group by obj.property1 order by min(obj.property2)",
228    " select obj_property11.value , min ( obj_property22.value ) "
229    + "from XWikiDocument as doc , BaseObject as obj , StringProperty as obj_property11, StringProperty as obj_property22 "
230    + "WHERE 1=1 and doc.fullName=obj.name and obj.className='Some.Class' "
231    + "and obj_property11.id.id=obj.id and obj_property11.id.name='property1' "
232    + "and obj_property22.id.id=obj.id and obj_property22.id.name='property2' "
233    + "group by obj_property11.value "
234    + "order by min ( obj_property22.value )");
235   
236    }
237   
 
238  1 toggle @Test
239    public void testInternalProperty() throws Exception
240    {
241  1 assertTranslate("select doc from Document as doc, doc.object('Blog.Categories') as c order by c.number",
242    "select doc from XWikiDocument as doc , BaseObject as c " +
243    "where 1=1 and doc.fullName=c.name and c.className='Blog.Categories' order by c.number");
244    }
245   
 
246  1 toggle @Test
247    public void testLists() throws Exception
248    {
249    // DBStringListProperty
250  1 assertTranslate("from doc.object('XWiki.ArticleClass') as a where :cat member of a.category",
251    "select doc.fullName from XWikiDocument as doc , BaseObject as a , DBStringListProperty as a_category1" +
252    " where ( :cat in elements( a_category1.list ) ) and doc.fullName=a.name and a.className='XWiki.ArticleClass' and a_category1.id.id=a.id and a_category1.id.name='category'");
253    // StringListProperty
254  1 assertTranslate("from doc.object('XWiki.Class') as c where c.stringlist like '%some%'",
255    "select doc.fullName from XWikiDocument as doc , BaseObject as c , StringListProperty as c_stringlist1" +
256    " where ( c_stringlist1.textValue like '%some%' ) and doc.fullName=c.name and c.className='XWiki.Class' and c_stringlist1.id.id=c.id and c_stringlist1.id.name='stringlist'");
257    // return DBStringListProperty
258  1 assertTranslate("select distinct a.category from Document as doc, doc.object('XWiki.ArticleClass') as a",
259    "select distinct elements(a_category1.list) from XWikiDocument as doc , BaseObject as a , DBStringListProperty as a_category1" +
260    " where 1=1 and doc.fullName=a.name and a.className='XWiki.ArticleClass' and a_category1.id.id=a.id and a_category1.id.name='category'");
261    }
262   
 
263  1 toggle @Test
264    public void testCustomMapping() throws Exception
265    {
266    // one CM prop
267  1 assertTranslate("select doc from Document as doc, doc.object('Custom.Mapping') as c where c.cmprop='some'",
268    "select doc from XWikiDocument as doc , BaseObject as c , Custom.Mapping as cCM1 " +
269    "where ( cCM1.cmprop = 'some' ) and doc.fullName=c.name and c.id=cCM1.id");
270    // CM and standard props
271  1 assertTranslate(
272    "select doc from Document as doc, doc.object('Custom.Mapping') as c where c.cmprop='some' and c.prop=1",
273    "select doc from XWikiDocument as doc , BaseObject as c , Custom.Mapping as cCM1, StringProperty as c_prop2 " +
274    "where ( cCM1.cmprop = 'some' and c_prop2.value = 1 ) and doc.fullName=c.name and c.id=cCM1.id and c_prop2.id.id=c.id and c_prop2.id.name='prop'");
275    }
276    }