1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.officeimporter.internal.splitter

File DefaultXDOMOfficeDocumentSplitterTest.java

 

Code metrics

0
48
5
1
152
95
5
0.1
9.6
5
1

Classes

Class Line # Actions
DefaultXDOMOfficeDocumentSplitterTest 45 48 0% 5 0
1.0100%
 

Contributing tests

This file is covered by 1 test. .

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 org.xwiki.officeimporter.internal.splitter;
21   
22    import java.io.StringReader;
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import org.junit.Assert;
27   
28    import org.jmock.Expectations;
29    import org.junit.Before;
30    import org.junit.Test;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.officeimporter.document.XDOMOfficeDocument;
33    import org.xwiki.officeimporter.internal.AbstractOfficeImporterTest;
34    import org.xwiki.officeimporter.splitter.TargetDocumentDescriptor;
35    import org.xwiki.officeimporter.splitter.XDOMOfficeDocumentSplitter;
36    import org.xwiki.rendering.block.XDOM;
37    import org.xwiki.rendering.parser.Parser;
38   
39    /**
40    * Test case for {@link DefaultXDOMOfficeDocumentSplitter}.
41    *
42    * @version $Id: d8b6d3b04d96e3247cc18a1992277dc37e077990 $
43    * @since 2.1M1
44    */
 
45    public class DefaultXDOMOfficeDocumentSplitterTest extends AbstractOfficeImporterTest
46    {
47    /**
48    * Parser for building XDOM instances.
49    */
50    private Parser xwikiSyntaxParser;
51   
52    /**
53    * Document splitter for testing.
54    */
55    private XDOMOfficeDocumentSplitter officeDocumentSplitter;
56   
 
57  1 toggle @Override
58    @Before
59    public void setUp() throws Exception
60    {
61  1 super.setUp();
62   
63  1 this.xwikiSyntaxParser = getComponentManager().getInstance(Parser.class, "xwiki/2.0");
64  1 this.officeDocumentSplitter = getComponentManager().getInstance(XDOMOfficeDocumentSplitter.class);
65    }
66   
67    /**
68    * Test basic document splitting.
69    *
70    * @throws Exception if it fails to parse the wiki syntax or if it fails to split the document
71    */
 
72  1 toggle @Test
73    public void testDocumentSplitting() throws Exception
74    {
75    // Create xwiki/2.0 document.
76  1 StringBuffer buffer = new StringBuffer();
77  1 buffer.append("=Heading1=").append('\n');
78  1 buffer.append("Content").append('\n');
79  1 buffer.append("==Heading11==").append('\n');
80  1 buffer.append("Content").append('\n');
81  1 buffer.append("==Heading12==").append('\n');
82  1 buffer.append("Content").append('\n');
83  1 buffer.append("=Heading2=").append('\n');
84  1 buffer.append("Content").append('\n');
85  1 XDOM xdom = xwikiSyntaxParser.parse(new StringReader(buffer.toString()));
86   
87    // Create xdom office document.
88  1 XDOMOfficeDocument officeDocument =
89    new XDOMOfficeDocument(xdom, new HashMap<String, byte[]>(), getComponentManager());
90  1 final DocumentReference baseDocument = new DocumentReference("xwiki", "Test", "Test");
91   
92    // Add expectations to mock document name serializer.
93  1 getMockery().checking(new Expectations()
94    {
 
95  1 toggle {
96  1 allowing(mockCompactWikiStringEntityReferenceSerializer).serialize(baseDocument);
97  1 will(returnValue("Test.Test"));
98  1 allowing(mockCompactWikiStringEntityReferenceSerializer).serialize(
99    new DocumentReference("xwiki", "Test", "Heading1"));
100  1 will(returnValue("Test.Heading1"));
101  1 allowing(mockCompactWikiStringEntityReferenceSerializer).serialize(
102    new DocumentReference("xwiki", "Test", "Heading11"));
103  1 will(returnValue("Test.Heading11"));
104  1 allowing(mockCompactWikiStringEntityReferenceSerializer).serialize(
105    new DocumentReference("xwiki", "Test", "Heading12"));
106  1 will(returnValue("Test.Heading12"));
107  1 allowing(mockCompactWikiStringEntityReferenceSerializer).serialize(
108    new DocumentReference("xwiki", "Test", "Heading2"));
109  1 will(returnValue("Test.Heading2"));
110    }
111    });
112   
113    // Add expectations to mock document name factory.
114  1 getMockery().checking(new Expectations()
115    {
 
116  1 toggle {
117  1 allowing(mockDocumentReferenceResolver).resolve("Test.Test");
118  1 will(returnValue(new DocumentReference("xwiki", "Test", "Test")));
119  1 allowing(mockDocumentReferenceResolver).resolve("Test.Heading1");
120  1 will(returnValue(new DocumentReference("xwiki", "Test", "Heading1")));
121  1 allowing(mockDocumentReferenceResolver).resolve("Test.Heading11");
122  1 will(returnValue(new DocumentReference("xwiki", "Test", "Heading11")));
123  1 allowing(mockDocumentReferenceResolver).resolve("Test.Heading12");
124  1 will(returnValue(new DocumentReference("xwiki", "Test", "Heading12")));
125  1 allowing(mockDocumentReferenceResolver).resolve("Test.Heading2");
126  1 will(returnValue(new DocumentReference("xwiki", "Test", "Heading2")));
127    }
128    });
129   
130    // Add expectations to mock document access bridge.
131  1 getMockery().checking(new Expectations()
132    {
 
133  1 toggle {
134  1 allowing(mockDocumentAccessBridge).exists("Test.Heading1");
135  1 will(returnValue(false));
136  1 allowing(mockDocumentAccessBridge).exists("Test.Heading11");
137  1 will(returnValue(false));
138  1 allowing(mockDocumentAccessBridge).exists("Test.Heading12");
139  1 will(returnValue(false));
140  1 allowing(mockDocumentAccessBridge).exists("Test.Heading2");
141  1 will(returnValue(false));
142    }
143    });
144   
145    // Perform the split operation.
146  1 Map<TargetDocumentDescriptor, XDOMOfficeDocument> result =
147    officeDocumentSplitter.split(officeDocument, new int[] {1, 2, 3, 4, 5, 6}, "headingNames", baseDocument);
148   
149    // There should be five XDOM office documents.
150  1 Assert.assertEquals(5, result.size());
151    }
152    }