1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
package org.zwobble.mammoth.tests;
import org.junit.Test;
import org.zwobble.mammoth.Mammoth;
import org.zwobble.mammoth.results.Result;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.Assert.assertThat;
import static org.zwobble.mammoth.results.Result.success;
import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals;
public class MammothTests {
@Test
public void docxContainingOneParagraphIsConvertedToSingleParagraphElement() {
assertThat(
convertToHtml("single-paragraph.docx"),
deepEquals(success("<p>Walking on imported air</p>")));
}
@Test
public void canReadFilesWithUtf8Bom() {
assertThat(
convertToHtml("utf8-bom.docx"),
deepEquals(success("<p>This XML has a byte order mark.</p>")));
}
@Test
public void emptyParagraphsAreIgnoredByDefault() {
assertThat(
convertToHtml("empty.docx"),
deepEquals(success("")));
}
@Test
public void emptyParagraphsArePreservedIfIgnoreEmptyParagraphsIsFalse() {
assertThat(
convertToHtml("empty.docx", Mammoth.Options.DEFAULT.preserveEmptyParagraphs()),
deepEquals(success("<p></p>")));
}
@Test
public void simpleListIsConvertedToListElements() {
assertThat(
convertToHtml("simple-list.docx"),
deepEquals(success("<ul><li>Apple</li><li>Banana</li></ul>")));
}
@Test
public void wordTablesAreConvertedToHtmlTables() {
assertThat(
convertToHtml("tables.docx"),
deepEquals(success(
"<p>Above</p>" +
"<table>" +
"<tr><td><p>Top left</p></td><td><p>Top right</p></td></tr>" +
"<tr><td><p>Bottom left</p></td><td><p>Bottom right</p></td></tr>" +
"</table>" +
"<p>Below</p>")));
}
@Test
public void inlineImagesAreIncludedInOutput() {
assertThat(
convertToHtml("tiny-picture.docx"),
deepEquals(success("<p><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>")));
}
@Test
public void imagesStoredOutsideOfDocumentAreIncludedInOutput() throws IOException {
Path tempDirectory = Files.createTempDirectory("mammoth-");
try {
Path documentPath = tempDirectory.resolve("external-picture.docx");
Files.copy(TestData.file("external-picture.docx").toPath(), documentPath);
Files.copy(TestData.file("tiny-picture.png").toPath(), tempDirectory.resolve("tiny-picture.png"));
assertThat(
Mammoth.convertToHtml(documentPath.toFile()),
deepEquals(success("<p><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>")));
} finally {
tempDirectory.toFile().delete();
}
}
@Test
public void contentTypesAreRead() {
assertThat(
convertToHtml("tiny-picture-custom-content-type.docx"),
deepEquals(success("<p><img src=\"data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=\" /></p>")));
}
@Test
public void footnotesAreAppendedToText() {
assertThat(
convertToHtml("footnotes.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42-")),
deepEquals(success(
"<p>Ouch" +
"<sup><a href=\"#doc-42-footnote-1\" id=\"doc-42-footnote-ref-1\">[1]</a></sup>." +
"<sup><a href=\"#doc-42-footnote-2\" id=\"doc-42-footnote-ref-2\">[2]</a></sup></p>" +
"<ol><li id=\"doc-42-footnote-1\"><p> A tachyon walks into a bar. <a href=\"#doc-42-footnote-ref-1\">↑</a></p></li>" +
"<li id=\"doc-42-footnote-2\"><p> Fin. <a href=\"#doc-42-footnote-ref-2\">↑</a></p></li></ol>")));
}
@Test
public void endNotesAreAppendedToText() {
assertThat(
convertToHtml("endnotes.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42-")),
deepEquals(success(
"<p>Ouch" +
"<sup><a href=\"#doc-42-endnote-2\" id=\"doc-42-endnote-ref-2\">[1]</a></sup>." +
"<sup><a href=\"#doc-42-endnote-3\" id=\"doc-42-endnote-ref-3\">[2]</a></sup></p>" +
"<ol><li id=\"doc-42-endnote-2\"><p> A tachyon walks into a bar. <a href=\"#doc-42-endnote-ref-2\">↑</a></p></li>" +
"<li id=\"doc-42-endnote-3\"><p> Fin. <a href=\"#doc-42-endnote-ref-3\">↑</a></p></li></ol>")));
}
@Test
public void relationshipsAreReadForEachFileContainingBodyXml() {
assertThat(
convertToHtml("footnote-hyperlink.docx", Mammoth.Options.DEFAULT.idPrefix("doc-42-")),
deepEquals(success(
"<p><sup><a href=\"#doc-42-footnote-1\" id=\"doc-42-footnote-ref-1\">[1]</a></sup></p>" +
"<ol><li id=\"doc-42-footnote-1\"><p> <a href=\"http://www.example.com\">Example</a> <a href=\"#doc-42-footnote-ref-1\">↑</a></p></li></ol>")));
}
@Test
public void textBoxesAreRead() {
assertThat(
convertToHtml("text-box.docx"),
deepEquals(success("<p>Datum plane</p>")));
}
@Test
public void canExtractRawText() {
assertThat(
Mammoth.extractRawText(TestData.file("simple-list.docx")),
deepEquals(success("Apple\n\nBanana\n\n")));
}
private Result<String> convertToHtml(String name) {
File file = TestData.file(name);
return Mammoth.convertToHtml(file);
}
private Result<String> convertToHtml(String name, Mammoth.Options options) {
File file = TestData.file(name);
return Mammoth.convertToHtml(file, options);
}
}
|