From 559c31d2f28d9a77655538d994c3213d916b8d0c Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 13 Nov 2016 22:10:33 +0000 Subject: Ensure embedded style map is not removed by Word --- .../mammoth/tests/docx/EmbeddedStyleMapTests.java | 66 +++++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) (limited to 'src/test/java/org') diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/EmbeddedStyleMapTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/EmbeddedStyleMapTests.java index 76322f9..527debb 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/EmbeddedStyleMapTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/EmbeddedStyleMapTests.java @@ -4,14 +4,21 @@ import org.junit.Test; import org.zwobble.mammoth.internal.archives.Archives; import org.zwobble.mammoth.internal.archives.InMemoryArchive; import org.zwobble.mammoth.internal.archives.MutableArchive; -import org.zwobble.mammoth.internal.docx.*; +import org.zwobble.mammoth.internal.docx.EmbeddedStyleMap; import org.zwobble.mammoth.internal.util.Streams; +import org.zwobble.mammoth.internal.xml.XmlElement; +import org.zwobble.mammoth.internal.xml.XmlNode; +import org.zwobble.mammoth.internal.xml.XmlNodes; +import org.zwobble.mammoth.internal.xml.parsing.XmlParser; import java.io.IOException; import java.util.Optional; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; +import static org.zwobble.mammoth.internal.util.Lists.list; import static org.zwobble.mammoth.internal.util.Maps.map; +import static org.zwobble.mammoth.tests.DeepReflectionMatcher.deepEquals; public class EmbeddedStyleMapTests { private static final String ORIGINAL_RELATIONSHIPS_XML = "" + @@ -19,16 +26,34 @@ public class EmbeddedStyleMapTests { "" + ""; + private static final XmlElement EXPECTED_RELATIONSHIPS_XML = XmlNodes.element("Relationships", list( + XmlNodes.element("Relationship", map( + "Id", "rId3", + "Type", "http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings", + "Target", "settings.xml" + )), + XmlNodes.element("Relationship", map( + "Id", "rMammothStyleMap", + "Type", "http://schemas.zwobble.org/mammoth/style-map", + "Target", "/mammoth/style-map" + )) + )); + private static final String ORIGINAL_CONTENT_TYPES_XML = "" + "" + "" + ""; + private static final XmlElement EXPECTED_CONTENT_TYPES_XML = XmlNodes.element("Types", list( + XmlNodes.element("Default", map("Extension", "png", "ContentType", "image/png")), + XmlNodes.element("Override", map("PartName", "/mammoth/style-map", "ContentType", "text/prs.mammoth.style-map")) + )); + @Test public void embeddedStyleMapPreservesUnrelatedFiles() throws IOException { MutableArchive archive = normalDocx(); EmbeddedStyleMap.embedStyleMap(archive, "p => h1"); - assertEquals("placeholder text", Streams.toString(Archives.getInputStream(archive, "placeholder"))); + assertEquals("placeholder text", readString(archive, "placeholder")); } @Test @@ -38,6 +63,40 @@ public class EmbeddedStyleMapTests { assertEquals(Optional.of("p => h1"), EmbeddedStyleMap.readStyleMap(archive)); } + @Test + public void embeddedStyleMapIsReferencedInRelationships() throws IOException { + MutableArchive archive = normalDocx(); + EmbeddedStyleMap.embedStyleMap(archive, "p => h1"); + assertThat(readRelationships(archive), deepEquals(EXPECTED_RELATIONSHIPS_XML)); + } + + @Test + public void embeddedStyleMapHasOverrideContentTypeInContentTypesXml() throws IOException { + MutableArchive archive = normalDocx(); + EmbeddedStyleMap.embedStyleMap(archive, "p => h1"); + assertThat(readContentTypes(archive), deepEquals(EXPECTED_CONTENT_TYPES_XML)); + } + + @Test + public void canOverwriteExistingStyleMap() throws IOException { + MutableArchive archive = normalDocx(); + EmbeddedStyleMap.embedStyleMap(archive, "p => h1"); + EmbeddedStyleMap.embedStyleMap(archive, "p => h1"); + assertEquals(Optional.of("p => h1"), EmbeddedStyleMap.readStyleMap(archive)); + assertThat(readRelationships(archive), deepEquals(EXPECTED_RELATIONSHIPS_XML)); + assertThat(readContentTypes(archive), deepEquals(EXPECTED_CONTENT_TYPES_XML)); + } + + private XmlNode readRelationships(MutableArchive archive) throws IOException { + return new XmlParser(EmbeddedStyleMap.RELATIONSHIPS_NAMESPACES) + .parseString(readString(archive, "word/_rels/document.xml.rels")); + } + + private XmlNode readContentTypes(MutableArchive archive) throws IOException { + return new XmlParser(EmbeddedStyleMap.CONTENT_TYPES_NAMESPACES) + .parseString(readString(archive, "[Content_Types].xml")); + } + private static MutableArchive normalDocx() { return InMemoryArchive.fromStrings(map( "placeholder", "placeholder text", @@ -46,4 +105,7 @@ public class EmbeddedStyleMapTests { )); } + private String readString(MutableArchive archive, String path) throws IOException { + return Streams.toString(Archives.getInputStream(archive, path)); + } } -- cgit v1.2.3