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

File XWikiDocumentTest.java

 

Code metrics

0
330
26
1
741
539
26
0.08
12.69
26
1

Classes

Class Line # Actions
XWikiDocumentTest 64 330 0% 26 7
0.980337198%
 

Contributing tests

This file is covered by 22 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.doc;
21   
22    import java.io.StringWriter;
23    import java.util.ArrayList;
24    import java.util.Arrays;
25    import java.util.Collections;
26    import java.util.HashMap;
27    import java.util.HashSet;
28    import java.util.LinkedHashSet;
29    import java.util.List;
30    import java.util.Locale;
31    import java.util.Map;
32    import java.util.ResourceBundle;
33    import java.util.Set;
34   
35    import org.apache.velocity.VelocityContext;
36    import org.jmock.Mock;
37    import org.jmock.core.Invocation;
38    import org.jmock.core.stub.CustomStub;
39    import org.xwiki.context.Execution;
40    import org.xwiki.model.reference.DocumentReference;
41    import org.xwiki.rendering.syntax.Syntax;
42    import org.xwiki.test.internal.MockConfigurationSource;
43    import org.xwiki.velocity.VelocityEngine;
44    import org.xwiki.velocity.VelocityManager;
45   
46    import com.xpn.xwiki.XWiki;
47    import com.xpn.xwiki.XWikiContext;
48    import com.xpn.xwiki.XWikiException;
49    import com.xpn.xwiki.api.DocumentSection;
50    import com.xpn.xwiki.objects.BaseObject;
51    import com.xpn.xwiki.objects.classes.BaseClass;
52    import com.xpn.xwiki.objects.classes.TextAreaClass;
53    import com.xpn.xwiki.store.XWikiStoreInterface;
54    import com.xpn.xwiki.store.XWikiVersioningStoreInterface;
55    import com.xpn.xwiki.test.AbstractBridgedXWikiComponentTestCase;
56    import com.xpn.xwiki.user.api.XWikiRightService;
57    import com.xpn.xwiki.web.XWikiMessageTool;
58   
59    /**
60    * Unit tests for {@link XWikiDocument}.
61    *
62    * @version $Id: e8e1dc62b66569a480c7bfd6973f01b256f6346e $
63    */
 
64    public class XWikiDocumentTest extends AbstractBridgedXWikiComponentTestCase
65    {
66    private static final String DOCWIKI = "Wiki";
67   
68    private static final String DOCSPACE = "Space";
69   
70    private static final String DOCNAME = "Page";
71   
72    private static final String DOCFULLNAME = DOCSPACE + "." + DOCNAME;
73   
74    private static final DocumentReference DOCUMENT_REFERENCE = new DocumentReference(DOCWIKI, DOCSPACE, DOCNAME);
75   
76    private static final String CLASSNAME = DOCFULLNAME;
77   
78    private static final DocumentReference CLASS_REFERENCE = DOCUMENT_REFERENCE;
79   
80    private XWikiDocument document;
81   
82    private XWikiDocument translatedDocument;
83   
84    private Mock mockXWiki;
85   
86    private Mock mockXWikiVersioningStore;
87   
88    private Mock mockXWikiStoreInterface;
89   
90    private Mock mockXWikiMessageTool;
91   
92    private Mock mockXWikiRightService;
93   
94    private Mock mockVelocityManager;
95   
96    private Mock mockVelocityEngine;
97   
98    private CustomStub velocityEngineEvaluateStub;
99   
100    private BaseClass baseClass;
101   
102    private BaseObject baseObject;
103   
104    private BaseObject baseObject2;
105   
 
106  22 toggle @Override
107    protected void setUp() throws Exception
108    {
109  22 super.setUp();
110   
111  22 this.document = new XWikiDocument(new DocumentReference(DOCWIKI, DOCSPACE, DOCNAME));
112  22 this.document.setSyntax(Syntax.XWIKI_1_0);
113  22 this.document.setLanguage("en");
114  22 this.document.setDefaultLanguage("en");
115  22 this.document.setNew(false);
116   
117  22 this.translatedDocument = new XWikiDocument();
118  22 this.translatedDocument.setSyntax(Syntax.XWIKI_2_0);
119  22 this.translatedDocument.setLanguage("fr");
120  22 this.translatedDocument.setNew(false);
121   
122  22 getContext().put("isInRenderingEngine", true);
123   
124  22 this.mockXWiki = mock(XWiki.class);
125   
126  22 this.mockXWikiVersioningStore = mock(XWikiVersioningStoreInterface.class);
127  22 this.mockXWikiVersioningStore.stubs().method("getXWikiDocumentArchive").will(returnValue(null));
128   
129  22 this.mockXWikiStoreInterface = mock(XWikiStoreInterface.class);
130  22 this.document.setStore((XWikiStoreInterface) this.mockXWikiStoreInterface.proxy());
131   
132  22 this.mockXWikiMessageTool = mock(XWikiMessageTool.class,
133    new Class[] { ResourceBundle.class, XWikiContext.class }, new Object[] { null, getContext() });
134  22 this.mockXWikiMessageTool.stubs().method("get").will(returnValue("message"));
135   
136  22 this.mockXWikiRightService = mock(XWikiRightService.class);
137  22 this.mockXWikiRightService.stubs().method("hasProgrammingRights").will(returnValue(true));
138   
139  22 this.mockXWiki.stubs().method("getVersioningStore").will(returnValue(this.mockXWikiVersioningStore.proxy()));
140  22 this.mockXWiki.stubs().method("getStore").will(returnValue(this.mockXWikiStoreInterface.proxy()));
141  22 this.mockXWiki.stubs().method("getDocument").will(returnValue(this.document));
142  22 this.mockXWiki.stubs().method("getLanguagePreference").will(returnValue("en"));
143  22 this.mockXWiki.stubs().method("getSectionEditingDepth").will(returnValue(2L));
144  22 this.mockXWiki.stubs().method("getRightService").will(returnValue(this.mockXWikiRightService.proxy()));
145  22 this.mockXWiki.stubs().method("exists").will(returnValue(false));
146  22 this.mockXWiki.stubs().method("evaluateTemplate").will(returnValue(""));
147   
148  22 getContext().setWiki((XWiki) this.mockXWiki.proxy());
149  22 getContext().put("msg", this.mockXWikiMessageTool.proxy());
150   
151  22 this.baseClass = this.document.getXClass();
152  22 this.baseClass.addTextField("string", "String", 30);
153  22 this.baseClass.addTextAreaField("area", "Area", 10, 10);
154  22 this.baseClass.addTextAreaField("puretextarea", "Pure text area", 10, 10);
155    // set the text areas an non interpreted content
156  22 ((TextAreaClass) this.baseClass.getField("puretextarea")).setContentType("puretext");
157  22 this.baseClass.addPasswordField("passwd", "Password", 30);
158  22 this.baseClass.addBooleanField("boolean", "Boolean", "yesno");
159  22 this.baseClass.addNumberField("int", "Int", 10, "integer");
160  22 this.baseClass.addStaticListField("stringlist", "StringList", "value1, value2");
161   
162  22 this.mockXWiki.stubs().method("getClass").will(returnValue(this.baseClass));
163  22 this.mockXWiki.stubs().method("getXClass").will(returnValue(this.baseClass));
164   
165  22 this.baseObject = this.document.newObject(CLASSNAME, getContext());
166  22 this.baseObject.setStringValue("string", "string");
167  22 this.baseObject.setLargeStringValue("area", "area");
168  22 this.baseObject.setStringValue("passwd", "passwd");
169  22 this.baseObject.setIntValue("boolean", 1);
170  22 this.baseObject.setIntValue("int", 42);
171  22 this.baseObject.setStringListValue("stringlist", Arrays.asList("VALUE1", "VALUE2"));
172   
173  22 this.baseObject2 = this.baseObject.clone();
174  22 this.document.addXObject(this.baseObject2);
175   
176  22 this.mockXWikiStoreInterface.stubs().method("search").will(returnValue(new ArrayList<XWikiDocument>()));
177    }
178   
 
179  22 toggle @Override
180    protected void registerComponents() throws Exception
181    {
182  22 super.registerComponents();
183   
184    // Mock xwiki.cfg
185  22 getComponentManager().registerComponent(MockConfigurationSource.getDescriptor("xwikicfg"),
186    getConfigurationSource());
187   
188    // Setup the mock Velocity engine.
189  22 this.mockVelocityManager = registerMockComponent(VelocityManager.class);
190  22 this.mockVelocityEngine = mock(VelocityEngine.class);
191  22 this.mockVelocityManager.stubs().method("getVelocityContext").will(returnValue(null));
192  22 this.mockVelocityManager.stubs().method("getVelocityEngine").will(returnValue(this.mockVelocityEngine.proxy()));
193  22 velocityEngineEvaluateStub = new CustomStub("Implements VelocityEngine.evaluate")
194    {
 
195  0 toggle @Override
196    public Object invoke(Invocation invocation) throws Throwable
197    {
198    // Output the given text without changes.
199  0 StringWriter writer = (StringWriter) invocation.parameterValues.get(1);
200  0 String text = (String) invocation.parameterValues.get(3);
201  0 writer.append(text);
202  0 return true;
203    }
204    };
205  22 this.mockVelocityEngine.stubs().method("evaluate").will(velocityEngineEvaluateStub);
206  22 this.mockVelocityEngine.stubs().method("startedUsingMacroNamespace");
207  22 this.mockVelocityEngine.stubs().method("stoppedUsingMacroNamespace");
208    }
209   
 
210  1 toggle public void testGetUniqueLinkedPages10()
211    {
212  1 XWikiDocument contextDocument =
213    new XWikiDocument(new DocumentReference("contextdocwiki", "contextdocspace", "contextdocpage"));
214  1 getContext().setDoc(contextDocument);
215   
216  1 this.mockXWiki.stubs().method("exists").will(returnValue(true));
217   
218  1 this.document.setContent("[TargetPage][TargetLabel>TargetPage][TargetSpace.TargetPage]"
219    + "[TargetLabel>TargetSpace.TargetPage?param=value#anchor][http://externallink][mailto:mailto][label>]");
220   
221  1 Set<String> linkedPages = this.document.getUniqueLinkedPages(getContext());
222   
223  1 assertEquals(new HashSet<String>(Arrays.asList("TargetPage", "TargetSpace.TargetPage")),
224    new HashSet<String>(linkedPages));
225    }
226   
 
227  1 toggle public void testGetUniqueLinkedPages()
228    {
229  1 XWikiDocument contextDocument =
230    new XWikiDocument(new DocumentReference("contextdocwiki", "contextdocspace", "contextdocpage"));
231  1 getContext().setDoc(contextDocument);
232   
233  1 this.document.setContent("[[TargetPage]][[TargetLabel>>TargetPage]][[TargetSpace.TargetPage]]"
234    + "[[TargetLabel>>TargetSpace.TargetPage?param=value#anchor]][[http://externallink]][[mailto:mailto]]"
235    + "[[]][[#anchor]][[?param=value]][[targetwiki:TargetSpace.TargetPage]]");
236  1 this.document.setSyntax(Syntax.XWIKI_2_0);
237   
238  1 Set<String> linkedPages = this.document.getUniqueLinkedPages(getContext());
239   
240  1 assertEquals(new LinkedHashSet<String>(Arrays.asList("Space.TargetPage.WebHome",
241    "TargetSpace.TargetPage.WebHome", "targetwiki:TargetSpace.TargetPage.WebHome")), linkedPages);
242    }
243   
 
244  1 toggle public void testGetSections10() throws XWikiException
245    {
246  1 this.document.setContent(
247    "content not in section\n" + "1 header 1\nheader 1 content\n" + "1.1 header 2\nheader 2 content");
248   
249  1 List<DocumentSection> headers = this.document.getSections();
250   
251  1 assertEquals(2, headers.size());
252   
253  1 DocumentSection header1 = headers.get(0);
254  1 DocumentSection header2 = headers.get(1);
255   
256  1 assertEquals("header 1", header1.getSectionTitle());
257  1 assertEquals(23, header1.getSectionIndex());
258  1 assertEquals(1, header1.getSectionNumber());
259  1 assertEquals("1", header1.getSectionLevel());
260  1 assertEquals("header 2", header2.getSectionTitle());
261  1 assertEquals(51, header2.getSectionIndex());
262  1 assertEquals(2, header2.getSectionNumber());
263  1 assertEquals("1.1", header2.getSectionLevel());
264    }
265   
 
266  1 toggle public void testGetSections() throws XWikiException
267    {
268  1 this.document.setContent(
269    "content not in section\n" + "= header 1=\nheader 1 content\n" + "== header 2==\nheader 2 content");
270  1 this.document.setSyntax(Syntax.XWIKI_2_0);
271   
272  1 List<DocumentSection> headers = this.document.getSections();
273   
274  1 assertEquals(2, headers.size());
275   
276  1 DocumentSection header1 = headers.get(0);
277  1 DocumentSection header2 = headers.get(1);
278   
279  1 assertEquals("header 1", header1.getSectionTitle());
280  1 assertEquals(-1, header1.getSectionIndex());
281  1 assertEquals(1, header1.getSectionNumber());
282  1 assertEquals("1", header1.getSectionLevel());
283  1 assertEquals("header 2", header2.getSectionTitle());
284  1 assertEquals(-1, header2.getSectionIndex());
285  1 assertEquals(2, header2.getSectionNumber());
286  1 assertEquals("1.1", header2.getSectionLevel());
287    }
288   
 
289  1 toggle public void testGetDocumentSection10() throws XWikiException
290    {
291  1 this.document.setContent(
292    "content not in section\n" + "1 header 1\nheader 1 content\n" + "1.1 header 2\nheader 2 content");
293   
294  1 DocumentSection header1 = this.document.getDocumentSection(1);
295  1 DocumentSection header2 = this.document.getDocumentSection(2);
296   
297  1 assertEquals("header 1", header1.getSectionTitle());
298  1 assertEquals(23, header1.getSectionIndex());
299  1 assertEquals(1, header1.getSectionNumber());
300  1 assertEquals("1", header1.getSectionLevel());
301  1 assertEquals("header 2", header2.getSectionTitle());
302  1 assertEquals(51, header2.getSectionIndex());
303  1 assertEquals(2, header2.getSectionNumber());
304  1 assertEquals("1.1", header2.getSectionLevel());
305    }
306   
 
307  1 toggle public void testGetDocumentSection() throws XWikiException
308    {
309  1 this.document.setContent(
310    "content not in section\n" + "= header 1=\nheader 1 content\n" + "== header 2==\nheader 2 content");
311  1 this.document.setSyntax(Syntax.XWIKI_2_0);
312   
313  1 DocumentSection header1 = this.document.getDocumentSection(1);
314  1 DocumentSection header2 = this.document.getDocumentSection(2);
315   
316  1 assertEquals("header 1", header1.getSectionTitle());
317  1 assertEquals(-1, header1.getSectionIndex());
318  1 assertEquals(1, header1.getSectionNumber());
319  1 assertEquals("1", header1.getSectionLevel());
320  1 assertEquals("header 2", header2.getSectionTitle());
321  1 assertEquals(-1, header2.getSectionIndex());
322  1 assertEquals(2, header2.getSectionNumber());
323  1 assertEquals("1.1", header2.getSectionLevel());
324    }
325   
326    /**
327    * Verify that if we have sections nested in groups, they are not taken into account when computing document
328    * sections by number. See <a href="http://jira.xwiki.org/browse/XWIKI-6195">XWIKI-6195</a>.
329    *
330    * @since 5.0M1
331    */
 
332  1 toggle public void testGetDocumentSectionWhenSectionInGroups() throws XWikiException
333    {
334  1 this.document.setContent(
335    "= Heading1 =\n" + "para1\n" + "== Heading2 ==\n" + "para2\n" + "(((\n" + "== Heading3 ==\n" + "para3\n"
336    + "(((\n" + "== Heading4 ==\n" + "para4\n" + ")))\n" + ")))\n" + "== Heading5 ==\n" + "para5\n");
337  1 this.document.setSyntax(Syntax.XWIKI_2_0);
338   
339  1 DocumentSection section = this.document.getDocumentSection(3);
340  1 assertEquals("Heading5", section.getSectionTitle());
341    }
342   
 
343  1 toggle public void testGetContentOfSection10() throws XWikiException
344    {
345  1 this.document.setContent(
346    "content not in section\n" + "1 header 1\nheader 1 content\n" + "1.1 header 2\nheader 2 content");
347   
348  1 String content1 = this.document.getContentOfSection(1);
349  1 String content2 = this.document.getContentOfSection(2);
350   
351  1 assertEquals("1 header 1\nheader 1 content\n1.1 header 2\nheader 2 content", content1);
352  1 assertEquals("1.1 header 2\nheader 2 content", content2);
353    }
354   
 
355  1 toggle public void testGetContentOfSection() throws XWikiException
356    {
357  1 this.document.setContent(
358    "content not in section\n" + "= header 1=\nheader 1 content\n" + "== header 2==\nheader 2 content\n"
359    + "=== header 3===\nheader 3 content\n" + "== header 4==\nheader 4 content");
360  1 this.document.setSyntax(Syntax.XWIKI_2_0);
361   
362  1 String content1 = this.document.getContentOfSection(1);
363  1 String content2 = this.document.getContentOfSection(2);
364  1 String content3 = this.document.getContentOfSection(3);
365   
366  1 assertEquals("= header 1 =\n\nheader 1 content\n\n== header 2 ==\n\nheader 2 content\n\n"
367    + "=== header 3 ===\n\nheader 3 content\n\n== header 4 ==\n\nheader 4 content", content1);
368  1 assertEquals("== header 2 ==\n\nheader 2 content\n\n=== header 3 ===\n\nheader 3 content", content2);
369  1 assertEquals("== header 4 ==\n\nheader 4 content", content3);
370   
371    // Validate that third level header is not skipped anymore
372  1 this.mockXWiki.stubs().method("getSectionEditingDepth").will(returnValue(3L));
373   
374  1 content3 = this.document.getContentOfSection(3);
375  1 String content4 = this.document.getContentOfSection(4);
376   
377  1 assertEquals("=== header 3 ===\n\nheader 3 content", content3);
378  1 assertEquals("== header 4 ==\n\nheader 4 content", content4);
379    }
380   
 
381  1 toggle public void testSectionSplit10() throws XWikiException
382    {
383  1 List<DocumentSection> sections;
384    // Simple test
385  1 this.document.setContent("1 Section 1\n" + "Content of first section\n" + "1.1 Subsection 2\n"
386    + "Content of second section\n" + "1 Section 3\n" + "Content of section 3");
387  1 sections = this.document.getSections();
388  1 assertEquals(3, sections.size());
389  1 assertEquals("Section 1", sections.get(0).getSectionTitle());
390  1 assertEquals(
391    "1 Section 1\n" + "Content of first section\n" + "1.1 Subsection 2\n" + "Content of second section\n",
392    this.document.getContentOfSection(1));
393  1 assertEquals("1.1", sections.get(1).getSectionLevel());
394  1 assertEquals("1.1 Subsection 2\nContent of second section\n", this.document.getContentOfSection(2));
395  1 assertEquals(3, sections.get(2).getSectionNumber());
396  1 assertEquals(80, sections.get(2).getSectionIndex());
397  1 assertEquals("1 Section 3\nContent of section 3", this.document.getContentOfSection(3));
398    // Test comments don't break the section editing
399  1 this.document.setContent("1 Section 1\n" + "Content of first section\n" + "## 1.1 Subsection 2\n"
400    + "Content of second section\n" + "1 Section 3\n" + "Content of section 3");
401  1 sections = this.document.getSections();
402  1 assertEquals(2, sections.size());
403  1 assertEquals("Section 1", sections.get(0).getSectionTitle());
404  1 assertEquals("1", sections.get(1).getSectionLevel());
405  1 assertEquals(2, sections.get(1).getSectionNumber());
406  1 assertEquals(83, sections.get(1).getSectionIndex());
407    // Test spaces are ignored
408  1 this.document.setContent("1 Section 1\n" + "Content of first section\n" + " 1.1 Subsection 2 \n"
409    + "Content of second section\n" + "1 Section 3\n" + "Content of section 3");
410  1 sections = this.document.getSections();
411  1 assertEquals(3, sections.size());
412  1 assertEquals("Subsection 2 ", sections.get(1).getSectionTitle());
413  1 assertEquals("1.1", sections.get(1).getSectionLevel());
414    // Test lower headings are ignored
415  1 this.document.setContent("1 Section 1\n" + "Content of first section\n" + "1.1.1 Lower subsection\n"
416    + "This content is not important\n" + " 1.1 Subsection 2 \n" + "Content of second section\n"
417    + "1 Section 3\n" + "Content of section 3");
418  1 sections = this.document.getSections();
419  1 assertEquals(3, sections.size());
420  1 assertEquals("Section 1", sections.get(0).getSectionTitle());
421  1 assertEquals("Subsection 2 ", sections.get(1).getSectionTitle());
422  1 assertEquals("1.1", sections.get(1).getSectionLevel());
423    // Test blank lines are preserved
424  1 this.document
425    .setContent("\n\n1 Section 1\n\n\n" + "Content of first section\n\n\n" + " 1.1 Subsection 2 \n\n"
426    + "Content of second section\n" + "1 Section 3\n" + "Content of section 3");
427  1 sections = this.document.getSections();
428  1 assertEquals(3, sections.size());
429  1 assertEquals(2, sections.get(0).getSectionIndex());
430  1 assertEquals("Subsection 2 ", sections.get(1).getSectionTitle());
431  1 assertEquals(43, sections.get(1).getSectionIndex());
432    }
433   
 
434  1 toggle public void testUpdateDocumentSection10() throws XWikiException
435    {
436  1 List<DocumentSection> sections;
437    // Fill the document
438  1 this.document.setContent("1 Section 1\n" + "Content of first section\n" + "1.1 Subsection 2\n"
439    + "Content of second section\n" + "1 Section 3\n" + "Content of section 3");
440  1 String content = this.document.updateDocumentSection(3, "1 Section 3\n" + "Modified content of section 3");
441  1 assertEquals("1 Section 1\n" + "Content of first section\n" + "1.1 Subsection 2\n"
442    + "Content of second section\n" + "1 Section 3\n" + "Modified content of section 3", content);
443  1 this.document.setContent(content);
444  1 sections = this.document.getSections();
445  1 assertEquals(3, sections.size());
446  1 assertEquals("Section 1", sections.get(0).getSectionTitle());
447  1 assertEquals(
448    "1 Section 1\n" + "Content of first section\n" + "1.1 Subsection 2\n" + "Content of second section\n",
449    this.document.getContentOfSection(1));
450  1 assertEquals("1.1", sections.get(1).getSectionLevel());
451  1 assertEquals("1.1 Subsection 2\nContent of second section\n", this.document.getContentOfSection(2));
452  1 assertEquals(3, sections.get(2).getSectionNumber());
453  1 assertEquals(80, sections.get(2).getSectionIndex());
454  1 assertEquals("1 Section 3\nModified content of section 3", this.document.getContentOfSection(3));
455    }
456   
 
457  1 toggle public void testUpdateDocumentSection() throws XWikiException
458    {
459  1 this.document.setContent(
460    "content not in section\n" + "= header 1=\nheader 1 content\n" + "== header 2==\nheader 2 content");
461  1 this.document.setSyntax(Syntax.XWIKI_2_0);
462   
463    // Modify section content
464  1 String content1 = this.document.updateDocumentSection(2, "== header 2==\nmodified header 2 content");
465   
466  1 assertEquals(
467    "content not in section\n\n= header 1 =\n\nheader 1 content\n\n== header 2 ==\n\nmodified header 2 content",
468    content1);
469   
470  1 String content2 = this.document.updateDocumentSection(1,
471    "= header 1 =\n\nmodified also header 1 content\n\n== header 2 ==\n\nheader 2 content");
472   
473  1 assertEquals(
474    "content not in section\n\n= header 1 =\n\nmodified also header 1 content\n\n== header 2 ==\n\nheader 2 content",
475    content2);
476   
477    // Remove a section
478  1 String content3 = this.document.updateDocumentSection(2, "");
479   
480  1 assertEquals("content not in section\n\n= header 1 =\n\nheader 1 content", content3);
481    }
482   
 
483  1 toggle public void testDisplay()
484    {
485  1 this.mockXWiki.stubs().method("getCurrentContentSyntaxId").will(returnValue("xwiki/2.0"));
486   
487  1 this.document.setSyntax(Syntax.XWIKI_2_0);
488   
489  1 assertEquals("string", this.document.display("string", "view", getContext()));
490  1 assertEquals(
491    "{{html clean=\"false\" wiki=\"false\"}}<input size='30' id='Space.Page_0_string' value='string' name='Space.Page_0_string' type='text'/>{{/html}}",
492    this.document.display("string", "edit", getContext()));
493   
494  1 assertEquals("{{html clean=\"false\" wiki=\"false\"}}<p>area</p>{{/html}}",
495    this.document.display("area", "view", getContext()));
496    }
497   
 
498  1 toggle public void testDisplay1020()
499    {
500  1 this.mockXWiki.stubs().method("getCurrentContentSyntaxId").will(returnValue("xwiki/1.0"));
501   
502  1 XWikiDocument doc10 = new XWikiDocument();
503  1 doc10.setSyntax(Syntax.XWIKI_1_0);
504  1 getContext().setDoc(doc10);
505   
506  1 this.document.setSyntax(Syntax.XWIKI_2_0);
507   
508  1 assertEquals("string", this.document.display("string", "view", getContext()));
509  1 assertEquals(
510    "{pre}<input size='30' id='Space.Page_0_string' value='string' name='Space.Page_0_string' type='text'/>{/pre}",
511    this.document.display("string", "edit", getContext()));
512   
513  1 assertEquals("<p>area</p>", this.document.display("area", "view", getContext()));
514    }
515   
 
516  1 toggle public void testDisplayTemplate20()
517    {
518  1 this.mockXWiki.stubs().method("getCurrentContentSyntaxId").will(returnValue("xwiki/2.0"));
519   
520  1 getContext().put("isInRenderingEngine", false);
521   
522  1 this.document.setSyntax(Syntax.XWIKI_2_0);
523   
524  1 assertEquals("string", this.document.display("string", "view", getContext()));
525  1 assertEquals(
526    "<input size='30' id='Space.Page_0_string' value='string' name='Space.Page_0_string' type='text'/>",
527    this.document.display("string", "edit", getContext()));
528   
529  1 assertEquals("<p>area</p>", this.document.display("area", "view", getContext()));
530    }
531   
 
532  1 toggle public void testConvertSyntax() throws XWikiException
533    {
534  1 this.document.setSyntax(Syntax.HTML_4_01);
535  1 this.document.setContent("<p>content not in section</p>" + "<h1>header 1</h1><p>header 1 content</p>"
536    + "<h2>header 2</h2><p>header 2 content</p>");
537  1 this.baseObject.setLargeStringValue("area",
538    "<p>object content not in section</p>" + "<h1>object header 1</h1><p>object header 1 content</p>"
539    + "<h2>object header 2</h2><p>object header 2 content</p>");
540  1 this.baseObject.setLargeStringValue("puretextarea",
541    "<p>object content not in section</p>" + "<h1>object header 1</h1><p>object header 1 content</p>"
542    + "<h2>object header 2</h2><p>object header 2 content</p>");
543   
544  1 this.document.convertSyntax("xwiki/2.0", getContext());
545   
546  1 assertEquals("content not in section\n\n" + "= header 1 =\n\nheader 1 content\n\n"
547    + "== header 2 ==\n\nheader 2 content", this.document.getContent());
548  1 assertEquals("object content not in section\n\n" + "= object header 1 =\n\nobject header 1 content\n\n"
549    + "== object header 2 ==\n\nobject header 2 content", this.baseObject.getStringValue("area"));
550  1 assertEquals(
551    "<p>object content not in section</p>" + "<h1>object header 1</h1><p>object header 1 content</p>"
552    + "<h2>object header 2</h2><p>object header 2 content</p>",
553    this.baseObject.getStringValue("puretextarea"));
554  1 assertEquals("xwiki/2.0", this.document.getSyntaxId());
555    }
556   
 
557  1 toggle public void testGetRenderedContent() throws XWikiException
558    {
559  1 this.document.setContent("**bold**");
560  1 this.document.setSyntax(Syntax.XWIKI_2_0);
561   
562  1 assertEquals("<p><strong>bold</strong></p>", this.document.getRenderedContent(getContext()));
563   
564  1 this.translatedDocument = new XWikiDocument(this.document.getDocumentReference(), Locale.FRENCH);
565  1 this.translatedDocument.setContent("//italic//");
566  1 this.translatedDocument.setSyntax(Syntax.XWIKI_1_0);
567  1 this.translatedDocument.setNew(false);
568   
569  1 this.mockXWiki.stubs().method("getLanguagePreference").will(returnValue(Locale.FRENCH.toString()));
570  1 this.mockXWiki.stubs().method("getDocument").with(eq(
571    new DocumentReference(this.translatedDocument.getDocumentReference(), this.translatedDocument.getLocale())),
572    ANYTHING).will(returnValue(this.translatedDocument));
573   
574  1 assertEquals("<p><em>italic</em></p>", this.document.getRenderedContent(getContext()));
575    }
576   
 
577  1 toggle public void testGetRenderedContentWithSourceSyntax()
578    {
579  1 this.document.setSyntax(Syntax.XWIKI_1_0);
580   
581  1 assertEquals("<p><strong>bold</strong></p>",
582    this.document.getRenderedContent("**bold**", "xwiki/2.0", getContext()));
583    }
584   
 
585  1 toggle public void testRename() throws XWikiException
586    {
587    // Possible ways to write parents, include documents, or make links:
588    // "name" -----means-----> DOCWIKI+":"+DOCSPACE+"."+input
589    // "space.name" -means----> DOCWIKI+":"+input
590    // "database:space.name" (no change)
591   
592  1 this.document.setContent("[[doc:pageinsamespace]]");
593  1 this.document.setSyntax(Syntax.XWIKI_2_1);
594  1 DocumentReference targetReference = new DocumentReference("newwikiname", "newspace", "newpage");
595  1 XWikiDocument targetDocument = this.document.duplicate(targetReference);
596  1 targetDocument.setStore((XWikiStoreInterface) this.mockXWikiStoreInterface.proxy());
597   
598  1 DocumentReference reference1 = new DocumentReference(DOCWIKI, DOCSPACE, "Page1");
599  1 XWikiDocument doc1 = new XWikiDocument(reference1);
600  1 doc1.setContent("[[doc:" + DOCWIKI + ":" + DOCSPACE + "." + DOCNAME + "]] [[someName>>doc:" + DOCSPACE + "."
601    + DOCNAME + "]] [[doc:" + DOCNAME + "]]");
602  1 doc1.setSyntax(Syntax.XWIKI_2_1);
603  1 doc1.setStore((XWikiStoreInterface) this.mockXWikiStoreInterface.proxy());
604   
605  1 DocumentReference reference2 = new DocumentReference("newwikiname", DOCSPACE, "Page2");
606  1 XWikiDocument doc2 = new XWikiDocument(reference2);
607  1 doc2.setContent("[[doc:" + DOCWIKI + ":" + DOCSPACE + "." + DOCNAME + "]]");
608  1 doc2.setSyntax(Syntax.XWIKI_2_1);
609  1 doc2.setStore((XWikiStoreInterface) this.mockXWikiStoreInterface.proxy());
610   
611  1 DocumentReference reference3 = new DocumentReference("newwikiname", "newspace", "Page3");
612  1 XWikiDocument doc3 = new XWikiDocument(reference3);
613  1 doc3.setContent("[[doc:" + DOCWIKI + ":" + DOCSPACE + "." + DOCNAME + "]]");
614  1 doc3.setSyntax(Syntax.XWIKI_2_1);
615  1 doc3.setStore((XWikiStoreInterface) this.mockXWikiStoreInterface.proxy());
616   
617    // Test to make sure it also drags children along.
618  1 DocumentReference reference4 = new DocumentReference(DOCWIKI, DOCSPACE, "Page4");
619  1 XWikiDocument doc4 = new XWikiDocument(reference4);
620  1 doc4.setParent(DOCSPACE + "." + DOCNAME);
621  1 doc4.setStore((XWikiStoreInterface) this.mockXWikiStoreInterface.proxy());
622   
623  1 DocumentReference reference5 = new DocumentReference("newwikiname", "newspace", "Page5");
624  1 XWikiDocument doc5 = new XWikiDocument(reference5);
625  1 doc5.setParent(DOCWIKI + ":" + DOCSPACE + "." + DOCNAME);
626  1 doc5.setStore((XWikiStoreInterface) this.mockXWikiStoreInterface.proxy());
627   
628  1 this.mockXWiki.stubs().method("copyDocument").will(returnValue(true));
629  1 this.mockXWiki.stubs().method("getDocument").with(eq(targetReference), ANYTHING)
630    .will(returnValue(targetDocument));
631  1 this.mockXWiki.stubs().method("getDocument").with(eq(reference1), ANYTHING).will(returnValue(doc1));
632  1 this.mockXWiki.stubs().method("getDocument").with(eq(reference2), ANYTHING).will(returnValue(doc2));
633  1 this.mockXWiki.stubs().method("getDocument").with(eq(reference3), ANYTHING).will(returnValue(doc3));
634  1 this.mockXWiki.stubs().method("getDocument").with(eq(reference4), ANYTHING).will(returnValue(doc4));
635  1 this.mockXWiki.stubs().method("getDocument").with(eq(reference5), ANYTHING).will(returnValue(doc5));
636  1 this.mockXWiki.stubs().method("saveDocument").isVoid();
637  1 this.mockXWiki.stubs().method("deleteDocument").isVoid();
638  1 this.mockXWikiStoreInterface.stubs().method("getTranslationList").will(returnValue(Arrays.asList()));
639   
640  1 this.document.rename(new DocumentReference("newwikiname", "newspace", "newpage"),
641    Arrays.asList(reference1, reference2, reference3), Arrays.asList(reference4, reference5), getContext());
642   
643    // Test links
644  1 assertEquals("[[doc:Wiki:Space.pageinsamespace]]", this.document.getContent());
645  1 assertEquals("[[doc:newwikiname:newspace.newpage]] " + "[[someName>>doc:newwikiname:newspace.newpage]] "
646    + "[[doc:newwikiname:newspace.newpage]]", doc1.getContent());
647  1 assertEquals("[[doc:newspace.newpage]]", doc2.getContent());
648  1 assertEquals("[[doc:newpage]]", doc3.getContent());
649   
650    // Test parents
651  1 assertEquals("newwikiname:newspace.newpage", doc4.getParent());
652  1 assertEquals(new DocumentReference("newwikiname", "newspace", "newpage"), doc5.getParentReference());
653    }
654   
655    /**
656    * Validate rename does not crash when the document has 1.0 syntax (it does not support everything but it does not
657    * crash).
658    */
 
659  1 toggle public void testRename10() throws XWikiException
660    {
661  1 this.document.setContent("[pageinsamespace]");
662  1 this.document.setSyntax(Syntax.XWIKI_1_0);
663  1 DocumentReference targetReference = new DocumentReference("newwikiname", "newspace", "newpage");
664  1 XWikiDocument targetDocument = this.document.duplicate(targetReference);
665   
666  1 this.mockXWiki.stubs().method("copyDocument").will(returnValue(true));
667  1 this.mockXWiki.stubs().method("getDocument").with(eq(targetReference), ANYTHING)
668    .will(returnValue(targetDocument));
669  1 this.mockXWiki.stubs().method("saveDocument").isVoid();
670  1 this.mockXWiki.stubs().method("deleteDocument").isVoid();
671   
672  1 this.document.rename(new DocumentReference("newwikiname", "newspace", "newpage"),
673    Collections.<DocumentReference>emptyList(), Collections.<DocumentReference>emptyList(), getContext());
674   
675    // Test links
676  1 assertEquals("[pageinsamespace]", this.document.getContent());
677    }
678   
679    /**
680    * @see XWIKI-7515: 'getIncludedPages' in class com.xpn.xwiki.api.Document threw java.lang.NullPointerException
681    */
 
682  1 toggle public void testGetIncludedPages()
683    {
684  1 this.document.setSyntax(Syntax.XWIKI_2_1);
685   
686  1 this.document.setContent("no include");
687  1 assertTrue(this.document.getIncludedPages(getContext()).isEmpty());
688   
689  1 this.document.setContent("bad {{include/}}");
690  1 assertTrue(this.document.getIncludedPages(getContext()).isEmpty());
691   
692  1 this.document.setContent("good deprecated {{include document=\"Foo.Bar\"/}}");
693  1 assertEquals(Arrays.asList("Foo.Bar"), this.document.getIncludedPages(getContext()));
694   
695  1 this.document.setContent("good {{include reference=\"One.Two\"/}}");
696  1 assertEquals(Arrays.asList("One.Two"), this.document.getIncludedPages(getContext()));
697   
698  1 this.document.setContent("bad recursive {{include reference=\"\"/}}");
699  1 assertTrue(this.document.getIncludedPages(getContext()).isEmpty());
700   
701  1 this.document.setContent("bad recursive {{include reference=\"" + DOCNAME + "\"/}}");
702  1 assertTrue(this.document.getIncludedPages(getContext()).isEmpty());
703   
704  1 this.document.setContent("bad recursive {{include reference=\"" + DOCSPACE + "." + DOCNAME + "\"/}}");
705  1 assertTrue(this.document.getIncludedPages(getContext()).isEmpty());
706    }
707   
708    /**
709    * XWIKI-8025: XWikiDocument#backup/restoreContext doesn't update the reference to the Velocity context stored on
710    * the XWiki context
711    */
 
712  1 toggle public void testBackupRestoreContextUpdatesVContext() throws Exception
713    {
714  1 final Execution execution = getComponentManager().getInstance(Execution.class);
715  1 this.mockVelocityManager.stubs().method("getVelocityContext")
716    .will(new CustomStub("Implements VelocityManager.getVelocityContext")
717    {
 
718  0 toggle @Override
719    public Object invoke(Invocation invocation) throws Throwable
720    {
721  0 return (VelocityContext) execution.getContext().getProperty("velocityContext");
722    }
723    });
724   
725  1 VelocityContext oldVelocityContext = new VelocityContext();
726  1 execution.getContext().setProperty("velocityContext", oldVelocityContext);
727   
728  1 Map<String, Object> backup = new HashMap<String, Object>();
729  1 XWikiDocument.backupContext(backup, getContext());
730   
731  1 VelocityContext newVelocityContext = (VelocityContext) execution.getContext().getProperty("velocityContext");
732  1 assertNotNull(newVelocityContext);
733  1 assertNotSame(oldVelocityContext, newVelocityContext);
734  1 assertSame(newVelocityContext, getContext().get("vcontext"));
735   
736  1 XWikiDocument.restoreContext(backup, getContext());
737   
738  1 assertSame(oldVelocityContext, execution.getContext().getProperty("velocityContext"));
739  1 assertSame(oldVelocityContext, getContext().get("vcontext"));
740    }
741    }