diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-13 22:38:53 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-13 22:38:53 +0000 |
| commit | 8d01886d494d8251c0825ca7d6df214d19415325 (patch) | |
| tree | 4a6755fff27f997c815418dabba20840aafd2322 /src/test | |
| parent | 6ae918357de9c9ff0b84e70016ea08aefeb48dea (diff) | |
Read v:imagedata
Diffstat (limited to 'src/test')
3 files changed, 69 insertions, 5 deletions
diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java index 5758dce..41cefd5 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlReaderMakers.java @@ -2,10 +2,10 @@ package org.zwobble.mammoth.tests.docx; import com.natpryce.makeiteasy.Instantiator; import com.natpryce.makeiteasy.Property; -import org.zwobble.mammoth.docx.BodyXmlReader; -import org.zwobble.mammoth.docx.Numbering; -import org.zwobble.mammoth.docx.Relationships; -import org.zwobble.mammoth.docx.Styles; +import org.zwobble.mammoth.docx.*; + +import java.io.IOException; +import java.io.InputStream; import static com.natpryce.makeiteasy.Property.newProperty; @@ -13,10 +13,23 @@ public class BodyXmlReaderMakers { public static final Property<BodyXmlReader, Styles> STYLES = newProperty(); public static final Property<BodyXmlReader, Numbering> NUMBERING = newProperty(); public static final Property<BodyXmlReader, Relationships> RELATIONSHIPS = newProperty(); + public static final Property<BodyXmlReader, ContentTypes> CONTENT_TYPES = newProperty(); + public static final Property<BodyXmlReader, DocxFile> DOCX_FILE = newProperty(); public static final Instantiator<BodyXmlReader> bodyReader = propertyLookup -> new BodyXmlReader( propertyLookup.valueOf(STYLES, Styles.EMPTY), propertyLookup.valueOf(NUMBERING, Numbering.EMPTY), - propertyLookup.valueOf(RELATIONSHIPS, Relationships.EMPTY)); + propertyLookup.valueOf(RELATIONSHIPS, Relationships.EMPTY), + propertyLookup.valueOf(CONTENT_TYPES, ContentTypes.DEFAULT), + propertyLookup.valueOf(DOCX_FILE, new DocxFile() { + @Override + public InputStream getInputStream(String name) throws IOException { + throw new UnsupportedOperationException(); + } + + @Override + public void close() throws IOException { + } + })); } diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java index c8f8887..cc0815e 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -1,5 +1,6 @@ package org.zwobble.mammoth.tests.docx; +import com.google.common.io.CharStreams; import com.natpryce.makeiteasy.Maker; import org.hamcrest.Matcher; import org.hamcrest.Matchers; @@ -13,12 +14,15 @@ import org.zwobble.mammoth.xml.XmlElement; import org.zwobble.mammoth.xml.XmlNode; import org.zwobble.mammoth.xml.XmlNodes; +import java.io.IOException; +import java.io.InputStreamReader; import java.util.List; import java.util.Optional; import static com.natpryce.makeiteasy.MakeItEasy.*; import static java.util.Arrays.asList; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.allOf; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.hasProperty; import static org.zwobble.mammoth.documents.NoteReference.endnoteReference; @@ -381,6 +385,25 @@ public class BodyXmlTests { } @Test + public void canReadImagedataElementsWithIdAttribute() throws IOException { + XmlElement element = element("v:imagedata", map("r:id", "rId5", "o:title", "It's a hat")); + Relationships relationships = new Relationships(map( + "rId5", new Relationship("media/hat.png"))); + String imageBytes = "Not an image at all!"; + DocxFile file = new InMemoryDocxFile(map("word/media/hat.png", imageBytes)); + + Image image = (Image) readSuccess( + a(bodyReader, with(RELATIONSHIPS, relationships), with(DOCX_FILE, file)), + element); + assertThat(image, allOf( + hasProperty("altText", deepEquals(Optional.of("It's a hat"))), + hasProperty("contentType", deepEquals(Optional.of("image/png"))))); + assertThat( + CharStreams.toString(new InputStreamReader(image.open())), + equalTo("Not an image at all!")); + } + + @Test public void appropriateElementsHaveTheirChildrenReadNormally() { assertChildrenAreReadNormally("w:ins"); assertChildrenAreReadNormally("w:smartTag"); diff --git a/src/test/java/org/zwobble/mammoth/tests/docx/InMemoryDocxFile.java b/src/test/java/org/zwobble/mammoth/tests/docx/InMemoryDocxFile.java new file mode 100644 index 0000000..2ed5e27 --- /dev/null +++ b/src/test/java/org/zwobble/mammoth/tests/docx/InMemoryDocxFile.java @@ -0,0 +1,28 @@ +package org.zwobble.mammoth.tests.docx; + +import org.zwobble.mammoth.docx.DocxFile; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.Optional; + +public class InMemoryDocxFile implements DocxFile { + private final Map<String, String> entries; + + public InMemoryDocxFile(Map<String, String> entries) { + this.entries = entries; + } + + @Override + public InputStream getInputStream(String name) throws IOException { + String value = Optional.ofNullable(entries.get(name)).get(); + return new ByteArrayInputStream(value.getBytes(StandardCharsets.UTF_8)); + } + + @Override + public void close() throws IOException { + } +} |
