1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.objects.classes

File DBListClassTest.java

 

Code metrics

0
135
20
1
355
300
20
0.15
6.75
20
1

Classes

Class Line # Actions
DBListClassTest 45 135 0% 20 155
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.objects.classes;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.junit.Before;
26    import org.junit.Rule;
27    import org.mockito.invocation.InvocationOnMock;
28    import org.mockito.stubbing.Answer;
29   
30    import com.xpn.xwiki.XWikiContext;
31    import com.xpn.xwiki.doc.XWikiDocument;
32    import com.xpn.xwiki.test.MockitoOldcoreRule;
33    import com.xpn.xwiki.test.reference.ReferenceComponentList;
34   
35    import static org.junit.Assert.assertEquals;
36    import static org.mockito.ArgumentMatchers.any;
37    import static org.mockito.Mockito.doAnswer;
38   
39    /**
40    * Unit tests for {@link DBListClass}.
41    *
42    * @version $Id: 05a45c74df836e3fcf0e28ab06d3530649876302 $
43    */
44    @ReferenceComponentList
 
45    public class DBListClassTest
46    {
47    @Rule
48    public MockitoOldcoreRule oldcore = new MockitoOldcoreRule();
49   
 
50  0 toggle @Before
51    public void before() throws Exception
52    {
53  0 doAnswer(new Answer<String>()
54    {
 
55  0 toggle @Override
56    public String answer(InvocationOnMock invocation) throws Throwable
57    {
58  0 return invocation.getArgument(0);
59    }
60    }).when(this.oldcore.getSpyXWiki()).parseContent(any(), any(XWikiContext.class));
61   
62  0 this.oldcore.getXWikiContext().setDoc(new XWikiDocument());
63    }
64   
 
65  0 toggle public void testGetDefaultQueryWhenNoSqlSCriptSpecified()
66    {
67  0 DBListClass dblc = new DBListClass();
68  0 assertEquals("", dblc.getSql());
69  0 assertEquals("select doc.name from XWikiDocument doc where 1 = 0",
70    dblc.getQuery(this.oldcore.getXWikiContext()));
71    }
72   
 
73  0 toggle public void testGetQueryWithSqlScriptSpecified()
74    {
75  0 DBListClass dblc = new DBListClass();
76  0 assertEquals("", dblc.getSql());
77  0 String sql = "select doc.creator from XWikiDocument as doc";
78  0 dblc.setSql(sql);
79  0 assertEquals(sql, dblc.getQuery(this.oldcore.getXWikiContext()));
80    }
81   
 
82  0 toggle public void testGetQueryWithClassSpecified()
83    {
84  0 DBListClass dblc = new DBListClass();
85  0 dblc.setClassname("XWiki.XWikiUsers");
86  0 assertEquals(
87    "select distinct doc.fullName from XWikiDocument as doc, BaseObject as obj where "
88    + "doc.fullName=obj.name and obj.className='XWiki.XWikiUsers'",
89    dblc.getQuery(this.oldcore.getXWikiContext()));
90    }
91   
 
92  0 toggle public void testGetQueryWithIdSpecified()
93    {
94  0 DBListClass dblc = new DBListClass();
95  0 dblc.setIdField("doc.name");
96  0 assertEquals("select distinct doc.name from XWikiDocument as doc",
97    dblc.getQuery(this.oldcore.getXWikiContext()));
98  0 dblc.setIdField("obj.className");
99  0 assertEquals("select distinct obj.className from BaseObject as obj",
100    dblc.getQuery(this.oldcore.getXWikiContext()));
101  0 dblc.setIdField("property");
102  0 assertEquals("select distinct doc.property from XWikiDocument as doc",
103    dblc.getQuery(this.oldcore.getXWikiContext()));
104    }
105   
 
106  0 toggle public void testGetQueryWithValueSpecified()
107    {
108  0 DBListClass dblc = new DBListClass();
109  0 dblc.setValueField("doc.name");
110  0 assertEquals("select distinct doc.name from XWikiDocument as doc",
111    dblc.getQuery(this.oldcore.getXWikiContext()));
112  0 dblc.setValueField("obj.className");
113  0 assertEquals("select distinct obj.className from BaseObject as obj",
114    dblc.getQuery(this.oldcore.getXWikiContext()));
115  0 dblc.setValueField("property");
116  0 assertEquals("select distinct doc.property from XWikiDocument as doc",
117    dblc.getQuery(this.oldcore.getXWikiContext()));
118    }
119   
 
120  0 toggle public void testGetQueryWithIdAndClassnameSpecified()
121    {
122  0 DBListClass dblc = new DBListClass();
123  0 dblc.setClassname("XWiki.XWikiUsers");
124  0 dblc.setIdField("doc.name");
125  0 assertEquals(
126    "select distinct doc.name from XWikiDocument as doc, BaseObject as obj"
127    + " where doc.fullName=obj.name and obj.className='XWiki.XWikiUsers'",
128    dblc.getQuery(this.oldcore.getXWikiContext()));
129  0 dblc.setIdField("obj.className");
130  0 assertEquals("select distinct obj.className from BaseObject as obj" + " where obj.className='XWiki.XWikiUsers'",
131    dblc.getQuery(this.oldcore.getXWikiContext()));
132  0 dblc.setIdField("property");
133  0 assertEquals(
134    "select distinct idprop.value from BaseObject as obj, StringProperty as idprop"
135    + " where obj.className='XWiki.XWikiUsers'" + " and obj.id=idprop.id.id and idprop.id.name='property'",
136    dblc.getQuery(this.oldcore.getXWikiContext()));
137    }
138   
 
139  0 toggle public void testGetQueryWithIdAndValueSpecified()
140    {
141  0 DBListClass dblc = new DBListClass();
142  0 dblc.setIdField("doc.name");
143  0 dblc.setValueField("doc.name");
144  0 assertEquals("select distinct doc.name from XWikiDocument as doc",
145    dblc.getQuery(this.oldcore.getXWikiContext()));
146  0 dblc.setValueField("doc.creator");
147  0 assertEquals("select distinct doc.name, doc.creator from XWikiDocument as doc",
148    dblc.getQuery(this.oldcore.getXWikiContext()));
149  0 dblc.setValueField("obj.className");
150  0 assertEquals("select distinct doc.name, obj.className from XWikiDocument as doc, BaseObject as obj"
151    + " where doc.fullName=obj.name", dblc.getQuery(this.oldcore.getXWikiContext()));
152  0 dblc.setValueField("property");
153  0 assertEquals("select distinct doc.name, doc.property from XWikiDocument as doc",
154    dblc.getQuery(this.oldcore.getXWikiContext()));
155   
156  0 dblc.setIdField("obj.className");
157  0 dblc.setValueField("doc.name");
158  0 assertEquals(
159    "select distinct obj.className, doc.name"
160    + " from XWikiDocument as doc, BaseObject as obj where doc.fullName=obj.name",
161    dblc.getQuery(this.oldcore.getXWikiContext()));
162  0 dblc.setValueField("obj.className");
163  0 assertEquals("select distinct obj.className from BaseObject as obj",
164    dblc.getQuery(this.oldcore.getXWikiContext()));
165  0 dblc.setValueField("obj.id");
166  0 assertEquals("select distinct obj.className, obj.id from BaseObject as obj",
167    dblc.getQuery(this.oldcore.getXWikiContext()));
168  0 dblc.setValueField("property");
169  0 assertEquals(
170    "select distinct obj.className, doc.property"
171    + " from XWikiDocument as doc, BaseObject as obj where doc.fullName=obj.name",
172    dblc.getQuery(this.oldcore.getXWikiContext()));
173   
174  0 dblc.setIdField("property");
175  0 dblc.setValueField("doc.name");
176  0 assertEquals("select distinct doc.property, doc.name from XWikiDocument as doc",
177    dblc.getQuery(this.oldcore.getXWikiContext()));
178  0 dblc.setValueField("obj.className");
179  0 assertEquals(
180    "select distinct doc.property, obj.className"
181    + " from XWikiDocument as doc, BaseObject as obj where doc.fullName=obj.name",
182    dblc.getQuery(this.oldcore.getXWikiContext()));
183  0 dblc.setValueField("property");
184  0 assertEquals("select distinct doc.property from XWikiDocument as doc",
185    dblc.getQuery(this.oldcore.getXWikiContext()));
186  0 dblc.setValueField("otherProperty");
187  0 assertEquals("select distinct doc.property, doc.otherProperty from XWikiDocument as doc",
188    dblc.getQuery(this.oldcore.getXWikiContext()));
189    }
190   
 
191  0 toggle public void testGetQueryWithIdValueAndClassSpecified()
192    {
193  0 DBListClass dblc = new DBListClass();
194  0 dblc.setClassname("XWiki.XWikiUsers");
195  0 dblc.setIdField("doc.name");
196  0 dblc.setValueField("doc.name");
197  0 assertEquals(
198    "select distinct doc.name from XWikiDocument as doc, BaseObject as obj"
199    + " where doc.fullName=obj.name and obj.className='XWiki.XWikiUsers'",
200    dblc.getQuery(this.oldcore.getXWikiContext()));
201  0 dblc.setValueField("doc.creator");
202  0 assertEquals(
203    "select distinct doc.name, doc.creator from XWikiDocument as doc, BaseObject as obj"
204    + " where doc.fullName=obj.name and obj.className='XWiki.XWikiUsers'",
205    dblc.getQuery(this.oldcore.getXWikiContext()));
206  0 dblc.setValueField("obj.className");
207  0 assertEquals(
208    "select distinct doc.name, obj.className from XWikiDocument as doc, BaseObject as obj"
209    + " where doc.fullName=obj.name and obj.className='XWiki.XWikiUsers'",
210    dblc.getQuery(this.oldcore.getXWikiContext()));
211  0 dblc.setValueField("property");
212  0 assertEquals(
213    "select distinct doc.name, valueprop.value"
214    + " from XWikiDocument as doc, BaseObject as obj, StringProperty as valueprop"
215    + " where doc.fullName=obj.name and obj.className='XWiki.XWikiUsers'"
216    + " and obj.id=valueprop.id.id and valueprop.id.name='property'",
217    dblc.getQuery(this.oldcore.getXWikiContext()));
218   
219  0 dblc.setIdField("obj.className");
220  0 dblc.setValueField("doc.name");
221  0 assertEquals(
222    "select distinct obj.className, doc.name" + " from XWikiDocument as doc, BaseObject as obj"
223    + " where doc.fullName=obj.name and obj.className='XWiki.XWikiUsers'",
224    dblc.getQuery(this.oldcore.getXWikiContext()));
225  0 dblc.setValueField("obj.className");
226  0 assertEquals("select distinct obj.className from BaseObject as obj" + " where obj.className='XWiki.XWikiUsers'",
227    dblc.getQuery(this.oldcore.getXWikiContext()));
228  0 dblc.setValueField("obj.id");
229  0 assertEquals(
230    "select distinct obj.className, obj.id from BaseObject as obj" + " where obj.className='XWiki.XWikiUsers'",
231    dblc.getQuery(this.oldcore.getXWikiContext()));
232  0 dblc.setValueField("property");
233  0 assertEquals(
234    "select distinct obj.className, valueprop.value" + " from BaseObject as obj, StringProperty as valueprop"
235    + " where obj.className='XWiki.XWikiUsers'"
236    + " and obj.id=valueprop.id.id and valueprop.id.name='property'",
237    dblc.getQuery(this.oldcore.getXWikiContext()));
238   
239  0 dblc.setIdField("property");
240  0 dblc.setValueField("doc.name");
241  0 assertEquals(
242    "select distinct idprop.value, doc.name"
243    + " from XWikiDocument as doc, BaseObject as obj, StringProperty as idprop"
244    + " where doc.fullName=obj.name and obj.className='XWiki.XWikiUsers'"
245    + " and obj.id=idprop.id.id and idprop.id.name='property'",
246    dblc.getQuery(this.oldcore.getXWikiContext()));
247  0 dblc.setValueField("obj.className");
248  0 assertEquals(
249    "select distinct idprop.value, obj.className" + " from BaseObject as obj, StringProperty as idprop"
250    + " where obj.className='XWiki.XWikiUsers'" + " and obj.id=idprop.id.id and idprop.id.name='property'",
251    dblc.getQuery(this.oldcore.getXWikiContext()));
252  0 dblc.setValueField("property");
253  0 assertEquals(
254    "select distinct idprop.value" + " from BaseObject as obj, StringProperty as idprop"
255    + " where obj.className='XWiki.XWikiUsers'" + " and obj.id=idprop.id.id and idprop.id.name='property'",
256    dblc.getQuery(this.oldcore.getXWikiContext()));
257  0 dblc.setValueField("otherProperty");
258  0 assertEquals(
259    "select distinct idprop.value, valueprop.value"
260    + " from BaseObject as obj, StringProperty as idprop, StringProperty as valueprop"
261    + " where obj.className='XWiki.XWikiUsers'" + " and obj.id=idprop.id.id and idprop.id.name='property'"
262    + " and obj.id=valueprop.id.id and valueprop.id.name='otherProperty'",
263    dblc.getQuery(this.oldcore.getXWikiContext()));
264    }
265   
266    /** Tests that {@link DBListClass#getList} returns values sorted according to the property's sort option. */
 
267  0 toggle public void testGetListIsSorted()
268    {
269  0 List<ListItem> values = new ArrayList<ListItem>(4);
270  0 values.add(new ListItem("a", "A"));
271  0 values.add(new ListItem("c", "D"));
272  0 values.add(new ListItem("d", "C"));
273  0 values.add(new ListItem("b", "B"));
274  0 DBListClass dblc = new DBListClass();
275  0 dblc.setCache(true);
276  0 dblc.setCachedDBList(values, this.oldcore.getXWikiContext());
277   
278  0 assertEquals("Default order was not preserved.", "[a, c, d, b]",
279    dblc.getList(this.oldcore.getXWikiContext()).toString());
280  0 dblc.setSort("none");
281  0 assertEquals("Default order was not preserved.", "[a, c, d, b]",
282    dblc.getList(this.oldcore.getXWikiContext()).toString());
283  0 dblc.setSort("id");
284  0 assertEquals("Items were not ordered by ID.", "[a, b, c, d]",
285    dblc.getList(this.oldcore.getXWikiContext()).toString());
286  0 dblc.setSort("value");
287  0 assertEquals("Items were not ordered by value.", "[a, b, d, c]",
288    dblc.getList(this.oldcore.getXWikiContext()).toString());
289    }
290   
 
291  0 toggle public void testReturnColWithOneColumn()
292    {
293  0 DBListClass dblc = new DBListClass();
294  0 assertEquals("doc.fullName", dblc.returnCol("select doc.fullName from XWikiDocument as doc", true));
295  0 assertEquals("-", dblc.returnCol("select doc.fullName from XWikiDocument as doc", false));
296    }
297   
 
298  0 toggle public void testReturnColWithOneColumnAndExtraWhitespace()
299    {
300  0 DBListClass dblc = new DBListClass();
301  0 assertEquals("doc.fullName", dblc.returnCol("select doc.fullName from XWikiDocument as doc", true));
302  0 assertEquals("-", dblc.returnCol("select doc.fullName from XWikiDocument as doc", false));
303    }
304   
 
305  0 toggle public void testReturnColWithOneColumnAndUppercaseTokens()
306    {
307  0 DBListClass dblc = new DBListClass();
308  0 assertEquals("doc.fullName", dblc.returnCol("SELECT doc.fullName FROM XWikiDocument as doc", true));
309  0 assertEquals("-", dblc.returnCol("SELECT doc.fullName FROM XWikiDocument as doc", false));
310    }
311   
 
312  0 toggle public void testReturnColWithTwoColumns()
313    {
314  0 DBListClass dblc = new DBListClass();
315  0 assertEquals("doc.fullName", dblc.returnCol("select doc.fullName, doc.title from XWikiDocument as doc", true));
316  0 assertEquals("doc.title", dblc.returnCol("select doc.fullName, doc.title from XWikiDocument as doc", false));
317    }
318   
 
319  0 toggle public void testReturnColWithTwoColumnsAndExtraWhitespace()
320    {
321  0 DBListClass dblc = new DBListClass();
322  0 assertEquals("doc.fullName",
323    dblc.returnCol("select doc.fullName , doc.title from XWikiDocument as doc", true));
324  0 assertEquals("doc.title",
325    dblc.returnCol("select doc.fullName , doc.title from XWikiDocument as doc", false));
326    }
327   
 
328  0 toggle public void testReturnColWithTwoColumnsAndUppercaseTokens()
329    {
330  0 DBListClass dblc = new DBListClass();
331  0 assertEquals("doc.fullName", dblc.returnCol("SELECT doc.fullName, doc.title FROM XWikiDocument as doc", true));
332  0 assertEquals("doc.title", dblc.returnCol("SELECT doc.fullName, doc.title FROM XWikiDocument as doc", false));
333    }
334   
 
335  0 toggle public void testReturnColWithNullQuery()
336    {
337  0 DBListClass dblc = new DBListClass();
338  0 assertEquals("-", dblc.returnCol(null, true));
339  0 assertEquals("-", dblc.returnCol(null, false));
340    }
341   
 
342  0 toggle public void testReturnColWithEmptyQuery()
343    {
344  0 DBListClass dblc = new DBListClass();
345  0 assertEquals("-", dblc.returnCol("", true));
346  0 assertEquals("-", dblc.returnCol("", false));
347    }
348   
 
349  0 toggle public void testReturnColWithInvalidQuery()
350    {
351  0 DBListClass dblc = new DBListClass();
352  0 assertEquals("-", dblc.returnCol("do something", true));
353  0 assertEquals("-", dblc.returnCol("do something", false));
354    }
355    }