1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
@link |
50 |
|
|
51 |
|
@version |
52 |
|
@since |
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 2 |
Complexity Density: 0.07 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
66 |
1 |
@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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (24) |
Complexity: 1 |
Complexity Density: 0.04 |
1PASS
|
|
77 |
1 |
@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 |
|
} |