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

File DefaultXDOMOfficeDocumentBuilderTest.java

 

Code metrics

0
22
3
1
111
59
3
0.14
7.33
3
1

Classes

Class Line # Actions
DefaultXDOMOfficeDocumentBuilderTest 46 22 0% 3 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.builder;
21   
22    import java.io.ByteArrayInputStream;
23    import java.io.InputStream;
24    import java.util.HashMap;
25    import java.util.Map;
26   
27    import org.jmock.Expectations;
28    import org.junit.Before;
29    import org.junit.Test;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.officeimporter.builder.XDOMOfficeDocumentBuilder;
32    import org.xwiki.officeimporter.converter.OfficeConverter;
33    import org.xwiki.officeimporter.document.OfficeDocument;
34    import org.xwiki.officeimporter.document.XDOMOfficeDocument;
35    import org.xwiki.officeimporter.internal.AbstractOfficeImporterTest;
36    import org.xwiki.rendering.listener.MetaData;
37   
38    import static org.junit.Assert.*;
39   
40    /**
41    * Test case for {@link DefaultXDOMOfficeDocumentBuilder}.
42    *
43    * @version $Id: 30aea7d9f39fd1b87de766c3b4b3b354e77bb8b3 $
44    * @since 2.1M1
45    */
 
46    public class DefaultXDOMOfficeDocumentBuilderTest extends AbstractOfficeImporterTest
47    {
48    /**
49    * The name of an input file to be used in tests.
50    */
51    private static final String INPUT_FILE_NAME = "office.doc";
52   
53    /**
54    * The name of the output file corresponding to {@link #INPUT_FILE_NAME}.
55    */
56    private static final String OUTPUT_FILE_NAME = "office.html";
57   
58    /**
59    * The {@link XDOMOfficeDocumentBuilder} component.
60    */
61    private XDOMOfficeDocumentBuilder xdomOfficeDocumentBuilder;
62   
 
63  1 toggle @Override
64    @Before
65    public void setUp() throws Exception
66    {
67  1 super.setUp();
68  1 this.xdomOfficeDocumentBuilder = getComponentManager().getInstance(XDOMOfficeDocumentBuilder.class);
69    }
70   
71    /**
72    * Test {@link OfficeDocument} building.
73    */
 
74  1 toggle @Test
75    public void testXDOMOfficeDocumentBuilding() throws Exception
76    {
77    // Create & register a mock document converter to by-pass the office server.
78  1 final InputStream mockOfficeFileStream = new ByteArrayInputStream(new byte[1024]);
79  1 final Map<String, InputStream> mockInput = new HashMap<String, InputStream>();
80  1 mockInput.put(INPUT_FILE_NAME, mockOfficeFileStream);
81  1 final Map<String, byte[]> mockOutput = new HashMap<String, byte[]>();
82  1 mockOutput.put(OUTPUT_FILE_NAME,
83    "<html><head><title></tile></head><body><p><strong>Hello There</strong></p></body></html>".getBytes());
84   
85  1 final OfficeConverter mockDocumentConverter = getMockery().mock(OfficeConverter.class);
86  1 final DocumentReference documentReference = new DocumentReference("xwiki", "Main", "Test");
87   
88  1 getMockery().checking(new Expectations()
89    {
 
90  1 toggle {
91  1 oneOf(mockOfficeServer).getConverter();
92  1 will(returnValue(mockDocumentConverter));
93   
94  1 allowing(mockDocumentConverter).convert(mockInput, INPUT_FILE_NAME, OUTPUT_FILE_NAME);
95  1 will(returnValue(mockOutput));
96   
97  1 allowing(mockDocumentReferenceResolver).resolve("xwiki:Main.Test");
98  1 will(returnValue(documentReference));
99   
100  1 allowing(mockDefaultStringEntityReferenceSerializer).serialize(documentReference);
101  1 will(returnValue("xwiki:Main.Test"));
102    }
103    });
104   
105  1 XDOMOfficeDocument document =
106    xdomOfficeDocumentBuilder.build(mockOfficeFileStream, INPUT_FILE_NAME, documentReference, true);
107  1 assertEquals("xwiki:Main.Test", document.getContentDocument().getMetaData().getMetaData(MetaData.BASE));
108  1 assertEquals("**Hello There**", document.getContentAsString());
109  1 assertEquals(0, document.getArtifacts().size());
110    }
111    }