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

File DocumentInstanceOutputFilterStreamTest.java

 

Code metrics

0
217
9
1
435
291
9
0.04
24.11
9
1

Classes

Class Line # Actions
DocumentInstanceOutputFilterStreamTest 53 217 0% 9 0
1.0100%
 

Contributing tests

This file is covered by 9 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.internal.filter.output;
21   
22    import java.text.ParseException;
23    import java.util.Arrays;
24    import java.util.List;
25    import java.util.Locale;
26    import java.util.Map;
27   
28    import org.junit.jupiter.api.Test;
29    import org.xwiki.filter.FilterException;
30    import org.xwiki.filter.instance.output.DocumentInstanceOutputProperties;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.rendering.syntax.Syntax;
33    import org.xwiki.rendering.syntax.SyntaxType;
34   
35    import com.xpn.xwiki.XWikiException;
36    import com.xpn.xwiki.doc.XWikiAttachment;
37    import com.xpn.xwiki.doc.XWikiDocument;
38    import com.xpn.xwiki.internal.filter.AbstractInstanceFilterStreamTest;
39    import com.xpn.xwiki.objects.BaseObject;
40    import com.xpn.xwiki.objects.classes.BaseClass;
41    import com.xpn.xwiki.objects.classes.NumberClass;
42   
43    import static org.junit.jupiter.api.Assertions.assertEquals;
44    import static org.junit.jupiter.api.Assertions.assertFalse;
45    import static org.junit.jupiter.api.Assertions.assertNotNull;
46    import static org.junit.jupiter.api.Assertions.assertTrue;
47   
48    /**
49    * Validate {@link DocumentInstanceOutputFilterStream}.
50    *
51    * @version $Id: c1ec9bf102e83ca702d94326ffe81d82d8702aae $
52    */
 
53    public class DocumentInstanceOutputFilterStreamTest extends AbstractInstanceFilterStreamTest
54    {
55    // Tests
56   
 
57  1 toggle @Test
58    public void testImportDocumentsPreserveVersion() throws FilterException, XWikiException, ParseException
59    {
60  1 DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
61   
62  1 outputProperties.setVersionPreserved(true);
63  1 outputProperties.setVerbose(false);
64   
65  1 importFromXML("document1", outputProperties);
66   
67  1 XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
68    this.oldcore.getXWikiContext());
69   
70  1 assertFalse(document.isNew());
71   
72  1 assertEquals(Locale.ENGLISH, document.getDefaultLocale());
73  1 assertEquals(new DocumentReference("wiki", "space", "parent"), document.getParentReference());
74  1 assertEquals("customclass", document.getCustomClass());
75  1 assertEquals("title", document.getTitle());
76  1 assertEquals("defaultTemplate", document.getDefaultTemplate());
77  1 assertEquals("validationScript", document.getValidationScript());
78  1 assertEquals(new Syntax(new SyntaxType("syntax", "syntax"), "1.0"), document.getSyntax());
79  1 assertEquals(true, document.isHidden());
80  1 assertEquals("content", document.getContent());
81   
82  1 assertEquals(new DocumentReference("wiki", "XWiki", "creator"), document.getCreatorReference());
83  1 assertEquals(toDate("2000-01-01 00:00:00.0 UTC"), document.getCreationDate());
84  1 assertEquals(new DocumentReference("wiki", "XWiki", "author"), document.getAuthorReference());
85  1 assertEquals(toDate("2000-01-02 00:00:00.0 UTC"), document.getDate());
86  1 assertEquals(toDate("2000-01-03 00:00:00.0 UTC"), document.getContentUpdateDate());
87  1 assertEquals(new DocumentReference("wiki", "XWiki", "contentAuthor"), document.getContentAuthorReference());
88  1 assertEquals(true, document.isMinorEdit());
89  1 assertEquals("comment", document.getComment());
90  1 assertEquals("1.42", document.getVersion());
91   
92    // Attachment
93   
94  1 assertEquals(1, document.getAttachmentList().size());
95  1 XWikiAttachment attachment = document.getAttachment("attachment.txt");
96  1 assertEquals("attachment.txt", attachment.getFilename());
97  1 assertEquals(10, attachment.getLongSize());
98  1 assertTrue(Arrays.equals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
99    attachment.getContent(this.oldcore.getXWikiContext())));
100   
101  1 assertEquals("XWiki.attachmentAuthor", attachment.getAuthor());
102  1 assertEquals(toDate("2000-01-05 00:00:00.0 UTC"), attachment.getDate());
103  1 assertEquals("15.1", attachment.getVersion());
104  1 assertEquals("attachment comment", attachment.getComment());
105   
106    // XClass
107   
108  1 BaseClass xclass = document.getXClass();
109  1 assertEquals(1, xclass.getFieldList().size());
110  1 assertEquals("customClass", xclass.getCustomClass());
111  1 assertEquals("customMapping", xclass.getCustomMapping());
112  1 assertEquals("defaultViewSheet", xclass.getDefaultViewSheet());
113  1 assertEquals("defaultEditSheet", xclass.getDefaultEditSheet());
114  1 assertEquals("defaultWeb", xclass.getDefaultWeb());
115  1 assertEquals("nameField", xclass.getNameField());
116  1 assertEquals("validationScript", xclass.getValidationScript());
117   
118  1 NumberClass numberFiled = (NumberClass) xclass.getField("prop1");
119  1 assertEquals("prop1", numberFiled.getName());
120  1 assertEquals(false, numberFiled.isDisabled());
121  1 assertEquals(1, numberFiled.getNumber());
122  1 assertEquals("long", numberFiled.getNumberType());
123  1 assertEquals("Prop1", numberFiled.getPrettyName());
124  1 assertEquals(30, numberFiled.getSize());
125  1 assertEquals(false, numberFiled.isUnmodifiable());
126   
127    // Objects
128   
129  1 Map<DocumentReference, List<BaseObject>> objects = document.getXObjects();
130  1 assertEquals(2, objects.size());
131   
132    // Object 1
133   
134  1 List<BaseObject> documentObjects = objects.get(new DocumentReference("wiki", "space", "page"));
135  1 assertEquals(1, documentObjects.size());
136  1 BaseObject documentObject = documentObjects.get(0);
137  1 assertEquals(0, documentObject.getNumber());
138  1 assertEquals(new DocumentReference("wiki", "space", "page"), documentObject.getXClassReference());
139  1 assertEquals("e2167721-2a64-430c-9520-bac1c0ee68cb", documentObject.getGuid());
140   
141  1 assertEquals(1, documentObject.getFieldList().size());
142  1 assertEquals(1, documentObject.getIntValue("prop1"));
143   
144    // Object 2
145   
146  1 List<BaseObject> otherObjects = objects.get(new DocumentReference("wiki", "otherspace", "otherclass"));
147  1 assertEquals(1, otherObjects.size());
148  1 BaseObject otherObject = otherObjects.get(0);
149  1 assertEquals(0, otherObject.getNumber());
150  1 assertEquals(new DocumentReference("wiki", "otherspace", "otherclass"), otherObject.getXClassReference());
151  1 assertEquals("8eaeac52-e2f2-47b2-87e1-bc6909597b39", otherObject.getGuid());
152   
153  1 assertEquals(1, otherObject.getFieldList().size());
154  1 assertEquals(2, otherObject.getIntValue("prop2"));
155    }
156   
 
157  1 toggle @Test
158    public void testImportDocumentsWithoutLocaleAndRevision() throws FilterException, XWikiException, ParseException
159    {
160  1 DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
161   
162  1 outputProperties.setVersionPreserved(true);
163  1 outputProperties.setVerbose(false);
164   
165  1 importFromXML("document1-2", outputProperties);
166   
167  1 XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
168    this.oldcore.getXWikiContext());
169   
170  1 assertFalse(document.isNew());
171   
172  1 assertEquals(Locale.ENGLISH, document.getDefaultLocale());
173  1 assertEquals(new DocumentReference("wiki", "space", "parent"), document.getParentReference());
174  1 assertEquals("customclass", document.getCustomClass());
175  1 assertEquals("title", document.getTitle());
176  1 assertEquals("defaultTemplate", document.getDefaultTemplate());
177  1 assertEquals("validationScript", document.getValidationScript());
178  1 assertEquals(new Syntax(new SyntaxType("syntax", "syntax"), "1.0"), document.getSyntax());
179  1 assertEquals(true, document.isHidden());
180  1 assertEquals("content", document.getContent());
181   
182  1 assertEquals(new DocumentReference("wiki", "XWiki", "creator"), document.getCreatorReference());
183  1 assertEquals(toDate("2000-01-01 00:00:00.0 UTC"), document.getCreationDate());
184  1 assertEquals(new DocumentReference("wiki", "XWiki", "author"), document.getAuthorReference());
185  1 assertEquals(toDate("2000-01-02 00:00:00.0 UTC"), document.getDate());
186  1 assertEquals(toDate("2000-01-03 00:00:00.0 UTC"), document.getContentUpdateDate());
187  1 assertEquals(new DocumentReference("wiki", "XWiki", "contentAuthor"), document.getContentAuthorReference());
188  1 assertEquals(true, document.isMinorEdit());
189  1 assertEquals("comment", document.getComment());
190  1 assertEquals("1.1", document.getVersion());
191   
192    // Attachment
193   
194  1 assertEquals(1, document.getAttachmentList().size());
195  1 XWikiAttachment attachment = document.getAttachment("attachment.txt");
196  1 assertEquals("attachment.txt", attachment.getFilename());
197  1 assertEquals(10, attachment.getLongSize());
198  1 assertTrue(Arrays.equals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
199    attachment.getContent(this.oldcore.getXWikiContext())));
200   
201  1 assertEquals("XWiki.attachmentAuthor", attachment.getAuthor());
202  1 assertEquals(toDate("2000-01-05 00:00:00.0 UTC"), attachment.getDate());
203  1 assertEquals("1.1", attachment.getVersion());
204  1 assertEquals("attachment comment", attachment.getComment());
205   
206    // XClass
207   
208  1 BaseClass xclass = document.getXClass();
209  1 assertEquals("customClass", xclass.getCustomClass());
210  1 assertEquals("customMapping", xclass.getCustomMapping());
211  1 assertEquals("defaultViewSheet", xclass.getDefaultViewSheet());
212  1 assertEquals("defaultEditSheet", xclass.getDefaultEditSheet());
213  1 assertEquals("defaultWeb", xclass.getDefaultWeb());
214  1 assertEquals("nameField", xclass.getNameField());
215  1 assertEquals("validationScript", xclass.getValidationScript());
216  1 assertEquals(1, xclass.getFieldList().size());
217   
218  1 NumberClass numberFiled = (NumberClass) xclass.getField("prop1");
219  1 assertEquals("prop1", numberFiled.getName());
220  1 assertEquals(false, numberFiled.isDisabled());
221  1 assertEquals(1, numberFiled.getNumber());
222  1 assertEquals("long", numberFiled.getNumberType());
223  1 assertEquals("Prop1", numberFiled.getPrettyName());
224  1 assertEquals(30, numberFiled.getSize());
225  1 assertEquals(false, numberFiled.isUnmodifiable());
226   
227    // Objects
228   
229  1 Map<DocumentReference, List<BaseObject>> objects = document.getXObjects();
230  1 assertEquals(2, objects.size());
231   
232    // Object 1
233   
234  1 List<BaseObject> documentObjects = objects.get(new DocumentReference("wiki", "space", "page"));
235  1 assertEquals(1, documentObjects.size());
236  1 BaseObject documentObject = documentObjects.get(0);
237  1 assertEquals(0, documentObject.getNumber());
238  1 assertEquals(new DocumentReference("wiki", "space", "page"), documentObject.getXClassReference());
239  1 assertEquals("e2167721-2a64-430c-9520-bac1c0ee68cb", documentObject.getGuid());
240   
241  1 assertEquals(1, documentObject.getFieldList().size());
242  1 assertEquals(1, documentObject.getIntValue("prop1"));
243   
244    // Object 2
245   
246  1 List<BaseObject> otherObjects = objects.get(new DocumentReference("wiki", "otherspace", "otherclass"));
247  1 assertEquals(1, otherObjects.size());
248  1 BaseObject otherObject = otherObjects.get(0);
249  1 assertEquals(0, otherObject.getNumber());
250  1 assertEquals(new DocumentReference("wiki", "otherspace", "otherclass"), otherObject.getXClassReference());
251  1 assertEquals("8eaeac52-e2f2-47b2-87e1-bc6909597b39", otherObject.getGuid());
252   
253  1 assertEquals(1, otherObject.getFieldList().size());
254  1 assertEquals(2, otherObject.getIntValue("prop2"));
255    }
256   
 
257  1 toggle @Test
258    public void testDocumentwithunexistingobjectproperty() throws FilterException, XWikiException
259    {
260  1 DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
261   
262  1 outputProperties.setVerbose(false);
263   
264  1 importFromXML("documentwithunexistingobjectproperty", outputProperties);
265   
266  1 XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
267    this.oldcore.getXWikiContext());
268   
269  1 assertFalse(document.isNew());
270   
271    // Objects
272   
273  1 Map<DocumentReference, List<BaseObject>> objects = document.getXObjects();
274  1 assertEquals(1, objects.size());
275   
276  1 List<BaseObject> documentObjects = objects.get(new DocumentReference("wiki", "space", "page"));
277  1 assertEquals(1, documentObjects.size());
278  1 BaseObject documentObject = documentObjects.get(0);
279  1 assertEquals(0, documentObject.getNumber());
280  1 assertEquals(new DocumentReference("wiki", "space", "page"), documentObject.getXClassReference());
281   
282  1 assertEquals(1, documentObject.getFieldList().size());
283  1 assertEquals(1, documentObject.getIntValue("prop1"));
284    }
285   
 
286  1 toggle @Test
287    public void testDocumentwithnumberversion() throws FilterException, XWikiException
288    {
289  1 DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
290   
291  1 outputProperties.setVerbose(false);
292   
293  1 importFromXML("documentwithnumberversion", outputProperties);
294   
295  1 XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
296    this.oldcore.getXWikiContext());
297   
298  1 assertFalse(document.isNew());
299   
300    // Version
301   
302  1 assertEquals("1.1", document.getVersion());
303    }
304   
 
305  1 toggle @Test
306    public void testDocumentwithoutauthorandcreator() throws FilterException, XWikiException
307    {
308  1 DocumentReference contextUser = new DocumentReference("wiki", "XWiki", "contextuser");
309  1 this.oldcore.getXWikiContext().setUserReference(contextUser);
310   
311  1 DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
312   
313  1 outputProperties.setVerbose(false);
314  1 outputProperties.setAuthorPreserved(true);
315   
316  1 importFromXML("documentwithnumberversion", outputProperties);
317   
318  1 XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
319    this.oldcore.getXWikiContext());
320   
321  1 assertFalse(document.isNew());
322   
323  1 assertEquals(contextUser, document.getCreatorReference());
324  1 assertEquals(contextUser, document.getAuthorReference());
325  1 assertEquals(contextUser, document.getContentAuthorReference());
326    }
327   
 
328  1 toggle @Test
329    public void testDocumentwithattachmentwithoutdate() throws FilterException, XWikiException
330    {
331  1 DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
332   
333  1 outputProperties.setVerbose(false);
334   
335  1 importFromXML("documentwithattachmentwithoutdate", outputProperties);
336   
337  1 XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
338    this.oldcore.getXWikiContext());
339   
340  1 assertFalse(document.isNew());
341   
342    // Attachment
343   
344  1 assertEquals(1, document.getAttachmentList().size());
345  1 XWikiAttachment attachment = document.getAttachment("attachment.txt");
346  1 assertEquals("attachment.txt", attachment.getFilename());
347  1 assertEquals(10, attachment.getLongSize());
348  1 assertTrue(Arrays.equals(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
349    attachment.getContent(this.oldcore.getXWikiContext())));
350   
351  1 assertNotNull(attachment.getDate());
352  1 assertEquals("1.1", attachment.getVersion());
353  1 assertEquals("", attachment.getComment());
354  1 assertEquals("text/plain", attachment.getMimeType());
355    }
356   
357   
 
358  1 toggle @Test
359    public void testDocumentwithattachmentwithoutcontent() throws FilterException, XWikiException
360    {
361  1 DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
362   
363  1 outputProperties.setVerbose(false);
364   
365  1 importFromXML("documentwithattachmentwithoutcontent", outputProperties);
366   
367  1 XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
368    this.oldcore.getXWikiContext());
369   
370  1 assertFalse(document.isNew());
371   
372    // Attachment
373   
374  1 assertEquals(1, document.getAttachmentList().size());
375  1 XWikiAttachment attachment = document.getAttachment("attachment.txt");
376  1 assertEquals("attachment.txt", attachment.getFilename());
377  1 assertEquals(10, attachment.getLongSize());
378    }
379   
 
380  1 toggle @Test
381    public void testDocumentwithunknownclassproperty() throws FilterException, XWikiException
382    {
383  1 DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
384   
385  1 outputProperties.setVerbose(false);
386   
387  1 importFromXML("documentwithunknownClassproperty", outputProperties);
388   
389  1 XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
390    this.oldcore.getXWikiContext());
391   
392  1 assertFalse(document.isNew());
393   
394    // Objects
395   
396  1 Map<DocumentReference, List<BaseObject>> objects = document.getXObjects();
397  1 assertEquals(1, objects.size());
398   
399  1 List<BaseObject> documentObjects = objects.get(new DocumentReference("wiki", "space", "page"));
400  1 assertEquals(1, documentObjects.size());
401  1 BaseObject documentObject = documentObjects.get(0);
402  1 assertEquals(0, documentObject.getNumber());
403  1 assertEquals(new DocumentReference("wiki", "space", "page"), documentObject.getXClassReference());
404   
405  1 assertEquals(1, documentObject.getFieldList().size());
406  1 assertEquals(1, documentObject.getIntValue("prop1"));
407    }
408   
 
409  1 toggle @Test
410    public void testDocumentwithobjectwithoutnumberandclass() throws FilterException, XWikiException
411    {
412  1 DocumentInstanceOutputProperties outputProperties = new DocumentInstanceOutputProperties();
413   
414  1 outputProperties.setVerbose(false);
415   
416  1 importFromXML("documentwithobjectwithoutnumberandclass", outputProperties);
417   
418  1 XWikiDocument document = this.oldcore.getSpyXWiki().getDocument(new DocumentReference("wiki", "space", "page"),
419    this.oldcore.getXWikiContext());
420   
421  1 assertFalse(document.isNew());
422   
423    // Objects
424   
425  1 Map<DocumentReference, List<BaseObject>> objects = document.getXObjects();
426  1 assertEquals(1, objects.size());
427   
428  1 List<BaseObject> documentObjects = objects.get(new DocumentReference("wiki", "otherspace", "otherclass"));
429  1 assertEquals(1, documentObjects.size());
430  1 BaseObject documentObject = documentObjects.get(0);
431  1 assertEquals(0, documentObject.getNumber());
432  1 assertEquals(1, documentObject.getFieldList().size());
433  1 assertEquals("propvalue", documentObject.getStringValue("prop"));
434    }
435    }