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

File LiveTableResultsTest.java

 

Code metrics

4
70
17
1
245
166
19
0.27
4.12
17
1.12

Classes

Class Line # Actions
LiveTableResultsTest 55 70 0% 19 81
0.1098901111%
 

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 org.xwiki.livetable;
21   
22    import java.util.Arrays;
23    import java.util.List;
24    import java.util.Map;
25   
26    import org.apache.commons.lang3.StringUtils;
27    import org.apache.velocity.tools.generic.MathTool;
28    import org.apache.velocity.tools.generic.NumberTool;
29    import org.junit.Before;
30    import org.junit.Test;
31    import org.mockito.ArgumentCaptor;
32    import org.xwiki.model.reference.DocumentReference;
33    import org.xwiki.query.internal.ScriptQuery;
34    import org.xwiki.query.script.QueryManagerScriptService;
35    import org.xwiki.rendering.syntax.Syntax;
36    import org.xwiki.script.service.ScriptService;
37    import org.xwiki.test.page.PageTest;
38    import org.xwiki.test.page.XWikiSyntax20ComponentList;
39    import org.xwiki.velocity.tools.JSONTool;
40    import org.xwiki.velocity.tools.RegexTool;
41   
42    import com.xpn.xwiki.XWikiContext;
43    import com.xpn.xwiki.plugin.tag.TagPluginApi;
44   
45    import static org.junit.Assert.*;
46    import static org.mockito.ArgumentMatchers.*;
47    import static org.mockito.Mockito.*;
48   
49    /**
50    * Unit tests for the {@code LiveTableResults} page.
51    *
52    * @version $Id: 6b90c3e793f7bfe6bf718948dca8cba27f901b41 $
53    */
54    @XWikiSyntax20ComponentList
 
55    public class LiveTableResultsTest extends PageTest
56    {
57    private QueryManagerScriptService queryService;
58   
59    private Map<String, Object> results;
60   
 
61  4 toggle @Before
62    @SuppressWarnings("deprecation")
63    public void setUp() throws Exception
64    {
65  4 setOutputSyntax(Syntax.PLAIN_1_0);
66  4 request.put("outputSyntax", "plain");
67  4 request.put("xpage", "plain");
68  4 oldcore.getXWikiContext().setAction("get");
69   
70  4 queryService = mock(QueryManagerScriptService.class);
71  4 oldcore.getMocker().registerComponent(ScriptService.class, "query", queryService);
72   
73  4 TagPluginApi tagPluginApi = mock(TagPluginApi.class);
74  4 doReturn(tagPluginApi).when(oldcore.getSpyXWiki()).getPluginApi(eq("tag"), any(XWikiContext.class));
75   
76  4 registerVelocityTool("stringtool", new StringUtils());
77  0 registerVelocityTool("mathtool", new MathTool());
78  0 registerVelocityTool("regextool", new RegexTool());
79  0 registerVelocityTool("numbertool", new NumberTool());
80   
81  0 loadPage(new DocumentReference("xwiki", "XWiki", "LiveTableResultsMacros"));
82    }
83   
 
84  0 toggle @Test
85    public void plainPageResults() throws Exception
86    {
87  0 setColumns("doc.name", "doc.date");
88  0 setSort("doc.date", false);
89  0 setQueryFilters("currentlanguage", "hidden");
90    // Offset starting from 1.
91  0 setOffset(13);
92  0 setLimit(7);
93   
94  0 ScriptQuery query = mock(ScriptQuery.class);
95  0 when(queryService.hql(" where 1=1 order by doc.date desc")).thenReturn(query);
96  0 when(query.addFilter("currentlanguage")).thenReturn(query);
97  0 when(query.addFilter("hidden")).thenReturn(query);
98  0 when(query.setLimit(7)).thenReturn(query);
99    // Offset starting from 0.
100  0 when(query.setOffset(12)).thenReturn(query);
101  0 when(query.bindValues(anyListOf(Object.class))).thenReturn(query);
102   
103  0 when(query.count()).thenReturn(17L);
104  0 when(query.execute()).thenReturn(Arrays.<Object>asList("A.B", "X.Y"));
105   
106  0 renderPage();
107   
108  0 assertEquals(17L, getTotalRowCount());
109  0 assertEquals(2, getRowCount());
110  0 assertEquals(13, getOffset());
111   
112  0 List<Map<String, String>> rows = getRows();
113  0 assertEquals(2, rows.size());
114   
115  0 Map<String, String> ab = rows.get(0);
116  0 assertEquals("A", ab.get("doc_space"));
117  0 assertEquals("B", ab.get("doc_name"));
118   
119  0 Map<String, String> xy = rows.get(1);
120  0 assertEquals("X", xy.get("doc_space"));
121  0 assertEquals("Y", xy.get("doc_name"));
122    }
123   
124    /**
125    * @see "XWIKI-12803: Class attribute not escaped in Live Tables"
126    */
 
127  0 toggle @Test
128    public void sqlReservedKeywordAsPropertyName() throws Exception
129    {
130  0 setColumns("where");
131  0 setSort("where", true);
132  0 setClassName("My.Class");
133   
134  0 renderPage();
135   
136  0 verify(queryService).hql(
137    ", BaseObject as obj , StringProperty prop_where "
138    + "where obj.name=doc.fullName and obj.className = ? and doc.fullName not in (?, ?) "
139    + "and obj.id=prop_where.id.id and prop_where.name = ? "
140    + "order by lower(prop_where.value) asc, prop_where.value asc");
141    }
142   
143    /**
144    * @see "XWIKI-12855: Unable to sort the Location column in Page Index"
145    */
 
146  0 toggle @Test
147    public void orderByLocation() throws Exception
148    {
149  0 setSort("doc.location", false);
150   
151  0 renderPage();
152   
153  0 verify(queryService).hql(" where 1=1 order by lower(doc.fullName) desc, doc.fullName desc");
154    }
155   
 
156  0 toggle @Test
157    public void filterByLocation() throws Exception
158    {
159  0 setColumns("doc.location");
160  0 setFilter("doc.location", "web");
161   
162  0 renderPage();
163   
164  0 verify(queryService).hql(
165    " where 1=1 and ((doc.name = 'WebHome' and upper(doc.space) like upper(?) escape '!')"
166    + " or (doc.name <> 'WebHome' and upper(doc.fullName) like upper(?) escape '!')) ");
167    }
168   
169    //
170    // Helper methods
171    //
172   
 
173  0 toggle @SuppressWarnings("unchecked")
174    private void renderPage() throws Exception
175    {
176  0 JSONTool jsonTool = mock(JSONTool.class);
177  0 registerVelocityTool("jsontool", jsonTool);
178   
179  0 renderPage(new DocumentReference("xwiki", "XWiki", "LiveTableResults"));
180   
181  0 ArgumentCaptor<Object> argument = ArgumentCaptor.forClass(Object.class);
182  0 verify(jsonTool).serialize(argument.capture());
183   
184  0 this.results = (Map<String, Object>) argument.getValue();
185    }
186   
 
187  0 toggle private void setClassName(String className)
188    {
189  0 request.put("classname", className);
190    }
191   
 
192  0 toggle private void setColumns(String... columns)
193    {
194  0 request.put("collist", StringUtils.join(columns, ','));
195    }
196   
 
197  0 toggle private void setOffset(int offset)
198    {
199  0 request.put("offset", String.valueOf(offset));
200    }
201   
 
202  0 toggle private void setLimit(int limit)
203    {
204  0 request.put("limit", String.valueOf(limit));
205    }
206   
 
207  0 toggle private void setSort(String column, Boolean ascending)
208    {
209  0 request.put("sort", column);
210  0 if (ascending != null) {
211  0 request.put("dir", ascending ? "asc" : "desc");
212    }
213    }
214   
 
215  0 toggle private void setFilter(String column, String value)
216    {
217  0 request.put(column, value);
218    }
219   
 
220  0 toggle private void setQueryFilters(String... filters)
221    {
222  0 request.put("queryFilters", StringUtils.join(filters, ','));
223    }
224   
 
225  0 toggle private Object getTotalRowCount()
226    {
227  0 return this.results.get("totalrows");
228    }
229   
 
230  0 toggle private Object getRowCount()
231    {
232  0 return this.results.get("returnedrows");
233    }
234   
 
235  0 toggle private Object getOffset()
236    {
237  0 return this.results.get("offset");
238    }
239   
 
240  0 toggle @SuppressWarnings("unchecked")
241    private List<Map<String, String>> getRows()
242    {
243  0 return (List<Map<String, String>>) this.results.get("rows");
244    }
245    }