1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
|
|
| 94.9% |
Uncovered Elements: 4 (78) |
Complexity: 20 |
Complexity Density: 0.37 |
|
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 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
42 |
32 |
@Override... |
43 |
|
public DocumentAccessBridge getDocumentAccessBridge() |
44 |
|
{ |
45 |
32 |
return dab; |
46 |
|
} |
47 |
|
}; |
48 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
49 |
12 |
@Before... |
50 |
|
public void setUp() throws Exception |
51 |
|
{ |
52 |
12 |
this.mockery.checking(new Expectations() |
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 1 |
Complexity Density: 0.08 |
|
53 |
12 |
{{... |
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 |
|
|
|
|
| 75% |
Uncovered Elements: 4 (16) |
Complexity: 5 |
Complexity Density: 0.62 |
|
74 |
32 |
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
90 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
98 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
106 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
1PASS
|
|
115 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
129 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
137 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
150 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
168 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1PASS
|
|
183 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
238 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
246 |
1 |
@Test... |
247 |
|
public void testLists() throws Exception |
248 |
|
{ |
249 |
|
|
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 |
|
|
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 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
263 |
1 |
@Test... |
264 |
|
public void testCustomMapping() throws Exception |
265 |
|
{ |
266 |
|
|
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 |
|
|
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 |
|
} |