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

File DefaultXHTMLOfficeDocumentBuilderTest.java

 

Code metrics

0
29
2
1
114
72
2
0.07
14.5
2
1

Classes

Class Line # Actions
DefaultXHTMLOfficeDocumentBuilderTest 54 29 0% 2 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.io.Reader;
25    import java.util.Arrays;
26    import java.util.Collections;
27    import java.util.HashMap;
28    import java.util.Map;
29   
30    import org.junit.Before;
31    import org.junit.Rule;
32    import org.junit.Test;
33    import org.w3c.dom.Document;
34    import org.xwiki.model.reference.DocumentReference;
35    import org.xwiki.model.reference.EntityReferenceSerializer;
36    import org.xwiki.officeimporter.builder.XHTMLOfficeDocumentBuilder;
37    import org.xwiki.officeimporter.converter.OfficeConverter;
38    import org.xwiki.officeimporter.document.XHTMLOfficeDocument;
39    import org.xwiki.officeimporter.server.OfficeServer;
40    import org.xwiki.test.mockito.MockitoComponentMockingRule;
41    import org.xwiki.xml.html.HTMLCleaner;
42    import org.xwiki.xml.html.HTMLCleanerConfiguration;
43   
44    import static org.junit.Assert.*;
45    import static org.mockito.ArgumentMatchers.*;
46    import static org.mockito.Mockito.*;
47   
48    /**
49    * Test case for {@link DefaultXHTMLOfficeDocumentBuilder}.
50    *
51    * @version $Id: 87fa0061414c8e9c9628fd036cf1e729755b3891 $
52    * @since 2.1M1
53    */
 
54    public class DefaultXHTMLOfficeDocumentBuilderTest
55    {
56    @Rule
57    public MockitoComponentMockingRule<XHTMLOfficeDocumentBuilder> mocker =
58    new MockitoComponentMockingRule<XHTMLOfficeDocumentBuilder>(DefaultXHTMLOfficeDocumentBuilder.class);
59   
60    private OfficeConverter officeConverter;
61   
62    private HTMLCleaner officeHTMLCleaner;
63   
64    private EntityReferenceSerializer<String> entityReferenceSerializer;
65   
 
66  1 toggle @Before
67    public void configure() throws Exception
68    {
69  1 this.officeConverter = mock(OfficeConverter.class);
70  1 OfficeServer officeServer = this.mocker.getInstance(OfficeServer.class);
71  1 when(officeServer.getConverter()).thenReturn(this.officeConverter);
72   
73  1 this.officeHTMLCleaner = this.mocker.getInstance(HTMLCleaner.class, "openoffice");
74  1 this.entityReferenceSerializer = this.mocker.getInstance(EntityReferenceSerializer.TYPE_STRING);
75    }
76   
 
77  1 toggle @Test
78    public void testXHTMLOfficeDocumentBuilding() throws Exception
79    {
80  1 DocumentReference documentReference = new DocumentReference("wiki", Arrays.asList("Path", "To"), "Page");
81  1 when(this.entityReferenceSerializer.serialize(documentReference)).thenReturn("wiki:Path.To.Page");
82   
83  1 InputStream officeFileStream = new ByteArrayInputStream("office content".getBytes());
84  1 Map<String, byte[]> artifacts = new HashMap<String, byte[]>();
85  1 artifacts.put("file.html", "HTML content".getBytes());
86  1 artifacts.put("file.txt", "Text content".getBytes());
87  1 when(this.officeConverter.convert(Collections.singletonMap("file.odt", officeFileStream), "file.odt",
88    "file.html")).thenReturn(artifacts);
89   
90  1 Map<String, byte[]> embeddedImages = Collections.singletonMap("image.png", "Image content".getBytes());
91  1 Document xhtmlDoc = mock(Document.class);
92  1 when(xhtmlDoc.getUserData("embeddedImages")).thenReturn(embeddedImages);
93   
94  1 HTMLCleanerConfiguration config = mock(HTMLCleanerConfiguration.class);
95  1 when(this.officeHTMLCleaner.getDefaultConfiguration()).thenReturn(config);
96  1 when(this.officeHTMLCleaner.clean(any(Reader.class), eq(config))).thenReturn(xhtmlDoc);
97   
98  1 XHTMLOfficeDocument result =
99    this.mocker.getComponentUnderTest().build(officeFileStream, "file.odt", documentReference, true);
100   
101  1 Map<String, String> params = new HashMap<String, String>();
102  1 params.put("targetDocument", "wiki:Path.To.Page");
103  1 params.put("attachEmbeddedImages", "true");
104  1 params.put("filterStyles", "strict");
105  1 verify(config).setParameters(params);
106   
107  1 assertEquals(xhtmlDoc, result.getContentDocument());
108   
109  1 Map<String, byte[]> expectedArtifacts = new HashMap<String, byte[]>();
110  1 expectedArtifacts.put("file.txt", artifacts.get("file.txt"));
111  1 expectedArtifacts.put("image.png", embeddedImages.get("image.png"));
112  1 assertEquals(expectedArtifacts, result.getArtifacts());
113    }
114    }