diff options
Diffstat (limited to 'src/test/java')
| -rw-r--r-- | src/test/java/org/zwobble/mammoth/tests/MammothTests.java | 16 | ||||
| -rw-r--r-- | src/test/java/org/zwobble/mammoth/tests/docx/EmbeddedStyleMapTests.java | 46 |
2 files changed, 60 insertions, 2 deletions
diff --git a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java index 665e00a..ab67f07 100644 --- a/src/test/java/org/zwobble/mammoth/tests/MammothTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/MammothTests.java @@ -3,10 +3,11 @@ package org.zwobble.mammoth.tests; import org.junit.Test; import org.zwobble.mammoth.DocumentConverter; import org.zwobble.mammoth.Result; +import org.zwobble.mammoth.internal.docx.EmbeddedStyleMap; +import org.zwobble.mammoth.internal.docx.InMemoryArchive; import org.zwobble.mammoth.internal.styles.parsing.ParseException; -import java.io.File; -import java.io.IOException; +import java.io.*; import java.nio.file.Files; import java.nio.file.Path; import java.util.function.Function; @@ -235,6 +236,17 @@ public class MammothTests { } @Test + public void embeddedStyleMapCanBeWrittenAndThenRead() throws IOException { + InMemoryArchive archive = InMemoryArchive.fromStream(new FileInputStream(TestData.file("single-paragraph.docx"))); + EmbeddedStyleMap.embedStyleMap(archive, "p => h1"); + + assertThat( + new DocumentConverter().convertToHtml(new ByteArrayInputStream(archive.toByteArray())), + isSuccess("<h1>Walking on imported air</h1>") + ); + } + + @Test public void canExtractRawTextFromFile() throws IOException { assertThat( new DocumentConverter().extractRawText(TestData.file("simple-list.docx")), diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/EmbeddedStyleMapTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/EmbeddedStyleMapTests.java new file mode 100644 index 0000000..1c1f9a8 --- /dev/null +++ b/src/test/java/org/zwobble/mammoth/tests/docx/EmbeddedStyleMapTests.java @@ -0,0 +1,46 @@ +package org.zwobble.mammoth.tests.docx; + +import org.junit.Test; +import org.zwobble.mammoth.internal.docx.*; +import org.zwobble.mammoth.internal.util.Streams; + +import java.io.IOException; +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.zwobble.mammoth.internal.util.Maps.map; + +public class EmbeddedStyleMapTests { + private static final String ORIGINAL_RELATIONSHIPS_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + + "<Relationships xmlns=\"http://schemas.openxmlformats.org/package/2006/relationships\">" + + "<Relationship Id=\"rId3\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings\" Target=\"settings.xml\"/>" + + "</Relationships>"; + + private static final String ORIGINAL_CONTENT_TYPES_XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" + + "<Types xmlns=\"http://schemas.openxmlformats.org/package/2006/content-types\">" + + "<Default Extension=\"png\" ContentType=\"image/png\"/>" + + "</Types>"; + + @Test + public void embeddedStyleMapPreservesUnrelatedFiles() throws IOException { + MutableArchive archive = normalDocx(); + EmbeddedStyleMap.embedStyleMap(archive, "p => h1"); + assertEquals("placeholder text", Streams.toString(Archives.getInputStream(archive, "placeholder"))); + } + + @Test + public void embeddedStyleMapCanBeReadAfterBeingEmbedded() throws IOException { + MutableArchive archive = normalDocx(); + EmbeddedStyleMap.embedStyleMap(archive, "p => h1"); + assertEquals(Optional.of("p => h1"), EmbeddedStyleMap.readStyleMap(archive)); + } + + private static MutableArchive normalDocx() { + return InMemoryArchive.fromStrings(map( + "placeholder", "placeholder text", + "word/_rels/document.xml.rels", ORIGINAL_RELATIONSHIPS_XML, + "[Content_Types].xml", ORIGINAL_CONTENT_TYPES_XML + )); + } + +} |
