1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.objects.classes

File DBListClassTest.java

 

Code metrics

0
150
21
1
400
342
21
0.14
7.14
21
1

Classes

Class Line # Actions
DBListClassTest 46 150 0% 21 0
1.0100%
 

Contributing tests

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