1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.plugin.feed; |
21 |
|
|
22 |
|
import java.net.URL; |
23 |
|
import java.util.ArrayList; |
24 |
|
import java.util.Collections; |
25 |
|
import java.util.HashMap; |
26 |
|
import java.util.List; |
27 |
|
import java.util.Map; |
28 |
|
|
29 |
|
import org.junit.Assert; |
30 |
|
|
31 |
|
import org.jmock.Mock; |
32 |
|
import org.jmock.core.Invocation; |
33 |
|
import org.jmock.core.stub.CustomStub; |
34 |
|
import org.xwiki.display.internal.DisplayConfiguration; |
35 |
|
import org.xwiki.model.reference.DocumentReference; |
36 |
|
import org.xwiki.rendering.syntax.Syntax; |
37 |
|
|
38 |
|
import com.sun.syndication.feed.synd.SyndEntry; |
39 |
|
import com.sun.syndication.feed.synd.SyndEntryImpl; |
40 |
|
import com.xpn.xwiki.XWiki; |
41 |
|
import com.xpn.xwiki.XWikiConfig; |
42 |
|
import com.xpn.xwiki.XWikiContext; |
43 |
|
import com.xpn.xwiki.XWikiException; |
44 |
|
import com.xpn.xwiki.api.Document; |
45 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
46 |
|
import com.xpn.xwiki.objects.classes.BaseClass; |
47 |
|
import com.xpn.xwiki.store.XWikiHibernateStore; |
48 |
|
import com.xpn.xwiki.store.XWikiHibernateVersioningStore; |
49 |
|
import com.xpn.xwiki.store.XWikiStoreInterface; |
50 |
|
import com.xpn.xwiki.store.XWikiVersioningStoreInterface; |
51 |
|
import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase; |
52 |
|
import com.xpn.xwiki.user.api.XWikiRightService; |
53 |
|
import com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl; |
54 |
|
import com.xpn.xwiki.web.XWikiServletRequestStub; |
55 |
|
import com.xpn.xwiki.web.XWikiServletURLFactory; |
56 |
|
|
57 |
|
|
58 |
|
@link |
59 |
|
|
|
|
| 98.7% |
Uncovered Elements: 2 (156) |
Complexity: 23 |
Complexity Density: 0.17 |
|
60 |
|
public class SyndEntryDocumentSourceTest extends AbstractBridgedXWikiComponentTestCase |
61 |
|
{ |
62 |
|
public static final String INCONSISTENCY = "Inconsistency!"; |
63 |
|
|
64 |
|
public static final String POLYMORPHISM_INCONSISTENCY = "Polymorphism inconsistency!"; |
65 |
|
|
66 |
|
public static final String ACCESS_RIGHTS_VIOLATED = "Access rights are violated!"; |
67 |
|
|
68 |
|
public static final String PARAMETERS_IGNORED = "Parameters are ignored!"; |
69 |
|
|
70 |
|
public static final String SVG_MIME_TYPE = "image/svg+xml"; |
71 |
|
|
72 |
|
public static final String PNG_MIME_TYPE = "image/png"; |
73 |
|
|
74 |
|
public static final String ARTICLE_CLASS_NAME = "XWiki.ArticleClass"; |
75 |
|
|
76 |
|
protected SyndEntryDocumentSource source; |
77 |
|
|
78 |
|
protected XWikiDocument doc; |
79 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (21) |
Complexity: 1 |
Complexity Density: 0.05 |
|
80 |
8 |
@Override... |
81 |
|
protected void setUp() throws Exception |
82 |
|
{ |
83 |
8 |
super.setUp(); |
84 |
8 |
mockUp(); |
85 |
|
|
86 |
8 |
getContext().setUser("Condor"); |
87 |
|
|
88 |
8 |
doc = new XWikiDocument(new DocumentReference("Wiki", "MilkyWay", "Fidis")); |
89 |
8 |
doc.setCreator("Condor"); |
90 |
8 |
doc.setAuthor("Albatross"); |
91 |
8 |
doc.setTitle("Fidis from MilkyWay"); |
92 |
8 |
doc.setContent("blah blah blah.."); |
93 |
8 |
doc.setSyntax(Syntax.XWIKI_2_1); |
94 |
|
|
95 |
8 |
initArticleClass(); |
96 |
|
|
97 |
8 |
doc.createNewObject(ARTICLE_CLASS_NAME, getContext()); |
98 |
8 |
doc.setStringValue(ARTICLE_CLASS_NAME, "title", "Old story"); |
99 |
8 |
doc.setStringValue(ARTICLE_CLASS_NAME, "content", "Once upon a <i>time</i> there was.."); |
100 |
8 |
List<String> categories = new ArrayList<String>(); |
101 |
8 |
categories.add("News"); |
102 |
8 |
categories.add("Information"); |
103 |
8 |
doc.setStringListValue(ARTICLE_CLASS_NAME, "category", categories); |
104 |
|
|
105 |
8 |
getContext().getWiki().saveDocument(doc, getContext()); |
106 |
8 |
getContext().setDoc(doc); |
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
8 |
getConfigurationSource().setProperty("xwiki.render.velocity.macrolist", ""); |
112 |
|
|
113 |
8 |
source = new SyndEntryDocumentSource(); |
114 |
|
} |
115 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
116 |
8 |
@Override... |
117 |
|
protected void registerComponents() throws Exception |
118 |
|
{ |
119 |
8 |
super.registerComponents(); |
120 |
|
|
121 |
|
|
122 |
8 |
Mock mockDisplayConfiguration = registerMockComponent(DisplayConfiguration.class); |
123 |
8 |
mockDisplayConfiguration.stubs().method("getDocumentDisplayerHint").will(returnValue("default")); |
124 |
8 |
mockDisplayConfiguration.stubs().method("getTitleHeadingDepth").will(returnValue(2)); |
125 |
|
} |
126 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (19) |
Complexity: 1 |
Complexity Density: 0.05 |
|
127 |
8 |
private void mockUp() throws Exception... |
128 |
|
{ |
129 |
8 |
final Map<String, XWikiDocument> docs = new HashMap<String, XWikiDocument>(); |
130 |
8 |
final XWikiContext context = getContext(); |
131 |
|
|
132 |
|
|
133 |
8 |
context.setRequest(new XWikiServletRequestStub()); |
134 |
8 |
context.setURL(new URL("http://localhost:8080/xwiki/bin/view/MilkyWay/Fidis")); |
135 |
|
|
136 |
8 |
final XWiki xwiki = new XWiki(new XWikiConfig(), context) |
137 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
138 |
88 |
@Override... |
139 |
|
public String getXWikiPreference(String prefname, String defaultValue, XWikiContext context) |
140 |
|
{ |
141 |
88 |
return defaultValue; |
142 |
|
} |
143 |
|
}; |
144 |
8 |
context.setURLFactory(new XWikiServletURLFactory(new URL("http://www.xwiki.org/"), "xwiki/", "bin/")); |
145 |
|
|
146 |
8 |
final Mock mockXWikiStore = |
147 |
|
mock(XWikiHibernateStore.class, new Class[] {XWiki.class, XWikiContext.class}, |
148 |
|
new Object[] {xwiki, context}); |
149 |
8 |
mockXWikiStore.stubs().method("loadXWikiDoc").will( |
150 |
|
new CustomStub("Implements XWikiStoreInterface.loadXWikiDoc") |
151 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
152 |
224 |
@Override... |
153 |
|
public Object invoke(Invocation invocation) throws Throwable |
154 |
|
{ |
155 |
224 |
XWikiDocument shallowDoc = (XWikiDocument) invocation.parameterValues.get(0); |
156 |
224 |
if (docs.containsKey(shallowDoc.getName())) { |
157 |
54 |
return docs.get(shallowDoc.getName()); |
158 |
|
} else { |
159 |
170 |
return shallowDoc; |
160 |
|
} |
161 |
|
} |
162 |
|
}); |
163 |
8 |
mockXWikiStore.stubs().method("saveXWikiDoc").will( |
164 |
|
new CustomStub("Implements XWikiStoreInterface.saveXWikiDoc") |
165 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
166 |
16 |
@Override... |
167 |
|
public Object invoke(Invocation invocation) throws Throwable |
168 |
|
{ |
169 |
16 |
XWikiDocument document = (XWikiDocument) invocation.parameterValues.get(0); |
170 |
16 |
document.setNew(false); |
171 |
16 |
document.setStore((XWikiStoreInterface) mockXWikiStore.proxy()); |
172 |
16 |
docs.put(document.getName(), document); |
173 |
16 |
return null; |
174 |
|
} |
175 |
|
}); |
176 |
8 |
mockXWikiStore.stubs().method("getTranslationList").will(returnValue(Collections.EMPTY_LIST)); |
177 |
8 |
mockXWikiStore.stubs().method("exists").will(returnValue(false)); |
178 |
|
|
179 |
8 |
final Mock mockXWikiVersioningStore = |
180 |
|
mock(XWikiHibernateVersioningStore.class, new Class[] {XWiki.class, XWikiContext.class}, new Object[] { |
181 |
|
xwiki, context}); |
182 |
8 |
mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(returnValue(null)); |
183 |
8 |
mockXWikiVersioningStore.stubs().method("resetRCSArchive").will(returnValue(null)); |
184 |
|
|
185 |
8 |
xwiki.setStore((XWikiStoreInterface) mockXWikiStore.proxy()); |
186 |
8 |
xwiki.setVersioningStore((XWikiVersioningStoreInterface) mockXWikiVersioningStore.proxy()); |
187 |
|
|
188 |
8 |
final Mock mockXWikiRightsService = mock(XWikiRightServiceImpl.class, new Class[] {}, new Object[] {}); |
189 |
8 |
mockXWikiRightsService.stubs().method("hasAccessLevel").will( |
190 |
|
new CustomStub("Implements XWikiRightService.hasAccessLevel") |
191 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
192 |
12 |
@Override... |
193 |
|
public Object invoke(Invocation invocation) throws Throwable |
194 |
|
{ |
195 |
|
|
196 |
12 |
String user = (String) invocation.parameterValues.get(1); |
197 |
|
|
198 |
|
|
199 |
12 |
return new Boolean(user.length() % 2 == 0); |
200 |
|
} |
201 |
|
}); |
202 |
8 |
xwiki.setRightService((XWikiRightService) mockXWikiRightsService.proxy()); |
203 |
|
} |
204 |
|
|
|
|
| 91.7% |
Uncovered Elements: 1 (12) |
Complexity: 2 |
Complexity Density: 0.2 |
|
205 |
8 |
protected BaseClass initArticleClass() throws XWikiException... |
206 |
|
{ |
207 |
8 |
XWikiDocument doc = getContext().getWiki().getDocument(ARTICLE_CLASS_NAME, getContext()); |
208 |
8 |
boolean needsUpdate = doc.isNew(); |
209 |
|
|
210 |
8 |
BaseClass bclass = doc.getXClass(); |
211 |
8 |
bclass.setName(ARTICLE_CLASS_NAME); |
212 |
|
|
213 |
8 |
needsUpdate |= bclass.addTextField("title", "Title", 64); |
214 |
8 |
needsUpdate |= bclass.addTextAreaField("content", "Content", 45, 4); |
215 |
8 |
needsUpdate |= bclass.addTextField("category", "Category", 64); |
216 |
|
|
217 |
8 |
if (needsUpdate) { |
218 |
8 |
getContext().getWiki().saveDocument(doc, getContext()); |
219 |
|
} |
220 |
8 |
return bclass; |
221 |
|
} |
222 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
223 |
7 |
protected SyndEntryImpl source(Object obj)... |
224 |
|
{ |
225 |
7 |
return source(obj, Collections.EMPTY_MAP); |
226 |
|
} |
227 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 0.5 |
|
228 |
10 |
protected SyndEntryImpl source(Object obj, Map params)... |
229 |
|
{ |
230 |
10 |
SyndEntryImpl entry = new SyndEntryImpl(); |
231 |
10 |
try { |
232 |
10 |
source.source(entry, obj, params, getContext()); |
233 |
|
} catch (Exception e) { |
234 |
|
} |
235 |
10 |
return entry; |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
@param |
242 |
|
@return |
243 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
244 |
4 |
protected int getXMLContentLength(String xmlFragment)... |
245 |
|
{ |
246 |
4 |
return SyndEntryDocumentSource.innerTextLength(SyndEntryDocumentSource.tidy(xmlFragment, |
247 |
|
SyndEntryDocumentSource.TIDY_HTML_CONFIG)); |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
1PASS
|
|
253 |
1 |
public void testSourceConsistency()... |
254 |
|
{ |
255 |
1 |
Assert.assertEquals(INCONSISTENCY, source(doc), source(doc)); |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
@link@link@link |
261 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
262 |
1 |
public void testSourcePolymorphism()... |
263 |
|
{ |
264 |
1 |
SyndEntryImpl fromXDoc = source(doc); |
265 |
1 |
SyndEntryImpl fromDoc = source(doc.newDocument(getContext())); |
266 |
1 |
SyndEntryImpl fromFullName = source(doc.getFullName()); |
267 |
1 |
Assert.assertEquals(POLYMORPHISM_INCONSISTENCY, fromXDoc, fromDoc); |
268 |
1 |
Assert.assertEquals(POLYMORPHISM_INCONSISTENCY, fromXDoc, fromFullName); |
269 |
1 |
Assert.assertEquals(POLYMORPHISM_INCONSISTENCY, fromDoc, fromFullName); |
270 |
|
} |
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
@throws |
276 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
1PASS
|
|
277 |
1 |
public void testSourceAccessRights() throws XWikiException... |
278 |
|
{ |
279 |
|
|
280 |
1 |
getContext().setUser("XWiki.Albatross"); |
281 |
1 |
try { |
282 |
1 |
source.source(new SyndEntryImpl(), doc, Collections.EMPTY_MAP, getContext()); |
283 |
0 |
Assert.fail(ACCESS_RIGHTS_VIOLATED); |
284 |
|
} catch (XWikiException expected) { |
285 |
|
|
286 |
1 |
Assert.assertEquals(XWikiException.ERROR_XWIKI_ACCESS_DENIED, expected.getCode()); |
287 |
|
} |
288 |
|
|
289 |
1 |
getContext().setUser("Condor"); |
290 |
1 |
source.source(new SyndEntryImpl(), doc, Collections.EMPTY_MAP, getContext()); |
291 |
|
|
292 |
|
} |
293 |
|
|
294 |
|
|
295 |
|
@link |
296 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (8) |
Complexity: 1 |
Complexity Density: 0.12 |
1PASS
|
|
297 |
1 |
public void testSourceContentType()... |
298 |
|
{ |
299 |
1 |
Map instanceParams = new HashMap(); |
300 |
1 |
instanceParams.put(SyndEntryDocumentSource.CONTENT_TYPE, SVG_MIME_TYPE); |
301 |
1 |
source.setParams(instanceParams); |
302 |
1 |
Assert.assertEquals(PARAMETERS_IGNORED, SVG_MIME_TYPE, source(doc).getDescription().getType()); |
303 |
|
|
304 |
1 |
Map methodParams = new HashMap(); |
305 |
1 |
methodParams.put(SyndEntryDocumentSource.CONTENT_TYPE, PNG_MIME_TYPE); |
306 |
1 |
SyndEntry entry = source(doc, methodParams); |
307 |
1 |
Assert.assertEquals(PARAMETERS_IGNORED, PNG_MIME_TYPE, entry.getDescription().getType()); |
308 |
|
} |
309 |
|
|
310 |
|
|
311 |
|
@link |
312 |
|
@link |
313 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
314 |
1 |
public void testArticleSourcePlainContentLength()... |
315 |
|
{ |
316 |
1 |
int maxLength = 15; |
317 |
1 |
Map params = new HashMap(); |
318 |
1 |
params.put(SyndEntryDocumentSource.CONTENT_TYPE, "text/plain"); |
319 |
1 |
params.put(SyndEntryDocumentSource.CONTENT_LENGTH, maxLength); |
320 |
1 |
params.put(SyndEntryDocumentSource.FIELD_DESCRIPTION, ARTICLE_CLASS_NAME + "_content"); |
321 |
1 |
source.setParams(params); |
322 |
1 |
doc.setStringValue(ARTICLE_CLASS_NAME, "content", "Somewhere in la Mancha, in a place.."); |
323 |
1 |
Assert.assertTrue(doc.display("content", getContext()).length() > maxLength); |
324 |
1 |
int descriptionLength = source(doc).getDescription().getValue().length(); |
325 |
1 |
Assert.assertTrue(PARAMETERS_IGNORED, descriptionLength <= maxLength); |
326 |
|
} |
327 |
|
|
328 |
|
|
329 |
|
@link |
330 |
|
@link |
331 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
332 |
1 |
public void testArticleSourceHTMLContentLength()... |
333 |
|
{ |
334 |
1 |
int maxLength = 16; |
335 |
1 |
Map params = new HashMap(); |
336 |
1 |
params.put(SyndEntryDocumentSource.CONTENT_TYPE, "text/html"); |
337 |
1 |
params.put(SyndEntryDocumentSource.CONTENT_LENGTH, maxLength); |
338 |
1 |
params.put(SyndEntryDocumentSource.FIELD_DESCRIPTION, ARTICLE_CLASS_NAME + "_content"); |
339 |
1 |
doc.setStringValue(ARTICLE_CLASS_NAME, "content", |
340 |
|
"Somewhere \n\tin <i>la</i> <a href=\"http://www.mancha.es\"> Mancha</a>, in a place.."); |
341 |
1 |
Assert.assertTrue(getXMLContentLength(doc.display("content", getContext())) > maxLength); |
342 |
1 |
String description = source(doc, params).getDescription().getValue(); |
343 |
1 |
int descriptionLength = getXMLContentLength(description); |
344 |
1 |
Assert.assertTrue(PARAMETERS_IGNORED, descriptionLength <= maxLength); |
345 |
|
} |
346 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 1 |
Complexity Density: 0.1 |
1PASS
|
|
347 |
1 |
public void testArticleSourceXMLContentLength()... |
348 |
|
{ |
349 |
1 |
int maxLength = 17; |
350 |
1 |
Map params = new HashMap(); |
351 |
1 |
params.put(SyndEntryDocumentSource.CONTENT_TYPE, "text/xml"); |
352 |
1 |
params.put(SyndEntryDocumentSource.CONTENT_LENGTH, maxLength); |
353 |
1 |
params.put(SyndEntryDocumentSource.FIELD_DESCRIPTION, ARTICLE_CLASS_NAME + "_content"); |
354 |
1 |
doc.setStringValue(ARTICLE_CLASS_NAME, "content", |
355 |
|
"<text>Somewhere \n\tin la <region> Mancha</region>, in a place..</text>"); |
356 |
1 |
Assert.assertTrue(getXMLContentLength(doc.display("content", getContext())) > maxLength); |
357 |
1 |
String description = source(doc, params).getDescription().getValue(); |
358 |
1 |
int descriptionLength = getXMLContentLength(description); |
359 |
1 |
Assert.assertTrue(PARAMETERS_IGNORED, descriptionLength <= maxLength); |
360 |
|
} |
361 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (9) |
Complexity: 1 |
Complexity Density: 0.11 |
1PASS
|
|
362 |
1 |
public void testPreviewContentEncoding()... |
363 |
|
{ |
364 |
1 |
String snippet = "<p>Test ê</p>"; |
365 |
1 |
String transformedHTML = SyndEntryDocumentSource.getHTMLPreview(snippet, 10); |
366 |
1 |
Assert.assertEquals(snippet, transformedHTML); |
367 |
1 |
String transformedXML = SyndEntryDocumentSource.getXMLPreview(snippet, 10); |
368 |
1 |
Assert.assertEquals(snippet, transformedXML); |
369 |
|
|
370 |
1 |
String plainSnippet = " Test Text ê Rest "; |
371 |
1 |
String previewExpected = "Test Text ê"; |
372 |
1 |
String transformedPlain = SyndEntryDocumentSource.getPlainPreview(plainSnippet, 12); |
373 |
1 |
Assert.assertEquals(previewExpected, transformedPlain); |
374 |
|
} |
375 |
|
} |