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

File SyndEntryDocumentSourceTest.java

 

Code metrics

4
135
20
1
384
274
24
0.18
6.75
20
1.2

Classes

Class Line # Actions
SyndEntryDocumentSourceTest 60 135 0% 24 2
0.987421498.7%
 

Contributing tests

This file is covered by 8 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.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    * Unit tests for {@link SyndEntryDocumentSource}.
59    */
 
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   
 
80  8 toggle @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    // Ensure that no Velocity Templates are going to be used when executing Velocity since otherwise
109    // the Velocity init would fail (since by default the macros.vm templates wouldn't be found as we're
110    // not providing it in our unit test resources).
111  8 getConfigurationSource().setProperty("xwiki.render.velocity.macrolist", "");
112   
113  8 source = new SyndEntryDocumentSource();
114    }
115   
 
116  8 toggle @Override
117    protected void registerComponents() throws Exception
118    {
119  8 super.registerComponents();
120   
121    // Setup display configuration.
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   
 
127  8 toggle 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    // Set URL/Request
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    {
 
138  88 toggle @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    {
 
152  191 toggle @Override
153    public Object invoke(Invocation invocation) throws Throwable
154    {
155  191 XWikiDocument shallowDoc = (XWikiDocument) invocation.parameterValues.get(0);
156  191 if (docs.containsKey(shallowDoc.getName())) {
157  43 return docs.get(shallowDoc.getName());
158    } else {
159  148 return shallowDoc;
160    }
161    }
162    });
163  8 mockXWikiStore.stubs().method("saveXWikiDoc").will(
164    new CustomStub("Implements XWikiStoreInterface.saveXWikiDoc")
165    {
 
166  16 toggle @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("executeRead").will(
177    new CustomStub("Implements XWikiStoreInterface.executeRead")
178    {
 
179  24 toggle @Override
180    public Object invoke(Invocation invocation) throws Throwable
181    {
182  24 return Collections.emptyList();
183    }
184    });
185  8 mockXWikiStore.stubs().method("getTranslationList").will(returnValue(Collections.EMPTY_LIST));
186  8 mockXWikiStore.stubs().method("exists").will(returnValue(false));
187   
188  8 final Mock mockXWikiVersioningStore =
189    mock(XWikiHibernateVersioningStore.class, new Class[] {XWiki.class, XWikiContext.class}, new Object[] {
190    xwiki, context});
191  8 mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(returnValue(null));
192  8 mockXWikiVersioningStore.stubs().method("resetRCSArchive").will(returnValue(null));
193   
194  8 xwiki.setStore((XWikiStoreInterface) mockXWikiStore.proxy());
195  8 xwiki.setVersioningStore((XWikiVersioningStoreInterface) mockXWikiVersioningStore.proxy());
196   
197  8 final Mock mockXWikiRightsService = mock(XWikiRightServiceImpl.class, new Class[] {}, new Object[] {});
198  8 mockXWikiRightsService.stubs().method("hasAccessLevel").will(
199    new CustomStub("Implements XWikiRightService.hasAccessLevel")
200    {
 
201  12 toggle @Override
202    public Object invoke(Invocation invocation) throws Throwable
203    {
204    // String right = (String) invocation.parameterValues.get(0);
205  12 String user = (String) invocation.parameterValues.get(1);
206    // String doc = (String) invocation.parameterValues.get(2);
207    // we give access to all the users with an even name length
208  12 return new Boolean(user.length() % 2 == 0);
209    }
210    });
211  8 xwiki.setRightService((XWikiRightService) mockXWikiRightsService.proxy());
212    }
213   
 
214  8 toggle protected BaseClass initArticleClass() throws XWikiException
215    {
216  8 XWikiDocument doc = getContext().getWiki().getDocument(ARTICLE_CLASS_NAME, getContext());
217  8 boolean needsUpdate = doc.isNew();
218   
219  8 BaseClass bclass = doc.getXClass();
220  8 bclass.setName(ARTICLE_CLASS_NAME);
221   
222  8 needsUpdate |= bclass.addTextField("title", "Title", 64);
223  8 needsUpdate |= bclass.addTextAreaField("content", "Content", 45, 4);
224  8 needsUpdate |= bclass.addTextField("category", "Category", 64);
225   
226  8 if (needsUpdate) {
227  8 getContext().getWiki().saveDocument(doc, getContext());
228    }
229  8 return bclass;
230    }
231   
 
232  7 toggle protected SyndEntryImpl source(Object obj)
233    {
234  7 return source(obj, Collections.EMPTY_MAP);
235    }
236   
 
237  10 toggle protected SyndEntryImpl source(Object obj, Map params)
238    {
239  10 SyndEntryImpl entry = new SyndEntryImpl();
240  10 try {
241  10 source.source(entry, obj, params, getContext());
242    } catch (Exception e) {
243    }
244  10 return entry;
245    }
246   
247    /**
248    * Computes the sum of lengths of all the text nodes from the given XML fragment.
249    *
250    * @param xmlFragment the XML fragment to be parsed
251    * @return the number of characters in all the text nodes within the given XML fragment
252    */
 
253  4 toggle protected int getXMLContentLength(String xmlFragment)
254    {
255  4 return SyndEntryDocumentSource.innerTextLength(SyndEntryDocumentSource.tidy(xmlFragment,
256    SyndEntryDocumentSource.TIDY_HTML_CONFIG));
257    }
258   
259    /**
260    * Tests if two successive calls of the source method with the same argument have the same result.
261    */
 
262  1 toggle public void testSourceConsistency()
263    {
264  1 Assert.assertEquals(INCONSISTENCY, source(doc), source(doc));
265    }
266   
267    /**
268    * Tests if different calls of the source method have the same result when the argument passed points to the same
269    * document, irrespective of its type: {@link XWikiDocument}, {@link Document}, and {@link String}.
270    */
 
271  1 toggle public void testSourcePolymorphism()
272    {
273  1 SyndEntryImpl fromXDoc = source(doc);
274  1 SyndEntryImpl fromDoc = source(doc.newDocument(getContext()));
275  1 SyndEntryImpl fromFullName = source(doc.getFullName());
276  1 Assert.assertEquals(POLYMORPHISM_INCONSISTENCY, fromXDoc, fromDoc);
277  1 Assert.assertEquals(POLYMORPHISM_INCONSISTENCY, fromXDoc, fromFullName);
278  1 Assert.assertEquals(POLYMORPHISM_INCONSISTENCY, fromDoc, fromFullName);
279    }
280   
281    /**
282    * Tests if the source method obeys the access rights.
283    *
284    * @throws XWikiException
285    */
 
286  1 toggle public void testSourceAccessRights() throws XWikiException
287    {
288    // odd user name length implies no access rights
289  1 getContext().setUser("XWiki.Albatross");
290  1 try {
291  1 source.source(new SyndEntryImpl(), doc, Collections.EMPTY_MAP, getContext());
292  0 Assert.fail(ACCESS_RIGHTS_VIOLATED);
293    } catch (XWikiException expected) {
294    // we should get an exception
295  1 Assert.assertEquals(XWikiException.ERROR_XWIKI_ACCESS_DENIED, expected.getCode());
296    }
297    // even user name length implies all access rights
298  1 getContext().setUser("Condor");
299  1 source.source(new SyndEntryImpl(), doc, Collections.EMPTY_MAP, getContext());
300    // we shouldn't get an exception
301    }
302   
303    /**
304    * Tests if {@link SyndEntryDocumentSource#CONTENT_TYPE} parameter is used correctly.
305    */
 
306  1 toggle public void testSourceContentType()
307    {
308  1 Map instanceParams = new HashMap();
309  1 instanceParams.put(SyndEntryDocumentSource.CONTENT_TYPE, SVG_MIME_TYPE);
310  1 source.setParams(instanceParams);
311  1 Assert.assertEquals(PARAMETERS_IGNORED, SVG_MIME_TYPE, source(doc).getDescription().getType());
312   
313  1 Map methodParams = new HashMap();
314  1 methodParams.put(SyndEntryDocumentSource.CONTENT_TYPE, PNG_MIME_TYPE);
315  1 SyndEntry entry = source(doc, methodParams);
316  1 Assert.assertEquals(PARAMETERS_IGNORED, PNG_MIME_TYPE, entry.getDescription().getType());
317    }
318   
319    /**
320    * Tests if {@link SyndEntryDocumentSource#CONTENT_LENGTH} parameter is used correctly when the
321    * {@link SyndEntryDocumentSource#CONTENT_TYPE} is <i>text/plain</i>.
322    */
 
323  1 toggle public void testArticleSourcePlainContentLength()
324    {
325  1 int maxLength = 15;
326  1 Map params = new HashMap();
327  1 params.put(SyndEntryDocumentSource.CONTENT_TYPE, "text/plain");
328  1 params.put(SyndEntryDocumentSource.CONTENT_LENGTH, maxLength);
329  1 params.put(SyndEntryDocumentSource.FIELD_DESCRIPTION, ARTICLE_CLASS_NAME + "_content");
330  1 source.setParams(params);
331  1 doc.setStringValue(ARTICLE_CLASS_NAME, "content", "Somewhere in la Mancha, in a place..");
332  1 Assert.assertTrue(doc.display("content", getContext()).length() > maxLength);
333  1 int descriptionLength = source(doc).getDescription().getValue().length();
334  1 Assert.assertTrue(PARAMETERS_IGNORED, descriptionLength <= maxLength);
335    }
336   
337    /**
338    * Tests if {@link SyndEntryDocumentSource#CONTENT_LENGTH} parameter is used correctly when the
339    * {@link SyndEntryDocumentSource#CONTENT_TYPE} is <i>text/html</i>.
340    */
 
341  1 toggle public void testArticleSourceHTMLContentLength()
342    {
343  1 int maxLength = 16;
344  1 Map params = new HashMap();
345  1 params.put(SyndEntryDocumentSource.CONTENT_TYPE, "text/html");
346  1 params.put(SyndEntryDocumentSource.CONTENT_LENGTH, maxLength);
347  1 params.put(SyndEntryDocumentSource.FIELD_DESCRIPTION, ARTICLE_CLASS_NAME + "_content");
348  1 doc.setStringValue(ARTICLE_CLASS_NAME, "content",
349    "Somewhere \n\tin <i>la</i> <a href=\"http://www.mancha.es\"> Mancha</a>, in a place..");
350  1 Assert.assertTrue(getXMLContentLength(doc.display("content", getContext())) > maxLength);
351  1 String description = source(doc, params).getDescription().getValue();
352  1 int descriptionLength = getXMLContentLength(description);
353  1 Assert.assertTrue(PARAMETERS_IGNORED, descriptionLength <= maxLength);
354    }
355   
 
356  1 toggle public void testArticleSourceXMLContentLength()
357    {
358  1 int maxLength = 17;
359  1 Map params = new HashMap();
360  1 params.put(SyndEntryDocumentSource.CONTENT_TYPE, "text/xml");
361  1 params.put(SyndEntryDocumentSource.CONTENT_LENGTH, maxLength);
362  1 params.put(SyndEntryDocumentSource.FIELD_DESCRIPTION, ARTICLE_CLASS_NAME + "_content");
363  1 doc.setStringValue(ARTICLE_CLASS_NAME, "content",
364    "<text>Somewhere \n\tin la <region> Mancha</region>, in a place..</text>");
365  1 Assert.assertTrue(getXMLContentLength(doc.display("content", getContext())) > maxLength);
366  1 String description = source(doc, params).getDescription().getValue();
367  1 int descriptionLength = getXMLContentLength(description);
368  1 Assert.assertTrue(PARAMETERS_IGNORED, descriptionLength <= maxLength);
369    }
370   
 
371  1 toggle public void testPreviewContentEncoding()
372    {
373  1 String snippet = "<p>Test ê</p>";
374  1 String transformedHTML = SyndEntryDocumentSource.getHTMLPreview(snippet, 10);
375  1 Assert.assertEquals(snippet, transformedHTML);
376  1 String transformedXML = SyndEntryDocumentSource.getXMLPreview(snippet, 10);
377  1 Assert.assertEquals(snippet, transformedXML);
378   
379  1 String plainSnippet = " Test Text ê Rest ";
380  1 String previewExpected = "Test Text ê";
381  1 String transformedPlain = SyndEntryDocumentSource.getPlainPreview(plainSnippet, 12);
382  1 Assert.assertEquals(previewExpected, transformedPlain);
383    }
384    }