diff options
| author | Michael Williamson <mike@zwobble.org> | 2016-02-13 23:09:03 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2016-02-13 23:09:03 +0000 |
| commit | 07d6f0cc788962d9faaec575da30cbdefdbe9637 (patch) | |
| tree | c9e499c7b3e7c93f9dbad9bc3a8bd9a94708dc10 /src/test | |
| parent | ddde81cf806a5534a01483b563651bd84cf7452d (diff) | |
Extract common test code for testing image reading
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java | 35 |
1 files changed, 19 insertions, 16 deletions
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 0683bff..9f7c6c5 100644 --- a/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java +++ b/src/test/java/org/zwobble/mammoth/tests/docx/BodyXmlTests.java @@ -19,6 +19,7 @@ import java.io.InputStreamReader; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.function.Function; import static com.natpryce.makeiteasy.MakeItEasy.*; import static java.util.Arrays.asList; @@ -387,26 +388,18 @@ 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!")); + assertCanReadEmbeddedImage(image -> + element("v:imagedata", map("r:id", image.relationshipId, "o:title", image.altText))); } @Test public void canReadInlinePictures() throws IOException { - XmlElement element = inlineImageXml(embeddedBlipXml("rId5"), "It's a hat"); + assertCanReadEmbeddedImage(image -> + inlineImageXml(embeddedBlipXml(image.relationshipId), image.altText)); + } + + private void assertCanReadEmbeddedImage(Function<EmbeddedImage, XmlElement> generateXml) throws IOException { + XmlElement element = generateXml.apply(new EmbeddedImage("rId5", "It's a hat")); Relationships relationships = new Relationships(map( "rId5", new Relationship("media/hat.png"))); String imageBytes = "Not an image at all!"; @@ -423,6 +416,16 @@ public class BodyXmlTests { equalTo("Not an image at all!")); } + private class EmbeddedImage { + private final String relationshipId; + private final String altText; + + public EmbeddedImage(String relationshipId, String altText) { + this.relationshipId = relationshipId; + this.altText = altText; + } + } + private XmlElement inlineImageXml(XmlElement blip, String description) { return element("w:drawing", list( element("wp:inline", imageXml(blip, description)))); |
