1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.feed

File SyndEntryDocumentSourceTest.java

 

Code metrics

4
133
19
1
375
265
23
0.17
7
19
1.21

Classes

Class Line # Actions
SyndEntryDocumentSourceTest 60 133 0% 23 2
0.9871794698.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  224 toggle @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    {
 
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("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    {
 
192  12 toggle @Override
193    public Object invoke(Invocation invocation) throws Throwable
194    {
195    // String right = (String) invocation.parameterValues.get(0);
196  12 String user = (String) invocation.parameterValues.get(1);
197    // String doc = (String) invocation.parameterValues.get(2);
198    // we give access to all the users with an even name length
199  12 return new Boolean(user.length() % 2 == 0);
200    }
201    });
202  8 xwiki.setRightService((XWikiRightService) mockXWikiRightsService.proxy());
203    }
204   
 
205  8 toggle 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   
 
223  7 toggle protected SyndEntryImpl source(Object obj)
224    {
225  7 return source(obj, Collections.EMPTY_MAP);
226    }
227   
 
228  10 toggle 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    * Computes the sum of lengths of all the text nodes from the given XML fragment.
240    *
241    * @param xmlFragment the XML fragment to be parsed
242    * @return the number of characters in all the text nodes within the given XML fragment
243    */
 
244  4 toggle protected int getXMLContentLength(String xmlFragment)
245    {
246  4 return SyndEntryDocumentSource.innerTextLength(SyndEntryDocumentSource.tidy(xmlFragment,
247    SyndEntryDocumentSource.TIDY_HTML_CONFIG));
248    }
249   
250    /**
251    * Tests if two successive calls of the source method with the same argument have the same result.
252    */
 
253  1 toggle public void testSourceConsistency()
254    {
255  1 Assert.assertEquals(INCONSISTENCY, source(doc), source(doc));
256    }
257   
258    /**
259    * Tests if different calls of the source method have the same result when the argument passed points to the same
260    * document, irrespective of its type: {@link XWikiDocument}, {@link Document}, and {@link String}.
261    */
 
262  1 toggle 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    * Tests if the source method obeys the access rights.
274    *
275    * @throws XWikiException
276    */
 
277  1 toggle public void testSourceAccessRights() throws XWikiException
278    {
279    // odd user name length implies no access rights
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    // we should get an exception
286  1 Assert.assertEquals(XWikiException.ERROR_XWIKI_ACCESS_DENIED, expected.getCode());
287    }
288    // even user name length implies all access rights
289  1 getContext().setUser("Condor");
290  1 source.source(new SyndEntryImpl(), doc, Collections.EMPTY_MAP, getContext());
291    // we shouldn't get an exception
292    }
293   
294    /**
295    * Tests if {@link SyndEntryDocumentSource#CONTENT_TYPE} parameter is used correctly.
296    */
 
297  1 toggle 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    * Tests if {@link SyndEntryDocumentSource#CONTENT_LENGTH} parameter is used correctly when the
312    * {@link SyndEntryDocumentSource#CONTENT_TYPE} is <i>text/plain</i>.
313    */
 
314  1 toggle 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    * Tests if {@link SyndEntryDocumentSource#CONTENT_LENGTH} parameter is used correctly when the
330    * {@link SyndEntryDocumentSource#CONTENT_TYPE} is <i>text/html</i>.
331    */
 
332  1 toggle 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   
 
347  1 toggle 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   
 
362  1 toggle 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    }