| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 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 |
|
|
| 51 |
|
|
| 52 |
|
@version |
| 53 |
|
|
| 54 |
|
@XWikiSyntax20ComponentList |
| |
|
| 11% |
Uncovered Elements: 81 (91) |
Complexity: 19 |
Complexity Density: 0.27 |
|
| 55 |
|
public class LiveTableResultsTest extends PageTest |
| 56 |
|
{ |
| 57 |
|
private QueryManagerScriptService queryService; |
| 58 |
|
|
| 59 |
|
private Map<String, Object> results; |
| 60 |
|
|
| |
|
| 69.2% |
Uncovered Elements: 4 (13) |
Complexity: 1 |
Complexity Density: 0.08 |
|
| 61 |
4 |
@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 |
|
|
| |
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 1 |
Complexity Density: 0.04 |
4-
|
|
| 84 |
0 |
@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 |
|
|
| 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 |
|
|
| 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 |
| 126 |
|
|
| |
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
4-
|
|
| 127 |
0 |
@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 |
| 145 |
|
|
| |
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
4-
|
|
| 146 |
0 |
@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 |
|
|
| |
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
4-
|
|
| 156 |
0 |
@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 |
|
|
| 171 |
|
|
| 172 |
|
|
| |
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
| 173 |
0 |
@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 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 187 |
0 |
private void setClassName(String className)... |
| 188 |
|
{ |
| 189 |
0 |
request.put("classname", className); |
| 190 |
|
} |
| 191 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 192 |
0 |
private void setColumns(String... columns)... |
| 193 |
|
{ |
| 194 |
0 |
request.put("collist", StringUtils.join(columns, ',')); |
| 195 |
|
} |
| 196 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 197 |
0 |
private void setOffset(int offset)... |
| 198 |
|
{ |
| 199 |
0 |
request.put("offset", String.valueOf(offset)); |
| 200 |
|
} |
| 201 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 202 |
0 |
private void setLimit(int limit)... |
| 203 |
|
{ |
| 204 |
0 |
request.put("limit", String.valueOf(limit)); |
| 205 |
|
} |
| 206 |
|
|
| |
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 1 |
|
| 207 |
0 |
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 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 215 |
0 |
private void setFilter(String column, String value)... |
| 216 |
|
{ |
| 217 |
0 |
request.put(column, value); |
| 218 |
|
} |
| 219 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 220 |
0 |
private void setQueryFilters(String... filters)... |
| 221 |
|
{ |
| 222 |
0 |
request.put("queryFilters", StringUtils.join(filters, ',')); |
| 223 |
|
} |
| 224 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 225 |
0 |
private Object getTotalRowCount()... |
| 226 |
|
{ |
| 227 |
0 |
return this.results.get("totalrows"); |
| 228 |
|
} |
| 229 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 230 |
0 |
private Object getRowCount()... |
| 231 |
|
{ |
| 232 |
0 |
return this.results.get("returnedrows"); |
| 233 |
|
} |
| 234 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 235 |
0 |
private Object getOffset()... |
| 236 |
|
{ |
| 237 |
0 |
return this.results.get("offset"); |
| 238 |
|
} |
| 239 |
|
|
| |
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 240 |
0 |
@SuppressWarnings("unchecked")... |
| 241 |
|
private List<Map<String, String>> getRows() |
| 242 |
|
{ |
| 243 |
0 |
return (List<Map<String, String>>) this.results.get("rows"); |
| 244 |
|
} |
| 245 |
|
} |