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

File XWikiDocumentTest.java

 

Code metrics

0
332
26
1
743
541
26
0.08
12.77
26
1

Classes

Class Line # Actions
XWikiDocumentTest 64 332 0% 26 7
0.9804469398%
 

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