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

File ImplicitlyAllowedValuesDBListQueryBuilderTest.java

 

Code metrics

0
93
9
1
273
199
9
0.1
10.33
9
1

Classes

Class Line # Actions
ImplicitlyAllowedValuesDBListQueryBuilderTest 44 93 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 7 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.internal.objects.classes;
21   
22    import org.junit.Before;
23    import org.junit.Rule;
24    import org.junit.Test;
25    import org.xwiki.model.reference.DocumentReference;
26    import org.xwiki.query.Query;
27    import org.xwiki.query.QueryBuilder;
28    import org.xwiki.query.QueryFilter;
29    import org.xwiki.query.QueryManager;
30    import org.xwiki.test.mockito.MockitoComponentMockingRule;
31   
32    import com.xpn.xwiki.doc.XWikiDocument;
33    import com.xpn.xwiki.objects.classes.DBListClass;
34   
35    import static org.junit.Assert.*;
36    import static org.mockito.Mockito.*;
37   
38    /**
39    * Unit tests for {@link ImplicitlyAllowedValuesDBListQueryBuilder}.
40    *
41    * @version $Id: 285d58cb2d510c924acbb008df48ddad2525a6f5 $
42    * @since 9.8RC1
43    */
 
44    public class ImplicitlyAllowedValuesDBListQueryBuilderTest
45    {
46    @Rule
47    public MockitoComponentMockingRule<QueryBuilder<DBListClass>> mocker =
48    new MockitoComponentMockingRule<QueryBuilder<DBListClass>>(ImplicitlyAllowedValuesDBListQueryBuilder.class);
49   
50    private QueryManager queryManager;
51   
52    private QueryFilter viewableValueFilter;
53   
54    private DBListClass dbListClass = new DBListClass();
55   
 
56  7 toggle @Before
57    public void configure() throws Exception
58    {
59  7 this.queryManager = this.mocker.getInstance(QueryManager.class);
60  7 this.viewableValueFilter = this.mocker.getInstance(QueryFilter.class, "viewableAllowedDBListPropertyValue");
61   
62  7 XWikiDocument ownerDocument = mock(XWikiDocument.class);
63  7 when(ownerDocument.getDocumentReference()).thenReturn(new DocumentReference("tests", "Some", "Page"));
64  7 this.dbListClass.setOwnerDocument(ownerDocument);
65    }
66   
 
67  35 toggle private Query assertQuery(String statement) throws Exception
68    {
69  35 Query query = mock(Query.class);
70  35 when(this.queryManager.createQuery(statement, Query.HQL)).thenReturn(query);
71   
72  35 assertSame(query, this.mocker.getComponentUnderTest().build(this.dbListClass));
73  35 return query;
74    }
75   
 
76  1 toggle @Test
77    public void buildDefaultQuery() throws Exception
78    {
79  1 Query query = assertQuery("select doc.name from XWikiDocument doc where 1 = 0");
80   
81  1 verify(query).setWiki("tests");
82  1 verify(query).addFilter(this.viewableValueFilter);
83    }
84   
 
85  1 toggle @Test
86    public void buildWithClassName() throws Exception
87    {
88  1 this.dbListClass.setClassname("Blog.CategoryClass");
89   
90  1 Query query = assertQuery("select distinct doc.fullName from XWikiDocument as doc, BaseObject as obj"
91    + " where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName");
92   
93  1 verify(query).bindValue("className", "Blog.CategoryClass");
94  1 verify(query).bindValue("templateName", "Blog.CategoryTemplate");
95    }
96   
 
97  1 toggle @Test
98    public void buildWithId() throws Exception
99    {
100  1 this.dbListClass.setIdField("doc.name");
101  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.name from XWikiDocument as doc");
102   
103  1 this.dbListClass.setIdField("obj.className");
104  1 assertQuery("select distinct doc.fullName as unfilterable0, obj.className "
105    + "from XWikiDocument as doc, BaseObject as obj " + "where doc.fullName = obj.name");
106   
107  1 this.dbListClass.setIdField("property");
108  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.property from XWikiDocument as doc");
109    }
110   
 
111  1 toggle @Test
112    public void buildWithValue() throws Exception
113    {
114  1 this.dbListClass.setValueField("doc.name");
115  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.name from XWikiDocument as doc");
116   
117  1 this.dbListClass.setValueField("obj.className");
118  1 assertQuery("select distinct doc.fullName as unfilterable0, obj.className "
119    + "from XWikiDocument as doc, BaseObject as obj " + "where doc.fullName = obj.name");
120   
121  1 this.dbListClass.setValueField("property");
122  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.property from XWikiDocument as doc");
123    }
124   
 
125  1 toggle @Test
126    public void buildWithIdAndClassName() throws Exception
127    {
128  1 this.dbListClass.setClassname("XWiki.XWikiUsers");
129  1 this.dbListClass.setIdField("doc.name");
130  1 Query query = assertQuery(
131    "select distinct doc.fullName as unfilterable0, doc.name " + "from XWikiDocument as doc, BaseObject as obj "
132    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName");
133  1 verify(query).bindValue("className", "XWiki.XWikiUsers");
134  1 verify(query).bindValue("templateName", "XWiki.XWikiUsersTemplate");
135   
136  1 this.dbListClass.setIdField("obj.className");
137  1 assertQuery("select distinct doc.fullName as unfilterable0, obj.className "
138    + "from XWikiDocument as doc, BaseObject as obj "
139    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName");
140   
141  1 this.dbListClass.setIdField("property");
142  1 query = assertQuery("select distinct doc.fullName as unfilterable0, idProp.value "
143    + "from XWikiDocument as doc, BaseObject as obj, StringProperty as idProp "
144    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName"
145    + " and obj.id = idProp.id.id and idProp.id.name = :idProp");
146  1 verify(query).bindValue("idProp", "property");
147    }
148   
 
149  1 toggle @Test
150    public void buildWithIdAndValue() throws Exception
151    {
152  1 this.dbListClass.setIdField("doc.name");
153  1 this.dbListClass.setValueField("doc.name");
154  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.name from XWikiDocument as doc");
155   
156  1 this.dbListClass.setValueField("doc.creator");
157  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.name, doc.creator from XWikiDocument as doc");
158   
159  1 this.dbListClass.setValueField("obj.className");
160  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.name, obj.className "
161    + "from XWikiDocument as doc, BaseObject as obj where doc.fullName = obj.name");
162   
163  1 this.dbListClass.setValueField("property");
164  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.name, doc.property from XWikiDocument as doc");
165   
166  1 this.dbListClass.setIdField("obj.className");
167  1 this.dbListClass.setValueField("doc.name");
168  1 assertQuery("select distinct doc.fullName as unfilterable0, obj.className, doc.name "
169    + "from XWikiDocument as doc, BaseObject as obj where doc.fullName = obj.name");
170   
171  1 this.dbListClass.setValueField("obj.className");
172  1 assertQuery("select distinct doc.fullName as unfilterable0, obj.className "
173    + "from XWikiDocument as doc, BaseObject as obj where doc.fullName = obj.name");
174   
175  1 this.dbListClass.setValueField("obj.id");
176  1 assertQuery("select distinct doc.fullName as unfilterable0, obj.className, obj.id "
177    + "from XWikiDocument as doc, BaseObject as obj where doc.fullName = obj.name");
178   
179  1 this.dbListClass.setValueField("property");
180  1 assertQuery("select distinct doc.fullName as unfilterable0, obj.className, doc.property "
181    + "from XWikiDocument as doc, BaseObject as obj where doc.fullName = obj.name");
182   
183  1 this.dbListClass.setIdField("property");
184  1 this.dbListClass.setValueField("doc.name");
185  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.property, doc.name from XWikiDocument as doc");
186   
187  1 this.dbListClass.setValueField("obj.className");
188  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.property, obj.className "
189    + "from XWikiDocument as doc, BaseObject as obj where doc.fullName = obj.name");
190   
191  1 this.dbListClass.setValueField("property");
192  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.property from XWikiDocument as doc");
193   
194  1 this.dbListClass.setValueField("otherProperty");
195  1 assertQuery(
196    "select distinct doc.fullName as unfilterable0, doc.property, doc.otherProperty from XWikiDocument as doc");
197    }
198   
 
199  1 toggle @Test
200    public void buildWithIdValueAndClassName() throws Exception
201    {
202  1 this.dbListClass.setClassname("XWiki.TagClass");
203  1 this.dbListClass.setIdField("doc.name");
204  1 this.dbListClass.setValueField("doc.name");
205  1 assertQuery(
206    "select distinct doc.fullName as unfilterable0, doc.name " + "from XWikiDocument as doc, BaseObject as obj "
207    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName");
208   
209  1 this.dbListClass.setValueField("doc.creator");
210  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.name, doc.creator "
211    + "from XWikiDocument as doc, BaseObject as obj "
212    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName");
213   
214  1 this.dbListClass.setValueField("obj.className");
215  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.name, obj.className "
216    + "from XWikiDocument as doc, BaseObject as obj "
217    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName");
218   
219  1 this.dbListClass.setValueField("property");
220  1 assertQuery("select distinct doc.fullName as unfilterable0, doc.name, valueProp.value "
221    + "from XWikiDocument as doc, BaseObject as obj, StringProperty as valueProp "
222    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName "
223    + "and obj.id = valueProp.id.id and valueProp.id.name = :valueProp");
224   
225  1 this.dbListClass.setIdField("obj.className");
226  1 this.dbListClass.setValueField("doc.name");
227  1 assertQuery("select distinct doc.fullName as unfilterable0, obj.className, doc.name "
228    + "from XWikiDocument as doc, BaseObject as obj "
229    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName");
230   
231  1 this.dbListClass.setValueField("obj.className");
232  1 assertQuery("select distinct doc.fullName as unfilterable0, obj.className "
233    + "from XWikiDocument as doc, BaseObject as obj "
234    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName");
235   
236  1 this.dbListClass.setValueField("obj.id");
237  1 assertQuery("select distinct doc.fullName as unfilterable0, obj.className, obj.id "
238    + "from XWikiDocument as doc, BaseObject as obj "
239    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName");
240   
241  1 this.dbListClass.setValueField("property");
242  1 assertQuery("select distinct doc.fullName as unfilterable0, obj.className, valueProp.value "
243    + "from XWikiDocument as doc, BaseObject as obj, StringProperty as valueProp "
244    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName "
245    + "and obj.id = valueProp.id.id and valueProp.id.name = :valueProp");
246   
247  1 this.dbListClass.setIdField("property");
248  1 this.dbListClass.setValueField("doc.name");
249  1 assertQuery("select distinct doc.fullName as unfilterable0, idProp.value, doc.name "
250    + "from XWikiDocument as doc, BaseObject as obj, StringProperty as idProp "
251    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName "
252    + "and obj.id = idProp.id.id and idProp.id.name = :idProp");
253   
254  1 this.dbListClass.setValueField("obj.className");
255  1 assertQuery("select distinct doc.fullName as unfilterable0, idProp.value, obj.className "
256    + "from XWikiDocument as doc, BaseObject as obj, StringProperty as idProp "
257    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName "
258    + "and obj.id = idProp.id.id and idProp.id.name = :idProp");
259   
260  1 this.dbListClass.setValueField("property");
261  1 assertQuery("select distinct doc.fullName as unfilterable0, idProp.value "
262    + "from XWikiDocument as doc, BaseObject as obj, StringProperty as idProp "
263    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName "
264    + "and obj.id = idProp.id.id and idProp.id.name = :idProp");
265   
266  1 this.dbListClass.setValueField("otherProperty");
267  1 assertQuery("select distinct doc.fullName as unfilterable0, idProp.value, valueProp.value "
268    + "from XWikiDocument as doc, BaseObject as obj, StringProperty as idProp, StringProperty as valueProp "
269    + "where doc.fullName = obj.name and obj.className = :className and doc.fullName <> :templateName "
270    + "and obj.id = idProp.id.id and idProp.id.name = :idProp "
271    + "and obj.id = valueProp.id.id and valueProp.id.name = :valueProp");
272    }
273    }